root/branches/release-40/default_templates/javascript.mtml @ 2603

Revision 2603, 25.5 kB (checked in by bsmith, 18 months ago)

bugzid:79938 - Normalizing Template Tags

  • Property svn:keywords set to Id Revision
Line 
1<mt:Ignore>
2/*  The following functions and variables are here to support legacy MT templates.
3    If you have refreshed your JavaScript template but still use older MT comment
4    templates, you may need to uncomment this block in order for those templates
5    to work properly. To use, simply remove the 'mt:Ignore' tags wrapping this
6    block of code.
7*/
8    function hideDocumentElement(id) { return mtHide(id) }
9    function showDocumentElement(id) { return mtShow(id) }
10    function individualArchivesOnLoad() { return mtEntryOnLoad() }
11    function writeCommenterGreeting() { return mtShowGreeting() }
12    function rememberMe(f) { return mtRememberMe(f) }
13    function forgetMe(f) { return mtForgetMe(f) }
14    var commenter_name;
15    var commenter_id;
16    var commenter_url;
17    var commenter_blog_ids;
18    var mtcmtmail;
19    var mtcmtauth;
20    var mtcmthome;
21    var captcha_timer;
22</mt:Ignore>
23
24// The cookie name to use for storing the blog-side comment session cookie.
25var mtCookieName = "<$mt:UserSessionCookieName$>";
26var mtCookieDomain = "<$mt:UserSessionCookieDomain$>";
27var mtCookiePath = "<$mt:UserSessionCookiePath$>";
28var mtCookieTimeout = <$mt:UserSessionCookieTimeout$>;
29
30<mt:Ignore>
31/***
32 * Simple routine for showing a DOM element (applying a CSS display
33 * attribute of 'none').
34 */
35</mt:Ignore>
36function mtHide(id) {
37    var el = (typeof id == "string") ? document.getElementById(id) : id;
38    if (el) el.style.display = 'none';
39}
40
41<mt:Ignore>
42/***
43 * Simple routine for showing a DOM element (applying a CSS display
44 * attribute of 'block').
45 */
46</mt:Ignore>
47function mtShow(id) {
48    var el = (typeof id == "string") ? document.getElementById(id) : id;
49    if (el) el.style.display = 'block';
50}
51
52<mt:Ignore>
53/***
54 * A utility function for assigning/adding handlers to window events.
55 */
56</mt:Ignore>
57function mtAttachEvent(eventName,func) {
58    var onEventName = 'on' + eventName;
59    var old = window[onEventName];
60    if( typeof old != 'function' )
61        window[onEventName] = func;
62    else {
63        window[onEventName] = function( evt ) {
64            old( evt );
65            return func( evt );
66        };
67    }
68}
69
70<mt:Ignore>
71/***
72 * Calls the event named, if there are handlers for it.
73 */
74</mt:Ignore>
75function mtFireEvent(eventName,param) {
76    var fn = window['on' + eventName];
77    if (typeof fn == 'function') return fn(param);
78    return;
79}
80
81<mt:Ignore>
82/***
83 * Displays a relative date.
84 * 'ts' is a Date object, 'fds' is a string of the date which
85 * will be displayed if the given date is older than 1 week.
86 */
87</mt:Ignore>
88function mtRelativeDate(ts, fds) {
89    var now = new Date();
90    var ref = ts;
91    var delta = Math.floor((now.getTime() - ref.getTime()) / 1000);
92
93    var str;
94    if (delta < 60) {
95        str = '<__trans phrase="moments ago" escape="js">';
96    } else if (delta <= 86400) {
97        // less than 1 day
98        var hours = Math.floor(delta / 3600);
99        var min = Math.floor((delta % 3600) / 60);
100        if (hours == 1)
101            str = '<__trans phrase="[quant,_1,hour,hours] ago" params="1" escape="js">';
102        else if (hours > 1)
103            str = '<__trans phrase="[quant,_1,hour,hours] ago" params="2" escape="js">'.replace(/2/, hours);
104        else if (min == 1)
105            str = '<__trans phrase="[quant,_1,minute,minutes] ago" params="1" escape="js">';
106        else
107            str = '<__trans phrase="[quant,_1,minute,minutes] ago" params="2" escape="js">'.replace(/2/, min);
108    } else if (delta <= 604800) {
109        // less than 1 week
110        var days = Math.floor(delta / 86400);
111        var hours = Math.floor((delta % 86400) / 3600);
112        if (days == 1)
113            str = '<__trans phrase="[quant,_1,day,days] ago" params="1" escape="js">';
114        else if (days > 1)
115            str = '<__trans phrase="[quant,_1,day,days] ago" params="2" escape="js">'.replace(/2/, days);
116        else if (hours == 1)
117            str = '<__trans phrase="[quant,_1,hour,hours] ago" params="1" escape="js">';
118        else
119            str = '<__trans phrase="[quant,_1,hour,hours] ago" params="2" escape="js">'.replace(/2/, hours);
120    }
121    return str ? str : fds;
122}
123
124<mt:Ignore>
125/***
126 * Used to display an edit link for the given entry.
127 */
128</mt:Ignore>
129function mtEditLink(entry_id, author_id) {
130    var u = mtGetUser();
131    if (! u) return;
132    if (! entry_id) return;
133    if (! author_id) return;
134    if (u.id != author_id) return;
135    var link = '<a href="<$mt:AdminScript$>?__mode=view&amp;_type=entry&amp;id=' + entry_id + '"><__trans phrase="Edit" escape="js"></a>';
136    document.write(link);
137}
138
139<mt:Ignore>
140/***
141 * Called when an input field on the comment form receives focus.
142 */
143</mt:Ignore>
144function mtCommentFormOnFocus() {
145    // if CAPTCHA is enabled, this causes the captcha image to be
146    // displayed if it hasn't been already.
147    mtShowCaptcha();
148}
149
150<mt:Ignore>
151/***
152 * Displays a captcha field for anonymous commenters.
153 */
154</mt:Ignore>
155var mtCaptchaVisible = false;
156function mtShowCaptcha() {
157    var u = mtGetUser();
158    if ( u && u.is_authenticated ) return;
159    if (mtCaptchaVisible) return;
160    var div = document.getElementById('comments-open-captcha');
161    if (div) {
162        div.innerHTML = '<$mt:CaptchaFields$>';
163        mtCaptchaVisible = true;
164    }
165}
166
167<mt:Ignore>
168/* user object
169    -- saved in user cookie --
170    u.name (display name)
171    u.url (link to home page)
172    u.email (for anonymous only)
173    u.userpic (url for commenter/author)
174    u.profile (link to profile)
175    u.is_trusted (boolean)
176    u.is_author (user has posting rights)
177    u.is_banned (banned status; neither post/comment perms)
178    u.can_post (has permission to post)
179    u.can_comment (has permission to comment)
180
181    -- status fields --
182    u.is_authenticated (boolean)
183    u.is_anonymous (user is anonymous)
184*/
185</mt:Ignore>
186
187var is_preview;
188var user;
189<mt:Ignore>
190/***
191 * Assigns a user object as the actively logged in user; also saves the
192 * user information in a browser cookie.
193 */
194</mt:Ignore>
195function mtSetUser(u) {
196    if (u) {
197        // persist this
198        user = u;
199        mtSaveUser();
200        // sync up user greeting
201        mtFireEvent('usersignin');
202    }
203}
204
205<mt:Ignore>
206/***
207 * Simple function that escapes single quote characters for storing
208 * in a cookie.
209 */
210</mt:Ignore>
211function mtEscapeJS(s) {
212    s = s.replace(/'/g, "&apos;");
213    return s;
214}
215
216<mt:Ignore>
217/***
218 * Simple function that unescapes single quote characters that were
219 * stored in a cookie.
220 */
221</mt:Ignore>
222function mtUnescapeJS(s) {
223    s = s.replace(/&apos;/g, "'");
224    return s;
225}
226
227<mt:Ignore>
228/***
229 * Serializes a user object into a string, suitable for storing as a cookie.
230 */
231</mt:Ignore>
232function mtBakeUserCookie(u) {
233    var str = "";
234    if (u.name) str += "name:'" + mtEscapeJS(u.name) + "';";
235    if (u.url) str += "url:'" + mtEscapeJS(u.url) + "';";
236    if (u.email) str += "email:'" + mtEscapeJS(u.email) + "';";
237    if (u.is_authenticated) str += "is_authenticated:'1';";
238    if (u.profile) str += "profile:'" + mtEscapeJS(u.profile) + "';";
239    if (u.userpic) str += "userpic:'" + mtEscapeJS(u.userpic) + "';";
240    if (u.sid) str += "sid:'" + mtEscapeJS(u.sid) + "';";
241    str += "is_trusted:'" + (u.is_trusted ? "1" : "0") + "';";
242    str += "is_author:'" + (u.is_author ? "1" : "0") + "';";
243    str += "is_banned:'" + (u.is_banned ? "1" : "0") + "';";
244    str += "can_post:'" + (u.can_post ? "1" : "0") + "';";
245    str += "can_comment:'" + (u.can_comment ? "1" : "0") + "';";
246    str = str.replace(/;$/, '');
247    return str;
248}
249
250<mt:Ignore>
251/***
252 * Unserializes a user cookie and returns a user object with the restored
253 * state.
254 */
255</mt:Ignore>
256function mtUnbakeUserCookie(s) {
257    if (!s) return;
258
259    var u = {};
260    var m;
261    while (m = s.match(/^((name|url|email|is_authenticated|profile|userpic|sid|is_trusted|is_author|is_banned|can_post|can_comment):'([^']+?)';?)/)) {
262        s = s.substring(m[1].length);
263        if (m[2].match(/^(is|can)_/)) // boolean fields
264            u[m[2]] = m[3] == '1' ? true : false;
265        else
266            u[m[2]] = mtUnescapeJS(m[3]);
267    }
268    if (u.is_authenticated) {
269        u.is_anonymous = false;
270    } else {
271        u.is_anonymous = true;
272        u.can_post = false;
273        u.is_author = false;
274        u.is_banned = false;
275        u.is_trusted = false;
276    }
277    return u;
278}
279
280<mt:Ignore>
281/***
282 * Retrieves an object of the currently logged in user's state.
283 * If no user is logged in or cookied, this will return null.
284 */
285</mt:Ignore>
286function mtGetUser() {
287    if (!user) {
288        var cookie = mtGetCookie(mtCookieName);
289        if (!cookie) return;
290        user = mtUnbakeUserCookie(cookie);
291        if (! user) {
292            user = {};
293            user.is_anonymous = true;
294            user.can_post = false;
295            user.is_author = false;
296            user.is_banned = false;
297            user.is_trusted = false;
298        }
299    }
300    return user;
301}
302
303<mt:Ignore>
304/***
305 * Issues a request to the MT comment script to retrieve the currently
306 * logged-in user (if any).
307 */
308</mt:Ignore>
309var mtFetchedUser = false;
310function mtFetchUser(cb) {
311    if (!cb) cb = 'mtSetUser';
312    if ( ( cb == 'mtSetUser' ) && mtGetUser() ) {
313        var url = document.URL;
314        url = url.replace(/#.+$/, '');
315        url += '#comments-open';
316        location.href = url;
317    } else {
318        // we aren't using AJAX for this, since we may have to request
319        // from a different domain. JSONP to the rescue.
320        mtFetchedUser = true;
321        var script = document.createElement('script');
322        script.src = '<$mt:CGIPath$><$mt:CommentScript$>?__mode=session_js&blog_id=<$mt:BlogID$>&jsonp=' + cb;
323        (document.getElementsByTagName('head'))[0].appendChild(script);
324    }
325}
326
327<mt:Ignore>
328/***
329 * Called when the 'Remember me' checkbox is changed. If the checkbox
330 * is cleared, the cached user cookie is immediately cleared.
331 */
332</mt:Ignore>
333function mtRememberMeOnClick(b) {
334    if (!b.checked)
335        mtClearUser(b.form);
336    return true;
337}
338
339<mt:Ignore>
340/***
341 * Called when comment form is sent.
342 * Required parameter: Form DOM object of comment form.
343 * If form has a 'bakecookie' member, it will be used to signal
344 * storing the anonymous commenter information to a cookie.
345 * If form has a 'armor' member, it will be used to store
346 * a token that is checked by the comment script.
347 */
348</mt:Ignore>
349var mtRequestSubmitted = false;
350function mtCommentOnSubmit(f) {
351    if (!mtRequestSubmitted) {
352        mtRequestSubmitted = true;
353
354        if (f.armor)
355            f.armor.value = '<$mt:BlogSitePath encode_sha1="1"$>';
356        if (f.bakecookie && f.bakecookie.checked)
357            mtSaveUser(f);
358
359        // disable submit buttons
360        if (f.preview_button) f.preview_button.disabled = true;
361        if (f.post) f.post.disabled = true;
362
363        var u = mtGetUser();
364        if ( !is_preview && ( u && u.is_authenticated ) ) {
365            // validate session; then submit
366            mtFetchedUser = false;
367            mtFetchUser('mtCommentSessionVerify');
368            return false;
369        }
370
371        return true;
372    }
373    return false;
374}
375
376function mtCommentSessionVerify(app_user) {
377    var u = mtGetUser();
378    var f = document['comments_form'];
379    if ( u && app_user && app_user.sid && ( u.sid == app_user.sid ) ) {
380        f.submit();
381    } else {
382        alert('<__trans phrase="Your session has expired. Please sign in again to comment." escape="js">');
383        mtClearUser();
384        mtFireEvent('usersignin');
385<mt:IfRegistrationRequired>
386        mtShow('comments-form');
387        mtHide('comments-open-footer');
388</mt:IfRegistrationRequired>
389    }
390}
391
392function mtUserOnLoad() {
393    var u = mtGetUser();
394
395    // if the user is authenticated, hide the 'anonymous' fields
396    // and any captcha input if already shown
397    if ( document.getElementById('comments-form')) {
398        if ( u && u.is_authenticated ) {
399            mtShow('comments-form');
400            mtHide('comments-open-data');
401            if (mtCaptchaVisible)
402                mtHide('comments-open-captcha');
403        } else {
404<mt:IfRegistrationRequired>
405            mtHide('comments-form');
406</mt:IfRegistrationRequired>
407        }
408        if ( u && u.is_banned )
409            mtHide('comments-form');
410
411        // if we're previewing a comment, make sure the captcha
412        // field is visible
413        if (is_preview)
414            mtShowCaptcha();
415        else
416            mtShowGreeting();
417
418        // populate anonymous comment fields if user is cookied as anonymous
419        var cf = document['comments_form'];
420        if (cf) {
421            if (u && u.is_anonymous) {
422                if (u.email) cf.email.value = u.email;
423                if (u.name) cf.author.value = u.name;
424                if (u.url) cf.url.value = u.url;
425                if (cf.bakecookie)
426                    cf.bakecookie.checked = u.name || u.email;
427            } else {
428                if (u && u.sid && cf.sid)
429                    cf.sid.value = u.sid;
430            }
431        }
432    }
433}
434
435<mt:Ignore>
436/***
437 * Called when an entry archive page is loaded.
438 * This routine controls which elements of the comment form are shown
439 * or hidden, depending on commenter type and blog configuration.
440 */
441</mt:Ignore>
442function mtEntryOnLoad() {
443    <mt:Unless tag="IfPingsAccepted">mtHide('trackbacks-info');</mt:Unless>
444    <mt:Unless tag="IfCommentsAccepted">mtHide('comments-open');</mt:Unless>
445    mtFireEvent('usersignin');
446}
447
448function mtEntryOnUnload() {
449    if (mtRequestSubmitted) {
450        var cf = document['comments_form'];
451        if (cf) {
452            if (cf.post && cf.post.disabled)
453                cf.post.disabled = false;
454            if (cf.preview_button && cf.preview_button.disabled)
455                cf.preview_button.disabled = false;
456        }
457        mtRequestSubmitted = false;
458    }
459    return true;
460}
461
462mtAttachEvent('usersignin', mtUserOnLoad);
463
464<mt:Ignore>
465/***
466 * Handles the action of the "Sign in" link. First clears any existing
467 * user cookie, then directs to the MT comment script to sign the user in.
468 */
469</mt:Ignore>
470function mtSignIn() {
471    var doc_url = document.URL;
472    doc_url = doc_url.replace(/#.+/, '');
473    var url = '<$mt:SignInLink$>';
474    if (is_preview) {
475        if ( document['comments_form'] ) {
476            var entry_id = document['comments_form'].entry_id.value;
477            url += '&entry_id=' + entry_id;
478        } else {
479            url += '&return_url=<$mt:BlogURL encode_url="1"$>';
480        }
481    } else {
482        url += '&return_url=' + encodeURIComponent(doc_url);
483    }
484    mtClearUser();
485    location.href = url;
486}
487
488function mtSignInOnClick(sign_in_element) {
489    var el;
490    if (sign_in_element) {
491        // display throbber
492        el = document.getElementById(sign_in_element);
493        if (!el)  // legacy MT 4.x element id
494            el = document.getElementById('comment-form-external-auth');
495    }
496    if (el)
497        el.innerHTML = '<__trans phrase="Signing in..." escape="js"> <img src="<$mt:StaticWebPath$>images/indicator.white.gif" height="16" width="16" alt="" />';
498
499    mtClearUser(); // clear any 'anonymous' user cookie to allow sign in
500    mtFetchUser('mtSetUserOrLogin');
501    return false;
502}
503
504function mtSetUserOrLogin(u) {
505    if (u && u.is_authenticated) {
506        mtSetUser(u);
507    } else {
508        // user really isn't logged in; so let's do this!
509        mtSignIn();
510    }
511}
512
513<mt:Ignore>
514/***
515 * Handles sign out from the web site.
516 * First clears any existing user cookie, then direts to the MT comment
517 * script to sign the user out.
518 */
519</mt:Ignore>
520function mtSignOut(entry_id) {
521    mtClearUser();
522    var doc_url = document.URL;
523    doc_url = doc_url.replace(/#.+/, '');
524    var url = '<$mt:SignOutLink$>';
525    if (is_preview) {
526        if ( document['comments_form'] ) {
527            var entry_id = document['comments_form'].entry_id.value;
528            url += '&entry_id=' + entry_id;
529        } else {
530            url += '&return_url=<$mt:BlogURL encode_url="1"$>';
531        }
532    } else {
533        url += '&return_url=' + encodeURIComponent(doc_url);
534    }
535    location.href = url;
536}
537
538<mt:Ignore>
539/***
540 * Handles the action of the "Sign out" link.
541 */
542</mt:Ignore>
543function mtSignOutOnClick() {
544    mtSignOut();
545    return false;
546}
547
548<mt:Ignore>
549/***
550 * Handles the display of the greeting message, depending on what kind of
551 * user is logged in and blog comment policy.
552 */
553</mt:Ignore>
554function mtShowGreeting() {
555<mt:IfRegistrationAllowed>
556    var reg_reqd = <mt:IfRegistrationRequired>true<mt:Else>false</mt:IfRegistrationRequired>;
557
558    var cf = document['comments_form'];
559    if (!cf) return;
560
561    var el = document.getElementById('comment-greeting');
562    if (!el)  // legacy MT 4.x element id
563        el = document.getElementById('comment-form-external-auth');
564    if (!el) return;
565
566    var eid = cf.entry_id;
567    var entry_id;
568    if (eid) entry_id = eid.value;
569
570    var phrase;
571    var u = mtGetUser();
572
573    if ( u && u.is_authenticated ) {
574        if ( u.is_banned ) {
575            phrase = '<__trans phrase="You do not have permission to comment on this blog. ([_1]sign out[_2])" params="<a href="javascript:void(0);" onclick="return mtSignOutOnClick();">%%</a>" escape="js">';
576        } else {
577            var user_link;
578            if ( u.is_author ) {
579                user_link = '<a href="<$mt:CGIPath$><$mt:CommentScript$>?__mode=edit_profile&return_url=' + encodeURIComponent( location.href );
580                user_link += '">' + u.name + '</a>';
581            } else {
582                // registered user, but not a user with posting rights
583                if (u.url)
584                    user_link = '<a href="' + u.url + '">' + u.name + '</a>';
585                else
586                    user_link = u.name;
587            }
588            // TBD: supplement phrase with userpic if one is available.
589            phrase = '<__trans phrase="Thanks for signing in, __NAME__. ([_1]sign out[_2])" params="<a href="javascript:void(0)" onclick="return mtSignOutOnClick();">%%</a>" escape="js">';
590            phrase = phrase.replace(/__NAME__/, user_link);
591        }
592    } else {
593        if (reg_reqd) {
594            phrase = '<__trans phrase="[_1]Sign in[_2] to comment." params="<a href="javascript:void(0)" onclick="return mtSignInOnClick('comment-greeting')">%%</a>" escape="js">';
595        } else {
596            phrase = '<__trans phrase="[_1]Sign in[_2] to comment, or comment anonymously." params="<a href="javascript:void(0)" onclick="return mtSignInOnClick('comment-greeting')">%%</a>" escape="js">';
597        }
598    }
599    el.innerHTML = phrase;
600<mt:Else>
601    mtShowCaptcha();
602</mt:IfRegistrationAllowed>
603}
604
605<mt:Ignore>
606/***
607 * Handles the action of the 'Reply' links.
608 */
609</mt:Ignore>
610function mtReplyCommentOnClick(parent_id, author) {
611    mtShow('comment-form-reply');
612
613    var checkbox = document.getElementById('comment-reply');
614    var label = document.getElementById('comment-reply-label');
615    var text = document.getElementById('comment-text');
616
617    // Populate label with new values
618    var reply_text = '<__trans phrase="Replying to <a href="[_1]" onclick="[_2]">comment from [_3]</a>" params="#comment-__PARENT__%%location.href=this.href; return false%%__AUTHOR__" escape="js">';
619    reply_text = reply_text.replace(/__PARENT__/, parent_id);
620    reply_text = reply_text.replace(/__AUTHOR__/, author);
621    label.innerHTML = reply_text;
622
623    checkbox.value = parent_id;
624    checkbox.checked = true;
625    try {
626        // text field may be hidden
627        text.focus();
628    } catch(e) {
629    }
630
631    mtSetCommentParentID();
632}
633
634<mt:Ignore>
635/***
636 * Sets the parent comment ID when replying to a comment.
637 */
638</mt:Ignore>
639function mtSetCommentParentID() {
640    var checkbox = document.getElementById('comment-reply');
641    var parent_id_field = document.getElementById('comment-parent-id');
642    if (!checkbox || !parent_id_field) return;
643
644    var pid = 0;
645    if (checkbox.checked == true)
646        pid = checkbox.value;
647    parent_id_field.value = pid;
648}
649
650<mt:Ignore>
651/***
652 * Persists a copy of the current user cookie into the browser cookie stash.
653 */
654</mt:Ignore>
655function mtSaveUser(f) {
656    // We can't reliably store the user cookie during a preview.
657    if (is_preview) return;
658
659    var u = mtGetUser();
660
661    if (f && (!u || u.is_anonymous)) {
662        if ( !u ) {
663            u = {};
664            u.is_authenticated = false;
665            u.can_comment = true;
666            u.is_author = false;
667            u.is_banned = false;
668            u.is_anonymous = true;
669            u.is_trusted = false;
670        }
671        if (f.author != undefined) u.name = f.author.value;
672        if (f.email != undefined) u.email = f.email.value;
673        if (f.url != undefined) u.url = f.url.value;
674    }
675
676    if (!u) return;
677
678    var cache_period = mtCookieTimeout * 1000;
679
680    // cache anonymous user info for a long period if the
681    // user has requested to be remembered
682    if (u.is_anonymous && f && f.bakecookie && f.bakecookie.checked)
683        cache_period = 365 * 24 * 60 * 60 * 1000;
684
685    var now = new Date();
686    mtFixDate(now);
687    now.setTime(now.getTime() + cache_period);
688
689    var cmtcookie = mtBakeUserCookie(u);
690    mtSetCookie(mtCookieName, cmtcookie, now, mtCookiePath, mtCookieDomain,
691        location.protocol == 'https:');
692}
693
694<mt:Ignore>
695/***
696 * Clears the blog-side user cookie.
697 */
698</mt:Ignore>
699function mtClearUser() {
700    user = null;
701    mtDeleteCookie(mtCookieName, mtCookiePath, mtCookieDomain,
702        location.protocol == 'https:');
703}
704
705<mt:Ignore>
706/***
707 * Sets a browser cookie.
708 */
709</mt:Ignore>
710function mtSetCookie(name, value, expires, path, domain, secure) {
711    if (domain && domain.match(/^\.?localhost$/))
712        domain = null;
713    var curCookie = name + "=" + escape(value) +
714        (expires ? "; expires=" + expires.toGMTString() : "") +
715        (path ? "; path=" + path : "") +
716        (domain ? "; domain=" + domain : "") +
717        (secure ? "; secure" : "");
718    document.cookie = curCookie;
719}
720
721<mt:Ignore>
722/***
723 * Retrieves a browser cookie.
724 */
725</mt:Ignore>
726function mtGetCookie(name) {
727    var prefix = name + '=';
728    var c = document.cookie;
729    var cookieStartIndex = c.indexOf(prefix);
730    if (cookieStartIndex == -1)
731        return '';
732    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
733    if (cookieEndIndex == -1)
734        cookieEndIndex = c.length;
735    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
736}
737
738<mt:Ignore>
739/***
740 * Deletes a browser cookie.
741 */
742</mt:Ignore>
743function mtDeleteCookie(name, path, domain, secure) {
744    if (mtGetCookie(name)) {
745        if (domain && domain.match(/^\.?localhost$/))
746            domain = null;
747        document.cookie = name + "=" +
748            (path ? "; path=" + path : "") +
749            (domain ? "; domain=" + domain : "") +
750            (secure ? "; secure" : "") +
751            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
752    }
753}
754
755function mtFixDate(date) {
756    var skew = (new Date(0)).getTime();
757    if (skew > 0)
758        date.setTime(date.getTime() - skew);
759}
760
761<mt:Ignore>
762/***
763 * Returns a XMLHttpRequest object (for Ajax operations).
764 */
765</mt:Ignore>
766function mtGetXmlHttp() {
767    if ( !window.XMLHttpRequest ) {
768        window.XMLHttpRequest = function() {
769            var types = [
770                "Microsoft.XMLHTTP",
771                "MSXML2.XMLHTTP.5.0",
772                "MSXML2.XMLHTTP.4.0",
773                "MSXML2.XMLHTTP.3.0",
774                "MSXML2.XMLHTTP"
775            ];
776
777            for ( var i = 0; i < types.length; i++ ) {
778                try {
779                    return new ActiveXObject( types[ i ] );
780                } catch( e ) {}
781            }
782
783            return undefined;
784        };
785    }
786    if ( window.XMLHttpRequest )
787        return new XMLHttpRequest();
788}
789
790// BEGIN: fast browser onload init
791// Modifications by David Davis, DWD
792// Dean Edwards/Matthias Miller/John Resig
793// http://dean.edwards.name/weblog/2006/06/again/?full#comment5338
794
795function mtInit() {
796    // quit if this function has already been called
797    if (arguments.callee.done) return;
798
799    // flag this function so we don't do the same thing twice
800    arguments.callee.done = true;
801
802    // kill the timer
803    // DWD - check against window
804    if ( window._timer ) clearInterval(window._timer);
805
806    // DWD - fire the window onload now, and replace it
807    if ( window.onload && ( window.onload !== window.mtInit ) ) {
808        window.onload();
809        window.onload = function() {};
810    }
811}
812
813/* for Mozilla/Opera9 */
814if (document.addEventListener) {
815    document.addEventListener("DOMContentLoaded", mtInit, false);
816}
817
818/* for Internet Explorer */
819/*@cc_on @*/
820/*@if (@_win32)
821document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
822var script = document.getElementById("__ie_onload");
823script.onreadystatechange = function() {
824    if (this.readyState == "complete") {
825        mtInit(); // call the onload handler
826    }
827};
828/*@end @*/
829
830/* for Safari */
831if (/WebKit/i.test(navigator.userAgent)) { // sniff
832    _timer = setInterval(function() {
833        if (/loaded|complete/.test(document.readyState)) {
834            mtInit(); // call the onload handler
835        }
836    }, 10);
837}
838
839/* for other browsers */
840window.onload = mtInit;
841
842// END: fast browser onload init
843
844<mt:IfRegistrationAllowed>
845/***
846 * If request contains a '#_login' or '#_logout' hash, use this to
847 * also delete the blog-side user cookie, since we're coming back from
848 * a login, logout or edit profile operation.
849 */
850var clearCookie = ( window.location.hash && window.location.hash.match( /^#_log(in|out)/ ) ) ? true : false;
851if (clearCookie) {
852    // clear any logged in state
853    mtClearUser();
854    if (RegExp.$1 == 'in')
855        mtFetchUser();
856} else {
857    <mt:Ignore>
858    /***
859     * Uncondition this call to fetch the current user state (if available)
860     * from MT upon page load if no user cookie is already present.
861     * This is okay if you have a private install, such as an Intranet;
862     * not recommended for public web sites!
863     */
864    </mt:Ignore>
865    if ( is_preview && !user )
866        mtFetchUser();
867}
868</mt:IfRegistrationAllowed>
Note: See TracBrowser for help on using the browser.