jquery.ocdialog.js 5.9 KB

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