fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2011-10-24 10:04 am

[dw-free] Need easier way to refer to the ajax-loader image, and turn it on and off

[commit: http://hg.dwscoalition.org/dw-free/rev/c5449359fa0e]

http://bugs.dwscoalition.org/show_bug.cgi?id=3983

Add $.throbber jQuery function. See bug for usage.

Patch by [personal profile] fu.

Files modified:
  • htdocs/js/dw/dw-core.js
--------------------------------------------------------------------------------
diff -r 73087735e83d -r c5449359fa0e htdocs/js/dw/dw-core.js
--- a/htdocs/js/dw/dw-core.js	Mon Oct 24 17:58:12 2011 +0800
+++ b/htdocs/js/dw/dw-core.js	Mon Oct 24 18:04:10 2011 +0800
@@ -48,3 +48,31 @@
     $.extractParams.cache[url] = params;
     return params;
 };
+
+$.throbber = {
+  src: Site.imgprefix + "/ajax-loader.gif",
+  image: function() { return $("<img>", { src:  $.throbber.src } ) }
+};
+
+// position is an optional first argument
+$.fn.throbber = function(position, jqxhr) {
+    var $this = $(this);
+
+    if ( $this.data("throbber") )
+        $this.data("throbber").remove();
+
+    if ( jqxhr === undefined ) jqxhr = position;
+    if ( position != "before" && position != "after" && position != "prepend" && position != "append" ) {
+        if ( $this.is(":input") )
+            position = "after"
+        else
+            position = "append"
+    }
+
+    var $img = $.throbber.image();
+    $this[position]($img);
+    $this.data("throbber", $img);
+    jqxhr.complete(function() { $img.remove(); $this.removeData("throbber") });
+
+    return $this;
+}
--------------------------------------------------------------------------------