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

158 lines
4.1 KiB
JavaScript
Raw Normal View History

/* global $ */
2016-09-30 00:20:16 +08:00
import clipboard from 'clipboard-js'
2016-09-30 00:20:16 +08:00
import Create from '../Create'
2017-03-11 01:49:27 -05:00
import ReactApp from './ReactApp'
2016-09-30 00:20:16 +08:00
import Search from './Search'
import CreateMap from './CreateMap'
import Account from './Account'
import ImportDialog from './ImportDialog'
2016-09-22 18:31:56 +08:00
const GlobalUI = {
2016-09-22 18:31:56 +08:00
notifyTimeout: null,
notifyQueue: [],
notifying: false,
2016-09-22 18:31:56 +08:00
lightbox: null,
2016-11-07 15:25:08 -05:00
init: function(serverData) {
const self = GlobalUI
2016-09-22 14:25:49 +08:00
2017-03-12 00:56:59 -05:00
self.ReactApp.init(serverData, self.openLightbox)
2016-10-03 08:32:37 +08:00
self.CreateMap.init(serverData)
self.Account.init(serverData)
self.ImportDialog.init(serverData, self.openLightbox, self.closeLightbox)
2017-03-12 12:26:49 -04:00
self.Search.init(serverData)
2016-09-22 18:31:56 +08:00
2017-03-13 12:22:52 -04:00
if (serverData.toast) self.notifyUser(serverData.toast)
2016-09-22 18:31:56 +08:00
2016-09-30 00:05:49 +08:00
// bind lightbox clicks
2016-11-07 15:25:08 -05:00
$('.openLightbox').click(function(event) {
2016-09-30 00:05:49 +08:00
self.openLightbox($(this).attr('data-open'))
event.preventDefault()
return false
})
2016-09-22 18:31:56 +08:00
2016-09-30 00:05:49 +08:00
$('#lightbox_screen, #lightbox_close').click(self.closeLightbox)
2016-09-22 18:31:56 +08:00
},
2016-11-07 15:25:08 -05:00
showDiv: function(selector) {
2016-09-22 18:31:56 +08:00
$(selector).show()
$(selector).animate({
opacity: 1
}, 200, 'easeOutCubic')
},
2016-11-07 15:25:08 -05:00
hideDiv: function(selector) {
2016-09-22 18:31:56 +08:00
$(selector).animate({
opacity: 0
2016-11-07 15:25:08 -05:00
}, 200, 'easeInCubic', function() { $(this).hide() })
2016-09-22 18:31:56 +08:00
},
2016-11-07 15:25:08 -05:00
openLightbox: function(which) {
const self = GlobalUI
2016-09-22 18:31:56 +08:00
2016-09-30 00:05:49 +08:00
$('.lightboxContent').hide()
$('#' + which).show()
2016-09-22 18:31:56 +08:00
2016-09-30 00:05:49 +08:00
self.lightbox = which
2016-09-22 18:31:56 +08:00
2016-09-30 00:05:49 +08:00
$('#lightbox_overlay').show()
2016-09-22 18:31:56 +08:00
2016-09-30 00:05:49 +08:00
var heightOfContent = '-' + ($('#lightbox_main').height() / 2) + 'px'
2016-09-22 18:31:56 +08:00
// animate the content in from the bottom
$('#lightbox_main').animate({
'top': '50%',
'margin-top': heightOfContent
2016-09-30 00:05:49 +08:00
}, 200, 'easeOutCubic')
2016-09-22 18:31:56 +08:00
// fade the black overlay in
$('#lightbox_screen').animate({
'opacity': '0.42'
2016-09-30 00:05:49 +08:00
}, 200)
2016-09-22 18:31:56 +08:00
2016-09-30 00:05:49 +08:00
if (which === 'switchMetacodes') {
Create.isSwitchingSet = true
2016-09-22 18:31:56 +08:00
}
},
2016-09-22 14:25:49 +08:00
2016-11-07 15:25:08 -05:00
closeLightbox: function(event) {
const self = GlobalUI
2016-09-22 18:31:56 +08:00
2016-09-30 00:05:49 +08:00
if (event) event.preventDefault()
2016-09-22 18:31:56 +08:00
// animate the lightbox content offscreen
$('#lightbox_main').animate({
'top': '100%',
'margin-top': '0'
2016-09-30 00:05:49 +08:00
}, 200, 'easeInCubic')
2016-09-22 18:31:56 +08:00
// fade the black overlay out
$('#lightbox_screen').animate({
'opacity': '0.0'
2016-11-07 15:25:08 -05:00
}, 200, function() {
2016-09-30 00:05:49 +08:00
$('#lightbox_overlay').hide()
})
2016-09-22 18:31:56 +08:00
2016-09-30 00:05:49 +08:00
if (self.lightbox === 'forkmap') GlobalUI.CreateMap.reset('fork_map')
if (self.lightbox === 'newmap') GlobalUI.CreateMap.reset('new_map')
2016-09-22 18:31:56 +08:00
if (Create && Create.isSwitchingSet) {
2016-09-30 00:05:49 +08:00
Create.cancelMetacodeSetSwitch()
2016-09-22 18:31:56 +08:00
}
2016-09-30 00:05:49 +08:00
self.lightbox = null
2016-09-22 18:31:56 +08:00
},
notifyUser: function(message, opts = {}) {
const self = GlobalUI
if (self.notifying) {
self.notifyQueue.push({ message, opts })
return
} else {
self._notifyUser(message, opts)
}
},
// note: use the wrapper function notifyUser instead of this one
_notifyUser: function(message, opts = {}) {
const self = GlobalUI
2017-03-13 12:22:52 -04:00
const { leaveOpen = false, timeOut = 5000 } = opts
ReactApp.toast = message
ReactApp.render()
2016-09-30 00:05:49 +08:00
clearTimeout(self.notifyTimeOut)
2016-09-22 18:31:56 +08:00
if (!leaveOpen) {
2016-11-07 15:25:08 -05:00
self.notifyTimeOut = setTimeout(function() {
GlobalUI.clearNotify()
}, timeOut)
2016-09-22 14:25:49 +08:00
}
self.notifying = true
2016-09-22 18:31:56 +08:00
},
2016-11-07 15:25:08 -05:00
clearNotify: function() {
const self = GlobalUI
// if there are messages remaining, display them
if (self.notifyQueue.length > 0) {
const { message, opts } = self.notifyQueue.shift()
self._notifyUser(message, opts)
} else {
2017-03-13 12:22:52 -04:00
ReactApp.toast = null
ReactApp.render()
self.notifying = false
}
2016-09-22 18:31:56 +08:00
},
2016-11-07 15:25:08 -05: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 18:31:56 +08:00
}
}
2016-09-22 14:25:49 +08:00
2017-03-11 01:49:27 -05:00
export { ReactApp, Search, CreateMap, Account, ImportDialog }
export default GlobalUI