Changeset 7957


Ignore:
Timestamp:
Nov 30, 2010, 9:42:03 PM (13 years ago)
Author:
rvelices
Message:

bug 2043: some Javascript errors in default theme (also makes the rating.js script async)

Location:
trunk/themes/default
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/themes/default/js/rating.js

    r7852 r7957  
    33function makeNiceRatingForm(options)
    44{
    5         gRatingOptions = options || {};
     5        gRatingOptions = options;
    66        var form = document.getElementById('rateForm');
    77        if (!form) return; //? template changed
     
    3737
    3838                pwgAddEventListener(rateButton, "click", updateRating);
    39                 pwgAddEventListener(rateButton, "mouseout", resetRatingStarDisplay);
    40                 pwgAddEventListener(rateButton, "mouseover", updateRatingStarDisplayEvt);
     39                pwgAddEventListener(rateButton, "mouseout", function() {updateRatingStarDisplay( gUserRating );});
     40                pwgAddEventListener(rateButton, "mouseover", function(e) {
     41                        updateRatingStarDisplay( e.target ? e.target.initialRateValue : e.srcElement.initialRateValue);
     42                        });
    4143        }
    42         resetRatingStarDisplay();
    43 }
    44 
    45 function resetRatingStarDisplay()
    46 {
    4744        updateRatingStarDisplay( gUserRating );
    4845}
     
    5249        for (var i=0; i<gRatingButtons.length; i++)
    5350                gRatingButtons[i].className = (userRating!=="" && userRating>=gRatingButtons[i].initialRateValue ) ? "rateButtonStarFull" : "rateButtonStarEmpty";
    54 }
    55 
    56 function updateRatingStarDisplayEvt(e)
    57 {
    58         updateRatingStarDisplay(
    59                 e.target ? e.target.initialRateValue : e.srcElement.initialRateValue);
    6051}
    6152
     
    9182        return false;
    9283}
     84
     85(function() {
     86if (typeof _pwgRatingAutoQueue!="undefined" && _pwgRatingAutoQueue.length)
     87{
     88    for (var i=0; i<_pwgRatingAutoQueue.length; i++)
     89        makeNiceRatingForm(_pwgRatingAutoQueue[i]);
     90}
     91_pwgRatingAutoQueue = {
     92        push: function(opts) {
     93                makeNiceRatingForm(opts);
     94        }
     95}
     96})();
  • trunk/themes/default/js/scripts.js

    r7852 r7957  
    2222}
    2323
    24 Function.prototype.pwgBind = function() {
    25                 var __method = this, object = arguments[0], args = Array.prototype.slice.call(arguments,1);
    26                 return function() {
    27                                 return __method.apply(object, args.concat(arguments) );
    28                 }
     24function pwgBind(object, method) {
     25        var args = Array.prototype.slice.call(arguments,2);
     26        return function() {
     27                        return method.apply(object, args.concat(Array.prototype.slice.call(arguments,0)) );
     28        }
    2929}
     30
    3031function PwgWS(urlRoot)
    3132{
     
    4041
    4142PwgWS.prototype = {
    42 
    4343        callService : function(method, parameters, options)
    4444        {
    4545                if (options)
    4646                {
    47                         for (var property in options)
    48                                 this.options[property] = options[property];
     47                        for (var prop in options)
     48                                this.options[prop] = options[prop];
    4949                }
    50                 try { this.transport = new XMLHttpRequest();}
     50                try { this.xhr = new XMLHttpRequest();}
    5151                catch(e) {
    52                         try { this.transport = new ActiveXObject('Msxml2.XMLHTTP'); }
     52                        try { this.xhr = new ActiveXObject('Msxml2.XMLHTTP'); }
    5353                        catch(e) {
    54                                 try { this.transport = new ActiveXObject('Microsoft.XMLHTTP'); }
     54                                try { this.xhr = new ActiveXObject('Microsoft.XMLHTTP'); }
    5555                                catch (e){
    56                                         dispatchError(0, "Cannot create request object");
     56                                        this.error(0, "Cannot create request object");
     57                                        return;
    5758                                }
    5859                        }
    5960                }
    60                 this.transport.onreadystatechange = this.onStateChange.pwgBind(this);
     61                this.xhr.onreadystatechange = pwgBind(this, this.onStateChange);
    6162
    6263                var url = this.urlRoot+"ws.php?format=json";
     
    6566                if (parameters)
    6667                {
    67                         for (var property in parameters)
     68                        for (var prop in parameters)
    6869                        {
    69                                 if ( typeof parameters[property] == 'object' && parameters[property])
     70                                if ( typeof parameters[prop] == 'object' && parameters[prop])
    7071                                {
    71                                         for (var i=0; i<parameters[property].length; i++)
    72                                                 body += "&"+property+"[]="+encodeURIComponent(parameters[property][i]);
     72                                        for (var i=0; i<parameters[prop].length; i++)
     73                                                body += "&"+prop+"[]="+encodeURIComponent(parameters[prop][i]);
    7374                                }
    7475                                else
    75                                         body += "&"+property+"="+encodeURIComponent(parameters[property]);
     76                                        body += "&"+prop+"="+encodeURIComponent(parameters[prop]);
    7677                        }
    7778                }
    7879
    7980                if (this.options.method == "POST" )
    80                 {
    81                         this.transport.open(this.options.method, url, this.options.async);
    82                         this.transport.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    83                         this.transport.send(body);
    84                 }
     81                        this.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    8582                else
    8683                {
    8784                        url += "&"+body;
    88                         this.transport.open(this.options.method, url, this.options.async);
    89                         this.transport.send(null);
     85                        body = null;
     86                }
     87                this.xhr.open(this.options.method, url, this.options.async);
     88                try {
     89                        this.xhr.send(body);
     90                } catch(e) {
     91                        this.error(0, e.message);
    9092                }
    9193        },
    9294
    9395        onStateChange: function() {
    94                 var readyState = this.transport.readyState;
     96                var readyState = this.xhr.readyState;
    9597                if (readyState==4)
    96                         this.respondToReadyState(readyState);
     98                {
     99                        try {
     100                                this.respondToReadyState(readyState);
     101                        } finally {
     102                                this.cleanup();
     103                        }
     104                }
    97105        },
    98106
    99         dispatchError: function( httpCode, text )
     107        error: function( httpCode, text )
    100108        {
    101109                !this.options.onFailure || this.options.onFailure( httpCode, text);
     110                this.cleanup();
    102111        },
    103112
    104113        respondToReadyState: function(readyState)
    105114        {
    106                 var transport = this.transport;
    107                 if (readyState==4 && transport.status == 200)
     115                var xhr = this.xhr;
     116                if (readyState==4 && xhr.status == 200)
    108117                {
    109118                        var resp;
    110119                        try {
    111                                 eval('resp = ' + transport.responseText);
     120                                resp = window.JSON && window.JSON.parse ? window.JSON.parse( xhr.responseText ) : (new Function("return " + xhr.responseText))();
    112121                        }
    113122                        catch (e) {
    114                                 this.dispatchError( 200, e.message + '\n' + transport.responseText.substr(0,512) );
     123                                this.error( 200, e.message + '\n' + xhr.responseText.substr(0,512) );
    115124                        }
    116125                        if (resp!=null)
    117126                        {
    118127                                if (resp.stat==null)
    119                                         this.dispatchError( 200, "Invalid response" );
     128                                        this.error( 200, "Invalid response" );
    120129                                else if (resp.stat=='ok')
    121                                 {
    122                                         if (this.options.onSuccess) this.options.onSuccess( resp.result );
    123                                 }
     130                                        !this.options.onSuccess || this.options.onSuccess( resp.result );
    124131                                else
    125                                         this.dispatchError( 200, resp.err + " " + resp.message);
     132                                        this.error( 200, resp.err + " " + resp.message);
    126133                        }
    127134                }
    128                 if (readyState==4 && transport.status != 200)
    129                         this.dispatchError( transport.status, transport.statusText );
     135                if (readyState==4 && xhr.status != 200)
     136                        this.error( xhr.status, xhr.statusText );
    130137        },
    131138
     139        cleanup: function()
     140        {
     141                if (this.xhr) this.xhr.onreadystatechange = null;
     142                this.xhr = null;
     143                this.options.onFailure = this.options.onSuccess = null;
     144        },
    132145
    133         transport: null,
    134         urlRoot: null,
    135         options: {}
     146        xhr: null
    136147}
    137148
    138149function pwgAddEventListener(elem, evt, fn)
    139150{
    140         if (window.attachEvent)
    141                 elem.attachEvent('on'+evt, fn);
     151        if (window.addEventListener)
     152                elem.addEventListener(evt, fn, false);
    142153        else
    143                 elem.addEventListener(evt, fn, false);
     154                elem.attachEvent('on'+evt, fn);         
    144155}
  • trunk/themes/default/template/picture.tpl

    r6993 r7957  
    209209                        {/if}
    210210                        {/foreach}
    211                         <script type="text/javascript" src="{$ROOT_URL}themes/default/js/rating.js"></script>
    212211                        <script type="text/javascript">
    213                         makeNiceRatingForm( {ldelim}rootUrl: '{$ROOT_URL|@escape:"javascript"}', image_id: {$current.id},
    214                         updateRateText: "{'Update your rating'|@translate|@escape:'javascript'}", updateRateElement: document.getElementById("updateRate"),
    215                         ratingSummaryText: "{'%.2f (rated %d times)'|@translate|@escape:'javascript'}", ratingSummaryElement: document.getElementById("ratingSummary") {rdelim} );
     212                                var _pwgRatingAutoQueue = _pwgRatingAutoQueue || [];
     213                                _pwgRatingAutoQueue.push(  {ldelim}rootUrl: '{$ROOT_URL|@escape:"javascript"}', image_id: {$current.id},
     214                                        updateRateText: "{'Update your rating'|@translate|@escape:'javascript'}", updateRateElement: document.getElementById("updateRate"),
     215                                        ratingSummaryText: "{'%.2f (rated %d times)'|@translate|@escape:'javascript'}", ratingSummaryElement: document.getElementById("ratingSummary") {rdelim} );
     216                                (function () {ldelim}
     217                                var s = document.createElement('script'); s.type = 'text/javascript'; s.async = true; s.src = '{$ROOT_URL}themes/default/js/rating.js';
     218                                var s0 = document.getElementsByTagName('script')[0]; s0.parentNode.insertBefore(s, s0);
     219                                })();
    216220                        </script>
    217221                        </div>
Note: See TracChangeset for help on using the changeset viewer.