Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

379 wiersze
9.4 KiB

  1. /* ===========================================================
  2. * bootstrap-modal.js v2.2.5
  3. * ===========================================================
  4. * Copyright 2012 Jordan Schroter
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ========================================================== */
  18. !function ($) {
  19. "use strict"; // jshint ;_;
  20. /* MODAL CLASS DEFINITION
  21. * ====================== */
  22. var Modal = function (element, options) {
  23. this.init(element, options);
  24. };
  25. Modal.prototype = {
  26. constructor: Modal,
  27. init: function (element, options) {
  28. var that = this;
  29. this.options = options;
  30. this.$element = $(element)
  31. .delegate('[data-dismiss="modal"]', 'click.dismiss.modal', $.proxy(this.hide, this));
  32. this.options.remote && this.$element.find('.modal-body').load(this.options.remote, function () {
  33. var e = $.Event('loaded');
  34. that.$element.trigger(e);
  35. });
  36. var manager = typeof this.options.manager === 'function' ?
  37. this.options.manager.call(this) : this.options.manager;
  38. manager = manager.appendModal ?
  39. manager : $(manager).modalmanager().data('modalmanager');
  40. manager.appendModal(this);
  41. },
  42. toggle: function () {
  43. return this[!this.isShown ? 'show' : 'hide']();
  44. },
  45. show: function () {
  46. var e = $.Event('show');
  47. if (this.isShown) return;
  48. this.$element.trigger(e);
  49. if (e.isDefaultPrevented()) return;
  50. this.escape();
  51. this.tab();
  52. this.options.loading && this.loading();
  53. },
  54. hide: function (e) {
  55. e && e.preventDefault();
  56. e = $.Event('hide');
  57. this.$element.trigger(e);
  58. if (!this.isShown || e.isDefaultPrevented()) return;
  59. this.isShown = false;
  60. this.escape();
  61. this.tab();
  62. this.isLoading && this.loading();
  63. $(document).off('focusin.modal');
  64. this.$element
  65. .removeClass('in')
  66. .removeClass('animated')
  67. .removeClass(this.options.attentionAnimation)
  68. .removeClass('modal-overflow')
  69. .attr('aria-hidden', true);
  70. $.support.transition && this.$element.hasClass('fade') ?
  71. this.hideWithTransition() :
  72. this.hideModal();
  73. },
  74. layout: function () {
  75. var prop = this.options.height ? 'height' : 'max-height',
  76. value = this.options.height || this.options.maxHeight;
  77. if (this.options.width){
  78. this.$element.css('width', this.options.width);
  79. var that = this;
  80. this.$element.css('margin-left', function () {
  81. if (/%/ig.test(that.options.width)){
  82. return -(parseInt(that.options.width) / 2) + '%';
  83. } else {
  84. return -($(this).width() / 2) + 'px';
  85. }
  86. });
  87. } else {
  88. this.$element.css('width', '');
  89. this.$element.css('margin-left', '');
  90. }
  91. this.$element.find('.modal-body')
  92. .css('overflow', '')
  93. .css(prop, '');
  94. if (value){
  95. this.$element.find('.modal-body')
  96. .css('overflow', 'auto')
  97. .css(prop, value);
  98. }
  99. var modalOverflow = $(window).height() - 10 < this.$element.height();
  100. if (modalOverflow || this.options.modalOverflow) {
  101. this.$element
  102. .css('margin-top', 0)
  103. .addClass('modal-overflow');
  104. } else {
  105. this.$element
  106. .css('margin-top', 0 - this.$element.height() / 2)
  107. .removeClass('modal-overflow');
  108. }
  109. },
  110. tab: function () {
  111. var that = this;
  112. if (this.isShown && this.options.consumeTab) {
  113. this.$element.on('keydown.tabindex.modal', '[data-tabindex]', function (e) {
  114. if (e.keyCode && e.keyCode == 9){
  115. var elements = [],
  116. tabindex = Number($(this).data('tabindex'));
  117. that.$element.find('[data-tabindex]:enabled:visible:not([readonly])').each(function (ev) {
  118. elements.push(Number($(this).data('tabindex')));
  119. });
  120. elements.sort(function(a,b){return a-b});
  121. var arrayPos = $.inArray(tabindex, elements);
  122. if (!e.shiftKey){
  123. arrayPos < elements.length-1 ?
  124. that.$element.find('[data-tabindex='+elements[arrayPos+1]+']').focus() :
  125. that.$element.find('[data-tabindex='+elements[0]+']').focus();
  126. } else {
  127. arrayPos == 0 ?
  128. that.$element.find('[data-tabindex='+elements[elements.length-1]+']').focus() :
  129. that.$element.find('[data-tabindex='+elements[arrayPos-1]+']').focus();
  130. }
  131. e.preventDefault();
  132. }
  133. });
  134. } else if (!this.isShown) {
  135. this.$element.off('keydown.tabindex.modal');
  136. }
  137. },
  138. escape: function () {
  139. var that = this;
  140. if (this.isShown && this.options.keyboard) {
  141. if (!this.$element.attr('tabindex')) this.$element.attr('tabindex', -1);
  142. this.$element.on('keyup.dismiss.modal', function (e) {
  143. e.which == 27 && that.hide();
  144. });
  145. } else if (!this.isShown) {
  146. this.$element.off('keyup.dismiss.modal')
  147. }
  148. },
  149. hideWithTransition: function () {
  150. var that = this
  151. , timeout = setTimeout(function () {
  152. that.$element.off($.support.transition.end);
  153. that.hideModal();
  154. }, 500);
  155. this.$element.one($.support.transition.end, function () {
  156. clearTimeout(timeout);
  157. that.hideModal();
  158. });
  159. },
  160. hideModal: function () {
  161. var prop = this.options.height ? 'height' : 'max-height';
  162. var value = this.options.height || this.options.maxHeight;
  163. if (value){
  164. this.$element.find('.modal-body')
  165. .css('overflow', '')
  166. .css(prop, '');
  167. }
  168. this.$element
  169. .hide()
  170. .trigger('hidden');
  171. },
  172. removeLoading: function () {
  173. this.$loading.remove();
  174. this.$loading = null;
  175. this.isLoading = false;
  176. },
  177. loading: function (callback) {
  178. callback = callback || function () {};
  179. var animate = this.$element.hasClass('fade') ? 'fade' : '';
  180. if (!this.isLoading) {
  181. var doAnimate = $.support.transition && animate;
  182. this.$loading = $('<div class="loading-mask ' + animate + '">')
  183. .append(this.options.spinner)
  184. .appendTo(this.$element);
  185. if (doAnimate) this.$loading[0].offsetWidth; // force reflow
  186. this.$loading.addClass('in');
  187. this.isLoading = true;
  188. doAnimate ?
  189. this.$loading.one($.support.transition.end, callback) :
  190. callback();
  191. } else if (this.isLoading && this.$loading) {
  192. this.$loading.removeClass('in');
  193. var that = this;
  194. $.support.transition && this.$element.hasClass('fade')?
  195. this.$loading.one($.support.transition.end, function () { that.removeLoading() }) :
  196. that.removeLoading();
  197. } else if (callback) {
  198. callback(this.isLoading);
  199. }
  200. },
  201. focus: function () {
  202. var $focusElem = this.$element.find(this.options.focusOn);
  203. $focusElem = $focusElem.length ? $focusElem : this.$element;
  204. $focusElem.focus();
  205. },
  206. attention: function (){
  207. // NOTE: transitionEnd with keyframes causes odd behaviour
  208. if (this.options.attentionAnimation){
  209. this.$element
  210. .removeClass('animated')
  211. .removeClass(this.options.attentionAnimation);
  212. var that = this;
  213. setTimeout(function () {
  214. that.$element
  215. .addClass('animated')
  216. .addClass(that.options.attentionAnimation);
  217. }, 0);
  218. }
  219. this.focus();
  220. },
  221. destroy: function () {
  222. var e = $.Event('destroy');
  223. this.$element.trigger(e);
  224. if (e.isDefaultPrevented()) return;
  225. this.$element
  226. .off('.modal')
  227. .removeData('modal')
  228. .removeClass('in')
  229. .attr('aria-hidden', true);
  230. if (this.$parent !== this.$element.parent()) {
  231. this.$element.appendTo(this.$parent);
  232. } else if (!this.$parent.length) {
  233. // modal is not part of the DOM so remove it.
  234. this.$element.remove();
  235. this.$element = null;
  236. }
  237. this.$element.trigger('destroyed');
  238. }
  239. };
  240. /* MODAL PLUGIN DEFINITION
  241. * ======================= */
  242. $.fn.modal = function (option, args) {
  243. return this.each(function () {
  244. var $this = $(this),
  245. data = $this.data('modal'),
  246. options = $.extend({}, $.fn.modal.defaults, $this.data(), typeof option == 'object' && option);
  247. if (!data) $this.data('modal', (data = new Modal(this, options)));
  248. if (typeof option == 'string') data[option].apply(data, [].concat(args));
  249. else if (options.show) data.show()
  250. })
  251. };
  252. $.fn.modal.defaults = {
  253. keyboard: true,
  254. backdrop: true,
  255. loading: false,
  256. show: true,
  257. width: null,
  258. height: null,
  259. maxHeight: null,
  260. modalOverflow: false,
  261. consumeTab: true,
  262. focusOn: null,
  263. replace: false,
  264. resize: false,
  265. attentionAnimation: 'shake',
  266. manager: 'body',
  267. spinner: '<div class="loading-spinner" style="width: 200px; margin-left: -100px;"><div class="progress progress-striped active"><div class="bar" style="width: 100%;"></div></div></div>',
  268. backdropTemplate: '<div class="modal-backdrop" />'
  269. };
  270. $.fn.modal.Constructor = Modal;
  271. /* MODAL DATA-API
  272. * ============== */
  273. $(function () {
  274. $(document).off('click.modal').on('click.modal.data-api', '[data-toggle="modal"]', function ( e ) {
  275. var $this = $(this),
  276. href = $this.attr('href'),
  277. $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))), //strip for ie7
  278. option = $target.data('modal') ? 'toggle' : $.extend({ remote: !/#/.test(href) && href }, $target.data(), $this.data());
  279. e.preventDefault();
  280. $target
  281. .modal(option)
  282. .one('hide', function () {
  283. $this.focus();
  284. })
  285. });
  286. });
  287. }(window.jQuery);