Repository navigation
Expand file tree
/
Copy pathavalon.dialog.js
More file actions
184 lines (181 loc) · 7.38 KB
/
Copy pathavalon.dialog.js
File metadata and controls
184 lines (181 loc) · 7.38 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
(function(avalon) {
var defaults = {
toggle: true,
width: 300,
minHeight: 150,
height: "auto",
minWidth: 150,
close: avalon.noop,
parent: "body",
modal: false,
autoOpen: true
};
var positionfixed = true;
new function() {
var test = document.createElement('div'),
control = test.cloneNode(false),
fake = false,
root = document.body || (function() {
fake = true;
return document.documentElement.appendChild(document.createElement('body'));
}());
var oldCssText = root.style.cssText;
root.style.cssText = 'padding:0;margin:0';
test.style.cssText = 'position:fixed;top:42px';
root.appendChild(test);
root.appendChild(control);
positionfixed = test.offsetTop !== control.offsetTop;
root.removeChild(test);
root.removeChild(control);
root.style.cssText = oldCssText;
if (fake) {
document.documentElement.removeChild(root);
}
};
if (window.Node && Node.prototype && !Node.prototype.contains) {
Node.prototype.contains = function(arg) {
return !!(this.compareDocumentPosition(arg) & 16);
};
}
var domParser = document.createElement("div");
domParser.innerHTML = '<div class="ui-widget-overlay ui-front"> </div>';
var overlay = domParser.firstChild;//全部dialog共用
avalon.ui.dialog = function(element, id, opts, model) {
var $element = avalon(element);
var options = avalon.mix({}, defaults);
avalon.mix(options, $element.data());
options.toggle = !!options.autoOpen;
if (!options.title) {
options.title = element.title || " ";
}
if (typeof opts === "function") {
options.close = opts;
}
if (typeof opts === "object") {
for (var i in opts) {
if (i === "$id")
continue;
options[i] = opts[i];
}
}
var model;
domParser.innerHTML = '<div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-front" tabindex="-1" style="position: absolute;" ' +
' ms-visible="toggle"' +
' ms-css-width="width"' +
' ms-css-height="height"' +
'><div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix" ms-draggable="drag" data-beforestart="beforestart" data-movable="false">' +
'<span class="ui-dialog-title" ms-html="title"></span>' +
'<button ms-ui="button" type="button" data-primary="ui-icon-closethick" class="ui-dialog-titlebar-close" data-text="false" ms-click="close">close</button></div>' +
'</div></div>';
var dialog = domParser.firstChild;
if (options.nodeNodeType === 1) {
var parent = options.parent;
} else {
var parent = options.parent === "parent" ? element.parentNode : document.body;
}
var full = false;
$element.addClass("ui-dialog-content ui-widget-content");
if (options.height === "auto") {
var style = element.style;
style.width = "auto";
style.height = "auto";
style.minHeight = element.clientHeight + "px";
}
element.removeAttribute("title");
element.parentNode.removeChild(element);
model = avalon.define(id, function(vm) {
vm.toggle = options.toggle;
vm.title = options.title;
vm.width = options.width;
vm.height = options.height;
vm.close = function() {
vm.toggle = false;
};
vm.$watch("toggle", function(v) {
if (v === false) {
avalon.Array.remove(overlayInstances, options);
if (!overlayInstances.length) {
if (overlay.parentNode) {
overlay.parentNode.removeChild(overlay);
}
}
} else {
resetCenter();
}
});
vm.drag = function(event, data) {
dialog.style.top = data.top + "px";
dialog.style.left = data.left + "px";
};
vm.beforestart = function(event, data) {
data.element = dialog;
data.$element = avalon(dialog);
};
});
function keepFocus() {
function checkFocus() {
var activeElement = document.activeElement,
isActive = dialog === activeElement || dialog.contains(activeElement);
if (!isActive) {
if (dialog.querySelectorAll) {
var hasFocus = dialog.querySelectorAll("[autofocus]");
if (!hasFocus.length) {
hasFocus = dialog.querySelectorAll("[tabindex]");
}
if (!hasFocus.length) {
hasFocus = [dialog]
}
hasFocus[0].focus();
}
}
}
checkFocus();
avalon.nextTick(checkFocus);
}
function resetCenter() {
if (full) {
if (positionfixed) {
dialog.style.position = "fixed";
var l = (avalon(window).width() - dialog.offsetWidth) / 2;
var t = (avalon(window).height() - dialog.offsetHeight) / 2;
} else {
dialog.style.setExpression('top', '( document.body.clientHeight - this.offsetHeight) / 2) + Math.max(document.documentElement.scrollTop,document.body.scrollTop) + "px"');
dialog.style.setExpression('left', '( document.body.clientWidth - this.offsetWidth / 2) + Math.max(document.documentElement.scrollLeft,document.body.scrollLeft) + "px"');
return;
}
} else {
l = (avalon(parent).width() - dialog.offsetWidth) / 2;
t = (avalon(parent).height() - dialog.offsetHeight) / 2;
}
dialog.style.left = l + "px";
dialog.style.top = t + "px";
keepFocus();
if (options.modal) {
parent.insertBefore(overlay, dialog);
overlay.style.display = "block";
avalon.Array.ensure(overlayInstances, options)
}
}
avalon.ready(function() {
parent.appendChild(dialog);
dialog.appendChild(element);
full = /body|html/i.test(dialog.offsetParent.tagName);
if (full) {
dialog.firstChild.setAttribute("data-containment", "window");
}
avalon.scan(dialog, model);
if (options.autoOpen) {
avalon.nextTick(resetCenter);
}
avalon(document.body).bind("scroll", function() {
options.autoOpen && resetCenter();
});
avalon(window).bind("resize", function() {
options.autoOpen && resetCenter();
});
});
return model;
}
var overlayInstances = avalon.ui.dialog.overlayInstances = [];
})(window.avalon);
//http://www.slipjs.com/jz.html