This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
main.js
320 lines (257 loc) · 10.9 KB
/
main.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
/*
Processing order description
1. Escaping
The Mobify tags identities whether a browser should recieve the transformation.
If so, it escapes the document, allowing for markup capture without loading
external resources.
2. Source DOM Construction
The escaped markup is retrieved from the DOM as a string. The escaped markup is
transformed into a DOM node after resource loading attributes are escaped.
3. Data select
A data object is created by select nodes from the source DOM using DOM methods.
4. Markup generation.
A dust template is rendered with the data as a context, producing the final HTML.
5. Document replacement
The current document is replaced by using document.open/write/close. This makes
the browser behave as if the templated HTML was the regular source.
*/
(function(window, document, Mobify, undefined) {
var config = Mobify.config
, $ = Mobify.$
, _ = Mobify._
, timing = Mobify.timing;
$.extend(config, {
isDebug: $.cookie('mobify-debug'),
location: window.location
});
// V6 tags don't set state, so we set it here.
// https://github.com/mobify/portal_app/issues/186
//
// mobify-path=<mobifyjs-path>
// mobify-all
// mobify-debug=<int>
if (config.tagVersion > 5) {
var hash = location.hash
, match
match = /mobify-path=([^&;]+)/g.exec(hash)
if (match) {
if (/mobify-all/.test(hash)) {
document.cookie = 'mobify-path=' + match[1] + '; path=/'
} else {
document.cookie = 'mobify-path=1; path=/'
sessionStorage['mobify-path'] = match[1]
}
}
match = /mobify-debug=([^&;]+)/g.exec(hash)
if (match) {
document.cookie = 'mobify-debug=' + match[1] + '; path=/'
}
}
// If loaded with tagInjector or Preview, set debug, otherwise debug is off.
// Setting `mobify-debug` cookie overrides defaults.
if (config.isDebug === null) {
config.isDebug = /^(?:https?:\/\/[^\/]+)?\/__\/|^\/\/(trust|preview).mobify.com\//.test(config.path)
? 2 : 0;
}
config.isDebug = +config.isDebug;
// configFile my already exists if rendering server side, so only grab mobify.js script tag
// if configFile is undefined.
if (!config.configFile) {
// V6 moved mobify.js to the first script.
config.configFile = config.tagVersion > 5
? $('script[src*="mobify.js"]').first().attr('src')
: $('script[src*="mobify.js"]').last().attr('src');
}
config.configDir = config.configFile.replace(/\/[^\/]*$/, '/');
if (window.enableStudioJS) {
Mobify.studioJS = {
get : function(key, callback) {
var handler = function(ev) {
if ((ev.data.command === 'html')
&& (ev.data.dest === 'page')
&& (ev.data.key === key)
&& (ev.source === window)) {
window.removeEventListener("message", handler, false);
callback(ev.data.value, ev.data.key);
}
}
window.addEventListener("message", handler, false);
},
set: function(key, value) {
Mobify.studioJS[key] = value;
window.postMessage({
dest : 'extension',
command : 'html',
key: key,
value: value
}, '*');
}
};
}
Mobify.load = function(evaluationSource) {
var conf = Mobify.conf = Mobify.conf.call(Mobify.data2 && Mobify.data2.M, Mobify.data && Mobify.data.M)
timing.addPoint('Setup Conf');
// if evaluatedSource is undefined, grabContent, and if that is also undefined, unmobify
if (evaluationSource === undefined) {
evaluationSource = Mobify.externals.grabContent();
if (evaluationSource === undefined) return Mobify.unmobify();
}
evaluationSource.config = config;
// Deprecated.
if (Mobify.data) {
evaluationSource.mobileViewport = "width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=no";
}
Mobify.conf.data = $.extend(evaluationSource, conf.data);
Mobify.evaluatedData = undefined;
// `DOMContentLoaded` will fire for the original `document` so hold the
// ready event until the new document loads.
$.holdReady(true);
// Extract data (interesting DOM nodes, or nested arrays, objects and primitives)
// See data.js for description of data extraction process and configuration map.
if (Mobify.data2) {
var cont = Mobify.data2
.makeCont({source: evaluationSource})
.on('complete', Mobify.acceptData);
Mobify.timing.addPoint('Prepared conf for evaluation');
Mobify.timing.addSelector('Start');
cont.eval();
} else {
Mobify.templateData(Mobify.data.evaluate({ data: evaluationSource }));
}
};
// acceptData added to the Mobify object so it can be overridden in server-side transformations.
Mobify.acceptData = function(data, cont) {
if (!Mobify.evaluatedData) {
Mobify.evaluatedData = data;
Mobify.evaluatedCont = cont;
timing.addPoint('Evaluated Conf');
}
var enabled = Mobify.externals.enable(data.OUTPUTHTML || '');
timing.addPoint('Enabled Markup');
// JB: When would the `DOMContentLoaded` branch be fired?
// If we requested more data to render the template, the document.readyState
// may be set before we render.
if (/complete|loaded|interactive/.test(document.readyState)) {
emitMarkup(enabled);
} else {
document.addEventListener('DOMContentLoaded', function DOMContentLoaded() {
document.removeEventListener('DOMContentLoaded', DOMContentLoaded, false);
emitMarkup(enabled);
}, false);
}
};
var emitMarkup = function(markup) {
timing.addPoint('DOMContentLoaded');
// If SASS compilation fails, the stylesheet will be blank except a SASS error.
// Set the document to visible to see these errors.
// if (config.isDebug) $('html').css('visibility', 'visible');
$('html').css('visibility', 'visible');
// iOS 4.3 does not clear listeners on `document` after `document.open`.
// Manually remove listeners added by the v4 tags.
Mobify.beforeload && document.removeEventListener('beforeload', Mobify.beforeload, true);
if (!markup) {
debug.warn('OUTPUTHTML is empty, unmobifying.');
return Mobify.unmobify();
}
var write = function(markup) {
// `document.open` clears events bound to `document`.
document.open();
// On `DOMContentLoaded` of the new `document`, fire the ready event on our
// jQuery. (the `DOMContentLoaded` it binds to fires early)
document.addEventListener('DOMContentLoaded', function DOMContentLoaded() {
document.removeEventListener('DOMContentLoaded', DOMContentLoaded, false);
$.holdReady(false);
}, false);
// In Webkit, `document.write` immediately executes inline scripts
// not preceded by an external resource.
document.write(markup);
timing.addPoint('Wrote Document');
Mobify.postDocWrite();
// Positioning this after the last `document.write`.
document.close();
if (Mobify.studioJS) {
Mobify.studioJS.get('renderHTML', function(markup) {
oldEmitMarkup(markup);
});
}
// Bind `orientationchange` listener and add `html` classes.
Mobify.enhance();
};
if (Mobify.isIOS8_0()){
// See `util.js` for more information about this fix.
Mobify.ios8_0ScrollFix(document, write);
} else {
write();
}
};
var oldEmitMarkup;
if (Mobify.studioJS) {
oldEmitMarkup = emitMarkup;
emitMarkup = function(markup) {
Mobify.studioJS.get('renderHTML', function(markup) {
oldEmitMarkup(markup);
});
Mobify.studioJS.set('resultHTML', markup || Mobify.studioJS.sourceHTML);
}
}
// Data1's templating func.
Mobify.templateData = function(evaluatedData) {
Mobify.evaluatedData = evaluatedData;
timing.addPoint('Evaluated Conf');
if (evaluatedData.unmobify) return Mobify.unmobify();
// Render data into template, receiving an HTML string.
dust.render('root', evaluatedData, function(err, out) {
if (err) debug.die(err);
evaluatedData.OUTPUTHTML = out;
Mobify.acceptData(evaluatedData);
});
};
// Bookkeeping after document.write is complete.
Mobify.postDocWrite = function() {
// Legacy google analytics call
if (Mobify.ga && Mobify.ga.init) {
Mobify.ga.init();
}
if (config.isDebug) {
timing.logPoints();
}
// JB: Livereload crashes Mobile Safari.
if (config.isDebug > 1) {
var m = (config.configFile.match(/^(?:https?:)?\/\/([^\/:]*)/) || '');
var hostname = m[1] || location.hostname;
if (!window.LiveReload) {
document.write('<script defer src="//cdn.mobify.com/livereload.js#'
+ hostname + '"></script>');
}
if (config.isDebug > 2) {
Mobify.weinre(hostname + ':8081');
}
}
}
Mobify.weinre = function(host) {
if (window.WeinreServerURL) return;
var weinreScript = document.createElement('script');
weinreScript.src = 'http://' + host + '/target/target-script-min.js#anonymous';
document.body.appendChild(weinreScript);
}
// Kickstart processing, and submit analytics upon its success or failure
Mobify.init = function(mode) {
if (!/complete|loaded/.test(document.readyState)) {
window.setTimeout(Mobify.init, 100);
return;
}
if (mode == "livereload") {
window.setTimeout(Mobify.load, 0);
return;
}
config.started = true;
// Protection against Mobify.snippet calling `document.write` after
// `DOMContentLoaded`.
Mobify.snippet = $.noop;
Mobify.load();
};
if (Mobify.config.configFile.match(/\?livereload=\d+$/)) {
setTimeout(Mobify.load, 0);
}
})(this, document, Mobify);
Mobify.timing.addPoint('Walked Mobify.js');