Changeset 7958 for branches/2.1


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

bug 2043: merge from trunk to branch 2.1
some Javascript errors in default theme (also makes the rating.js script async)

Location:
branches/2.1/themes/default
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2.1/themes/default/js/rating.js

    r6576 r7958  
    9292        return false;
    9393}
     94
     95(function() {
     96if (typeof _pwgRatingAutoQueue!="undefined" && _pwgRatingAutoQueue.length)
     97{
     98    for (var i=0; i<_pwgRatingAutoQueue.length; i++)
     99        makeNiceRatingForm(_pwgRatingAutoQueue[i]);
     100}
     101_pwgRatingAutoQueue = {
     102        push: function(opts) {
     103                makeNiceRatingForm(opts);
     104        }
     105}
     106})();
  • branches/2.1/themes/default/js/scripts.js

    r6576 r7958  
    5252}
    5353
    54 Function.prototype.pwgBind = function() {
    55                 var __method = this, object = arguments[0], args = Array.prototype.slice.call(arguments,1);
    56                 return function() {
    57                                 return __method.apply(object, args.concat(arguments) );
    58                 }
     54function pwgBind(object, method) {
     55        var args = Array.prototype.slice.call(arguments,2);
     56        return function() {
     57                        return method.apply(object, args.concat(Array.prototype.slice.call(arguments,0)) );
     58        }
    5959}
     60
    6061function PwgWS(urlRoot)
    6162{
     
    6364        this.options = {
    6465                method: "GET",
    65                 async:  true,
     66                async: true,
    6667                onFailure: null,
    6768                onSuccess: null
     
    7071
    7172PwgWS.prototype = {
    72 
    7373        callService : function(method, parameters, options)
    7474        {
    7575                if (options)
    7676                {
    77                         for (var property in options)
    78                                 this.options[property] = options[property];
     77                        for (var prop in options)
     78                                this.options[prop] = options[prop];
    7979                }
    80                 try { this.transport = new XMLHttpRequest();}
     80                try { this.xhr = new XMLHttpRequest();}
    8181                catch(e) {
    82                         try { this.transport = new ActiveXObject('Msxml2.XMLHTTP'); }
     82                        try { this.xhr = new ActiveXObject('Msxml2.XMLHTTP'); }
    8383                        catch(e) {
    84                                 try { this.transport = new ActiveXObject('Microsoft.XMLHTTP'); }
     84                                try { this.xhr = new ActiveXObject('Microsoft.XMLHTTP'); }
    8585                                catch (e){
    86                                         dispatchError(0, "Cannot create request object");
     86                                        this.error(0, "Cannot create request object");
     87                                        return;
    8788                                }
    8889                        }
    8990                }
    90                 this.transport.onreadystatechange = this.onStateChange.pwgBind(this);
     91                this.xhr.onreadystatechange = pwgBind(this, this.onStateChange);
    9192
    9293                var url = this.urlRoot+"ws.php?format=json";
     
    9596                if (parameters)
    9697                {
    97                         for (var property in parameters)
     98                        for (var prop in parameters)
    9899                        {
    99                                 if ( typeof parameters[property] == 'object' && parameters[property])
     100                                if ( typeof parameters[prop] == 'object' && parameters[prop])
    100101                                {
    101                                         for (var i=0; i<parameters[property].length; i++)
    102                                                 body += "&"+property+"[]="+encodeURIComponent(parameters[property][i]);
     102                                        for (var i=0; i<parameters[prop].length; i++)
     103                                                body += "&"+prop+"[]="+encodeURIComponent(parameters[prop][i]);
    103104                                }
    104105                                else
    105                                         body += "&"+property+"="+encodeURIComponent(parameters[property]);
     106                                        body += "&"+prop+"="+encodeURIComponent(parameters[prop]);
    106107                        }
    107108                }
    108109
    109110                if (this.options.method == "POST" )
    110                 {
    111                         this.transport.open(this.options.method, url, this.options.async);
    112                         this.transport.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    113                         this.transport.send(body);
    114                 }
     111                        this.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    115112                else
    116113                {
    117114                        url += "&"+body;
    118                         this.transport.open(this.options.method, url, this.options.async);
    119                         this.transport.send(null);
     115                        body = null;
     116                }
     117                this.xhr.open(this.options.method, url, this.options.async);
     118                try {
     119                        this.xhr.send(body);
     120                } catch(e) {
     121                        this.error(0, e.message);
    120122                }
    121123        },
    122124
    123125        onStateChange: function() {
    124                 var readyState = this.transport.readyState;
     126                var readyState = this.xhr.readyState;
    125127                if (readyState==4)
    126                         this.respondToReadyState(readyState);
     128                {
     129                        try {
     130                                this.respondToReadyState(readyState);
     131                        } finally {
     132                                this.cleanup();
     133                        }
     134                }
    127135        },
    128136
    129         dispatchError: function( httpCode, text )
     137        error: function( httpCode, text )
    130138        {
    131139                !this.options.onFailure || this.options.onFailure( httpCode, text);
     140                this.cleanup();
    132141        },
    133142
    134143        respondToReadyState: function(readyState)
    135144        {
    136                 var transport = this.transport;
    137                 if (readyState==4 && transport.status == 200)
     145                var xhr = this.xhr;
     146                if (readyState==4 && xhr.status == 200)
    138147                {
    139148                        var resp;
    140149                        try {
    141                                 eval('resp = ' + transport.responseText);
     150                                resp = window.JSON && window.JSON.parse ? window.JSON.parse( xhr.responseText ) : (new Function("return " + xhr.responseText))();
    142151                        }
    143152                        catch (e) {
    144                                 this.dispatchError( 200, e.message + '\n' + transport.responseText.substr(0,512) );
     153                                this.error( 200, e.message + '\n' + xhr.responseText.substr(0,512) );
    145154                        }
    146155                        if (resp!=null)
    147156                        {
    148157                                if (resp.stat==null)
    149                                         this.dispatchError( 200, "Invalid response" );
     158                                        this.error( 200, "Invalid response" );
    150159                                else if (resp.stat=='ok')
    151                                 {
    152                                         if (this.options.onSuccess) this.options.onSuccess( resp.result );
    153                                 }
     160                                        !this.options.onSuccess || this.options.onSuccess( resp.result );
    154161                                else
    155                                         this.dispatchError( 200, resp.err + " " + resp.message);
     162                                        this.error( 200, resp.err + " " + resp.message);
    156163                        }
    157164                }
    158                 if (readyState==4 && transport.status != 200)
    159                         this.dispatchError( transport.status, transport.statusText );
     165                if (readyState==4 && xhr.status != 200)
     166                        this.error( xhr.status, xhr.statusText );
    160167        },
    161168
     169        cleanup: function()
     170        {
     171                if (this.xhr) this.xhr.onreadystatechange = null;
     172                this.xhr = null;
     173                this.options.onFailure = this.options.onSuccess = null;
     174        },
    162175
    163         transport: null,
    164         urlRoot: null,
    165         options: {}
     176        xhr: null
    166177}
    167178
    168179function pwgAddEventListener(elem, evt, fn)
    169180{
    170         if (window.attachEvent)
    171                 elem.attachEvent('on'+evt, fn);
     181        if (window.addEventListener)
     182                elem.addEventListener(evt, fn, false);
    172183        else
    173                 elem.addEventListener(evt, fn, false);
     184                elem.attachEvent('on'+evt, fn);         
    174185}
  • branches/2.1/themes/default/template/picture.tpl

    r6614 r7958  
    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.