-
Notifications
You must be signed in to change notification settings - Fork 1
/
dia.js
99 lines (86 loc) · 2.6 KB
/
dia.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"use strict";
var DIA = (function(exports) {
var onloadCallback, overlay, modal, modalText, modalClose, confirmButtons, promptInput;
var close = function() {
overlay.hide();
modal.hide();
if (confirmButtons) {
confirmButtons.remove();
confirmButtons = null;
}
if (promptInput) {
promptInput.remove();
promptInput = null;
}
};
var show = function() {
overlay.show();
modal.show();
};
var alert = function(msg) {
modalText.text(msg);
show();
};
var confirm = function(msg, callback, buttons, isPrompt) {
if (!buttons) buttons = {OK:true, Cancel:false};
var html = '<div class="dia-buttons">';
for (var key in buttons) {
html += '<div class="dia-button" data-value="' + buttons[key] + '">' + key + '</div>';
}
html += '</div>';
modal.append(html);
confirmButtons = $('#dia-modal .dia-buttons');
$('#dia-modal .dia-button').on('click', function() {
if (isPrompt) callback(promptInput.val());
else callback($(this).data('value'));
close();
});
alert(msg);
};
var prompt = function(msg, callback, placeholder, buttons) {
modal.append('<input class="dia-input" ' + ((placeholder) ? 'placeholder="' + placeholder + '"' : '') + ' />');
promptInput = $('#dia-modal .dia-input');
confirm(msg, callback, buttons, true);
};
var init = function() {
var html = '<div id="dia-overlay"></div>';
$('body').append(html);
overlay = $('#dia-overlay');
overlay.hide();
html = '<div id="dia-modal"><div class="dia-close">x</div><div class="dia-text"></div></div>';
$('body').append(html);
modal = $('#dia-modal');
modal.hide();
modalText = $('#dia-modal .dia-text');
modalClose = $('#dia-modal .dia-close');
modalClose.on('click', close);
console.log(modalClose);
if (onloadCallback) onloadCallback();
};
var onload = function(callback) {
onloadCallback = callback;
};
var link = document.createElement('link');
link.setAttribute('rel', 'stylesheet');
link.setAttribute('href', 'dia.css');
document.getElementsByTagName('head')[0].appendChild(link);
if (typeof jQuery !== 'undefined') {
init();
} else {
var script = document.createElement('script');
script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js';
document.getElementsByTagName('head')[0].appendChild(script);
var checkReady = function(callback) {
if (typeof jQuery !== 'undefined') callback(jQuery);
else setTimeout(function() { checkReady(callback); }, 100);
};
checkReady(function($) {
init();
});
}
exports.alert = alert;
exports.confirm = confirm;
exports.prompt = prompt;
exports.onload = onload;
return exports;
}(DIA || {}));