wikijs-fork/client/js/components/alerts.js

110 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-02-09 01:52:37 +00:00
'use strict'
2016-08-24 01:09:09 +00:00
/* global Vue, _ */
2016-08-24 01:09:09 +00:00
/**
* Alerts
*/
class Alerts { // eslint-disable-line no-unused-vars
/**
* Constructor
*
* @class
*/
2017-02-09 01:52:37 +00:00
constructor () {
let self = this
self.mdl = new Vue({
el: '#alerts',
data: {
children: []
},
methods: {
acknowledge: (uid) => {
self.close(uid)
}
}
})
self.uidNext = 1
}
2016-08-24 01:09:09 +00:00
/**
* Show a new Alert
*
* @param {Object} options Alert properties
* @return {null} Void
*/
2017-02-09 01:52:37 +00:00
push (options) {
let self = this
2016-08-24 01:09:09 +00:00
2017-02-09 01:52:37 +00:00
let nAlert = _.defaults(options, {
_uid: self.uidNext,
class: 'info',
message: '---',
sticky: false,
title: '---'
})
2016-08-24 01:09:09 +00:00
2017-02-09 01:52:37 +00:00
self.mdl.children.push(nAlert)
2016-08-24 01:09:09 +00:00
2017-02-09 01:52:37 +00:00
if (!nAlert.sticky) {
_.delay(() => {
self.close(nAlert._uid)
}, 5000)
}
2016-08-24 01:09:09 +00:00
2017-02-09 01:52:37 +00:00
self.uidNext++
}
2016-08-24 01:09:09 +00:00
/**
* Shorthand method for pushing errors
*
* @param {String} title The title
* @param {String} message The message
*/
2017-02-09 01:52:37 +00:00
pushError (title, message) {
this.push({
class: 'error',
message,
sticky: false,
title
})
}
2016-08-24 01:09:09 +00:00
/**
* Shorthand method for pushing success messages
*
* @param {String} title The title
* @param {String} message The message
*/
2017-02-09 01:52:37 +00:00
pushSuccess (title, message) {
this.push({
class: 'success',
message,
sticky: false,
title
})
}
2016-08-24 01:09:09 +00:00
/**
* Close an alert
*
* @param {Integer} uid The unique ID of the alert
*/
2017-02-09 01:52:37 +00:00
close (uid) {
let self = this
let nAlertIdx = _.findIndex(self.mdl.children, ['_uid', uid])
let nAlert = _.nth(self.mdl.children, nAlertIdx)
if (nAlertIdx >= 0 && nAlert) {
nAlert.class += ' exit'
Vue.set(self.mdl.children, nAlertIdx, nAlert)
_.delay(() => {
self.mdl.children.splice(nAlertIdx, 1)
}, 500)
}
}
}