-
Notifications
You must be signed in to change notification settings - Fork 154
/
eldarion-ajax.min.js
32 lines (32 loc) · 5.69 KB
/
eldarion-ajax.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* ====================================================================
* eldarion-ajax.min.js v0.7.0
* ====================================================================
* Copyright (c) 2013, Eldarion, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Eldarion, Inc. nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ==================================================================== */
!function(e){"use strict";var t=function(){};t.prototype.click=function(t){var a=e(this),o=a.attr("href"),r=a.attr("data-method");r||(r="get"),a.trigger("eldarion-ajax:begin",[a]),t.preventDefault(),e.ajax({url:o,type:r,dataType:"json",statusCode:{200:function(e){a.trigger("eldarion-ajax:success",[a,e])},500:function(){a.trigger("eldarion-ajax:error",[a,500])},404:function(){a.trigger("eldarion-ajax:error",[a,404])}}})},t.prototype.submit=function(t){var a=e(this),o=a.attr("action"),r=a.attr("method"),n=a.serialize();a.trigger("eldarion-ajax:begin",[a]),t.preventDefault(),e.ajax({url:o,type:r,data:n,dataType:"json",statusCode:{200:function(e){a.trigger("eldarion-ajax:success",[a,e])},500:function(){a.trigger("eldarion-ajax:error",[a,500])},404:function(){a.trigger("eldarion-ajax:error",[a,404])}}})},t.prototype.cancel=function(t){var a=e(this),o=a.attr("data-cancel-closest");t.preventDefault(),a.closest(o).remove()},e(function(){e("body").on("click","a.ajax",t.prototype.click),e("body").on("submit","form.ajax",t.prototype.submit),e("body").on("click","a[data-cancel-closest]",t.prototype.cancel)})}(window.jQuery),!function(e){"use strict";var t=function(){};t.prototype.redirect=function(e,t,a){return a.location?(window.location.href=a.location,!1):void 0},t.prototype.replace=function(t,a,o){e(a.data("replace")).replaceWith(o.html)},t.prototype.replaceClosest=function(e,t,a){t.closest(t.data("replace-closest")).replaceWith(a.html)},t.prototype.replaceInner=function(t,a,o){e(a.data("replace-inner")).html(o.html)},t.prototype.replaceClosestInner=function(e,t,a){t.closest(t.data("replace-closest-inner")).html(a.html)},t.prototype.append=function(t,a,o){e(a.data("append")).append(o.html)},t.prototype.prepend=function(t,a,o){e(a.data("prepend")).prepend(o.html)},t.prototype.refresh=function(t,a){e.each(e(a.data("refresh")),function(t,a){e.getJSON(e(a).data("refresh-url"),function(t){e(a).replaceWith(t.html)})})},t.prototype.refreshClosest=function(t,a){e.each(e(a.data("refresh-closest")),function(t,o){e.getJSON(e(o).data("refresh-url"),function(t){a.closest(e(o)).replaceWith(t.html)})})},t.prototype.clear=function(t,a){e(a.data("clear")).html("")},t.prototype.remove=function(t,a){e(a.data("remove")).remove()},t.prototype.clearClosest=function(e,t){t.closest(t.data("clear-closest")).html("")},t.prototype.removeClosest=function(e,t){t.closest(t.data("remove-closest")).remove()},t.prototype.fragments=function(t,a,o){if(o.fragments)for(var r in o.fragments)e(r).replaceWith(o.fragments[r]);if(o["inner-fragments"])for(var n in o["inner-fragments"])e(n).html(o["inner-fragments"][n]);if(o["append-fragments"])for(var c in o["append-fragments"])e(c).append(o["append-fragments"][c]);if(o["prepend-fragments"])for(var s in o["prepend-fragments"])e(s).prepend(o["prepend-fragments"][s])},e(function(){e(document).on("eldarion-ajax:success",t.prototype.redirect),e(document).on("eldarion-ajax:success",t.prototype.fragments),e(document).on("eldarion-ajax:success","[data-replace]",t.prototype.replace),e(document).on("eldarion-ajax:success","[data-replace-closest]",t.prototype.replaceClosest),e(document).on("eldarion-ajax:success","[data-replace-inner]",t.prototype.replaceInner),e(document).on("eldarion-ajax:success","[data-replace-closest-inner]",t.prototype.replaceClosestInner),e(document).on("eldarion-ajax:success","[data-append]",t.prototype.append),e(document).on("eldarion-ajax:success","[data-prepend]",t.prototype.prepend),e(document).on("eldarion-ajax:success","[data-refresh]",t.prototype.refresh),e(document).on("eldarion-ajax:success","[data-refresh-closest]",t.prototype.refreshClosest),e(document).on("eldarion-ajax:success","[data-clear]",t.prototype.clear),e(document).on("eldarion-ajax:success","[data-remove]",t.prototype.remove),e(document).on("eldarion-ajax:success","[data-clear-closest]",t.prototype.clearClosest),e(document).on("eldarion-ajax:success","[data-remove-closest]",t.prototype.removeClosest)})}(window.jQuery);