bug 2043: merge from trunk to branch 2.1

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

git-svn-id: http://piwigo.org/svn/branches/2.1@7958 68402e56-0260-453c-a942-63ccdbb3a9ee
This commit is contained in:
rvelices 2010-11-30 20:42:35 +00:00
parent 31ab88f829
commit c7f1e1d8ad
3 changed files with 79 additions and 51 deletions

View file

@ -90,4 +90,17 @@ function updateRating(e)
} }
); );
return false; return false;
} }
(function() {
if (typeof _pwgRatingAutoQueue!="undefined" && _pwgRatingAutoQueue.length)
{
for (var i=0; i<_pwgRatingAutoQueue.length; i++)
makeNiceRatingForm(_pwgRatingAutoQueue[i]);
}
_pwgRatingAutoQueue = {
push: function(opts) {
makeNiceRatingForm(opts);
}
}
})();

View file

@ -51,124 +51,135 @@ function popuphelp(url)
); );
} }
Function.prototype.pwgBind = function() { function pwgBind(object, method) {
var __method = this, object = arguments[0], args = Array.prototype.slice.call(arguments,1); var args = Array.prototype.slice.call(arguments,2);
return function() { return function() {
return __method.apply(object, args.concat(arguments) ); return method.apply(object, args.concat(Array.prototype.slice.call(arguments,0)) );
} }
} }
function PwgWS(urlRoot) function PwgWS(urlRoot)
{ {
this.urlRoot = urlRoot; this.urlRoot = urlRoot;
this.options = { this.options = {
method: "GET", method: "GET",
async: true, async: true,
onFailure: null, onFailure: null,
onSuccess: null onSuccess: null
}; };
}; };
PwgWS.prototype = { PwgWS.prototype = {
callService : function(method, parameters, options) callService : function(method, parameters, options)
{ {
if (options) if (options)
{ {
for (var property in options) for (var prop in options)
this.options[property] = options[property]; this.options[prop] = options[prop];
} }
try { this.transport = new XMLHttpRequest();} try { this.xhr = new XMLHttpRequest();}
catch(e) { catch(e) {
try { this.transport = new ActiveXObject('Msxml2.XMLHTTP'); } try { this.xhr = new ActiveXObject('Msxml2.XMLHTTP'); }
catch(e) { catch(e) {
try { this.transport = new ActiveXObject('Microsoft.XMLHTTP'); } try { this.xhr = new ActiveXObject('Microsoft.XMLHTTP'); }
catch (e){ catch (e){
dispatchError(0, "Cannot create request object"); this.error(0, "Cannot create request object");
return;
} }
} }
} }
this.transport.onreadystatechange = this.onStateChange.pwgBind(this); this.xhr.onreadystatechange = pwgBind(this, this.onStateChange);
var url = this.urlRoot+"ws.php?format=json"; var url = this.urlRoot+"ws.php?format=json";
var body = "method="+method; var body = "method="+method;
if (parameters) if (parameters)
{ {
for (var property in parameters) for (var prop in parameters)
{ {
if ( typeof parameters[property] == 'object' && parameters[property]) if ( typeof parameters[prop] == 'object' && parameters[prop])
{ {
for (var i=0; i<parameters[property].length; i++) for (var i=0; i<parameters[prop].length; i++)
body += "&"+property+"[]="+encodeURIComponent(parameters[property][i]); body += "&"+prop+"[]="+encodeURIComponent(parameters[prop][i]);
} }
else else
body += "&"+property+"="+encodeURIComponent(parameters[property]); body += "&"+prop+"="+encodeURIComponent(parameters[prop]);
} }
} }
if (this.options.method == "POST" ) if (this.options.method == "POST" )
{ this.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
this.transport.open(this.options.method, url, this.options.async);
this.transport.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
this.transport.send(body);
}
else else
{ {
url += "&"+body; url += "&"+body;
this.transport.open(this.options.method, url, this.options.async); body = null;
this.transport.send(null); }
this.xhr.open(this.options.method, url, this.options.async);
try {
this.xhr.send(body);
} catch(e) {
this.error(0, e.message);
} }
}, },
onStateChange: function() { onStateChange: function() {
var readyState = this.transport.readyState; var readyState = this.xhr.readyState;
if (readyState==4) if (readyState==4)
this.respondToReadyState(readyState); {
try {
this.respondToReadyState(readyState);
} finally {
this.cleanup();
}
}
}, },
dispatchError: function( httpCode, text ) error: function( httpCode, text )
{ {
!this.options.onFailure || this.options.onFailure( httpCode, text); !this.options.onFailure || this.options.onFailure( httpCode, text);
this.cleanup();
}, },
respondToReadyState: function(readyState) respondToReadyState: function(readyState)
{ {
var transport = this.transport; var xhr = this.xhr;
if (readyState==4 && transport.status == 200) if (readyState==4 && xhr.status == 200)
{ {
var resp; var resp;
try { try {
eval('resp = ' + transport.responseText); resp = window.JSON && window.JSON.parse ? window.JSON.parse( xhr.responseText ) : (new Function("return " + xhr.responseText))();
} }
catch (e) { catch (e) {
this.dispatchError( 200, e.message + '\n' + transport.responseText.substr(0,512) ); this.error( 200, e.message + '\n' + xhr.responseText.substr(0,512) );
} }
if (resp!=null) if (resp!=null)
{ {
if (resp.stat==null) if (resp.stat==null)
this.dispatchError( 200, "Invalid response" ); this.error( 200, "Invalid response" );
else if (resp.stat=='ok') else if (resp.stat=='ok')
{ !this.options.onSuccess || this.options.onSuccess( resp.result );
if (this.options.onSuccess) this.options.onSuccess( resp.result );
}
else else
this.dispatchError( 200, resp.err + " " + resp.message); this.error( 200, resp.err + " " + resp.message);
} }
} }
if (readyState==4 && transport.status != 200) if (readyState==4 && xhr.status != 200)
this.dispatchError( transport.status, transport.statusText ); this.error( xhr.status, xhr.statusText );
}, },
cleanup: function()
{
if (this.xhr) this.xhr.onreadystatechange = null;
this.xhr = null;
this.options.onFailure = this.options.onSuccess = null;
},
transport: null, xhr: null
urlRoot: null,
options: {}
} }
function pwgAddEventListener(elem, evt, fn) function pwgAddEventListener(elem, evt, fn)
{ {
if (window.attachEvent) if (window.addEventListener)
elem.attachEvent('on'+evt, fn);
else
elem.addEventListener(evt, fn, false); elem.addEventListener(evt, fn, false);
else
elem.attachEvent('on'+evt, fn);
} }

