jquery.ocdialog.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. $(window).resize(function() {
  72. self.parent = self.$dialog.parent().length > 0 ? self.$dialog.parent() : $('body');
  73. var pos = self.parent.position();
  74. self.$dialog.css({
  75. left: pos.left + ($(window).innerWidth() - self.$dialog.outerWidth())/2,
  76. top: pos.top + ($(window).innerHeight() - self.$dialog.outerHeight())/2,
  77. width: Math.min(self.options.width, $(window).innerWidth() - 20 ),
  78. height: Math.min(self.options.height, $(window).innerHeight() - 20)
  79. });
  80. // set sizes of content
  81. self._setSizes();
  82. });
  83. this._setOptions(this.options);
  84. $(window).trigger('resize');
  85. this._createOverlay();
  86. },
  87. _init: function() {
  88. this.$dialog.focus();
  89. this._trigger('open');
  90. },
  91. _setOption: function(key, value) {
  92. var self = this;
  93. switch(key) {
  94. case 'title':
  95. if(this.$title) {
  96. this.$title.text(value);
  97. } else {
  98. var $title = $('<h2 class="oc-dialog-title">'
  99. + value
  100. + '</h2>');
  101. this.$title = $title.prependTo(this.$dialog);
  102. }
  103. this._setSizes();
  104. break;
  105. case 'buttons':
  106. if(this.$buttonrow) {
  107. this.$buttonrow.empty();
  108. } else {
  109. var $buttonrow = $('<div class="oc-dialog-buttonrow" />');
  110. this.$buttonrow = $buttonrow.appendTo(this.$dialog);
  111. }
  112. if (value.length === 1) {
  113. this.$buttonrow.addClass('onebutton');
  114. } else if (value.length === 2) {
  115. this.$buttonrow.addClass('twobuttons');
  116. } else if (value.length === 3) {
  117. this.$buttonrow.addClass('threebuttons');
  118. }
  119. $.each(value, function(idx, val) {
  120. var $button = $('<button>').text(val.text);
  121. if (val.classes) {
  122. $button.addClass(val.classes);
  123. }
  124. if(val.defaultButton) {
  125. $button.addClass('primary');
  126. self.$defaultButton = $button;
  127. }
  128. self.$buttonrow.append($button);
  129. $button.click(function() {
  130. val.click.apply(self.element[0], arguments);
  131. });
  132. });
  133. this.$buttonrow.find('button')
  134. .on('focus', function(event) {
  135. self.$buttonrow.find('button').removeClass('primary');
  136. $(this).addClass('primary');
  137. });
  138. this._setSizes();
  139. break;
  140. case 'closeButton':
  141. if(value) {
  142. var $closeButton = $('<a class="oc-dialog-close"></a>');
  143. this.$dialog.prepend($closeButton);
  144. $closeButton.on('click', function() {
  145. self.close();
  146. });
  147. } else {
  148. this.$dialog.find('.oc-dialog-close').remove();
  149. }
  150. break;
  151. case 'width':
  152. this.$dialog.css('width', value);
  153. break;
  154. case 'height':
  155. this.$dialog.css('height', value);
  156. break;
  157. case 'close':
  158. this.closeCB = value;
  159. break;
  160. }
  161. //this._super(key, value);
  162. $.Widget.prototype._setOption.apply(this, arguments );
  163. },
  164. _setOptions: function(options) {
  165. //this._super(options);
  166. $.Widget.prototype._setOptions.apply(this, arguments);
  167. },
  168. _setSizes: function() {
  169. var content_height = this.$dialog.height();
  170. if(this.$title) {
  171. content_height -= this.$title.outerHeight(true);
  172. }
  173. if(this.$buttonrow) {
  174. content_height -= this.$buttonrow.outerHeight(true);
  175. }
  176. this.parent = this.$dialog.parent().length > 0 ? this.$dialog.parent() : $('body');
  177. content_height = Math.min(content_height, this.parent.height()-20);
  178. if (content_height> 0) {
  179. this.element.css({
  180. height: content_height + 'px',
  181. width: this.$dialog.innerWidth() - 30 + 'px'
  182. });
  183. } else {
  184. this.element.css({
  185. width : this.$dialog.innerWidth() - 30 + 'px'
  186. });
  187. }
  188. },
  189. _createOverlay: function() {
  190. if(!this.options.modal) {
  191. return;
  192. }
  193. var self = this;
  194. this.overlay = $('<div>')
  195. .addClass('oc-dialog-dim')
  196. .appendTo($('#content'));
  197. this.overlay.on('click keydown keyup', function(event) {
  198. if(event.target !== self.$dialog.get(0) && self.$dialog.find($(event.target)).length === 0) {
  199. event.preventDefault();
  200. event.stopPropagation();
  201. return;
  202. }
  203. });
  204. },
  205. _destroyOverlay: function() {
  206. if (!this.options.modal) {
  207. return;
  208. }
  209. if (this.overlay) {
  210. this.overlay.off('click keydown keyup');
  211. this.overlay.remove();
  212. this.overlay = null;
  213. }
  214. },
  215. widget: function() {
  216. return this.$dialog;
  217. },
  218. close: function() {
  219. this._destroyOverlay();
  220. var self = this;
  221. // Ugly hack to catch remaining keyup events.
  222. setTimeout(function() {
  223. self._trigger('close', self);
  224. self.$dialog.hide();
  225. }, 200);
  226. },
  227. destroy: function() {
  228. if(this.$title) {
  229. this.$title.remove();
  230. }
  231. if(this.$buttonrow) {
  232. this.$buttonrow.remove();
  233. }
  234. if(this.originalTitle) {
  235. this.element.attr('title', this.originalTitle);
  236. }
  237. this.element.removeClass('oc-dialog-content')
  238. .css(this.originalCss).detach().insertBefore(this.$dialog);
  239. this.$dialog.remove();
  240. }
  241. });
  242. }(jQuery));