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

160 lines
5 KiB
JavaScript
Raw Normal View History

2016-09-29 16:20:16 +00:00
/* global Metamaps, $ */
import clipboard from 'clipboard-js'
2016-09-29 16:20:16 +00:00
import Active from '../Active'
import Create from '../Create'
import Search from './Search'
import CreateMap from './CreateMap'
import Account from './Account'
import ImportDialog from './ImportDialog'
2016-09-22 10:31:56 +00:00
/*
* Metamaps.DataModel
2016-09-22 10:31:56 +00:00
* 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()
self.ImportDialog.init(Metamaps.Erb, self.openLightbox, self.closeLightbox)
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
if (Active.Mapper) Active.Mapper = new Metamaps.DataModel.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.DataModel.MapCollection(myCollection, { id: 'mine', sortBy: 'updated_at' })
Metamaps.Maps.Shared = new Metamaps.DataModel.MapCollection(sharedCollection, { id: 'shared', sortBy: 'updated_at' })
Metamaps.Maps.Starred = new Metamaps.DataModel.MapCollection(starredCollection, { id: 'starred', sortBy: 'updated_at' })
2016-09-22 10:31:56 +00:00
// 'Mapper' refers to another mapper
Metamaps.Maps.Mapper = new Metamaps.DataModel.MapCollection(mapperCollection, mapperOptionsObj)
Metamaps.Maps.Featured = new Metamaps.DataModel.MapCollection(featuredCollection, { id: 'featured', sortBy: 'updated_at' })
Metamaps.Maps.Active = new Metamaps.DataModel.MapCollection(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) {
clipboard.copy({
'text/plain': inviteLink
}).then(() => {
$('#joinCodesBox .popup').remove()
$('#joinCodesBox').append('<p class="popup" style="text-align: center">Copied!</p>')
window.setTimeout(() => $('#joinCodesBox .popup').remove(), 1500)
}, () => {
$('#joinCodesBox .popup').remove()
$('#joinCodesBox').append(`<p class="popup" style="text-align: center">Your browser doesn't support copying, please copy manually.</p>`)
window.setTimeout(() => $('#joinCodesBox .popup').remove(), 1500)
})
2016-09-22 10:31:56 +00:00
}
}
2016-09-22 06:25:49 +00:00
export { Search, CreateMap, Account, ImportDialog }
export default GlobalUI