ui.core.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * jQuery UI @VERSION
  3. *
  4. * Copyright (c) 2008 Paul Bakaus (ui.jquery.com)
  5. * Dual licensed under the MIT (MIT-LICENSE.txt)
  6. * and GPL (GPL-LICENSE.txt) licenses.
  7. *
  8. * http://docs.jquery.com/UI
  9. *
  10. * $Date: 2008-05-04 16:52:15 +0200 (So, 04 Mai 2008) $
  11. * $Rev: 5419 $
  12. */
  13. ;(function($) {
  14. $.ui = {
  15. plugin: {
  16. add: function(module, option, set) {
  17. var proto = $.ui[module].prototype;
  18. for(var i in set) {
  19. proto.plugins[i] = proto.plugins[i] || [];
  20. proto.plugins[i].push([option, set[i]]);
  21. }
  22. },
  23. call: function(instance, name, args) {
  24. var set = instance.plugins[name];
  25. if(!set) { return; }
  26. for (var i = 0; i < set.length; i++) {
  27. if (instance.options[set[i][0]]) {
  28. set[i][1].apply(instance.element, args);
  29. }
  30. }
  31. }
  32. },
  33. cssCache: {},
  34. css: function(name) {
  35. if ($.ui.cssCache[name]) { return $.ui.cssCache[name]; }
  36. var tmp = $('<div class="ui-resizable-gen">').addClass(name).css({position:'absolute', top:'-5000px', left:'-5000px', display:'block'}).appendTo('body');
  37. //if (!$.browser.safari)
  38. //tmp.appendTo('body');
  39. //Opera and Safari set width and height to 0px instead of auto
  40. //Safari returns rgba(0,0,0,0) when bgcolor is not set
  41. $.ui.cssCache[name] = !!(
  42. (!(/auto|default/).test(tmp.css('cursor')) || (/^[1-9]/).test(tmp.css('height')) || (/^[1-9]/).test(tmp.css('width')) ||
  43. !(/none/).test(tmp.css('backgroundImage')) || !(/transparent|rgba\(0, 0, 0, 0\)/).test(tmp.css('backgroundColor')))
  44. );
  45. try { $('body').get(0).removeChild(tmp.get(0)); } catch(e){}
  46. return $.ui.cssCache[name];
  47. },
  48. disableSelection: function(e) {
  49. e.unselectable = "on";
  50. e.onselectstart = function() { return false; };
  51. if (e.style) { e.style.MozUserSelect = "none"; }
  52. },
  53. enableSelection: function(e) {
  54. e.unselectable = "off";
  55. e.onselectstart = function() { return true; };
  56. if (e.style) { e.style.MozUserSelect = ""; }
  57. },
  58. hasScroll: function(e, a) {
  59. var scroll = /top/.test(a||"top") ? 'scrollTop' : 'scrollLeft', has = false;
  60. if (e[scroll] > 0) return true; e[scroll] = 1;
  61. has = e[scroll] > 0 ? true : false; e[scroll] = 0;
  62. return has;
  63. }
  64. };
  65. // $.widget is a factory to create jQuery plugins
  66. // taking some boilerplate code out of the plugin code
  67. // created by Scott González and Jörn Zaefferer
  68. function getter(namespace, plugin, method) {
  69. var methods = $[namespace][plugin].getter || [];
  70. methods = (typeof methods == "string" ? methods.split(/,?\s+/) : methods);
  71. return ($.inArray(method, methods) != -1);
  72. };
  73. var widgetPrototype = {
  74. init: function() {},
  75. destroy: function() {
  76. this.element.removeData(this.widgetName);
  77. },
  78. getData: function(key) {
  79. return this.options[key];
  80. },
  81. setData: function(key, value) {
  82. this.options[key] = value;
  83. },
  84. enable: function() {
  85. this.setData('disabled', false);
  86. },
  87. disable: function() {
  88. this.setData('disabled', true);
  89. }
  90. };
  91. $.widget = function(name, prototype) {
  92. var namespace = name.split(".")[0];
  93. name = name.split(".")[1];
  94. // create plugin method
  95. $.fn[name] = function(options, data) {
  96. var isMethodCall = (typeof options == 'string'),
  97. args = arguments;
  98. if (isMethodCall && getter(namespace, name, options)) {
  99. var instance = $.data(this[0], name);
  100. return (instance ? instance[options](data) : undefined);
  101. }
  102. return this.each(function() {
  103. var instance = $.data(this, name);
  104. if (!instance) {
  105. $.data(this, name, new $[namespace][name](this, options));
  106. } else if (isMethodCall) {
  107. instance[options].apply(instance, $.makeArray(args).slice(1));
  108. }
  109. });
  110. };
  111. // create widget constructor
  112. $[namespace][name] = function(element, options) {
  113. var self = this;
  114. this.widgetName = name;
  115. this.options = $.extend({}, $[namespace][name].defaults, options);
  116. this.element = $(element)
  117. .bind('setData.' + name, function(e, key, value) {
  118. return self.setData(key, value);
  119. })
  120. .bind('getData.' + name, function(e, key) {
  121. return self.getData(key);
  122. })
  123. .bind('remove', function() {
  124. return self.destroy();
  125. });
  126. this.init();
  127. };
  128. // add widget prototype
  129. $[namespace][name].prototype = $.extend({}, widgetPrototype, prototype);
  130. };
  131. /** Mouse Interaction Plugin **/
  132. $.widget("ui.mouse", {
  133. init: function() {
  134. var self = this;
  135. this.element
  136. .bind('mousedown.mouse', function() { return self.click.apply(self, arguments); })
  137. .bind('mouseup.mouse', function() { (self.timer && clearInterval(self.timer)); })
  138. .bind('click.mouse', function() { if(self.initialized) { self.initialized = false; return false; } });
  139. //Prevent text selection in IE
  140. if ($.browser.msie) {
  141. this.unselectable = this.element.attr('unselectable');
  142. this.element.attr('unselectable', 'on');
  143. }
  144. },
  145. destroy: function() {
  146. this.element.unbind('.mouse').removeData("mouse");
  147. ($.browser.msie && this.element.attr('unselectable', this.unselectable));
  148. },
  149. trigger: function() { return this.click.apply(this, arguments); },
  150. click: function(e) {
  151. if( e.which != 1 //only left click starts dragging
  152. || $.inArray(e.target.nodeName.toLowerCase(), this.options.dragPrevention || []) != -1 // Prevent execution on defined elements
  153. || (this.options.condition && !this.options.condition.apply(this.options.executor || this, [e, this.element])) //Prevent execution on condition
  154. ) { return true; }
  155. var self = this;
  156. this.initialized = false;
  157. var initialize = function() {
  158. self._MP = { left: e.pageX, top: e.pageY }; // Store the click mouse position
  159. $(document).bind('mouseup.mouse', function() { return self.stop.apply(self, arguments); });
  160. $(document).bind('mousemove.mouse', function() { return self.drag.apply(self, arguments); });
  161. if(!self.initalized && Math.abs(self._MP.left-e.pageX) >= self.options.distance || Math.abs(self._MP.top-e.pageY) >= self.options.distance) {
  162. (self.options.start && self.options.start.call(self.options.executor || self, e, self.element));
  163. (self.options.drag && self.options.drag.call(self.options.executor || self, e, this.element)); //This is actually not correct, but expected
  164. self.initialized = true;
  165. }
  166. };
  167. if(this.options.delay) {
  168. if(this.timer) { clearInterval(this.timer); }
  169. this.timer = setTimeout(initialize, this.options.delay);
  170. } else {
  171. initialize();
  172. }
  173. return false;
  174. },
  175. stop: function(e) {
  176. if(!this.initialized) {
  177. return $(document).unbind('mouseup.mouse').unbind('mousemove.mouse');
  178. }
  179. (this.options.stop && this.options.stop.call(this.options.executor || this, e, this.element));
  180. $(document).unbind('mouseup.mouse').unbind('mousemove.mouse');
  181. return false;
  182. },
  183. drag: function(e) {
  184. var o = this.options;
  185. if ($.browser.msie && !e.button) {
  186. return this.stop.call(this, e); // IE mouseup check
  187. }
  188. if(!this.initialized && (Math.abs(this._MP.left-e.pageX) >= o.distance || Math.abs(this._MP.top-e.pageY) >= o.distance)) {
  189. (o.start && o.start.call(o.executor || this, e, this.element));
  190. this.initialized = true;
  191. } else {
  192. if(!this.initialized) { return false; }
  193. }
  194. (o.drag && o.drag.call(this.options.executor || this, e, this.element));
  195. return false;
  196. }
  197. });
  198. })(jQuery);