root/trunk/Vanilla/plugins/Vanilla/templates/vanilla+scoring/mtcs-js.mtml @ 1220

Revision 1220, 3.9 kB (checked in by bsmith, 13 months ago)

adding two Vanilla template sets: Vanilla+Scoring and Vanilla+Widgets

Line 
1// This file is not used by this template set.
2// It is here as a placeholder for adding a
3// feature where scoring can be disabled once
4// a user has scored the object.
5
6
7/* MTCS Scoring *****************************************************************/
8
9<mt:Ignore>
10/***
11 * Functions for scoring entries
12 * Scoring requires blog context, it is thus conditioned.
13 */
14</mt:Ignore>
15<mt:IfBlog>
16function mtScore(entry_id) {
17    var span = DOM.getElement('scoring-id-' + entry_id);
18    if (!span) return false;
19    if (DOM.hasClassName(span, 'scoring-pending')) return false;
20    if (DOM.hasClassName(span, 'scoring-scored')) return false;
21    if (!DOM.hasClassName(span, 'scoring-scorable')) return false;
22
23    var xh = mtGetXmlHttp();
24    if (!xh) return false;
25
26    DOM.addClassName( span, 'scoring-pending' );
27    var url = '<$mt:CGIPath$><$mt:CommunityScript$>';
28    xh.open('POST', url, true);
29    xh.onreadystatechange = function() {
30        if ( xh.readyState == 4 ) {
31            if ( xh.status && ( xh.status != 200 ) ) {
32                // error - ignore
33            } else {
34                eval(xh.responseText);
35            }
36        }
37    };
38    xh.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
39    xh.send( '__mode=vote&blog_id=<$mt:BlogID$>&f=scored,count&jsonp=mtScore_cb&id=' + entry_id);
40    return false;
41}
42
43function mtUpdateScores() {
44    var u = mtGetUser();
45    <mt:IfAnonymousRecommendAllowed>
46    <mt:Else>
47        if (!u) return false;
48    </mt:IfAnonymousRecommendAllowed>
49
50    var entry_ids = '';
51    var scores = DOM.getElementsByClassName("scoring");
52    for (var i = 0; i < scores.length; i++) {
53        var id = scores[i].id;
54        id = id.replace(/^scoring-id-/, '');
55        if (entry_ids != '') entry_ids += ",";
56        entry_ids += id;
57    }
58    if (entry_ids == '') return false;
59
60    var xh = mtGetXmlHttp();
61    if (!xh) return false;
62
63    var url = '<$mt:CGIPath$><$mt:CommunityScript$>';
64    xh.open('POST', url, true);
65    xh.onreadystatechange = function() {
66        if ( xh.readyState == 4 ) {
67            if ( xh.status && ( xh.status != 200 ) ) {
68                // error - ignore
69            } else {
70                eval(xh.responseText);
71            }
72        }
73    };
74    xh.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
75    xh.send( '__mode=score&blog_id=<$mt:BlogID$>&f=scored,count&jsonp=mtScore_cb&id=' + entry_ids);
76    return false;
77}
78
79function mtScore_cb(s_hash) {
80    var u = mtGetUser();
81    if (s_hash['error']) {
82        var els = DOM.getElementsByClassName('scoring-pending');
83        for (var i = 0; i < els.length; i++)
84            DOM.removeCLassName(els[i], 'scoring-pending');
85        // display error
86        alert(s_hash['error']);
87        return;
88    }
89    for (var id in s_hash) {
90        var span = DOM.getElement('scoring-id-' + id);
91        if ( span ) {
92            DOM.removeClassName( span, 'scoring-pending' );
93            DOM.removeClassName( span, 'scoring-scorable' );
94            if ( s_hash[id].scored ) {
95                DOM.addClassName( span, 'scoring-scored' );
96            } else {
97    <mt:IfAnonymousRecommendAllowed>
98                DOM.addClassName( span, 'scoring-scorable' );
99    <mt:Else>
100                if ( u && u.is_authenticated )
101                    DOM.addClassName( span, 'scoring-scorable' );
102    </mt:IfAnonymousRecommendAllowed>
103            }
104        }
105        var score = DOM.getElement('scoring-score-' + id);
106        if ( score )
107            score.innerHTML = s_hash[id].count ? s_hash[id].count : 0;
108        var label = DOM.getElement('scoring-score-label-' + id);
109        if ( label ) {
110            switch ( s_hash[id].count ) {
111                case 1:
112                    label.innerHTML = '<__trans phrase="Vote">';
113                    break;
114                default:
115                    label.innerHTML = '<__trans phrase="Votes">';
116                    break;
117            }
118        }
119    }
120}
121</mt:IfBlog>
Note: See TracBrowser for help on using the browser.