forked from jejacks0n/mercury
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mercury_loader.html
406 lines (389 loc) · 34 KB
/
mercury_loader.html
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
392
393
394
395
396
397
398
399
400
401
402
403
404
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Mercury Editor - Annotated Source - mercury_loader.js</title>
<link href="/mercury/assets/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css"/>
<script src="/mercury/assets/javascripts/application.js" type="text/javascript"></script>
<!--[if lt IE 7]>
<script type="text/javascript" src="/mercury/assets/javascripts/unitpngfix.js"></script>
<![endif]-->
</head>
<body>
<div id="background"></div>
<h1 id="logo"><a href="/mercury">Mercury Editor</a></h1>
<ul id="navigation">
<li><a href="/mercury">Home</a></li>
<li><a href="/mercury/downloads">Downloads</a></li>
<li><a href="/mercury/walkthrough">Walkthrough</a></li>
<li><a href="/mercury/documentation">Documentation</a></li>
<li class="active"><a href="/mercury/annotated_source">Annotated Source</a></li>
</ul>
<table cellspacing=0 cellpadding=0>
<thead>
<tr>
<th class=docs><h1>mercury_loader.js</h1></th>
<th class=code></th>
</tr>
</thead>
<tbody>
<tr id='section-1'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-1">¶</a>
</div>
</td>
<td class=code>
<div class='highlight'><pre><span class="cm">/*!</span>
<span class="cm"> * Mercury Editor is a CoffeeScript and jQuery based WYSIWYG editor. Documentation and other useful information can be</span>
<span class="cm"> * found at https://github.com/jejacks0n/mercury</span>
<span class="cm"> *</span>
<span class="cm"> * Supported browsers:</span>
<span class="cm"> * - Firefox 4+</span>
<span class="cm"> * - Chrome 10+</span>
<span class="cm"> * - Safari 5+</span>
<span class="cm"> *</span>
<span class="cm"> * Copyright (c) 2011 Jeremy Jackson</span>
<span class="cm"> *</span>
<span class="cm"> * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated</span>
<span class="cm"> * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the</span>
<span class="cm"> * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit</span>
<span class="cm"> * persons to whom the Software is furnished to do so, subject to the following conditions:</span>
<span class="cm"> *</span>
<span class="cm"> * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the</span>
<span class="cm"> * Software.</span>
<span class="cm"> *</span>
<span class="cm"> * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE</span>
<span class="cm"> * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR</span>
<span class="cm"> * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR</span>
<span class="cm"> * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</span>
<span class="cm"> *</span>
<span class="cm"> */</span></pre></div>
</td>
</tr>
<tr id='section-Default_Packages'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-Default_Packages">¶</a>
</div>
<h2>Default Packages</h2>
<p>Some default packages are provided for you. If you want to define your own, feel free to do so before including this
script. These, or your own packages can be specified to the loader in query params (read below for details).</p>
</td>
<td class=code>
<div class='highlight'><pre><span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nb">window</span><span class="p">.</span><span class="nx">mercuryPackages</span><span class="p">)</span> <span class="nb">window</span><span class="p">.</span><span class="nx">mercuryPackages</span> <span class="o">=</span> <span class="p">{</span>
<span class="nx">development</span><span class="o">:</span> <span class="p">{</span><span class="nx">javascripts</span><span class="o">:</span> <span class="s1">'mercury/dependencies/jquery-1.7.js,mercury.js'</span><span class="p">,</span> <span class="nx">stylesheets</span><span class="o">:</span> <span class="s1">'mercury.css'</span><span class="p">},</span>
<span class="nx">bundled</span><span class="o">:</span> <span class="p">{</span><span class="nx">javascripts</span><span class="o">:</span> <span class="s1">'javascripts/jquery-1.7.js,javascripts/mercury.min.js,javascripts/mercury_dialogs.js'</span><span class="p">,</span> <span class="nx">stylesheets</span><span class="o">:</span> <span class="s1">'stylesheets/mercury.bundle.css'</span><span class="p">}</span>
<span class="p">};</span></pre></div>
</td>
</tr>
<tr id='section-Mercury_Loader'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-Mercury_Loader">¶</a>
</div>
<h2>Mercury Loader</h2>
</td>
<td class=code>
<div class='highlight'><pre><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span></pre></div>
</td>
</tr>
<tr id='section-4'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>Useragent detection, which we use to determine if the client is supported. We do this method instead of checking
features because many of the features are supported in IE, but aren’t implemented to the W3C spec.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="kd">var</span> <span class="nx">browser</span> <span class="o">=</span> <span class="p">{</span>
<span class="nx">webkit</span><span class="o">:</span> <span class="sr">/(webkit)[ \/]([\w.]+)/</span><span class="p">,</span>
<span class="nx">opera</span><span class="o">:</span> <span class="sr">/(opera)(?:.*version)?[ \/]([\w.]+)/</span><span class="p">,</span>
<span class="nx">msie</span><span class="o">:</span> <span class="sr">/(msie) ([\w.]+)/</span><span class="p">,</span>
<span class="nx">mozilla</span><span class="o">:</span> <span class="sr">/(mozilla)(?:.*? rv:([\w.]+))?/</span>
<span class="p">};</span>
<span class="kd">var</span> <span class="nx">ua</span> <span class="o">=</span> <span class="nx">navigator</span><span class="p">.</span><span class="nx">userAgent</span><span class="p">.</span><span class="nx">toLowerCase</span><span class="p">();</span>
<span class="kd">var</span> <span class="nx">match</span> <span class="o">=</span> <span class="nx">browser</span><span class="p">.</span><span class="nx">webkit</span><span class="p">.</span><span class="nx">exec</span><span class="p">(</span><span class="nx">ua</span><span class="p">)</span> <span class="o">||</span> <span class="nx">browser</span><span class="p">.</span><span class="nx">opera</span><span class="p">.</span><span class="nx">exec</span><span class="p">(</span><span class="nx">ua</span><span class="p">)</span> <span class="o">||</span> <span class="nx">browser</span><span class="p">.</span><span class="nx">msie</span><span class="p">.</span><span class="nx">exec</span><span class="p">(</span><span class="nx">ua</span><span class="p">)</span> <span class="o">||</span> <span class="nx">ua</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="s2">"compatible"</span><span class="p">)</span> <span class="o"><</span> <span class="mi">0</span> <span class="o">&&</span> <span class="nx">browser</span><span class="p">.</span><span class="nx">mozilla</span><span class="p">.</span><span class="nx">exec</span><span class="p">(</span><span class="nx">ua</span><span class="p">)</span> <span class="o">||</span> <span class="p">[];</span>
<span class="nx">browser</span> <span class="o">=</span> <span class="p">{</span><span class="nx">version</span><span class="o">:</span> <span class="nx">match</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">||</span> <span class="s2">"0"</span> <span class="p">};</span>
<span class="nx">browser</span><span class="p">[</span><span class="nx">match</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">||</span> <span class="s2">""</span><span class="p">]</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span></pre></div>
</td>
</tr>
<tr id='section-5'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>If the browser isn’t supported, we don’t try to do anything more. We do direct userAgent detection here because IE
thinks it’s supported but isn’t — in part because it has it’s own implementation of the contentEditable spec.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="k">if</span> <span class="p">(</span><span class="nb">document</span><span class="p">.</span><span class="nx">getElementsByTagName</span> <span class="o">&&</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span> <span class="o">&&</span> <span class="nb">document</span><span class="p">.</span><span class="nx">designMode</span> <span class="o">&&</span> <span class="o">!</span><span class="nx">browser</span><span class="p">.</span><span class="nx">konqueror</span> <span class="o">&&</span> <span class="o">!</span><span class="nx">browser</span><span class="p">.</span><span class="nx">msie</span><span class="p">)</span> <span class="p">{</span></pre></div>
</td>
</tr>
<tr id='section-6'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>supported</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span></pre></div>
</td>
</tr>
<tr id='section-7'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<p>Default options, which can be overridden by specifying them in query params to the loader script.
You can provide any additional options to the loader, and they will be passed to the PageEditor instance when it’s
created, so for instance you could put <code>visible=false</code>, and the editor be hidden after it’s created.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="kd">var</span> <span class="nx">options</span> <span class="o">=</span> <span class="p">{</span></pre></div>
</td>
</tr>
<tr id='section-8'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p>A path or url from which the javascripts and css should be loaded.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="nx">src</span><span class="o">:</span> <span class="s1">'/assets'</span><span class="p">,</span></pre></div>
</td>
</tr>
<tr id='section-9'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<p>A value defined in the packages above. Development is used by default. If you want to provide your own package
you can just define one before including this script.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="nx">pack</span><span class="o">:</span> <span class="s1">'development'</span>
<span class="p">};</span></pre></div>
</td>
</tr>
<tr id='section-10'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<p>Hide the document during loading so there isn’t a flicker while mercury is being loaded.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="kd">var</span> <span class="nx">head</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementsByTagName</span><span class="p">(</span><span class="s2">"head"</span><span class="p">)[</span><span class="mi">0</span><span class="p">];</span>
<span class="k">if</span> <span class="p">(</span><span class="nb">window</span> <span class="o">==</span> <span class="nx">top</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">style</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s1">'style'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">rules</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">createTextNode</span><span class="p">(</span><span class="s1">'body{visibility:hidden;display:none}'</span><span class="p">);</span>
<span class="nx">style</span><span class="p">.</span><span class="nx">type</span> <span class="o">=</span> <span class="s1">'text/css'</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">style</span><span class="p">.</span><span class="nx">styleSheet</span><span class="p">)</span> <span class="nx">style</span><span class="p">.</span><span class="nx">styleSheet</span><span class="p">.</span><span class="nx">cssText</span> <span class="o">=</span> <span class="nx">rules</span><span class="p">.</span><span class="nx">nodeValue</span><span class="p">;</span>
<span class="k">else</span> <span class="nx">style</span><span class="p">.</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">rules</span><span class="p">);</span>
<span class="nx">head</span><span class="p">.</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">style</span><span class="p">);</span>
<span class="p">}</span></pre></div>
</td>
</tr>
<tr id='section-11'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-11">¶</a>
</div>
<p>Because Mercury loads the document it’s going to edit into an iframe we do some tweaks to the current document to
make that feel more seamless.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="kd">function</span> <span class="nx">loadMercury</span><span class="p">()</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nb">document</span><span class="p">.</span><span class="nx">mercuryLoaded</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">timer</span><span class="p">)</span> <span class="nb">window</span><span class="p">.</span><span class="nx">clearTimeout</span><span class="p">(</span><span class="nx">timer</span><span class="p">);</span>
<span class="nb">document</span><span class="p">.</span><span class="nx">mercuryLoaded</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span></pre></div>
</td>
</tr>
<tr id='section-12'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-12">¶</a>
</div>
<p>If the current window is the top window, it means that Mercury hasn’t been loaded yet. So we load it.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="k">if</span> <span class="p">(</span><span class="nb">window</span> <span class="o">==</span> <span class="nx">top</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">i</span><span class="p">;</span></pre></div>
</td>
</tr>
<tr id='section-13'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-13">¶</a>
</div>
<p>Find the loader script and determine what options were provided so the defaults can be overridden. To provide
options just pass them in as query params (eg. <code>mercury_loader.js?src=/asset_path&pack=bundled</code>)</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="kd">var</span> <span class="nx">scripts</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementsByTagName</span><span class="p">(</span><span class="s1">'script'</span><span class="p">);</span>
<span class="k">for</span> <span class="p">(</span><span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o"><=</span> <span class="nx">scripts</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="nx">i</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">match</span> <span class="o">=</span> <span class="nx">scripts</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">src</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="sr">/mercury_loader\.js\??(.*)?$/</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">match</span> <span class="o">||</span> <span class="o">!</span><span class="nx">match</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="k">continue</span><span class="p">;</span>
<span class="nx">match</span><span class="p">[</span><span class="mi">1</span><span class="p">].</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/([^&=]*)=([^&=]*)/g</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">m</span><span class="p">,</span> <span class="nx">attr</span><span class="p">,</span> <span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">options</span><span class="p">[</span><span class="nx">attr</span><span class="p">]</span> <span class="o">=</span> <span class="nx">value</span><span class="p">;</span>
<span class="p">});</span>
<span class="p">}</span>
<span class="kd">var</span> <span class="nx">pack</span> <span class="o">=</span> <span class="nb">window</span><span class="p">.</span><span class="nx">mercuryPackages</span><span class="p">[</span><span class="nx">options</span><span class="p">.</span><span class="nx">pack</span><span class="p">];</span>
<span class="nx">setTimeout</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span></pre></div>
</td>
</tr>
<tr id='section-14'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-14">¶</a>
</div>
<p>Once we’re ready to load Mercury we clear the document contents, and add in the css and javascript tags.
Once the script has loaded we display the body again, and instantiate a new instance of Mercury.PageEditor.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="nb">document</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">innerHTML</span> <span class="o">=</span> <span class="s1">'<div style="height:0;overflow:hidden;">Mercury</div>'</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o"><=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">styleSheets</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="nx">i</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
<span class="nb">document</span><span class="p">.</span><span class="nx">styleSheets</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">disabled</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
<span class="p">}</span></pre></div>
</td>
</tr>
<tr id='section-15'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-15">¶</a>
</div>
<p>Load all the stylesheets.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="kd">var</span> <span class="nx">stylesheets</span> <span class="o">=</span> <span class="nx">pack</span><span class="p">.</span><span class="nx">stylesheets</span><span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="s1">','</span><span class="p">);</span>
<span class="k">for</span> <span class="p">(</span><span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o"><=</span> <span class="nx">stylesheets</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="nx">i</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">link</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s1">'link'</span><span class="p">);</span>
<span class="nx">link</span><span class="p">.</span><span class="nx">href</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">src</span> <span class="o">+</span> <span class="s1">'/'</span> <span class="o">+</span> <span class="nx">stylesheets</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span>
<span class="nx">link</span><span class="p">.</span><span class="nx">media</span> <span class="o">=</span> <span class="s1">'screen'</span><span class="p">;</span>
<span class="nx">link</span><span class="p">.</span><span class="nx">rel</span> <span class="o">=</span> <span class="s1">'stylesheet'</span><span class="p">;</span>
<span class="nx">link</span><span class="p">.</span><span class="nx">type</span> <span class="o">=</span> <span class="s1">'text/css'</span><span class="p">;</span>
<span class="nx">head</span><span class="p">.</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">link</span><span class="p">);</span>
<span class="p">}</span></pre></div>
</td>
</tr>
<tr id='section-16'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-16">¶</a>
</div>
<p>Load all the javascripts.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="kd">var</span> <span class="nx">javascripts</span> <span class="o">=</span> <span class="nx">pack</span><span class="p">.</span><span class="nx">javascripts</span><span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="s1">','</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">loaded</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="kd">function</span> <span class="nx">loadScript</span><span class="p">(</span><span class="nx">src</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">script</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s1">'script'</span><span class="p">);</span>
<span class="nx">script</span><span class="p">.</span><span class="nx">src</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">src</span> <span class="o">+</span> <span class="s1">'/'</span> <span class="o">+</span> <span class="nx">src</span><span class="p">;</span>
<span class="nx">script</span><span class="p">.</span><span class="nx">type</span> <span class="o">=</span> <span class="s1">'text/javascript'</span><span class="p">;</span>
<span class="nx">script</span><span class="p">.</span><span class="nx">charset</span> <span class="o">=</span> <span class="s1">'utf-8'</span><span class="p">;</span>
<span class="nx">head</span><span class="p">.</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">script</span><span class="p">);</span>
<span class="nx">script</span><span class="p">.</span><span class="nx">onload</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">loaded</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">loaded</span> <span class="o">>=</span> <span class="nx">javascripts</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="p">{</span>
<span class="nb">document</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">visibility</span> <span class="o">=</span> <span class="s1">'visible'</span><span class="p">;</span>
<span class="nb">document</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">display</span> <span class="o">=</span> <span class="s1">'block'</span><span class="p">;</span></pre></div>
</td>
</tr>
<tr id='section-17'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-17">¶</a>
</div>
<p>Instantiate the PageEditor, passing in the options that were provided to the loader.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="k">new</span> <span class="nx">Mercury</span><span class="p">.</span><span class="nx">PageEditor</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">options</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-18'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-18">¶</a>
</div>
<p>If there’s a mercuryLoaded function available, call it. You can provide one before the loading script
and it will be called after everything is loaded, but before everything is initialized. You can bind
to the mercury:ready event or use Mercury.bind(‘ready’, function() {}).</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span><span class="p">(</span><span class="nb">window</span><span class="p">.</span><span class="nx">mercuryLoaded</span><span class="p">)</span> <span class="o">==</span> <span class="s1">'function'</span><span class="p">)</span> <span class="p">{</span>
<span class="nb">window</span><span class="p">.</span><span class="nx">mercuryLoaded</span><span class="p">();</span>
<span class="p">}</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nx">loadScript</span><span class="p">(</span><span class="nx">javascripts</span><span class="p">[</span><span class="nx">loaded</span><span class="p">]);</span>
<span class="p">}</span>
<span class="p">};</span>
<span class="p">}</span>
<span class="nx">loadScript</span><span class="p">(</span><span class="nx">javascripts</span><span class="p">[</span><span class="nx">loaded</span><span class="p">]);</span>
<span class="p">},</span> <span class="mi">1</span><span class="p">);</span>
<span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">top</span><span class="p">.</span><span class="nx">Mercury</span><span class="p">)</span> <span class="p">{</span></pre></div>
</td>
</tr>
<tr id='section-19'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-19">¶</a>
</div>
<p>Since this file will be included in the iframe as well, we use it to tell Mercury that the document is ready to
be worked on. By firing this event we’re able to build the regions and get everything ready without having to
wait for assets and slow javascripts to load or complete.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="nb">window</span><span class="p">.</span><span class="nx">Mercury</span> <span class="o">=</span> <span class="nx">top</span><span class="p">.</span><span class="nx">Mercury</span><span class="p">;</span>
<span class="nx">Mercury</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="s1">'initialize:frame'</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span></pre></div>
</td>
</tr>
<tr id='section-20'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-20">¶</a>
</div>
<p>This is a common technique for determining if the document has loaded yet, and is based on the methods used in
Prototype.js. The following portions just call loadMercury once it’s appropriate to do so.</p>
<p>Support for the DOMContentLoaded event is based on work by Dan Webb, Matthias Miller, Dean Edwards, John Resig,
and Diego Perini.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="kd">var</span> <span class="nx">timer</span><span class="p">;</span>
<span class="kd">function</span> <span class="nx">checkReadyState</span><span class="p">()</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nb">document</span><span class="p">.</span><span class="nx">readyState</span> <span class="o">===</span> <span class="s1">'complete'</span><span class="p">)</span> <span class="p">{</span>
<span class="nb">document</span><span class="p">.</span><span class="nx">stopObserving</span><span class="p">(</span><span class="s1">'readystatechange'</span><span class="p">,</span> <span class="nx">checkReadyState</span><span class="p">);</span>
<span class="nx">loadMercury</span><span class="p">();</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="kd">function</span> <span class="nx">pollDoScroll</span><span class="p">()</span> <span class="p">{</span>
<span class="k">try</span> <span class="p">{</span> <span class="nb">document</span><span class="p">.</span><span class="nx">documentElement</span><span class="p">.</span><span class="nx">doScroll</span><span class="p">(</span><span class="s1">'left'</span><span class="p">);</span> <span class="p">}</span>
<span class="k">catch</span><span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">timer</span> <span class="o">=</span> <span class="nx">pollDoScroll</span><span class="p">.</span><span class="nx">defer</span><span class="p">();</span>
<span class="k">return</span><span class="p">;</span>
<span class="p">}</span>
<span class="nx">loadMercury</span><span class="p">();</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="nb">document</span><span class="p">.</span><span class="nx">addEventListener</span><span class="p">)</span> <span class="p">{</span>
<span class="nb">document</span><span class="p">.</span><span class="nx">addEventListener</span><span class="p">(</span><span class="s1">'DOMContentLoaded'</span><span class="p">,</span> <span class="nx">loadMercury</span><span class="p">,</span> <span class="kc">false</span><span class="p">);</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nb">document</span><span class="p">.</span><span class="nx">observe</span><span class="p">(</span><span class="s1">'readystatechange'</span><span class="p">,</span> <span class="nx">checkReadyState</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="nb">window</span> <span class="o">==</span> <span class="nx">top</span><span class="p">)</span> <span class="p">{</span> <span class="nx">timer</span> <span class="o">=</span> <span class="nx">pollDoScroll</span><span class="p">.</span><span class="nx">defer</span><span class="p">();</span> <span class="p">}</span>
<span class="p">}</span>
<span class="p">})();</span></pre></div>
</td>
</tr>
</table>
<div id="footer">
<hr>
Copyright 2011 Jeremy Jackson. All rights reserved.
</div>
</body>
</html>