View file

@ -208,11 +208,15 @@ y.callService(
<input type="submit" name="rate" value="{$mark}" class="rateButton" title="{$mark}"> <input type="submit" name="rate" value="{$mark}" class="rateButton" title="{$mark}">
{/if} {/if}
{/foreach} {/foreach}
<script type="text/javascript" src="{$ROOT_URL}themes/default/js/rating.js"></script>
<script type="text/javascript"> <script type="text/javascript">
makeNiceRatingForm( {ldelim}rootUrl: '{$ROOT_URL|@escape:"javascript"}', image_id: {$current.id}, var _pwgRatingAutoQueue = _pwgRatingAutoQueue || [];
updateRateText: "{'Update your rating'|@translate|@escape:'javascript'}", updateRateElement: document.getElementById("updateRate"), _pwgRatingAutoQueue.push( {ldelim}rootUrl: '{$ROOT_URL|@escape:"javascript"}', image_id: {$current.id},
ratingSummaryText: "{'%.2f (rated %d times)'|@translate|@escape:'javascript'}", ratingSummaryElement: document.getElementById("ratingSummary") {rdelim} ); updateRateText: "{'Update your rating'|@translate|@escape:'javascript'}", updateRateElement: document.getElementById("updateRate"),
ratingSummaryText: "{'%.2f (rated %d times)'|@translate|@escape:'javascript'}", ratingSummaryElement: document.getElementById("ratingSummary") {rdelim} );
(function () {ldelim}
var s = document.createElement('script'); s.type = 'text/javascript'; s.async = true; s.src = '{$ROOT_URL}themes/default/js/rating.js';
var s0 = document.getElementsByTagName('script')[0]; s0.parentNode.insertBefore(s, s0);
})();
</script> </script>
</div> </div>
</form> </form>