From 1efd78ad7bbff6fcc761df3ee38b92e39b58eae7 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Wed, 21 Sep 2016 14:27:49 +0800 Subject: [PATCH] initial attempt at focussing input field when entering multiple topics --- .../javascripts/src/Metamaps.Backbone.js | 8 +-- app/assets/javascripts/src/Metamaps.Import.js | 9 ++- .../javascripts/src/Metamaps.PasteInput.js | 55 ++++++------------- app/assets/javascripts/src/Metamaps.Topic.js | 19 ++++--- .../javascripts/src/Metamaps.TopicCard.js | 12 ++-- 5 files changed, 48 insertions(+), 55 deletions(-) diff --git a/app/assets/javascripts/src/Metamaps.Backbone.js b/app/assets/javascripts/src/Metamaps.Backbone.js index a37229d2..2c1f58af 100644 --- a/app/assets/javascripts/src/Metamaps.Backbone.js +++ b/app/assets/javascripts/src/Metamaps.Backbone.js @@ -63,7 +63,7 @@ Metamaps.Backbone.Map = Backbone.Model.extend({ authorizeToEdit: function (mapper) { if (mapper && ( this.get('permission') === 'commons' || - this.get('collaborator_ids').includes(mapper.get('id')) || + (this.get('collaborator_ids') || []).includes(mapper.get('id')) || this.get('user_id') === mapper.get('id'))) { return true } else { @@ -350,9 +350,9 @@ Metamaps.Backbone.init = function () { }, authorizeToEdit: function (mapper) { if (mapper && - (this.get('calculated_permission') === 'commons' || - this.get('collaborator_ids').includes(mapper.get('id')) || - this.get('user_id') === mapper.get('id'))) { + (this.get('user_id') === mapper.get('id') || + this.get('calculated_permission') === 'commons' || + this.get('collaborator_ids').includes(mapper.get('id')))) { return true } else { return false diff --git a/app/assets/javascripts/src/Metamaps.Import.js b/app/assets/javascripts/src/Metamaps.Import.js index 2ed7e00a..2dee51d0 100644 --- a/app/assets/javascripts/src/Metamaps.Import.js +++ b/app/assets/javascripts/src/Metamaps.Import.js @@ -254,7 +254,7 @@ Metamaps.Import = { }, createTopicWithParameters: function (name, metacode_name, permission, desc, - link, xloc, yloc, import_id) { + link, xloc, yloc, import_id, opts) { var self = Metamaps.Import $(document).trigger(Metamaps.Map.events.editedByActiveMapper) var metacode = Metamaps.Metacodes.where({name: metacode_name})[0] || null @@ -271,7 +271,8 @@ Metamaps.Import = { permission: topic_permission, defer_to_map_id: defer_to_map_id, desc: desc || "", - link: link || "" + link: link || "", + calculated_permission: Metamaps.Active.Map.get('permission') }) Metamaps.Topics.add(topic) @@ -288,7 +289,9 @@ Metamaps.Import = { Metamaps.Mappings.add(mapping) // this function also includes the creation of the topic in the database - Metamaps.Topic.renderTopic(mapping, topic, true, true) + Metamaps.Topic.renderTopic(mapping, topic, true, true, { + success: opts.success + }) Metamaps.GlobalUI.hideDiv('#instructions') }, diff --git a/app/assets/javascripts/src/Metamaps.PasteInput.js b/app/assets/javascripts/src/Metamaps.PasteInput.js index 55798587..aaf848d0 100644 --- a/app/assets/javascripts/src/Metamaps.PasteInput.js +++ b/app/assets/javascripts/src/Metamaps.PasteInput.js @@ -80,43 +80,24 @@ Metamaps.PasteInput = { var import_id = null // don't store a cidMapping var permission = null // use default - // try { - // // fetch title in 150ms or less - // Promise.race([ - // new Promise(function(resolve, reject) { - // fetch(text).then(function(response) { - // return response.text() - // }).then(function(html) { - // title = html.replace(/[\s\S]*(.*)<\/title>[\s\S]*/m, '$1') - // resolve() - // }) - // }), new Promise(function(resolve, reject) { - // window.setTimeout(function() { - // resolve() - // }, 150) - // }) - // ]).then(function() { - // finish() - // }).catch(function(error) { - // throw error - // }) - // } catch (err) { - // console.warn("Your browser can't fetch the title") // TODO move to webpack to avoid this error - // } - finish() - - function finish() { - Metamaps.Import.createTopicWithParameters( - title, - 'Reference', // metacode - todo fix - permission, - text, // desc - todo load from url? - text, // link - todo fix because this isn't being POSTed - coords.x, - coords.y, - import_id - ) - } + Metamaps.Import.createTopicWithParameters( + title, + 'Reference', // metacode - todo fix + permission, + text, // desc - todo load from url? + text, // link - todo fix because this isn't being POSTed + coords.x, + coords.y, + import_id, + { + success: function(topic) { + Metamaps.TopicCard.showCard(topic.get('node'), function() { + $('#showcard #titleActivator').click() + .find('textarea, input').focus() + }) + } + } + ) }, handleJSON: function (text) { diff --git a/app/assets/javascripts/src/Metamaps.Topic.js b/app/assets/javascripts/src/Metamaps.Topic.js index 52fabed3..9e6782cb 100644 --- a/app/assets/javascripts/src/Metamaps.Topic.js +++ b/app/assets/javascripts/src/Metamaps.Topic.js @@ -186,11 +186,10 @@ Metamaps.Topic = { error: function () {} }) }, - /* - * - * - */ - renderTopic: function (mapping, topic, createNewInDB, permitCreateSynapseAfter) { + + // opts is additional options in a hash + // TODO: move createNewInDB and permitCerateSYnapseAfter into opts + renderTopic: function (mapping, topic, createNewInDB, permitCreateSynapseAfter, opts) { var self = Metamaps.Topic var nodeOnViz, tempPos @@ -265,18 +264,24 @@ Metamaps.Topic = { }) } - var mappingSuccessCallback = function (mappingModel, response) { + var mappingSuccessCallback = function (mappingModel, response, topicModel) { var newTopicData = { mappingid: mappingModel.id, mappableid: mappingModel.get('mappable_id') } $(document).trigger(Metamaps.JIT.events.newTopic, [newTopicData]) + // call a success callback if provided + if (opts.success) { + opts.success(topicModel) + } } var topicSuccessCallback = function (topicModel, response) { if (Metamaps.Active.Map) { mapping.save({ mappable_id: topicModel.id }, { - success: mappingSuccessCallback, + success: function (model, response) { + mappingSuccessCallback(model, response, topicModel) + }, error: function (model, response) { console.log('error saving mapping to database') } diff --git a/app/assets/javascripts/src/Metamaps.TopicCard.js b/app/assets/javascripts/src/Metamaps.TopicCard.js index f1424ed9..1453104d 100644 --- a/app/assets/javascripts/src/Metamaps.TopicCard.js +++ b/app/assets/javascripts/src/Metamaps.TopicCard.js @@ -37,7 +37,7 @@ Metamaps.TopicCard = { * Will open the Topic Card for the node that it's passed * @param {$jit.Graph.Node} node */ - showCard: function (node) { + showCard: function (node, opts) { var self = Metamaps.TopicCard var topic = node.getData('topic') @@ -46,7 +46,11 @@ Metamaps.TopicCard = { self.authorizedToEdit = topic.authorizeToEdit(Metamaps.Active.Mapper) // populate the card that's about to show with the right topics data self.populateShowCard(topic) - $('.showcard').fadeIn('fast') + return $('.showcard').fadeIn('fast', function() { + if (opts.complete) { + opts.complete() + } + }) }, hideCard: function () { var self = Metamaps.TopicCard @@ -413,8 +417,8 @@ Metamaps.TopicCard = { nodeValues.attachments = '' } - var inmapsAr = topic.get('inmaps') - var inmapsLinks = topic.get('inmapsLinks') + var inmapsAr = topic.get('inmaps') || [] + var inmapsLinks = topic.get('inmapsLinks') || [] nodeValues.inmaps = '' if (inmapsAr.length < 6) { for (i = 0; i < inmapsAr.length; i++) {