2016-09-26 13:37:42 +08:00
|
|
|
/* global Metamaps, $, Hogan, Bloodhound */
|
2016-04-24 11:50:35 -04:00
|
|
|
|
2016-09-22 17:36:47 +08:00
|
|
|
import Mouse from './Mouse'
|
|
|
|
import Selected from './Selected'
|
|
|
|
import Synapse from './Synapse'
|
|
|
|
import Topic from './Topic'
|
|
|
|
import Visualize from './Visualize'
|
2016-09-22 18:31:56 +08:00
|
|
|
import GlobalUI from './GlobalUI'
|
2016-09-22 17:36:47 +08:00
|
|
|
|
2016-04-24 11:50:35 -04:00
|
|
|
/*
|
|
|
|
* Metamaps.Create.js
|
|
|
|
*
|
|
|
|
* Dependencies:
|
|
|
|
* - Metamaps.Backbone
|
|
|
|
* - Metamaps.Metacodes
|
2016-09-25 22:27:11 +08:00
|
|
|
* - Metamaps.Topics
|
2016-04-24 11:50:35 -04:00
|
|
|
*/
|
|
|
|
|
2016-09-22 15:21:59 +08:00
|
|
|
const Create = {
|
2016-04-24 11:50:35 -04:00
|
|
|
isSwitchingSet: false, // indicates whether the metacode set switch lightbox is open
|
|
|
|
selectedMetacodeSet: null,
|
|
|
|
selectedMetacodeSetIndex: null,
|
|
|
|
selectedMetacodeNames: [],
|
|
|
|
newSelectedMetacodeNames: [],
|
|
|
|
selectedMetacodes: [],
|
|
|
|
newSelectedMetacodes: [],
|
|
|
|
init: function () {
|
2016-09-22 15:21:59 +08:00
|
|
|
var self = Create
|
2016-04-24 11:50:35 -04:00
|
|
|
self.newTopic.init()
|
|
|
|
self.newSynapse.init()
|
|
|
|
|
|
|
|
// // SWITCHING METACODE SETS
|
|
|
|
|
|
|
|
$('#metacodeSwitchTabs').tabs({
|
|
|
|
selected: self.selectedMetacodeSetIndex
|
|
|
|
}).addClass('ui-tabs-vertical ui-helper-clearfix')
|
|
|
|
$('#metacodeSwitchTabs .ui-tabs-nav li').removeClass('ui-corner-top').addClass('ui-corner-left')
|
|
|
|
$('.customMetacodeList li').click(self.toggleMetacodeSelected) // within the custom metacode set tab
|
|
|
|
},
|
|
|
|
toggleMetacodeSelected: function () {
|
2016-09-22 15:21:59 +08:00
|
|
|
var self = Create
|
2016-04-24 11:50:35 -04:00
|
|
|
|
|
|
|
if ($(this).attr('class') != 'toggledOff') {
|
|
|
|
$(this).addClass('toggledOff')
|
|
|
|
var value_to_remove = $(this).attr('id')
|
|
|
|
var name_to_remove = $(this).attr('data-name')
|
|
|
|
self.newSelectedMetacodes.splice(self.newSelectedMetacodes.indexOf(value_to_remove), 1)
|
|
|
|
self.newSelectedMetacodeNames.splice(self.newSelectedMetacodeNames.indexOf(name_to_remove), 1)
|
|
|
|
} else if ($(this).attr('class') == 'toggledOff') {
|
|
|
|
$(this).removeClass('toggledOff')
|
|
|
|
self.newSelectedMetacodes.push($(this).attr('id'))
|
|
|
|
self.newSelectedMetacodeNames.push($(this).attr('data-name'))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
updateMetacodeSet: function (set, index, custom) {
|
2016-09-22 15:21:59 +08:00
|
|
|
if (custom && Create.newSelectedMetacodes.length == 0) {
|
2016-09-26 13:37:42 +08:00
|
|
|
window.alert('Please select at least one metacode to use!')
|
2016-04-24 11:50:35 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var codesToSwitchToIds
|
|
|
|
var metacodeModels = new Metamaps.Backbone.MetacodeCollection()
|
2016-09-22 15:21:59 +08:00
|
|
|
Create.selectedMetacodeSetIndex = index
|
|
|
|
Create.selectedMetacodeSet = 'metacodeset-' + set
|
2016-04-24 11:50:35 -04:00
|
|
|
|
|
|
|
if (!custom) {
|
|
|
|
codesToSwitchToIds = $('#metacodeSwitchTabs' + set).attr('data-metacodes').split(',')
|
|
|
|
$('.customMetacodeList li').addClass('toggledOff')
|
2016-09-22 15:21:59 +08:00
|
|
|
Create.selectedMetacodes = []
|
|
|
|
Create.selectedMetacodeNames = []
|
|
|
|
Create.newSelectedMetacodes = []
|
|
|
|
Create.newSelectedMetacodeNames = []
|
2016-04-24 11:50:35 -04:00
|
|
|
}
|
|
|
|
else if (custom) {
|
|
|
|
// uses .slice to avoid setting the two arrays to the same actual array
|
2016-09-22 15:21:59 +08:00
|
|
|
Create.selectedMetacodes = Create.newSelectedMetacodes.slice(0)
|
|
|
|
Create.selectedMetacodeNames = Create.newSelectedMetacodeNames.slice(0)
|
|
|
|
codesToSwitchToIds = Create.selectedMetacodes.slice(0)
|
2016-04-24 11:50:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// sort by name
|
|
|
|
for (var i = 0; i < codesToSwitchToIds.length; i++) {
|
|
|
|
metacodeModels.add(Metamaps.Metacodes.get(codesToSwitchToIds[i]))
|
|
|
|
}
|
|
|
|
metacodeModels.sort()
|
|
|
|
|
|
|
|
$('#metacodeImg, #metacodeImgTitle').empty()
|
|
|
|
$('#metacodeImg').removeData('cloudcarousel')
|
|
|
|
var newMetacodes = ''
|
|
|
|
metacodeModels.each(function (metacode) {
|
|
|
|
newMetacodes += '<img class="cloudcarousel" width="40" height="40" src="' + metacode.get('icon') + '" data-id="' + metacode.id + '" title="' + metacode.get('name') + '" alt="' + metacode.get('name') + '"/>'
|
|
|
|
})
|
|
|
|
|
|
|
|
$('#metacodeImg').empty().append(newMetacodes).CloudCarousel({
|
|
|
|
titleBox: $('#metacodeImgTitle'),
|
|
|
|
yRadius: 40,
|
|
|
|
xRadius: 190,
|
|
|
|
xPos: 170,
|
|
|
|
yPos: 40,
|
|
|
|
speed: 0.3,
|
|
|
|
mouseWheel: true,
|
|
|
|
bringToFront: true
|
|
|
|
})
|
|
|
|
|
2016-09-22 18:31:56 +08:00
|
|
|
GlobalUI.closeLightbox()
|
2016-04-24 11:50:35 -04:00
|
|
|
$('#topic_name').focus()
|
|
|
|
|
|
|
|
var mdata = {
|
|
|
|
'metacodes': {
|
2016-09-22 15:21:59 +08:00
|
|
|
'value': custom ? Create.selectedMetacodes.toString() : Create.selectedMetacodeSet
|
2016-04-24 11:50:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
dataType: 'json',
|
|
|
|
url: '/user/updatemetacodes',
|
|
|
|
data: mdata,
|
|
|
|
success: function (data) {
|
|
|
|
console.log('selected metacodes saved')
|
|
|
|
},
|
|
|
|
error: function () {
|
|
|
|
console.log('failed to save selected metacodes')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
cancelMetacodeSetSwitch: function () {
|
2016-09-22 15:21:59 +08:00
|
|
|
var self = Create
|
2016-04-24 11:50:35 -04:00
|
|
|
self.isSwitchingSet = false
|
|
|
|
|
|
|
|
if (self.selectedMetacodeSet != 'metacodeset-custom') {
|
|
|
|
$('.customMetacodeList li').addClass('toggledOff')
|
|
|
|
self.selectedMetacodes = []
|
|
|
|
self.selectedMetacodeNames = []
|
|
|
|
self.newSelectedMetacodes = []
|
|
|
|
self.newSelectedMetacodeNames = []
|
|
|
|
} else { // custom set is selected
|
|
|
|
// reset it to the current actual selection
|
|
|
|
$('.customMetacodeList li').addClass('toggledOff')
|
|
|
|
for (var i = 0; i < self.selectedMetacodes.length; i++) {
|
|
|
|
$('#' + self.selectedMetacodes[i]).removeClass('toggledOff')
|
|
|
|
}
|
|
|
|
// uses .slice to avoid setting the two arrays to the same actual array
|
|
|
|
self.newSelectedMetacodeNames = self.selectedMetacodeNames.slice(0)
|
|
|
|
self.newSelectedMetacodes = self.selectedMetacodes.slice(0)
|
|
|
|
}
|
|
|
|
$('#metacodeSwitchTabs').tabs('option', 'active', self.selectedMetacodeSetIndex)
|
|
|
|
$('#topic_name').focus()
|
|
|
|
},
|
|
|
|
newTopic: {
|
|
|
|
init: function () {
|
|
|
|
$('#topic_name').keyup(function () {
|
2016-09-22 15:21:59 +08:00
|
|
|
Create.newTopic.name = $(this).val()
|
2016-04-24 11:50:35 -04:00
|
|
|
})
|
2016-09-19 20:30:34 -04:00
|
|
|
|
|
|
|
$('.pinCarousel').click(function() {
|
2016-09-22 15:21:59 +08:00
|
|
|
if (Create.newTopic.pinned) {
|
2016-09-19 20:30:34 -04:00
|
|
|
$('.pinCarousel').removeClass('isPinned')
|
2016-09-22 15:21:59 +08:00
|
|
|
Create.newTopic.pinned = false
|
2016-09-19 20:30:34 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$('.pinCarousel').addClass('isPinned')
|
2016-09-22 15:21:59 +08:00
|
|
|
Create.newTopic.pinned = true
|
2016-09-19 20:30:34 -04:00
|
|
|
}
|
|
|
|
})
|
2016-04-24 11:50:35 -04:00
|
|
|
|
|
|
|
var topicBloodhound = new Bloodhound({
|
|
|
|
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
|
|
|
|
queryTokenizer: Bloodhound.tokenizers.whitespace,
|
|
|
|
remote: {
|
|
|
|
url: '/topics/autocomplete_topic?term=%QUERY',
|
|
|
|
wildcard: '%QUERY',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// initialize the autocomplete results for the metacode spinner
|
|
|
|
$('#topic_name').typeahead(
|
|
|
|
{
|
|
|
|
highlight: true,
|
|
|
|
minLength: 2,
|
|
|
|
},
|
|
|
|
[{
|
|
|
|
name: 'topic_autocomplete',
|
|
|
|
limit: 8,
|
|
|
|
display: function (s) { return s.label; },
|
|
|
|
templates: {
|
|
|
|
suggestion: function (s) {
|
|
|
|
return Hogan.compile($('#topicAutocompleteTemplate').html()).render(s)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
source: topicBloodhound,
|
|
|
|
}]
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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) {
|
2016-09-22 17:36:47 +08:00
|
|
|
Topic.getTopicFromAutocomplete(datum.id)
|
2016-04-24 11:50:35 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
// initialize metacode spinner and then hide it
|
|
|
|
$('#metacodeImg').CloudCarousel({
|
|
|
|
titleBox: $('#metacodeImgTitle'),
|
|
|
|
yRadius: 40,
|
|
|
|
xRadius: 190,
|
|
|
|
xPos: 170,
|
|
|
|
yPos: 40,
|
|
|
|
speed: 0.3,
|
|
|
|
mouseWheel: true,
|
|
|
|
bringToFront: true
|
|
|
|
})
|
|
|
|
$('.new_topic').hide()
|
2016-09-28 15:32:49 -04:00
|
|
|
$('#new_topic').attr('oncontextmenu','return false') //prevents the mouse up event from opening the default context menu on this element
|
2016-04-24 11:50:35 -04:00
|
|
|
},
|
|
|
|
name: null,
|
|
|
|
newId: 1,
|
|
|
|
beingCreated: false,
|
|
|
|
metacode: null,
|
|
|
|
x: null,
|
|
|
|
y: null,
|
|
|
|
addSynapse: false,
|
2016-09-19 20:30:34 -04:00
|
|
|
pinned: false,
|
2016-04-24 11:50:35 -04:00
|
|
|
open: function () {
|
|
|
|
$('#new_topic').fadeIn('fast', function () {
|
|
|
|
$('#topic_name').focus()
|
|
|
|
})
|
2016-09-22 15:21:59 +08:00
|
|
|
Create.newTopic.beingCreated = true
|
|
|
|
Create.newTopic.name = ''
|
2016-09-25 22:27:11 +08:00
|
|
|
GlobalUI.hideDiv('#instructions')
|
2016-04-24 11:50:35 -04:00
|
|
|
},
|
2016-09-19 20:30:34 -04:00
|
|
|
hide: function (force) {
|
2016-09-25 22:27:11 +08:00
|
|
|
if (Create.newTopic.beingCreated === false) return
|
2016-09-22 15:21:59 +08:00
|
|
|
if (force || !Create.newTopic.pinned) {
|
2016-09-19 20:30:34 -04:00
|
|
|
$('#new_topic').fadeOut('fast')
|
2016-09-22 15:21:59 +08:00
|
|
|
Create.newTopic.beingCreated = false
|
2016-09-19 20:30:34 -04:00
|
|
|
}
|
|
|
|
if (force) {
|
|
|
|
$('.pinCarousel').removeClass('isPinned')
|
2016-09-22 15:21:59 +08:00
|
|
|
Create.newTopic.pinned = false
|
2016-09-19 20:30:34 -04:00
|
|
|
}
|
2016-04-24 11:50:35 -04:00
|
|
|
$('#topic_name').typeahead('val', '')
|
2016-09-25 22:27:11 +08:00
|
|
|
if (Metamaps.Topics.length === 0) {
|
|
|
|
GlobalUI.showDiv('#instructions')
|
|
|
|
}
|
2016-04-24 11:50:35 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
newSynapse: {
|
|
|
|
init: function () {
|
2016-09-22 15:21:59 +08:00
|
|
|
var self = Create.newSynapse
|
2016-04-24 11:50:35 -04:00
|
|
|
|
|
|
|
var synapseBloodhound = new Bloodhound({
|
|
|
|
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
|
|
|
|
queryTokenizer: Bloodhound.tokenizers.whitespace,
|
|
|
|
remote: {
|
|
|
|
url: '/search/synapses?term=%QUERY',
|
|
|
|
wildcard: '%QUERY',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
var existingSynapseBloodhound = new Bloodhound({
|
|
|
|
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
|
|
|
|
queryTokenizer: Bloodhound.tokenizers.whitespace,
|
|
|
|
remote: {
|
|
|
|
url: '/search/synapses?topic1id=%TOPIC1&topic2id=%TOPIC2',
|
|
|
|
prepare: function (query, settings) {
|
2016-09-22 15:21:59 +08:00
|
|
|
var self = Create.newSynapse
|
2016-09-22 17:36:47 +08:00
|
|
|
if (Selected.Nodes.length < 2) {
|
2016-04-24 11:50:35 -04:00
|
|
|
settings.url = settings.url.replace('%TOPIC1', self.topic1id).replace('%TOPIC2', self.topic2id)
|
|
|
|
return settings
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// initialize the autocomplete results for synapse creation
|
|
|
|
$('#synapse_desc').typeahead(
|
|
|
|
{
|
|
|
|
highlight: true,
|
|
|
|
minLength: 2,
|
|
|
|
},
|
|
|
|
[{
|
|
|
|
name: 'synapse_autocomplete',
|
|
|
|
display: function (s) { return s.label; },
|
|
|
|
templates: {
|
|
|
|
suggestion: function (s) {
|
|
|
|
return Hogan.compile("<div class='genericSynapseDesc'>{{label}}</div>").render(s)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
source: synapseBloodhound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'existing_synapses',
|
|
|
|
limit: 50,
|
|
|
|
display: function (s) { return s.label; },
|
|
|
|
templates: {
|
|
|
|
suggestion: function (s) {
|
|
|
|
return Hogan.compile($('#synapseAutocompleteTemplate').html()).render(s)
|
|
|
|
},
|
|
|
|
header: '<h3>Existing synapses</h3>'
|
|
|
|
},
|
|
|
|
source: existingSynapseBloodhound,
|
|
|
|
}]
|
|
|
|
)
|
|
|
|
|
|
|
|
$('#synapse_desc').keyup(function (e) {
|
|
|
|
var ESC = 27, BACKSPACE = 8, DELETE = 46
|
|
|
|
if (e.keyCode === BACKSPACE && $(this).val() === '' ||
|
|
|
|
e.keyCode === DELETE && $(this).val() === '' ||
|
|
|
|
e.keyCode === ESC) {
|
2016-09-22 15:21:59 +08:00
|
|
|
Create.newSynapse.hide()
|
2016-04-24 11:50:35 -04:00
|
|
|
} // if
|
2016-09-22 15:21:59 +08:00
|
|
|
Create.newSynapse.description = $(this).val()
|
2016-04-24 11:50:35 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
$('#synapse_desc').focusout(function () {
|
2016-09-22 15:21:59 +08:00
|
|
|
if (Create.newSynapse.beingCreated) {
|
2016-09-22 17:36:47 +08:00
|
|
|
Synapse.createSynapseLocally()
|
2016-04-24 11:50:35 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
$('#synapse_desc').bind('typeahead:select', function (event, datum, dataset) {
|
|
|
|
if (datum.id) { // if they clicked on an existing synapse get it
|
2016-09-22 17:36:47 +08:00
|
|
|
Synapse.getSynapseFromAutocomplete(datum.id)
|
2016-04-24 11:50:35 -04:00
|
|
|
} else {
|
2016-09-22 15:21:59 +08:00
|
|
|
Create.newSynapse.description = datum.value
|
2016-09-22 17:36:47 +08:00
|
|
|
Synapse.createSynapseLocally()
|
2016-04-24 11:50:35 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
beingCreated: false,
|
|
|
|
description: null,
|
|
|
|
topic1id: null,
|
|
|
|
topic2id: null,
|
|
|
|
newSynapseId: null,
|
|
|
|
open: function () {
|
|
|
|
$('#new_synapse').fadeIn(100, function () {
|
|
|
|
$('#synapse_desc').focus()
|
|
|
|
})
|
2016-09-22 15:21:59 +08:00
|
|
|
Create.newSynapse.beingCreated = true
|
2016-04-24 11:50:35 -04:00
|
|
|
},
|
|
|
|
hide: function () {
|
|
|
|
$('#new_synapse').fadeOut('fast')
|
|
|
|
$('#synapse_desc').typeahead('val', '')
|
2016-09-22 15:21:59 +08:00
|
|
|
Create.newSynapse.beingCreated = false
|
|
|
|
Create.newTopic.addSynapse = false
|
|
|
|
Create.newSynapse.topic1id = 0
|
|
|
|
Create.newSynapse.topic2id = 0
|
2016-09-22 17:36:47 +08:00
|
|
|
Mouse.synapseStartCoordinates = []
|
|
|
|
Visualize.mGraph.plot()
|
2016-04-24 11:50:35 -04:00
|
|
|
},
|
|
|
|
}
|
2016-09-22 15:21:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Create
|