From 85dcad928f4aa03ef12c004d43216488cf8ab3d5 Mon Sep 17 00:00:00 2001 From: Connor Turland Date: Thu, 6 Oct 2016 09:12:01 -0400 Subject: [PATCH] enable pulling in of references to maps through typeahead (#636) --- .../javascripts/src/Metamaps.Erb.js.erb | 1 + app/controllers/topics_controller.rb | 19 ++++++++---- app/helpers/topics_helper.rb | 18 ++++++----- app/models/topic.rb | 10 ++++++ frontend/src/Metamaps/Create.js | 9 +++++- frontend/src/Metamaps/Topic.js | 31 +++++++++++++++++++ frontend/src/Metamaps/TopicCard.js | 11 +++++-- 7 files changed, 82 insertions(+), 17 deletions(-) diff --git a/app/assets/javascripts/src/Metamaps.Erb.js.erb b/app/assets/javascripts/src/Metamaps.Erb.js.erb index e8f3a25b..75535f82 100644 --- a/app/assets/javascripts/src/Metamaps.Erb.js.erb +++ b/app/assets/javascripts/src/Metamaps.Erb.js.erb @@ -8,6 +8,7 @@ window.Metamaps = window.Metamaps || {} Metamaps.Erb = {} Metamaps.Erb['REALTIME_SERVER'] = '<%= ENV['REALTIME_SERVER'] %>' +Metamaps.Erb['RAILS_ENV'] = '<%= ENV['RAILS_ENV'] %>' Metamaps.Erb['junto_spinner_darkgrey.gif'] = '<%= asset_path('junto_spinner_darkgrey.gif') %>' Metamaps.Erb['user.png'] = '<%= asset_path('user.png') %>' Metamaps.Erb['icons/wildcard.png'] = '<%= asset_path('icons/wildcard.png') %>' diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index ce430f43..986266be 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -10,12 +10,19 @@ class TopicsController < ApplicationController # GET /topics/autocomplete_topic def autocomplete_topic term = params[:term] - @topics = if term && !term.empty? - policy_scope(Topic.where('LOWER("name") like ?', term.downcase + '%')).order('"name"') - else - [] - end - render json: autocomplete_array_json(@topics) + if term && !term.empty? + @topics = policy_scope(Topic.where('LOWER("name") like ?', term.downcase + '%')).order('"name"') + @mapTopics = @topics.select { |t| t.metacode.name == 'Metamap' } + # prioritize topics which point to maps, over maps + @exclude = @mapTopics.length > 0 ? @mapTopics.map(&:name) : [''] + @maps = policy_scope(Map.where('LOWER("name") like ? AND name NOT IN (?)', term.downcase + '%', @exclude)).order('"name"') + else + @topics = [] + @maps = [] + end + @all= @topics.concat(@maps).sort { |a, b| a.name <=> b.name } + + render json: autocomplete_array_json(@all) end # GET topics/:id diff --git a/app/helpers/topics_helper.rb b/app/helpers/topics_helper.rb index e1a1d179..926e51b1 100644 --- a/app/helpers/topics_helper.rb +++ b/app/helpers/topics_helper.rb @@ -3,21 +3,23 @@ module TopicsHelper ## this one is for building our custom JSON autocomplete format for typeahead def autocomplete_array_json(topics) topics.map do |t| + is_map = t.is_a?(Map) { id: t.id, label: t.name, value: t.name, description: t.desc ? t.desc&.truncate(70) : '', # make this return matched results - type: t.metacode.name, - typeImageURL: t.metacode.icon, - permission: t.permission, - mapCount: t.maps.count, - synapseCount: t.synapses.count, originator: t.user.name, originatorImage: t.user.image.url(:thirtytwo), - rtype: :topic, - inmaps: t.inmaps, - inmapsLinks: t.inmapsLinks + permission: t.permission, + + rtype: is_map ? 'map' : 'topic', + inmaps: is_map ? [] : t.inmaps, + inmapsLinks: is_map ? [] : t.inmapsLinks + type: is_map ? metamapsMetacode.name : t.metacode.name, + typeImageURL: is_map ? metamapMetacode.icon : t.metacode.icon, + mapCount: is_map ? 0 : t.maps.count, + synapseCount: is_map ? 0 : t.synapses.count, } end end diff --git a/app/models/topic.rb b/app/models/topic.rb index 7d83ecac..c3028cc4 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -15,6 +15,8 @@ class Topic < ApplicationRecord belongs_to :metacode + before_create :create_metamap? + validates :permission, presence: true validates :permission, inclusion: { in: Perm::ISSIONS.map(&:to_s) } @@ -128,4 +130,12 @@ class Topic < ApplicationRecord def mk_permission Perm.short(permission) end + + protected + def create_metamap? + if link == '' and metacode.name == 'Metamap' + @map = Map.create({ name: name, permission: permission, desc: '', arranged: true, user_id: user_id }) + self.link = Rails.application.routes.url_helpers.map_url(:host => ENV['MAILER_DEFAULT_URL'], :id => @map.id) + end + end end diff --git a/frontend/src/Metamaps/Create.js b/frontend/src/Metamaps/Create.js index 87b91540..e52024be 100644 --- a/frontend/src/Metamaps/Create.js +++ b/frontend/src/Metamaps/Create.js @@ -194,7 +194,14 @@ const Create = { // tell the autocomplete to submit the form with the topic you clicked on if you pick from the autocomplete $('#topic_name').bind('typeahead:select', function (event, datum, dataset) { - Topic.getTopicFromAutocomplete(datum.id) + if (datum.rtype === 'topic') { + Topic.getTopicFromAutocomplete(datum.id) + } else if (datum.rtype === 'map') { + Topic.getMapFromAutocomplete({ + id: datum.id, + name: datum.label + }) + } }) // initialize metacode spinner and then hide it diff --git a/frontend/src/Metamaps/Topic.js b/frontend/src/Metamaps/Topic.js index 5be6cc57..0de5526a 100644 --- a/frontend/src/Metamaps/Topic.js +++ b/frontend/src/Metamaps/Topic.js @@ -356,6 +356,37 @@ const Topic = { self.renderTopic(mapping, topic, true, true) }) }, + getMapFromAutocomplete: function (data) { + var self = Metamaps.Topic + + // hide the 'double-click to add a topic' message + Metamaps.GlobalUI.hideDiv('#instructions') + + $(document).trigger(Metamaps.Map.events.editedByActiveMapper) + + var metacode = Metamaps.Metacodes.findWhere({ name: 'Metamap' }) + + var topic = new Metamaps.Backbone.Topic({ + name: data.name, + metacode_id: metacode.id, + defer_to_map_id: Metamaps.Active.Map.id, + link: window.location.origin + '/maps/' + data.id + }) + Metamaps.Topics.add(topic) + + var mapping = new Metamaps.Backbone.Mapping({ + xloc: Metamaps.Create.newTopic.x, + yloc: Metamaps.Create.newTopic.y, + mappable_id: topic.cid, + mappable_type: 'Topic', + }) + Metamaps.Mappings.add(mapping) + + // these can't happen until the value is retrieved, which happens in the line above + Metamaps.Create.newTopic.hide() + + self.renderTopic(mapping, topic, true, true) // this function also includes the creation of the topic in the database + }, getTopicFromSearch: function (event, id) { var self = Topic diff --git a/frontend/src/Metamaps/TopicCard.js b/frontend/src/Metamaps/TopicCard.js index 0b2d1497..40c51fbd 100644 --- a/frontend/src/Metamaps/TopicCard.js +++ b/frontend/src/Metamaps/TopicCard.js @@ -135,9 +135,13 @@ const TopicCard = { loader.setRange(0.9); // default is 1.3 loader.show() // Hidden by default var e = embedly('card', document.getElementById('embedlyLink')) - if (!e) { + if (!e && Metamaps.Erb.RAILS_ENV != 'development') { self.handleInvalidLink() } + else if (!e) { + $('#embedlyLink').attr('target', '_blank').html(topic.get('link')).show() + $('#embedlyLinkLoader').hide() + } } }, 100) } @@ -154,8 +158,11 @@ const TopicCard = { loader.show() // Hidden by default var e = embedly('card', document.getElementById('embedlyLink')) self.showLinkRemover() - if (!e) { + if (!e && Metamaps.Erb.RAILS_ENV != 'development') { self.handleInvalidLink() + } else if (!e) { + $('#embedlyLink').attr('target', '_blank').html(topic.get('link')).show() + $('#embedlyLinkLoader').hide() } }