element/packages/notification/src/main.js

58 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-07-27 06:15:02 +00:00
import Vue from 'vue';
2016-08-11 03:11:59 +00:00
let NotificationConstructor = Vue.extend(require('./main.vue'));
2016-07-27 06:15:02 +00:00
2016-08-11 03:11:59 +00:00
let instance;
let instances = [];
let seed = 1;
2016-07-27 06:15:02 +00:00
var Notification = function(options) {
options = options || {};
2016-08-11 03:11:59 +00:00
let userOnClose = options.onClose;
let id = 'notification_' + seed++;
2016-07-27 06:15:02 +00:00
options.onClose = function() {
Notification.close(id, userOnClose);
};
instance = new NotificationConstructor({
data: options
});
instance.id = id;
instance.vm = instance.$mount();
2016-08-11 02:40:34 +00:00
document.body.appendChild(instance.vm.$el);
instance.vm.visible = true;
2016-07-27 06:15:02 +00:00
instance.dom = instance.vm.$el;
2016-08-11 03:11:59 +00:00
let topDist = 0;
for (let i = 0, len = instances.length; i < len; i++) {
2016-07-27 06:15:02 +00:00
topDist += instances[i].$el.offsetHeight + 16;
}
topDist += 16;
instance.top = topDist;
instances.push(instance);
};
Notification.close = function(id, userOnClose) {
2016-08-11 03:11:59 +00:00
let index;
let removedHeight;
2016-07-27 06:15:02 +00:00
for (var i = 0, len = instances.length; i < len; i++) {
if (id === instances[i].id) {
if (typeof userOnClose === 'function') {
userOnClose(instances[i]);
}
2016-08-11 03:11:59 +00:00
index = i;
removedHeight = instances[i].dom.offsetHeight;
2016-07-27 06:15:02 +00:00
instances.splice(i, 1);
break;
}
}
if (len > 1) {
for (i = index; i < len - 1 ; i++) {
2016-08-11 02:40:34 +00:00
instances[i].dom.style.top = parseInt(instances[i].dom.style.top, 10) - removedHeight - 16 + 'px';
2016-07-27 06:15:02 +00:00
}
}
};
export default Notification;