Advertisement
Guest User

Untitled

a guest
Mar 10th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. /*!
  2. * PopConfirm 0.4.3
  3. * http://ifnot.github.io/PopConfirm/
  4. *
  5. * Use jQuery & Bootstrap
  6. * http://jquery.com/
  7. * http://getbootstrap.com/
  8. *
  9. * Copyright 2014 Anael Favre and other contributors
  10. * Released under the MIT license
  11. * https://raw.github.com/AnaelFavre/PopConfirm/master/LICENCE
  12. *
  13. * Thanks to contributors :
  14. * Thomas Hanson https://github.com/diresquirrel
  15. * Mohamed Aymen https://github.com/kernel64
  16. * Muhammad Ubaid Raza https://github.com/mubaidr
  17. */
  18.  
  19. $(document).ready(function() {
  20.  
  21. // Delete confirmation
  22. $(".btn-delete").popConfirm({
  23. title: "Delete",
  24. content: "Delete item?",
  25. placement: "top"
  26. });
  27.  
  28. // Relist confirmation
  29. $(".btn-relist").popConfirm({
  30. title: "Relist",
  31. content: "Relist item?",
  32. placement: "bottom"
  33. });
  34. });
  35.  
  36. (function ($) {
  37. 'use strict';
  38. /*global jQuery, $*/
  39. /*jslint nomen: true, evil: true*/
  40. $.fn.extend({
  41. popConfirm: function (options) {
  42. var defaults = {
  43. title: 'Confirmation',
  44. content: 'Are you really sure ?',
  45. placement: 'left',
  46. container: 'body',
  47. yesBtn: 'Yes',
  48. noBtn: 'No',
  49. },
  50. last = null;
  51. options = $.extend(defaults, options);
  52. return this.each(function () {
  53. var self = $(this),
  54. arrayActions = [],
  55. arrayDelegatedActions = [],
  56. eventToConfirm,
  57. optName,
  58. optValue,
  59. i,
  60. elmType,
  61. code,
  62. form;
  63.  
  64. // Load data-* attriutes
  65. for (optName in options) {
  66. if (options.hasOwnProperty(optName)) {
  67. optValue = $(this).attr('data-confirm-' + optName);
  68. if (optValue) {
  69. options[optName] = optValue;
  70. }
  71. }
  72. }
  73.  
  74. // If there are jquery click events
  75. if (jQuery._data(this, "events") && jQuery._data(this, "events").click) {
  76.  
  77. // Save all click handlers
  78. for (i = 0; i < jQuery._data(this, "events").click.length; i = i + 1) {
  79. arrayActions.push(jQuery._data(this, "events").click[i].handler);
  80. }
  81.  
  82. // unbind it to prevent it firing
  83. $(self).unbind("click");
  84. }
  85.  
  86. // If there are jquery delegated click events
  87. if (self.data('remote') && jQuery._data(document, "events") && jQuery._data(document, "events").click) {
  88.  
  89. // Save all delegated click handlers that apply
  90. for (i = 0; i < jQuery._data(document, "events").click.length; i = i + 1) {
  91. elmType = self[0].tagName.toLowerCase();
  92. if (jQuery._data(document, "events").click[i].selector && jQuery._data(document, "events").click[i].selector.indexOf(elmType + "[data-remote]") !== -1) {
  93. arrayDelegatedActions.push(jQuery._data(document, "events").click[i].handler);
  94. }
  95. }
  96. }
  97.  
  98. // If there are hard onclick attribute
  99. if (self.attr('onclick')) {
  100. // Extracting the onclick code to evaluate and bring it into a closure
  101. code = self.attr('onclick');
  102. arrayActions.push(function () {
  103. eval(code);
  104. });
  105. $(self).prop("onclick", null);
  106. }
  107.  
  108. // If there are href link defined
  109. if (!self.data('remote') && self.attr('href')) {
  110. // Assume there is a href attribute to redirect to
  111. arrayActions.push(function () {
  112. window.location.href = self.attr('href');
  113. });
  114. }
  115.  
  116. // If the button is a submit one
  117. if (self.attr('type') && self.attr('type') === 'submit') {
  118. // Get the form related to this button then store submiting in closure
  119. form = $(this).parents('form:first');
  120. arrayActions.push(function () {
  121. form.submit();
  122. });
  123. }
  124.  
  125. self.popover({
  126. trigger: 'manual',
  127. title: options.title,
  128. html: true,
  129. placement: options.placement,
  130. container: options.container,
  131. //Avoid using multiline strings, nno support in older browsers.
  132. content: options.content + '<p class="button-group" style="margin-top: 10px; text-align: center;"><button type="button" class="btn btn-small confirm-dialog-btn-abord">' + options.noBtn + '</button> <button type="button" class="btn btn-small btn-danger confirm-dialog-btn-confirm">' + options.yesBtn + '</button></p>'
  133. }).click(function (e) {
  134. if (last && last !== self) {
  135. last.popover('hide').removeClass('popconfirm-active');
  136. }
  137. last = self;
  138. });
  139.  
  140. $(document).on('click', function () {
  141. if (last) {
  142. last.popover('hide').removeClass('popconfirm-active');
  143. }
  144. });
  145.  
  146. self.bind('click', function (e) {
  147. eventToConfirm = e;
  148.  
  149. e.preventDefault();
  150. e.stopPropagation();
  151.  
  152. $('.popconfirm-active').not(self).popover('hide').removeClass('popconfirm-active');
  153. self.popover('show').addClass('popconfirm-active');
  154.  
  155. $(document).find('.popover .confirm-dialog-btn-confirm').bind('click', function (e) {
  156. for (i = 0; i < arrayActions.length; i = i + 1) {
  157. arrayActions[i].apply(self);
  158. }
  159.  
  160. for (i = 0; i < arrayDelegatedActions.length; i = i + 1) {
  161. arrayDelegatedActions[i].apply(self, [eventToConfirm.originalEvent]);
  162. }
  163.  
  164. self.popover('hide').removeClass('popconfirm-active');
  165. });
  166. $(document).find('.popover .confirm-dialog-btn-abord').bind('click', function (e) {
  167. self.popover('hide').removeClass('popconfirm-active');
  168. });
  169. });
  170. });
  171. }
  172. });
  173.  
  174. }(jQuery));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement