jquery.ocdialog.js 6.1 KB

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