jquery.ocdialog.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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) {
  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 'closeButton':
  128. if(value) {
  129. var $closeButton = $('<a class="oc-dialog-close"></a>');
  130. this.$dialog.prepend($closeButton);
  131. $closeButton.on('click', function() {
  132. self.close();
  133. });
  134. } else {
  135. this.$dialog.find('.oc-dialog-close').remove();
  136. }
  137. break;
  138. case 'width':
  139. this.$dialog.css('width', value);
  140. break;
  141. case 'height':
  142. this.$dialog.css('height', value);
  143. break;
  144. case 'close':
  145. this.closeCB = value;
  146. break;
  147. }
  148. //this._super(key, value);
  149. $.Widget.prototype._setOption.apply(this, arguments );
  150. },
  151. _setOptions: function(options) {
  152. //this._super(options);
  153. $.Widget.prototype._setOptions.apply(this, arguments);
  154. },
  155. _setSizes: function() {
  156. var lessHeight = 0;
  157. if(this.$title) {
  158. lessHeight += this.$title.outerHeight(true);
  159. }
  160. if(this.$buttonrow) {
  161. lessHeight += this.$buttonrow.outerHeight(true);
  162. }
  163. this.element.css({
  164. 'height': 'calc(100% - ' + lessHeight + 'px)'
  165. });
  166. },
  167. _createOverlay: function() {
  168. if(!this.options.modal) {
  169. return;
  170. }
  171. var self = this;
  172. this.overlay = $('<div>')
  173. .addClass('oc-dialog-dim')
  174. .appendTo($('#content'));
  175. this.overlay.on('click keydown keyup', function(event) {
  176. if(event.target !== self.$dialog.get(0) && self.$dialog.find($(event.target)).length === 0) {
  177. event.preventDefault();
  178. event.stopPropagation();
  179. return;
  180. }
  181. });
  182. },
  183. _destroyOverlay: function() {
  184. if (!this.options.modal) {
  185. return;
  186. }
  187. if (this.overlay) {
  188. this.overlay.off('click keydown keyup');
  189. this.overlay.remove();
  190. this.overlay = null;
  191. }
  192. },
  193. widget: function() {
  194. return this.$dialog;
  195. },
  196. close: function() {
  197. this._destroyOverlay();
  198. var self = this;
  199. // Ugly hack to catch remaining keyup events.
  200. setTimeout(function() {
  201. self._trigger('close', self);
  202. self.$dialog.hide();
  203. }, 200);
  204. },
  205. destroy: function() {
  206. if(this.$title) {
  207. this.$title.remove();
  208. }
  209. if(this.$buttonrow) {
  210. this.$buttonrow.remove();
  211. }
  212. if(this.originalTitle) {
  213. this.element.attr('title', this.originalTitle);
  214. }
  215. this.element.removeClass('oc-dialog-content')
  216. .css(this.originalCss).detach().insertBefore(this.$dialog);
  217. this.$dialog.remove();
  218. }
  219. });
  220. }(jQuery));