|
| 1 | +/*! |
| 2 | + * bootstrap-actionable v1.0.0 (http://javatmp.com) |
| 3 | + * A small Javascript code that help implement click event actions for <a> and <button> tags by declarative way |
| 4 | + * and provide functionalities to load AJAX content in Bootstrap Modal Wrapper instance. |
| 5 | + * |
| 6 | + * Copyright 2018 JavaTMP |
| 7 | + * Licensed under MIT (https://github.com/JavaTMP/bootstrap-actionable/blob/master/LICENSE) |
| 8 | + */ |
| 9 | + |
| 10 | +(function (root, factory) { |
| 11 | + "use strict"; |
| 12 | + // CommonJS module is defined |
| 13 | + if (typeof module !== "undefined" && module.exports) { |
| 14 | + module.exports = factory(require("jquery"), require("bootstrap"), require("bootstrap-modal-wrapper")); |
| 15 | + } |
| 16 | + // AMD module is defined |
| 17 | + else if (typeof define === "function" && define.amd) { |
| 18 | + define("bootstrap-actionable", ["jquery", "bootstrap"], function ($) { |
| 19 | + return factory($); |
| 20 | + }); |
| 21 | + } else { |
| 22 | + // planted over the root! |
| 23 | + root.BootstrapActionable = factory(root.jQuery, root.BootstrapModalWrapperFactory); |
| 24 | + } |
| 25 | + |
| 26 | +}(this, function ($, BootstrapModalWrapperFactory) { |
| 27 | + "use strict"; |
| 28 | + |
| 29 | + var defaults = { |
| 30 | + loadingHtml: "Loading ...", |
| 31 | + ajaxHttpMethod: "GET", |
| 32 | + ajaxContainerReady: "ajax-container-ready" |
| 33 | + }; |
| 34 | + |
| 35 | + // The actual plugin constructor |
| 36 | + function BootstrapActionable() { |
| 37 | + this.options = $.extend({}, defaults); |
| 38 | + this.init(); |
| 39 | + } |
| 40 | + |
| 41 | + BootstrapActionable.prototype.init = function () { |
| 42 | + // Initialize Actionable plugin by listening on click event for any actionType attribute value |
| 43 | + var $this = this; |
| 44 | + $("body").on("click", '[actionType]', function (event) { |
| 45 | + event.preventDefault(); |
| 46 | + var actionType = $(this).attr("actionType") ? $(this).attr("actionType") : "ajax"; |
| 47 | + var actionScope = $(this).attr("actionScope") ? $(this).attr("actionScope") : "html"; |
| 48 | + if (actionType === "ajax-model") { |
| 49 | + var href = $(this).attr("href") ? $(this).attr("href") : $(this).attr("actionLink"); |
| 50 | + $this.fetchAjaxContentByModal(href); |
| 51 | + } else if (actionType === "action-ref") { |
| 52 | + var actionRefName = $(this).attr("action-ref-by-name") ? $(this).attr("action-ref-by-name") : ""; |
| 53 | + if ($(actionScope + " [action-name='" + actionRefName + "']").length > 0) |
| 54 | + $(actionScope + " [action-name='" + actionRefName + "']:first").trigger("click"); |
| 55 | + } else if (actionType === "action-ref-href") { |
| 56 | + var actionRefName = $(this).attr("action-ref-by-href") ? $(this).attr("action-ref-by-href") : ""; |
| 57 | + if ($(actionScope + " [href='" + actionRefName + "']").length > 0) { |
| 58 | + var linkTag = $(actionScope + " [href='" + actionRefName + "']:first"); |
| 59 | + // check if current link tag has attribute target |
| 60 | + if (linkTag.attr('target')) { |
| 61 | + window.open(linkTag.attr("href"), linkTag.attr('target')); |
| 62 | + } else { |
| 63 | + $(linkTag).trigger("click"); |
| 64 | + } |
| 65 | + } |
| 66 | + } else { |
| 67 | + console.log("ERROR: No Action handler attached for actionType=[" + actionType + "]"); |
| 68 | + } |
| 69 | + }); |
| 70 | + }; |
| 71 | + |
| 72 | + BootstrapActionable.prototype.fetchAjaxContentByModal = function (remoteUrl, passData) { |
| 73 | + var $this = this; |
| 74 | + var ajaxModalContainer = BootstrapModalWrapperFactory.createModal({ |
| 75 | + message: $this.options.loadingHtml, |
| 76 | + closable: false, |
| 77 | + closeByBackdrop: false, |
| 78 | + closeByKeyboard: false |
| 79 | + }); |
| 80 | + ajaxModalContainer.originalModal.removeClass("fade"); |
| 81 | + ajaxModalContainer.originalModal.find(".modal-dialog").css({transition: 'all .3s'}); |
| 82 | + |
| 83 | + ajaxModalContainer.show(); |
| 84 | + |
| 85 | + if (passData === undefined) { |
| 86 | + passData = {}; |
| 87 | + } |
| 88 | + |
| 89 | + passData["ajaxModalId"] = ajaxModalContainer.options.id; |
| 90 | + |
| 91 | + $.ajax({ |
| 92 | + type: $this.options.ajaxHttpMethod, |
| 93 | + dataType: "html", |
| 94 | + url: remoteUrl, |
| 95 | + data: {}, |
| 96 | + success: function (response, textStatus, jqXHR) { |
| 97 | + // make sure the modal dialog is open before update |
| 98 | + // its body with ajax response and triggering javaTmpAjaxContainerReady event. |
| 99 | + var timeOut = 700; |
| 100 | + function runWhenDialogOpen() { |
| 101 | +// console.log("time out [" + Math.round(timeOut / 2) + "], isOpen [" + ajaxModalContainer.isOpen + "], is show [" + ajaxModalContainer.originalModal.hasClass("show") + "]"); |
| 102 | + if (ajaxModalContainer.isOpen) { |
| 103 | + ajaxModalContainer.updateSize("modal-lg"); |
| 104 | + setTimeout(function () { |
| 105 | + ajaxModalContainer.updateMessage(response); |
| 106 | + setTimeout(function () { |
| 107 | + $("#" + ajaxModalContainer.options.id).trigger($this.options.ajaxContainerReady, [ajaxModalContainer]); |
| 108 | + }, 0); |
| 109 | + }, 350); |
| 110 | + } else { |
| 111 | + timeOut = timeOut <= 50 ? 50 : Math.round(timeOut / 2); |
| 112 | + setTimeout(runWhenDialogOpen, timeOut); |
| 113 | + |
| 114 | + } |
| 115 | + } |
| 116 | + runWhenDialogOpen(); |
| 117 | + } |
| 118 | + }); |
| 119 | + }; |
| 120 | + |
| 121 | + return new BootstrapActionable(); |
| 122 | +})); |
0 commit comments