root/branches/release-39/default_templates/javascript.mtml @ 2487

Revision 2487, 25.0 kB (checked in by bchoate, 18 months ago)

Issue focus in try block to avoid error if text field is hidden. BugId:79979

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