-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmvcct.enhancer.js
391 lines (379 loc) · 20.6 KB
/
mvcct.enhancer.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
(function () {
var DEBUG = true;
(function (undefined) {
var window = this || (0, eval)('this');
(function (factory) {
if (typeof define === 'function' && define['amd']) {
// [1] AMD anonymous module
define(['exports', 'require'], factory);
} else if (typeof exports === 'object' && typeof module === 'object') {
// [2] CommonJS/Node.js
factory(module['exports'] || exports); // module.exports is for Node.js
} else {
// [3] No module loader (plain <script> tag) - put directly in global namespace
var mvcct = window["mvcct"] = window["mvcct"] || {};
factory(mvcct['enhancer'] = {});
}
}(
(function (enhancerExports, amdRequire) {
var enhancer = typeof enhancerExports !== 'undefined' ? enhancerExports : {};
//Start actual code
var transformations = [];
var asyncReady = false;
var waitAsync = null;
var oldReady = null;
var baseEvent = "_enhancer.dependency.";
var dependencyOnPathAttribute = "data-enhancer-dependency";
var jQuery = window["jQuery"];
if (jQuery) {
oldReady = jQuery["fn"]["ready"];
jQuery["fn"]["ready"] = function (x) {
transformations.push({
transform: null,
initialize: x,
processOptions: null,
name: "document.ready: "+x.constructor.name,
preProcessOptions: null,
isReady: true
});
};
}
function init(options) {
for (var i = 0; i < transformations.length; i++) {
var item = transformations[i];
if (item.preProcessOptions) {
try{
item.preProcessOptions(options, item);
}
catch(ex){
if (DEBUG) {
alert(ex + ". " + item.name);
}
}
}
}
for (var i = 0; i < transformations.length; i++) {
var item = transformations[i];
if (item.processOptions) {
try{
item.processOptions(options, item);
}
catch(ex){
if (DEBUG) {
alert(ex + ". " + item.name);
}
}
}
}
var newTransformations=[];
for (var i = 0; i < transformations.length; i++) {
var item = transformations[i];
if (item.initialize ) {
if(item.isReady && !options.runReady) continue;
try{
if(item.transform){
item.transform(document.querySelector('body'), true);
newTransformations.push(item);
}
else
item.initialize(document.querySelector('body'));
}
catch (ex) {
if (DEBUG) {
alert(ex + ". " + item.name);
}
}
}
}
transformations=newTransformations;
}
enhancer["init"] = function (options) {
options = options || {};
if (jQuery) {
jQuery["fn"]["ready"] = oldReady;
jQuery(document)["ready"](function () { init(options); });
}
else init(options);
};
enhancer["asyncReady"] = function () {
if (waitAsync) enhancer["init"](waitAsync);
asyncReady = true;
};
enhancer["waitAsync"] = function (options) {
if (asyncReady) enhancer["init"](options);
waitAsync = options || {};
};
enhancer["register"] = function (transform, initialize, processOptions, name, preProcessOptions, type) {
transformations.push({
transform: transform,
initialize: initialize,
processOptions: processOptions,
name: name,
preProcessOptions: preProcessOptions,
type: type
});
};
enhancer["transform"] = function (node, types) {
for (var i = 0; i < transformations.length; i++) {
var item = transformations[i];
if(types && !types[item.type || 'default']) continue;
if (item.transform) {
try {
item.transform(node, false);
}
catch (ex) {
if (DEBUG) {
alert(ex + ". " + item.name);
}
}
}
}
};
enhancer["dependency"] = function (name, sourceNode, targetNode, eventNames, action) {
var mainF = function () {
if (!sourceNode.getAttribute(dependencyOnPathAttribute)) {
try {
sourceNode.setAttribute(dependencyOnPathAttribute, "true");
action(targetNode, sourceNode);
var ev = document.createEvent("Event");
ev.initEvent(baseEvent + name, false, true);
targetNode.dispatchEvent(ev);
}
finally {
sourceNode.setAttribute(dependencyOnPathAttribute, "");
}
}
};
var triggerF=function () {
var ev = document.createEvent("Event");
ev.initEvent(baseEvent + name, false, true);
sourceNode.dispatchEvent(ev);
};
sourceNode.addEventListener(baseEvent + name, mainF);
for (var i = 0; i < eventNames.length; i++)
sourceNode.addEventListener(eventNames[i], triggerF);
return {
mainF: mainF,
triggerF: triggerF,
name: baseEvent + name,
events: eventNames,
node: sourceNode
};
}
enhancer["removeDependency"] = function (handle) {
var events = handle.events;
for (var i = 0; i < events.length; i++)
handle.sourceNode.removeEventListener(events[i], handle.triggerF);
handle.sourceNode.removeEventListener(handle.name, handle.mainF);
};
//html5 enhancement
(function (enhancer) {
var originalSupport = autodetect();
var processedSupport = {
"clientTimeZoneOffset": new Date().getTimezoneOffset()
};
var html5Infos = {
"Html5InputOriginalSupport": originalSupport,
"Html5InputSupport": processedSupport
};
var html5ProcessInfos = null;//describe required processing based on user options and defaults
var html5ProcessInfosDefaults = {
"cookie": "_browser_basic_capabilities",
"forms": null
};
var handlers = null;
enhancer["getSupport"] = function () {
return html5Infos;
};
function needFallback(type) {
try {
var input = document.createElement("input");
input.setAttribute("type", type);
if (input.type === type) {
var illegal = '1illegal';
input.setAttribute('value', illegal);
return (input.value === illegal);
} else return true;
} catch (e) {
return true;;
}
}
function addToOptions(prefix, object, outputArray) {
for(var prop in object){
var val = object[prop];
if(typeof(val) === 'boolean') val= val ? "True":"False";
else val = ''+val;
if (!object.hasOwnProperty(prop)) continue;
outputArray.push({
"Key": prefix ? prefix + "." + prop : prop,
"Value": val
});
}
}
function copyAttrs(elm, mains) {
for (var iAttr = 0; iAttr < elm.attributes.length; iAttr++) {
var attribute = elm.attributes[iAttr].name;
if (attribute === "min" || attribute === "max" || attribute == "step" || attribute === "type" || attribute === "value") continue;
else mains.setAttribute(attribute, elm.attributes[iAttr].value);
}
}
function autodetect() {
//html5 auto detection
return {
"number": !needFallback('number'),
"range": !needFallback('range'),
"date": !needFallback('date'),
"month": !needFallback('month'),
"week": !needFallback('week'),
"time": !needFallback('time'),
"datetime": !needFallback('datetime-local'),
"email": !needFallback('email'),
"search": !needFallback('search'),
"tel": !needFallback('tel'),
"url": !needFallback('url'),
"color": !needFallback('color')
}
}
function packInfosForServer() {
//pack html5Infos in cookie and or hidden field for server
var infos = [];
addToOptions("Html5InputSupport", html5Infos["Html5InputSupport"], infos);
addToOptions("Html5InputOriginalSupport", html5Infos["Html5InputOriginalSupport"], infos);
var sInfos = JSON.stringify(infos);
if (html5ProcessInfos["forms"]) {
var flist = document.querySelectorAll('form');
for (var i = 0; i < flist.length; i++)
{
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", html5ProcessInfos["forms"]);
input.setAttribute("value", sInfos);
flist[i].appendChild(input);
}
}
if (html5ProcessInfos["cookie"]) {
document.cookie = html5ProcessInfos["cookie"] + "=" + encodeURIComponent(sInfos) + "; path=/";
}
}
function preProcessOptions(options, entry) {
var fallbacks = options["fallbacks"];
for (var prop in originalSupport) {
var support = originalSupport[prop];
var fallback = fallbacks[prop];
if (support && (!fallback || !fallback["force"])) processedSupport[prop] = 4;
else if (!fallback) processedSupport[prop] = 1;
else processedSupport[prop] = fallback["type"];
}
}
function processOptions(options, entry) {
options = options || {};
html5ProcessInfos = {
"fallbackHtml5": options["fallbackHtml5"] === undefined ? false : options["fallbackHtml5"],
"cookie": options["cookie"] === undefined ? html5ProcessInfosDefaults["cookie"] : options["cookie"],
"forms": options["forms"] === undefined ? html5ProcessInfosDefaults["forms"] : options["forms"],
"fallbacks": options["fallbacks"] || {},
"handlers": {}
};
if (!options["handlers"] || !options["handlers"]["replace"])
{
html5ProcessInfos["handlers"]["replace"] = function (type, support) {
if (type == "number" || type == "date" || type == "datetime-local" || type == "month" || type == "time" || type == "week" || type == "color")
return "text";
else if (type == "range") {
if (support.range > 2 || support.number < 4) return "text";
return "number";
}
else return type;
};
}
else html5ProcessInfos["handlers"]["replace"] = options["handlers"]["replace"];
if (!options["handlers"] || !options["handlers"]["translateVal"])
html5ProcessInfos["handlers"]["translateVal"] = function (val, type, el) { return val; }
else html5ProcessInfos["handlers"]["translateVal"] = options["handlers"]["translateVal"];
handlers = html5ProcessInfos["handlers"];
handlers["fullReplace"] = options && options["handlers"] ? options["handlers"]["fullReplace"] : null;
handlers["enhance"] = options && options["handlers"] ? options["handlers"]["enhance"] : {};
packInfosForServer();
}
function processAllNodes(ancestor) {
if(!html5ProcessInfos["fallbackHtml5"]) return;
var parse = enhancer["parse"];
var format = enhancer["format"];
if(format && parse){
format = function(x){return enhancer["format"]("datetime", x, true);};
parse = function(x){return enhancer["parse"]("datetime", x, true);};
}
if (ancestor.tagName == "INPUT") process(ancestor, parse, format);
else {
var allInputs = ancestor.querySelectorAll("input");
for (var i = 0; i < allInputs.length; i++) process(allInputs[i], parse, format);
}
}
function addClientOffset(x, parse, format, ret)
{
if(!x) return x;
x=x.trim();
if(!x) return x;
var din = parse(x);
var off = -din.getTimezoneOffset();
if (ret) ret["off"]=off;
return format(new Date(din.getTime()+off*1000*60));
}
function addOffsetDetectStandard(evt){
var el=evt["target"];
var date = enhancer["parse"]("datetime", el.value);
if(date)
el.nextElementSibling.value=-date.getTimezoneOffset();
}
function addOffsetDetect (node)
{
node.addEventListener('blur', addOffsetDetectStandard);
}
function process(node, parse, format) {
var type = node.getAttribute("type");
var stype = type == "datetime-local" ? "datetime" : type;
var addListener =false;
if (stype == "datetime" && parse && format && node.getAttribute("data-is-utc")){
var toOff = {"off": 0};
node.value = addClientOffset(node.value, parse, format, toOff);
node.setAttribute('value', node.value);
node.nextElementSibling.value=toOff["off"];
var min = node.getAttribute("min");
if (min) node.setAttribute("min", addClientOffset(min, parse, format));
min = node.getAttribute("data-val-range-min");
if (min) node.setAttribute("data-val-range-min", addClientOffset(min, parse, format));
var max = node.getAttribute("max");
if (max) node.setAttribute("max", addClientOffset(max, parse, format));
max = node.getAttribute("data-val-range-max");
if (max) node.setAttribute("data-val-range-max", addClientOffset(max, parse, format));
addListener = true;
}
if (processedSupport[stype] > 3)
{
if(addListener) addOffsetDetect (node);
return;
}
var replace = handlers["replace"](type, processedSupport);
if (replace == type) return;
if (handlers["fullReplace"]) {
handlers["fullReplace"](node);
return;
}
var input = document.createElement("input");
input.setAttribute("type", replace);
input.setAttribute("value", handlers["translateVal"](node.getAttribute("value"), stype, replace));
copyAttrs(node, input);
input.setAttribute("data-original-type", type);
if(type == "range") input.setAttribute("data-is-range", "true");
node.parentNode.replaceChild(input, node);
if(addListener) addOffsetDetect (input);
if (handlers["enhance"] && handlers["enhance"][stype]) handlers["enhance"][stype](input, node);
}
enhancer["register"](null, false, function (options) { options = options || {}; preProcessOptions(options["browserSupport"]) }, "html5 support");
enhancer["register"](processAllNodes, true, function (options) { options = options || {}; processOptions(options["browserSupport"]) }, "html5 enhance");
}(enhancer));
//Finish actual code
})
));
}());
})();