metamaps--metamaps/frontend/src/Metamaps/GlobalUI/index.js

639 lines
20 KiB
JavaScript
Raw Normal View History

2016-09-29 16:05:49 +00:00
/* global Metamaps, $, Hogan, Bloodhound, CanvasLoader */
2016-09-22 10:31:56 +00:00
import Active from './Active'
import Create from './Create'
import Filter from './Filter'
import Router from './Router'
/*
* Metamaps.Backbone
* Metamaps.Erb
* Metamaps.Maps
*/
2016-09-22 06:25:49 +00:00
const GlobalUI = {
2016-09-22 10:31:56 +00:00
notifyTimeout: null,
lightbox: null,
init: function () {
2016-09-29 16:05:49 +00:00
var self = GlobalUI
2016-09-22 06:25:49 +00:00
2016-09-29 16:05:49 +00:00
self.Search.init()
self.CreateMap.init()
self.Account.init()
2016-09-22 10:31:56 +00:00
if ($('#toast').html().trim()) self.notifyUser($('#toast').html())
2016-09-29 16:05:49 +00:00
// bind lightbox clicks
2016-09-22 10:31:56 +00:00
$('.openLightbox').click(function (event) {
2016-09-29 16:05:49 +00:00
self.openLightbox($(this).attr('data-open'))
event.preventDefault()
return false
})
2016-09-22 10:31:56 +00:00
2016-09-29 16:05:49 +00:00
$('#lightbox_screen, #lightbox_close').click(self.closeLightbox)
2016-09-22 10:31:56 +00:00
// initialize global backbone models and collections
2016-09-29 16:05:49 +00:00
if (Active.Mapper) Active.Mapper = new Metamaps.Backbone.Mapper(Active.Mapper)
2016-09-22 10:31:56 +00:00
2016-09-29 16:05:49 +00:00
var myCollection = Metamaps.Maps.Mine ? Metamaps.Maps.Mine : []
var sharedCollection = Metamaps.Maps.Shared ? Metamaps.Maps.Shared : []
var starredCollection = Metamaps.Maps.Starred ? Metamaps.Maps.Starred : []
var mapperCollection = []
var mapperOptionsObj = { id: 'mapper', sortBy: 'updated_at' }
2016-09-22 10:31:56 +00:00
if (Metamaps.Maps.Mapper) {
2016-09-29 16:05:49 +00:00
mapperCollection = Metamaps.Maps.Mapper.models
mapperOptionsObj.mapperId = Metamaps.Maps.Mapper.id
2016-09-22 10:31:56 +00:00
}
2016-09-29 16:05:49 +00:00
var featuredCollection = Metamaps.Maps.Featured ? Metamaps.Maps.Featured : []
var activeCollection = Metamaps.Maps.Active ? Metamaps.Maps.Active : []
Metamaps.Maps.Mine = new Metamaps.Backbone.MapsCollection(myCollection, { id: 'mine', sortBy: 'updated_at' })
Metamaps.Maps.Shared = new Metamaps.Backbone.MapsCollection(sharedCollection, { id: 'shared', sortBy: 'updated_at' })
Metamaps.Maps.Starred = new Metamaps.Backbone.MapsCollection(starredCollection, { id: 'starred', sortBy: 'updated_at' })
2016-09-22 10:31:56 +00:00
// 'Mapper' refers to another mapper
2016-09-29 16:05:49 +00:00
Metamaps.Maps.Mapper = new Metamaps.Backbone.MapsCollection(mapperCollection, mapperOptionsObj)
Metamaps.Maps.Featured = new Metamaps.Backbone.MapsCollection(featuredCollection, { id: 'featured', sortBy: 'updated_at' })
Metamaps.Maps.Active = new Metamaps.Backbone.MapsCollection(activeCollection, { id: 'active', sortBy: 'updated_at' })
2016-09-22 10:31:56 +00:00
},
showDiv: function (selector) {
$(selector).show()
$(selector).animate({
opacity: 1
}, 200, 'easeOutCubic')
},
hideDiv: function (selector) {
$(selector).animate({
opacity: 0
}, 200, 'easeInCubic', function () { $(this).hide() })
},
openLightbox: function (which) {
2016-09-29 16:05:49 +00:00
var self = GlobalUI
2016-09-22 10:31:56 +00:00
2016-09-29 16:05:49 +00:00
$('.lightboxContent').hide()
$('#' + which).show()
2016-09-22 10:31:56 +00:00
2016-09-29 16:05:49 +00:00
self.lightbox = which
2016-09-22 10:31:56 +00:00
2016-09-29 16:05:49 +00:00
$('#lightbox_overlay').show()
2016-09-22 10:31:56 +00:00
2016-09-29 16:05:49 +00:00
var heightOfContent = '-' + ($('#lightbox_main').height() / 2) + 'px'
2016-09-22 10:31:56 +00:00
// animate the content in from the bottom
$('#lightbox_main').animate({
'top': '50%',
'margin-top': heightOfContent
2016-09-29 16:05:49 +00:00
}, 200, 'easeOutCubic')
2016-09-22 10:31:56 +00:00
// fade the black overlay in
$('#lightbox_screen').animate({
'opacity': '0.42'
2016-09-29 16:05:49 +00:00
}, 200)
2016-09-22 10:31:56 +00:00
2016-09-29 16:05:49 +00:00
if (which === 'switchMetacodes') {
Create.isSwitchingSet = true
2016-09-22 10:31:56 +00:00
}
},
2016-09-22 06:25:49 +00:00
2016-09-22 10:31:56 +00:00
closeLightbox: function (event) {
2016-09-29 16:05:49 +00:00
var self = GlobalUI
2016-09-22 10:31:56 +00:00
2016-09-29 16:05:49 +00:00
if (event) event.preventDefault()
2016-09-22 10:31:56 +00:00
// animate the lightbox content offscreen
$('#lightbox_main').animate({
'top': '100%',
'margin-top': '0'
2016-09-29 16:05:49 +00:00
}, 200, 'easeInCubic')
2016-09-22 10:31:56 +00:00
// fade the black overlay out
$('#lightbox_screen').animate({
'opacity': '0.0'
}, 200, function () {
2016-09-29 16:05:49 +00:00
$('#lightbox_overlay').hide()
})
2016-09-22 10:31:56 +00:00
2016-09-29 16:05:49 +00:00
if (self.lightbox === 'forkmap') GlobalUI.CreateMap.reset('fork_map')
if (self.lightbox === 'newmap') GlobalUI.CreateMap.reset('new_map')
2016-09-22 10:31:56 +00:00
if (Create && Create.isSwitchingSet) {
2016-09-29 16:05:49 +00:00
Create.cancelMetacodeSetSwitch()
2016-09-22 10:31:56 +00:00
}
2016-09-29 16:05:49 +00:00
self.lightbox = null
2016-09-22 10:31:56 +00:00
},
notifyUser: function (message, leaveOpen) {
2016-09-29 16:05:49 +00:00
var self = GlobalUI
2016-09-22 10:31:56 +00:00
$('#toast').html(message)
self.showDiv('#toast')
2016-09-29 16:05:49 +00:00
clearTimeout(self.notifyTimeOut)
2016-09-22 10:31:56 +00:00
if (!leaveOpen) {
self.notifyTimeOut = setTimeout(function () {
2016-09-22 06:25:49 +00:00
self.hideDiv('#toast')
2016-09-29 16:05:49 +00:00
}, 8000)
2016-09-22 06:25:49 +00:00
}
2016-09-22 10:31:56 +00:00
},
2016-09-29 16:05:49 +00:00
clearNotify: function () {
var self = GlobalUI
2016-09-22 10:31:56 +00:00
2016-09-29 16:05:49 +00:00
clearTimeout(self.notifyTimeOut)
2016-09-22 10:31:56 +00:00
self.hideDiv('#toast')
},
2016-09-29 16:05:49 +00:00
shareInvite: function (inviteLink) {
window.prompt('To copy the invite link, press: Ctrl+C, Enter', inviteLink)
2016-09-22 10:31:56 +00:00
}
}
2016-09-22 06:25:49 +00:00
GlobalUI.CreateMap = {
2016-09-22 10:31:56 +00:00
newMap: null,
2016-09-29 16:05:49 +00:00
emptyMapForm: '',
emptyForkMapForm: '',
2016-09-22 10:31:56 +00:00
topicsToMap: [],
synapsesToMap: [],
init: function () {
2016-09-29 16:05:49 +00:00
var self = GlobalUI.CreateMap
2016-09-22 06:25:49 +00:00
2016-09-29 16:05:49 +00:00
self.newMap = new Metamaps.Backbone.Map({ permission: 'commons' })
2016-09-22 06:25:49 +00:00
2016-09-29 16:05:49 +00:00
self.bindFormEvents()
2016-09-22 06:25:49 +00:00
2016-09-29 16:05:49 +00:00
self.emptyMapForm = $('#new_map').html()
2016-09-22 10:31:56 +00:00
},
bindFormEvents: function () {
2016-09-29 16:05:49 +00:00
var self = GlobalUI.CreateMap
2016-09-22 06:25:49 +00:00
2016-09-29 16:05:49 +00:00
$('.new_map input, .new_map div').unbind('keypress').bind('keypress', function (event) {
2016-09-22 10:31:56 +00:00
if (event.keyCode === 13) self.submit()
})
2016-09-22 06:25:49 +00:00
2016-09-22 10:31:56 +00:00
$('.new_map button.cancel').unbind().bind('click', function (event) {
2016-09-29 16:05:49 +00:00
event.preventDefault()
GlobalUI.closeLightbox()
})
$('.new_map button.submitMap').unbind().bind('click', self.submit)
2016-09-22 10:31:56 +00:00
// bind permission changer events on the createMap form
2016-09-29 16:05:49 +00:00
$('.permIcon').unbind().bind('click', self.switchPermission)
2016-09-22 10:31:56 +00:00
},
closeSuccess: function () {
2016-09-29 16:05:49 +00:00
$('#mapCreatedSuccess').fadeOut(300, function () {
$(this).remove()
})
2016-09-22 10:31:56 +00:00
},
generateSuccessMessage: function (id) {
2016-09-29 16:05:49 +00:00
var stringStart = "<div id='mapCreatedSuccess'><h6>SUCCESS!</h6>Your map has been created. Do you want to: <a id='mapGo' href='/maps/"
stringStart += id
stringStart += "' onclick='GlobalUI.CreateMap.closeSuccess();'>Go to your new map</a>"
stringStart += "<span>OR</span><a id='mapStay' href='#' onclick='GlobalUI.CreateMap.closeSuccess(); return false;'>Stay on this "
var page = Active.Map ? 'map' : 'page'
var stringEnd = '</a></div>'
return stringStart + page + stringEnd
2016-09-22 10:31:56 +00:00
},
switchPermission: function () {
2016-09-29 16:05:49 +00:00
var self = GlobalUI.CreateMap
2016-09-22 06:25:49 +00:00
2016-09-29 16:05:49 +00:00
self.newMap.set('permission', $(this).attr('data-permission'))
$(this).siblings('.permIcon').find('.mapPermIcon').removeClass('selected')
$(this).find('.mapPermIcon').addClass('selected')
2016-09-22 06:25:49 +00:00
2016-09-29 16:05:49 +00:00
var permText = $(this).find('.tip').html()
$(this).parents('.new_map').find('.permText').html(permText)
2016-09-22 10:31:56 +00:00
},
submit: function (event) {
2016-09-29 16:05:49 +00:00
if (event) event.preventDefault()
2016-09-22 10:31:56 +00:00
2016-09-29 16:05:49 +00:00
var self = GlobalUI.CreateMap
2016-09-22 10:31:56 +00:00
if (GlobalUI.lightbox === 'forkmap') {
2016-09-29 16:05:49 +00:00
self.newMap.set('topicsToMap', self.topicsToMap)
self.newMap.set('synapsesToMap', self.synapsesToMap)
2016-09-22 10:31:56 +00:00
}
2016-09-29 16:05:49 +00:00
var formId = GlobalUI.lightbox === 'forkmap' ? '#fork_map' : '#new_map'
var $form = $(formId)
2016-09-22 10:31:56 +00:00
2016-09-29 16:05:49 +00:00
self.newMap.set('name', $form.find('#map_name').val())
self.newMap.set('desc', $form.find('#map_desc').val())
2016-09-22 10:31:56 +00:00
2016-09-29 16:05:49 +00:00
if (self.newMap.get('name').length === 0) {
self.throwMapNameError()
return
2016-09-22 10:31:56 +00:00
}
self.newMap.save(null, {
success: self.success
2016-09-29 16:05:49 +00:00
// TODO add error message
})
2016-09-22 10:31:56 +00:00
2016-09-29 16:05:49 +00:00
GlobalUI.closeLightbox()
GlobalUI.notifyUser('Working...')
2016-09-22 10:31:56 +00:00
},
throwMapNameError: function () {
2016-09-22 06:25:49 +00:00
2016-09-29 16:05:49 +00:00
var formId = GlobalUI.lightbox === 'forkmap' ? '#fork_map' : '#new_map'
var $form = $(formId)
2016-09-22 06:25:49 +00:00
2016-09-29 16:05:49 +00:00
var message = $("<div class='feedback_message'>Please enter a map name...</div>")
2016-09-22 06:25:49 +00:00
2016-09-29 16:05:49 +00:00
$form.find('#map_name').after(message)
setTimeout(function () {
message.fadeOut('fast', function () {
message.remove()
})
}, 5000)
2016-09-22 10:31:56 +00:00
},
success: function (model) {
2016-09-29 16:05:49 +00:00
var self = GlobalUI.CreateMap
// push the new map onto the collection of 'my maps'
Metamaps.Maps.Mine.add(model)
2016-09-22 06:25:49 +00:00
2016-09-29 16:05:49 +00:00
GlobalUI.clearNotify()
$('#wrapper').append(self.generateSuccessMessage(model.id))
2016-09-22 10:31:56 +00:00
},
reset: function (id) {
2016-09-29 16:05:49 +00:00
var self = GlobalUI.CreateMap
2016-09-22 06:25:49 +00:00
2016-09-29 16:05:49 +00:00
var form = $('#' + id)
2016-09-22 06:25:49 +00:00
2016-09-29 16:05:49 +00:00
if (id === 'fork_map') {
self.topicsToMap = []
self.synapsesToMap = []
form.html(self.emptyForkMapForm)
} else {
form.html(self.emptyMapForm)
2016-09-22 10:31:56 +00:00
}
2016-09-22 06:25:49 +00:00
2016-09-29 16:05:49 +00:00
self.bindFormEvents()
self.newMap = new Metamaps.Backbone.Map({ permission: 'commons' })
2016-09-22 10:31:56 +00:00
2016-09-29 16:05:49 +00:00
return false
}
}
2016-09-22 06:25:49 +00:00
GlobalUI.Account = {
2016-09-22 10:31:56 +00:00
isOpen: false,
changing: false,
init: function () {
2016-09-29 16:05:49 +00:00
var self = GlobalUI.Account
2016-09-22 10:31:56 +00:00
2016-09-29 16:05:49 +00:00
$('.sidebarAccountIcon').click(self.toggleBox)
$('.sidebarAccountBox').click(function (event) {
event.stopPropagation()
})
$('body').click(self.close)
2016-09-22 10:31:56 +00:00
},
toggleBox: function (event) {
2016-09-29 16:05:49 +00:00
var self = GlobalUI.Account
2016-09-22 10:31:56 +00:00
2016-09-29 16:05:49 +00:00
if (self.isOpen) self.close()
else self.open()
2016-09-22 10:31:56 +00:00
2016-09-29 16:05:49 +00:00
event.stopPropagation()
2016-09-22 10:31:56 +00:00
},
open: function () {
2016-09-29 16:05:49 +00:00
var self = GlobalUI.Account
2016-09-22 10:31:56 +00:00
2016-09-29 16:05:49 +00:00
Filter.close()
$('.sidebarAccountIcon .tooltipsUnder').addClass('hide')
2016-09-22 10:31:56 +00:00
if (!self.isOpen && !self.changing) {
2016-09-29 16:05:49 +00:00
self.changing = true
2016-09-22 10:31:56 +00:00
$('.sidebarAccountBox').fadeIn(200, function () {
2016-09-29 16:05:49 +00:00
self.changing = false
self.isOpen = true
$('.sidebarAccountBox #user_email').focus()
})
2016-09-22 10:31:56 +00:00
}
},
close: function () {
2016-09-29 16:05:49 +00:00
var self = GlobalUI.Account
2016-09-22 10:31:56 +00:00
2016-09-29 16:05:49 +00:00
$('.sidebarAccountIcon .tooltipsUnder').removeClass('hide')
2016-09-22 10:31:56 +00:00
if (!self.changing) {
2016-09-29 16:05:49 +00:00
self.changing = true
$('.sidebarAccountBox #user_email').blur()
2016-09-22 10:31:56 +00:00
$('.sidebarAccountBox').fadeOut(200, function () {
2016-09-29 16:05:49 +00:00
self.changing = false
self.isOpen = false
})
2016-09-22 06:25:49 +00:00
}
2016-09-22 10:31:56 +00:00
}
}
2016-09-22 06:25:49 +00:00
GlobalUI.Search = {
2016-09-22 06:25:49 +00:00
locked: false,
isOpen: false,
limitTopicsToMe: false,
limitMapsToMe: false,
timeOut: null,
changing: false,
optionsInitialized: false,
init: function () {
2016-09-29 16:05:49 +00:00
var self = GlobalUI.Search
2016-09-22 06:25:49 +00:00
2016-09-29 16:05:49 +00:00
// TODO does this overlap with Metamaps.Loading?
// devin sez: I'd like to remove Metamaps.Loading from the rails code
var loader = new CanvasLoader('searchLoading')
loader.setColor('#4fb5c0') // default is '#000000'
loader.setDiameter(24) // default is 40
loader.setDensity(41) // default is 40
loader.setRange(0.9) // default is 1.3
loader.show() // Hidden by default
2016-09-22 06:25:49 +00:00
// bind the hover events
2016-09-29 16:05:49 +00:00
$('.sidebarSearch').hover(function () {
2016-09-22 06:25:49 +00:00
self.open()
}, function () {
self.close(800, false)
2016-09-29 16:05:49 +00:00
})
2016-09-22 06:25:49 +00:00
$('.sidebarSearchIcon').click(function (e) {
2016-09-29 16:05:49 +00:00
$('.sidebarSearchField').focus()
})
2016-09-22 06:25:49 +00:00
$('.sidebarSearch').click(function (e) {
2016-09-29 16:05:49 +00:00
e.stopPropagation()
})
2016-09-22 06:25:49 +00:00
$('body').click(function (e) {
2016-09-29 16:05:49 +00:00
self.close(0, false)
})
2016-09-22 06:25:49 +00:00
// open if the search is closed and user hits ctrl+/
// close if they hit ESC
$('body').bind('keyup', function (e) {
switch (e.which) {
case 191:
if ((e.ctrlKey && !self.isOpen) || (e.ctrlKey && self.locked)) {
2016-09-29 16:05:49 +00:00
self.open(true) // true for focus
2016-09-22 06:25:49 +00:00
}
2016-09-29 16:05:49 +00:00
break
2016-09-22 06:25:49 +00:00
case 27:
if (self.isOpen) {
2016-09-29 16:05:49 +00:00
self.close(0, true)
2016-09-22 06:25:49 +00:00
}
2016-09-29 16:05:49 +00:00
break
2016-09-22 06:25:49 +00:00
default:
2016-09-29 16:05:49 +00:00
break // console.log(e.which)
2016-09-22 06:25:49 +00:00
}
2016-09-29 16:05:49 +00:00
})
2016-09-22 06:25:49 +00:00
2016-09-29 16:05:49 +00:00
self.startTypeahead()
2016-09-22 06:25:49 +00:00
},
2016-09-29 16:05:49 +00:00
lock: function () {
var self = GlobalUI.Search
self.locked = true
2016-09-22 06:25:49 +00:00
},
2016-09-29 16:05:49 +00:00
unlock: function () {
var self = GlobalUI.Search
self.locked = false
2016-09-22 06:25:49 +00:00
},
open: function (focus) {
2016-09-29 16:05:49 +00:00
var self = GlobalUI.Search
2016-09-22 06:25:49 +00:00
2016-09-29 16:05:49 +00:00
clearTimeout(self.timeOut)
2016-09-22 06:25:49 +00:00
if (!self.isOpen && !self.changing && !self.locked) {
2016-09-29 16:05:49 +00:00
self.changing = true
2016-09-22 06:25:49 +00:00
$('.sidebarSearch .twitter-typeahead, .sidebarSearch .tt-hint, .sidebarSearchField').animate({
width: '400px'
}, 300, function () {
2016-09-29 16:05:49 +00:00
if (focus) $('.sidebarSearchField').focus()
2016-09-22 06:25:49 +00:00
$('.sidebarSearchField, .sidebarSearch .tt-hint').css({
padding: '7px 10px 3px 10px',
width: '380px'
2016-09-29 16:05:49 +00:00
})
self.changing = false
self.isOpen = true
})
2016-09-22 06:25:49 +00:00
}
},
close: function (closeAfter, bypass) {
// for now
return
2016-09-29 16:05:49 +00:00
var self = GlobalUI.Search
2016-09-22 06:25:49 +00:00
self.timeOut = setTimeout(function () {
2016-09-29 16:05:49 +00:00
if (!self.locked && !self.changing && self.isOpen && (bypass || $('.sidebarSearchField.tt-input').val() === '')) {
self.changing = true
2016-09-22 06:25:49 +00:00
$('.sidebarSearchField, .sidebarSearch .tt-hint').css({
padding: '7px 0 3px 0',
width: '400px'
2016-09-29 16:05:49 +00:00
})
2016-09-22 06:25:49 +00:00
$('.sidebarSearch .twitter-typeahead, .sidebarSearch .tt-hint, .sidebarSearchField').animate({
width: '0'
}, 300, function () {
2016-09-29 16:05:49 +00:00
$('.sidebarSearchField').typeahead('val', '')
$('.sidebarSearchField').blur()
self.changing = false
self.isOpen = false
})
2016-09-22 06:25:49 +00:00
}
2016-09-29 16:05:49 +00:00
}, closeAfter)
2016-09-22 06:25:49 +00:00
},
startTypeahead: function () {
2016-09-29 16:05:49 +00:00
var self = GlobalUI.Search
2016-09-22 06:25:49 +00:00
2016-09-29 16:05:49 +00:00
var mapheader = Active.Mapper ? '<div class="searchMapsHeader searchHeader"><h3 class="search-heading">Maps</h3><input type="checkbox" class="limitToMe" id="limitMapsToMe"></input><label for="limitMapsToMe" class="limitToMeLabel">added by me</label><div class="minimizeResults minimizeMapResults"></div><div class="clearfloat"></div></div>' : '<div class="searchMapsHeader searchHeader"><h3 class="search-heading">Maps</h3><div class="minimizeResults minimizeMapResults"></div><div class="clearfloat"></div></div>'
var topicheader = Active.Mapper ? '<div class="searchTopicsHeader searchHeader"><h3 class="search-heading">Topics</h3><input type="checkbox" class="limitToMe" id="limitTopicsToMe"></input><label for="limitTopicsToMe" class="limitToMeLabel">added by me</label><div class="minimizeResults minimizeTopicResults"></div><div class="clearfloat"></div></div>' : '<div class="searchTopicsHeader searchHeader"><h3 class="search-heading">Topics</h3><div class="minimizeResults minimizeTopicResults"></div><div class="clearfloat"></div></div>'
var mapperheader = '<div class="searchMappersHeader searchHeader"><h3 class="search-heading">Mappers</h3><div class="minimizeResults minimizeMapperResults"></div><div class="clearfloat"></div></div>'
2016-09-22 06:25:49 +00:00
var topics = {
name: 'topics',
limit: 9999,
2016-09-29 16:05:49 +00:00
display: s => s.label,
2016-09-22 06:25:49 +00:00
templates: {
2016-09-29 16:05:49 +00:00
notFound: function (s) {
2016-09-22 06:25:49 +00:00
return Hogan.compile(topicheader + $('#topicSearchTemplate').html()).render({
2016-09-29 16:05:49 +00:00
value: 'No results',
label: 'No results',
2016-09-22 06:25:49 +00:00
typeImageURL: Metamaps.Erb['icons/wildcard.png'],
2016-09-29 16:05:49 +00:00
rtype: 'noresult'
})
2016-09-22 06:25:49 +00:00
},
header: topicheader,
2016-09-29 16:05:49 +00:00
suggestion: function (s) {
return Hogan.compile($('#topicSearchTemplate').html()).render(s)
}
2016-09-22 06:25:49 +00:00
},
source: new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/search/topics',
2016-09-29 16:05:49 +00:00
prepare: function (query, settings) {
settings.url += '?term=' + query
2016-09-22 10:31:56 +00:00
if (Active.Mapper && self.limitTopicsToMe) {
2016-09-29 16:05:49 +00:00
settings.url += '&user=' + Active.Mapper.id.toString()
2016-09-22 06:25:49 +00:00
}
2016-09-29 16:05:49 +00:00
return settings
}
}
})
}
2016-09-22 06:25:49 +00:00
var maps = {
name: 'maps',
limit: 9999,
2016-09-29 16:05:49 +00:00
display: s => s.label,
2016-09-22 06:25:49 +00:00
templates: {
2016-09-29 16:05:49 +00:00
notFound: function (s) {
2016-09-22 06:25:49 +00:00
return Hogan.compile(mapheader + $('#mapSearchTemplate').html()).render({
2016-09-29 16:05:49 +00:00
value: 'No results',
label: 'No results',
rtype: 'noresult'
})
2016-09-22 06:25:49 +00:00
},
header: mapheader,
2016-09-29 16:05:49 +00:00
suggestion: function (s) {
return Hogan.compile($('#mapSearchTemplate').html()).render(s)
}
2016-09-22 06:25:49 +00:00
},
source: new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/search/maps',
2016-09-29 16:05:49 +00:00
prepare: function (query, settings) {
settings.url += '?term=' + query
2016-09-22 10:31:56 +00:00
if (Active.Mapper && self.limitMapsToMe) {
2016-09-29 16:05:49 +00:00
settings.url += '&user=' + Active.Mapper.id.toString()
2016-09-22 06:25:49 +00:00
}
2016-09-29 16:05:49 +00:00
return settings
}
}
})
}
2016-09-22 06:25:49 +00:00
var mappers = {
name: 'mappers',
limit: 9999,
2016-09-29 16:05:49 +00:00
display: s => s.label,
2016-09-22 06:25:49 +00:00
templates: {
2016-09-29 16:05:49 +00:00
notFound: function (s) {
2016-09-22 06:25:49 +00:00
return Hogan.compile(mapperheader + $('#mapperSearchTemplate').html()).render({
2016-09-29 16:05:49 +00:00
value: 'No results',
label: 'No results',
rtype: 'noresult',
2016-09-22 06:25:49 +00:00
profile: Metamaps.Erb['user.png']
2016-09-29 16:05:49 +00:00
})
2016-09-22 06:25:49 +00:00
},
header: mapperheader,
2016-09-29 16:05:49 +00:00
suggestion: function (s) {
return Hogan.compile($('#mapperSearchTemplate').html()).render(s)
}
2016-09-22 06:25:49 +00:00
},
source: new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/search/mappers?term=%QUERY',
2016-09-29 16:05:49 +00:00
wildcard: '%QUERY'
}
})
}
2016-09-22 06:25:49 +00:00
// Take all that crazy setup data and put it together into one beautiful typeahead call!
$('.sidebarSearchField').typeahead(
{
2016-09-29 16:05:49 +00:00
highlight: true
2016-09-22 06:25:49 +00:00
},
[topics, maps, mappers]
2016-09-29 16:05:49 +00:00
)
2016-09-22 06:25:49 +00:00
2016-09-29 16:05:49 +00:00
// Set max height of the search results box to prevent it from covering bottom left footer
2016-09-22 06:25:49 +00:00
$('.sidebarSearchField').bind('typeahead:render', function (event) {
2016-09-29 16:05:49 +00:00
self.initSearchOptions()
self.hideLoader()
var h = $(window).height()
$('.tt-dropdown-menu').css('max-height', h - 100)
2016-09-22 06:25:49 +00:00
if (self.limitTopicsToMe) {
2016-09-29 16:05:49 +00:00
$('#limitTopicsToMe').prop('checked', true)
2016-09-22 06:25:49 +00:00
}
if (self.limitMapsToMe) {
2016-09-29 16:05:49 +00:00
$('#limitMapsToMe').prop('checked', true)
2016-09-22 06:25:49 +00:00
}
2016-09-29 16:05:49 +00:00
})
2016-09-22 06:25:49 +00:00
$(window).resize(function () {
2016-09-29 16:05:49 +00:00
var h = $(window).height()
$('.tt-dropdown-menu').css('max-height', h - 100)
})
2016-09-22 06:25:49 +00:00
// tell the autocomplete to launch a new tab with the topic, map, or mapper you clicked on
2016-09-29 16:05:49 +00:00
$('.sidebarSearchField').bind('typeahead:select', self.handleResultClick)
2016-09-22 06:25:49 +00:00
// don't do it, if they clicked on a 'addToMap' button
$('.sidebarSearch button.addToMap').click(function (event) {
2016-09-29 16:05:49 +00:00
event.stopPropagation()
})
2016-09-22 06:25:49 +00:00
// make sure that when you click on 'limit to me' or 'toggle section' it works
2016-09-29 16:05:49 +00:00
$('.sidebarSearchField.tt-input').keyup(function () {
2016-09-22 06:25:49 +00:00
if ($('.sidebarSearchField.tt-input').val() === '') {
2016-09-29 16:05:49 +00:00
self.hideLoader()
2016-09-22 06:25:49 +00:00
} else {
2016-09-29 16:05:49 +00:00
self.showLoader()
2016-09-22 06:25:49 +00:00
}
2016-09-29 16:05:49 +00:00
})
2016-09-22 06:25:49 +00:00
},
handleResultClick: function (event, datum, dataset) {
2016-09-29 16:05:49 +00:00
var self = GlobalUI.Search
self.hideLoader()
if (['topic', 'map', 'mapper'].indexOf(datum.rtype) !== -1) {
self.close(0, true)
if (datum.rtype === 'topic') {
Router.topics(datum.id)
} else if (datum.rtype === 'map') {
Router.maps(datum.id)
} else if (datum.rtype === 'mapper') {
Router.explore('mapper', datum.id)
2016-09-22 06:25:49 +00:00
}
}
},
initSearchOptions: function () {
2016-09-29 16:05:49 +00:00
var self = GlobalUI.Search
2016-09-22 06:25:49 +00:00
2016-09-29 16:05:49 +00:00
function toggleResultSet (set) {
var s = $('.tt-dataset-' + set + ' .tt-suggestion, .tt-dataset-' + set + ' .resultnoresult')
2016-09-22 06:25:49 +00:00
if (s.is(':visible')) {
2016-09-29 16:05:49 +00:00
s.hide()
$(this).removeClass('minimizeResults').addClass('maximizeResults')
2016-09-22 06:25:49 +00:00
} else {
2016-09-29 16:05:49 +00:00
s.show()
$(this).removeClass('maximizeResults').addClass('minimizeResults')
2016-09-22 06:25:49 +00:00
}
}
2016-09-29 16:05:49 +00:00
$('.limitToMe').unbind().bind('change', function (e) {
if ($(this).attr('id') === 'limitTopicsToMe') {
self.limitTopicsToMe = !self.limitTopicsToMe
2016-09-22 06:25:49 +00:00
}
2016-09-29 16:05:49 +00:00
if ($(this).attr('id') === 'limitMapsToMe') {
self.limitMapsToMe = !self.limitMapsToMe
2016-09-22 06:25:49 +00:00
}
// set the value of the search equal to itself to retrigger the
// autocomplete event
2016-09-29 16:05:49 +00:00
var searchQuery = $('.sidebarSearchField.tt-input').val()
$('.sidebarSearchField').typeahead('val', '')
.typeahead('val', searchQuery)
})
2016-09-22 06:25:49 +00:00
// when the user clicks minimize section, hide the results for that section
$('.minimizeMapperResults').unbind().click(function (e) {
2016-09-29 16:05:49 +00:00
toggleResultSet.call(this, 'mappers')
})
2016-09-22 06:25:49 +00:00
$('.minimizeTopicResults').unbind().click(function (e) {
2016-09-29 16:05:49 +00:00
toggleResultSet.call(this, 'topics')
})
2016-09-22 06:25:49 +00:00
$('.minimizeMapResults').unbind().click(function (e) {
2016-09-29 16:05:49 +00:00
toggleResultSet.call(this, 'maps')
})
2016-09-22 06:25:49 +00:00
},
hideLoader: function () {
2016-09-29 16:05:49 +00:00
$('#searchLoading').hide()
2016-09-22 06:25:49 +00:00
},
showLoader: function () {
2016-09-29 16:05:49 +00:00
$('#searchLoading').show()
2016-09-22 06:25:49 +00:00
}
}
export default GlobalUI