jquery.ocdialog.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. (function($) {
  2. $.widget('oc.ocdialog', {
  3. options: {
  4. width: 'auto',
  5. height: 'auto',
  6. closeButton: true,
  7. closeOnEscape: true,
  8. modal: false
  9. },
  10. _create: function() {
  11. var self = this;
  12. this.originalCss = {
  13. display: this.element[0].style.display,
  14. width: this.element[0].style.width,
  15. height: this.element[0].style.height
  16. };
  17. this.originalTitle = this.element.attr('title');
  18. this.options.title = this.options.title || this.originalTitle;
  19. this.$dialog = $('<div class="oc-dialog" />')
  20. .attr({
  21. // Setting tabIndex makes the div focusable
  22. tabIndex: -1,
  23. role: 'dialog'
  24. })
  25. .insertBefore(this.element);
  26. this.$dialog.append(this.element.detach());
  27. this.element.removeAttr('title').addClass('oc-dialog-content').appendTo(this.$dialog);
  28. this.$dialog.css({
  29. display: 'inline-block',
  30. position: 'fixed'
  31. });
  32. $(document).on('keydown keyup', function(event) {
  33. if (
  34. event.target !== self.$dialog.get(0) &&
  35. self.$dialog.find($(event.target)).length === 0
  36. ) {
  37. return;
  38. }
  39. // Escape
  40. if (
  41. event.keyCode === 27 &&
  42. event.type === 'keydown' &&
  43. self.options.closeOnEscape
  44. ) {
  45. event.stopImmediatePropagation();
  46. self.close();
  47. return false;
  48. }
  49. // Enter
  50. if(event.keyCode === 13) {
  51. event.stopImmediatePropagation();
  52. if(event.type === 'keyup') {
  53. event.preventDefault();
  54. return false;
  55. }
  56. // If no button is selected we trigger the primary
  57. if (
  58. self.$buttonrow &&
  59. self.$buttonrow.find($(event.target)).length === 0
  60. ) {
  61. var $button = self.$buttonrow.find('button.primary');
  62. if($button && !$button.prop('disabled')) {
  63. $button.trigger('click');
  64. }
  65. } else if(self.$buttonrow) {
  66. $(event.target).trigger('click');
  67. }
  68. return false;
  69. }
  70. });
  71. this._setOptions(this.options);
  72. this._createOverlay();
  73. },
  74. _init: function() {
  75. this.$dialog.focus();
  76. this._trigger('open');
  77. },
  78. _setOption: function(key, value) {
  79. var self = this;
  80. switch(key) {
  81. case 'title':
  82. if(this.$title) {
  83. this.$title.text(value);
  84. } else {
  85. var $title = $('<h2 class="oc-dialog-title">'
  86. + value
  87. + '</h2>');
  88. this.$title = $title.prependTo(this.$dialog);
  89. }
  90. this._setSizes();
  91. break;
  92. case 'buttons':
  93. if(this.$buttonrow) {
  94. this.$buttonrow.empty();
  95. } else {
  96. var $buttonrow = $('<div class="oc-dialog-buttonrow" />');
  97. this.$buttonrow = $buttonrow.appendTo(this.$dialog);
  98. }
  99. if (value.length === 1) {
  100. this.$buttonrow.addClass('onebutton');
  101. } else if (value.length === 2) {
  102. this.$buttonrow.addClass('twobuttons');
  103. } else if (value.length === 3) {
  104. this.$buttonrow.addClass('threebuttons');
  105. }
  106. $.each(value, function(idx, val) {
  107. var $button = $('<button>').text(val.text);
  108. if (val.classes) {
  109. $button.addClass(val.classes);
  110. }
  111. if(val.defaultButton) {
  112. $button.addClass('primary');
  113. self.$defaultButton = $button;
  114. }
  115. self.$buttonrow.append($button);
  116. $button.click(function() {
  117. val.click.apply(self.element[0], arguments);
  118. });
  119. });
  120. this.$buttonrow.find('button')
  121. .on('focus', function(event) {
  122. self.$buttonrow.find('button').removeClass('primary');
  123. $(this).addClass('primary');
  124. });
  125. this._setSizes();
  126. break;
  127. case 'style':
  128. if (value.buttons !== undefined) {
  129. this.$buttonrow.addClass(value.buttons);
  130. }
  131. break;
  132. case 'closeButton':
  133. if(value) {
  134. var $closeButton = $('<a class="oc-dialog-close"></a>');
  135. this.$dialog.prepend($closeButton);
  136. $closeButton.on('click', function() {
  137. self.close();
  138. });
  139. } else {
  140. this.$dialog.find('.oc-dialog-close').remove();
  141. }
  142. break;
  143. case 'width':
  144. this.$dialog.css('width', value);
  145. break;
  146. case 'height':
  147. this.$dialog.css('height', value);
  148. break;
  149. case 'close':
  150. this.closeCB = value;
  151. break;
  152. }
  153. //this._super(key, value);
  154. $.Widget.prototype._setOption.apply(this, arguments );
  155. },
  156. _setOptions: function(options) {
  157. //this._super(options);
  158. $.Widget.prototype._setOptions.apply(this, arguments);
  159. },
  160. _setSizes: function() {
  161. var lessHeight = 0;
  162. if(this.$title) {
  163. lessHeight += this.$title.outerHeight(true);
  164. }
  165. if(this.$buttonrow) {
  166. lessHeight += this.$buttonrow.outerHeight(true);
  167. }
  168. this.element.css({
  169. 'height': 'calc(100% - ' + lessHeight + 'px)'
  170. });
  171. },
  172. _createOverlay: function() {
  173. if(!this.options.modal) {
  174. return;
  175. }
  176. var self = this;
  177. this.overlay = $('<div>')
  178. .addClass('oc-dialog-dim')
  179. .appendTo($('#content'));
  180. this.overlay.on('click keydown keyup', function(event) {
  181. if(event.target !== self.$dialog.get(0) && self.$dialog.find($(event.target)).length === 0) {
  182. event.preventDefault();
  183. event.stopPropagation();
  184. return;
  185. }
  186. });
  187. },
  188. _destroyOverlay: function() {
  189. if (!this.options.modal) {
  190. return;
  191. }
  192. if (this.overlay) {
  193. this.overlay.off('click keydown keyup');
  194. this.overlay.remove();
  195. this.overlay = null;
  196. }
  197. },
  198. widget: function() {
  199. return this.$dialog;
  200. },
  201. close: function() {
  202. this._destroyOverlay();
  203. var self = this;
  204. // Ugly hack to catch remaining keyup events.
  205. setTimeout(function() {
  206. self._trigger('close', self);
  207. }, 200);
  208. self.$dialog.remove();
  209. this.destroy();
  210. },
  211. destroy: function() {
  212. if(this.$title) {
  213. this.$title.remove();
  214. }
  215. if(this.$buttonrow) {
  216. this.$buttonrow.remove();
  217. }
  218. if(this.originalTitle) {
  219. this.element.attr('title', this.originalTitle);
  220. }
  221. this.element.removeClass('oc-dialog-content')
  222. .css(this.originalCss).detach().insertBefore(this.$dialog);
  223. this.$dialog.remove();
  224. }
  225. });
  226. }(jQuery));