diff --git a/frontend/src/Metamaps/AutoLayout.js b/frontend/src/Metamaps/AutoLayout.js index f3e91440..1408ba62 100644 --- a/frontend/src/Metamaps/AutoLayout.js +++ b/frontend/src/Metamaps/AutoLayout.js @@ -49,7 +49,7 @@ const AutoLayout = { } } - if (opts.map && self.coordsTaken(nextX, nextY, opts.map)) { + if (opts.mappings && self.coordsTaken(nextX, nextY, opts.mappings)) { // check if the coordinate is already taken on the current map return self.getNextCoord(opts) } else { @@ -59,8 +59,7 @@ const AutoLayout = { } } }, - coordsTaken: function (x, y, map) { - const mappings = map.getMappings() + coordsTaken: function (x, y, mappings) { if (mappings.findWhere({ xloc: x, yloc: y })) { return true } else { diff --git a/frontend/src/Metamaps/Backbone/index.js b/frontend/src/Metamaps/Backbone/index.js index 1994c483..389d7dcf 100644 --- a/frontend/src/Metamaps/Backbone/index.js +++ b/frontend/src/Metamaps/Backbone/index.js @@ -85,46 +85,6 @@ _Backbone.Map = Backbone.Model.extend({ getUser: function () { return Mapper.get(this.get('user_id')) }, - fetchContained: function () { - var bb = _Backbone - var that = this - var start = function (data) { - that.set('mappers', new bb.MapperCollection(data.mappers)) - that.set('topics', new bb.TopicCollection(data.topics)) - that.set('synapses', new bb.SynapseCollection(data.synapses)) - that.set('mappings', new bb.MappingCollection(data.mappings)) - } - - $.ajax({ - url: '/maps/' + this.id + '/contains.json', - success: start, - async: false - }) - }, - getTopics: function () { - if (!this.get('topics')) { - this.fetchContained() - } - return this.get('topics') - }, - getSynapses: function () { - if (!this.get('synapses')) { - this.fetchContained() - } - return this.get('synapses') - }, - getMappings: function () { - if (!this.get('mappings')) { - this.fetchContained() - } - return this.get('mappings') - }, - getMappers: function () { - if (!this.get('mappers')) { - this.fetchContained() - } - return this.get('mappers') - }, updateView: function () { var map = Active.Map var isActiveMap = this.id === map.id diff --git a/frontend/src/Metamaps/Import.js b/frontend/src/Metamaps/Import.js index f70a1290..f3e872ec 100644 --- a/frontend/src/Metamaps/Import.js +++ b/frontend/src/Metamaps/Import.js @@ -227,7 +227,7 @@ const Import = { parsedTopics.forEach(function (topic) { let coords = { x: topic.x, y: topic.y } if (!coords.x || !coords.y) { - coords = AutoLayout.getNextCoord({ map: Active.Map }) + coords = AutoLayout.getNextCoord({ mappings: Metamaps.Mappings }) } if (!topic.name && topic.link || @@ -353,7 +353,7 @@ const Import = { handleURL: function (url, opts = {}) { let coords = opts.coords if (!coords || coords.x === undefined || coords.y === undefined) { - coords = AutoLayout.getNextCoord({ map: Active.Map }) + coords = AutoLayout.getNextCoord({ mappings: Metamaps.Mappings }) } const name = opts.name || 'Link' diff --git a/frontend/src/Metamaps/Realtime.js b/frontend/src/Metamaps/Realtime.js index 6522d460..f905bb84 100644 --- a/frontend/src/Metamaps/Realtime.js +++ b/frontend/src/Metamaps/Realtime.js @@ -981,7 +981,6 @@ const Realtime = { else if (!couldEditBefore && canEditNow) { Map.canEditNow() } else { - model.fetchContained() model.trigger('changeByOther') } } diff --git a/frontend/src/Metamaps/Synapse.js b/frontend/src/Metamaps/Synapse.js index 400cb0b0..8253d6ba 100644 --- a/frontend/src/Metamaps/Synapse.js +++ b/frontend/src/Metamaps/Synapse.js @@ -19,35 +19,21 @@ import Visualize from './Visualize' * - Metamaps.Topics */ +const noOp = () => {} const Synapse = { // this function is to retrieve a synapse JSON object from the database // @param id = the id of the synapse to retrieve - get: function (id, callback) { + get: function (id, callback = noOp) { // if the desired topic is not yet in the local topic repository, fetch it if (Metamaps.Synapses.get(id) == undefined) { - if (!callback) { - var e = $.ajax({ - url: '/synapses/' + id + '.json', - async: false - }) - Metamaps.Synapses.add($.parseJSON(e.responseText)) - return Metamaps.Synapses.get(id) - } else { - return $.ajax({ - url: '/synapses/' + id + '.json', - success: function (data) { - Metamaps.Synapses.add(data) - callback(Metamaps.Synapses.get(id)) - } - }) - } - } else { - if (!callback) { - return Metamaps.Synapses.get(id) - } else { - return callback(Metamaps.Synapses.get(id)) - } - } + $.ajax({ + url: '/synapses/' + id + '.json', + success: function (data) { + Metamaps.Synapses.add(data) + callback(Metamaps.Synapses.get(id)) + } + }) + } else callback(Metamaps.Synapses.get(id)) }, /* * @@ -152,21 +138,19 @@ const Synapse = { node1, node2 - var synapse = self.get(id) - - var mapping = new Metamaps.Backbone.Mapping({ - mappable_type: 'Synapse', - mappable_id: synapse.id, + self.get(id, synapse => { + var mapping = new Metamaps.Backbone.Mapping({ + mappable_type: 'Synapse', + mappable_id: synapse.id, + }) + Metamaps.Mappings.add(mapping) + topic1 = Metamaps.Topics.get(Create.newSynapse.topic1id) + node1 = topic1.get('node') + topic2 = Metamaps.Topics.get(Create.newSynapse.topic2id) + node2 = topic2.get('node') + Create.newSynapse.hide() + self.renderSynapse(mapping, synapse, node1, node2, true) }) - Metamaps.Mappings.add(mapping) - - topic1 = Metamaps.Topics.get(Create.newSynapse.topic1id) - node1 = topic1.get('node') - topic2 = Metamaps.Topics.get(Create.newSynapse.topic2id) - node2 = topic2.get('node') - Create.newSynapse.hide() - - self.renderSynapse(mapping, synapse, node1, node2, true) } } diff --git a/frontend/src/Metamaps/Topic.js b/frontend/src/Metamaps/Topic.js index 34e2bb64..5be6cc57 100644 --- a/frontend/src/Metamaps/Topic.js +++ b/frontend/src/Metamaps/Topic.js @@ -28,37 +28,21 @@ import Visualize from './Visualize' * - Metamaps.Synapses * - Metamaps.Topics */ - +const noOp = () => {} const Topic = { // this function is to retrieve a topic JSON object from the database // @param id = the id of the topic to retrieve - get: function (id, callback) { + get: function (id, callback = noOp) { // if the desired topic is not yet in the local topic repository, fetch it if (Metamaps.Topics.get(id) == undefined) { - // console.log("Ajax call!") - if (!callback) { - var e = $.ajax({ - url: '/topics/' + id + '.json', - async: false - }) - Metamaps.Topics.add($.parseJSON(e.responseText)) - return Metamaps.Topics.get(id) - } else { - return $.ajax({ - url: '/topics/' + id + '.json', - success: function (data) { - Metamaps.Topics.add(data) - callback(Metamaps.Topics.get(id)) - } - }) - } - } else { - if (!callback) { - return Metamaps.Topics.get(id) - } else { - return callback(Metamaps.Topics.get(id)) - } - } + $.ajax({ + url: '/topics/' + id + '.json', + success: function (data) { + Metamaps.Topics.add(data) + callback(Metamaps.Topics.get(id)) + } + }) + } else callback(Metamaps.Topics.get(id)) }, launch: function (id) { var bb = Metamaps.Backbone @@ -192,7 +176,7 @@ const Topic = { }, // opts is additional options in a hash - // TODO: move createNewInDB and permitCerateSYnapseAfter into opts + // TODO: move createNewInDB and permitCreateSynapseAfter into opts renderTopic: function (mapping, topic, createNewInDB, permitCreateSynapseAfter, opts = {}) { var self = Topic @@ -335,7 +319,7 @@ const Topic = { Metamaps.Topics.add(topic) if (Create.newTopic.pinned) { - var nextCoords = AutoLayout.getNextCoord() + var nextCoords = AutoLayout.getNextCoord({ mappings: Metamaps.Mappings }) } var mapping = new Metamaps.Backbone.Mapping({ xloc: nextCoords ? nextCoords.x : Create.newTopic.x, @@ -357,40 +341,38 @@ const Topic = { Create.newTopic.hide() - var topic = self.get(id) + self.get(id, (topic) => { + if (Create.newTopic.pinned) { + var nextCoords = AutoLayout.getNextCoord({ mappings: Metamaps.Mappings }) + } + var mapping = new Metamaps.Backbone.Mapping({ + xloc: nextCoords ? nextCoords.x : Create.newTopic.x, + yloc: nextCoords ? nextCoords.y : Create.newTopic.y, + mappable_type: 'Topic', + mappable_id: topic.id, + }) + Metamaps.Mappings.add(mapping) - if (Create.newTopic.pinned) { - var nextCoords = AutoLayout.getNextCoord() - } - var mapping = new Metamaps.Backbone.Mapping({ - xloc: nextCoords ? nextCoords.x : Create.newTopic.x, - yloc: nextCoords ? nextCoords.y : Create.newTopic.y, - mappable_type: 'Topic', - mappable_id: topic.id, + self.renderTopic(mapping, topic, true, true) }) - Metamaps.Mappings.add(mapping) - - self.renderTopic(mapping, topic, true, true) }, getTopicFromSearch: function (event, id) { var self = Topic $(document).trigger(Map.events.editedByActiveMapper) - var topic = self.get(id) - - var nextCoords = AutoLayout.getNextCoord() - var mapping = new Metamaps.Backbone.Mapping({ - xloc: nextCoords.x, - yloc: nextCoords.y, - mappable_type: 'Topic', - mappable_id: topic.id, + self.get(id, (topic) => { + var nextCoords = AutoLayout.getNextCoord({ mappings: Metamaps.Mappings }) + var mapping = new Metamaps.Backbone.Mapping({ + xloc: nextCoords.x, + yloc: nextCoords.y, + mappable_type: 'Topic', + mappable_id: topic.id, + }) + Metamaps.Mappings.add(mapping) + self.renderTopic(mapping, topic, true, true) + GlobalUI.notifyUser('Topic was added to your map!') }) - Metamaps.Mappings.add(mapping) - - self.renderTopic(mapping, topic, true, true) - - GlobalUI.notifyUser('Topic was added to your map!') event.stopPropagation() event.preventDefault()