root/trunk/SimpleTemplateSets/templates/vanilla+authentication+comments/javascript.mtml @ 891

Revision 891, 26.5 kB (checked in by bsmith, 16 months ago)

committing initial Vanilla sets

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