web/data/ui.core.js
brancholdstable
changeset 7676 cc3987eb793c
parent 7388 dc319ece0bd6
parent 7670 6397a9051f65
child 7693 e2f75311d7be
equal deleted inserted replaced
7388:dc319ece0bd6 7676:cc3987eb793c
     1 /*
       
     2  * jQuery UI @VERSION
       
     3  *
       
     4  * Copyright (c) 2010 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 ;(function($) {
       
    11 
       
    12 /** jQuery core modifications and additions **/
       
    13 
       
    14 var _remove = $.fn.remove;
       
    15 $.fn.remove = function() {
       
    16 	$("*", this).add(this).triggerHandler("remove");
       
    17 	return _remove.apply(this, arguments );
       
    18 };
       
    19 
       
    20 function isVisible(element) {
       
    21 	function checkStyles(element) {
       
    22 		var style = element.style;
       
    23 		return (style.display != 'none' && style.visibility != 'hidden');
       
    24 	}
       
    25 	
       
    26 	var visible = checkStyles(element);
       
    27 	
       
    28 	(visible && $.each($.dir(element, 'parentNode'), function() {
       
    29 		return (visible = checkStyles(this));
       
    30 	}));
       
    31 	
       
    32 	return visible;
       
    33 }
       
    34 
       
    35 $.extend($.expr[':'], {
       
    36 	data: function(a, i, m) {
       
    37 		return $.data(a, m[3]);
       
    38 	},
       
    39 	
       
    40 	// TODO: add support for object, area
       
    41 	tabbable: function(a, i, m) {
       
    42 		var nodeName = a.nodeName.toLowerCase();
       
    43 		
       
    44 		return (
       
    45 			// in tab order
       
    46 			a.tabIndex >= 0 &&
       
    47 			
       
    48 			( // filter node types that participate in the tab order
       
    49 				
       
    50 				// anchor tag
       
    51 				('a' == nodeName && a.href) ||
       
    52 				
       
    53 				// enabled form element
       
    54 				(/input|select|textarea|button/.test(nodeName) &&
       
    55 					'hidden' != a.type && !a.disabled)
       
    56 			) &&
       
    57 			
       
    58 			// visible on page
       
    59 			isVisible(a)
       
    60 		);
       
    61 	}
       
    62 });
       
    63 
       
    64 $.keyCode = {
       
    65 	BACKSPACE: 8,
       
    66 	CAPS_LOCK: 20,
       
    67 	COMMA: 188,
       
    68 	CONTROL: 17,
       
    69 	DELETE: 46,
       
    70 	DOWN: 40,
       
    71 	END: 35,
       
    72 	ENTER: 13,
       
    73 	ESCAPE: 27,
       
    74 	HOME: 36,
       
    75 	INSERT: 45,
       
    76 	LEFT: 37,
       
    77 	NUMPAD_ADD: 107,
       
    78 	NUMPAD_DECIMAL: 110,
       
    79 	NUMPAD_DIVIDE: 111,
       
    80 	NUMPAD_ENTER: 108,
       
    81 	NUMPAD_MULTIPLY: 106,
       
    82 	NUMPAD_SUBTRACT: 109,
       
    83 	PAGE_DOWN: 34,
       
    84 	PAGE_UP: 33,
       
    85 	PERIOD: 190,
       
    86 	RIGHT: 39,
       
    87 	SHIFT: 16,
       
    88 	SPACE: 32,
       
    89 	TAB: 9,
       
    90 	UP: 38
       
    91 };
       
    92 
       
    93 // $.widget is a factory to create jQuery plugins
       
    94 // taking some boilerplate code out of the plugin code
       
    95 // created by Scott González and Jörn Zaefferer
       
    96 function getter(namespace, plugin, method, args) {
       
    97 	function getMethods(type) {
       
    98 		var methods = $[namespace][plugin][type] || [];
       
    99 		return (typeof methods == 'string' ? methods.split(/,?\s+/) : methods);
       
   100 	}
       
   101 	
       
   102 	var methods = getMethods('getter');
       
   103 	if (args.length == 1 && typeof args[0] == 'string') {
       
   104 		methods = methods.concat(getMethods('getterSetter'));
       
   105 	}
       
   106 	return ($.inArray(method, methods) != -1);
       
   107 }
       
   108 
       
   109 $.widget = function(name, prototype) {
       
   110 	var namespace = name.split(".")[0];
       
   111 	name = name.split(".")[1];
       
   112 	
       
   113 	// create plugin method
       
   114 	$.fn[name] = function(options) {
       
   115 		var isMethodCall = (typeof options == 'string'),
       
   116 			args = Array.prototype.slice.call(arguments, 1);
       
   117 		
       
   118 		// prevent calls to internal methods
       
   119 		if (isMethodCall && options.substring(0, 1) == '_') {
       
   120 			return this;
       
   121 		}
       
   122 		
       
   123 		// handle getter methods
       
   124 		if (isMethodCall && getter(namespace, name, options, args)) {
       
   125 			var instance = $.data(this[0], name);
       
   126 			return (instance ? instance[options].apply(instance, args)
       
   127 				: undefined);
       
   128 		}
       
   129 		
       
   130 		// handle initialization and non-getter methods
       
   131 		return this.each(function() {
       
   132 			var instance = $.data(this, name);
       
   133 			
       
   134 			// constructor
       
   135 			(!instance && !isMethodCall &&
       
   136 				$.data(this, name, new $[namespace][name](this, options)));
       
   137 			
       
   138 			// method call
       
   139 			(instance && isMethodCall && $.isFunction(instance[options]) &&
       
   140 				instance[options].apply(instance, args));
       
   141 		});
       
   142 	};
       
   143 	
       
   144 	// create widget constructor
       
   145 	$[namespace][name] = function(element, options) {
       
   146 		var self = this;
       
   147 		
       
   148 		this.widgetName = name;
       
   149 		this.widgetEventPrefix = $[namespace][name].eventPrefix || name;
       
   150 		this.widgetBaseClass = namespace + '-' + name;
       
   151 		
       
   152 		this.options = $.extend({},
       
   153 			$.widget.defaults,
       
   154 			$[namespace][name].defaults,
       
   155 			$.metadata && $.metadata.get(element)[name],
       
   156 			options);
       
   157 		
       
   158 		this.element = $(element)
       
   159 			.bind('setData.' + name, function(e, key, value) {
       
   160 				return self._setData(key, value);
       
   161 			})
       
   162 			.bind('getData.' + name, function(e, key) {
       
   163 				return self._getData(key);
       
   164 			})
       
   165 			.bind('remove', function() {
       
   166 				return self.destroy();
       
   167 			});
       
   168 		
       
   169 		this._init();
       
   170 	};
       
   171 	
       
   172 	// add widget prototype
       
   173 	$[namespace][name].prototype = $.extend({}, $.widget.prototype, prototype);
       
   174 	
       
   175 	// TODO: merge getter and getterSetter properties from widget prototype
       
   176 	// and plugin prototype
       
   177 	$[namespace][name].getterSetter = 'option';
       
   178 };
       
   179 
       
   180 $.widget.prototype = {
       
   181 	_init: function() {},
       
   182 	destroy: function() {
       
   183 		this.element.removeData(this.widgetName);
       
   184 	},
       
   185 	
       
   186 	option: function(key, value) {
       
   187 		var options = key,
       
   188 			self = this;
       
   189 		
       
   190 		if (typeof key == "string") {
       
   191 			if (value === undefined) {
       
   192 				return this._getData(key);
       
   193 			}
       
   194 			options = {};
       
   195 			options[key] = value;
       
   196 		}
       
   197 		
       
   198 		$.each(options, function(key, value) {
       
   199 			self._setData(key, value);
       
   200 		});
       
   201 	},
       
   202 	_getData: function(key) {
       
   203 		return this.options[key];
       
   204 	},
       
   205 	_setData: function(key, value) {
       
   206 		this.options[key] = value;
       
   207 		
       
   208 		if (key == 'disabled') {
       
   209 			this.element[value ? 'addClass' : 'removeClass'](
       
   210 				this.widgetBaseClass + '-disabled');
       
   211 		}
       
   212 	},
       
   213 	
       
   214 	enable: function() {
       
   215 		this._setData('disabled', false);
       
   216 	},
       
   217 	disable: function() {
       
   218 		this._setData('disabled', true);
       
   219 	},
       
   220 	
       
   221 	_trigger: function(type, e, data) {
       
   222 		var eventName = (type == this.widgetEventPrefix
       
   223 			? type : this.widgetEventPrefix + type);
       
   224 		e = e  || $.event.fix({ type: eventName, target: this.element[0] });
       
   225 		return this.element.triggerHandler(eventName, [e, data], this.options[type]);
       
   226 	}
       
   227 };
       
   228 
       
   229 $.widget.defaults = {
       
   230 	disabled: false
       
   231 };
       
   232 
       
   233 
       
   234 /** jQuery UI core **/
       
   235 
       
   236 $.ui = {
       
   237 	plugin: {
       
   238 		add: function(module, option, set) {
       
   239 			var proto = $.ui[module].prototype;
       
   240 			for(var i in set) {
       
   241 				proto.plugins[i] = proto.plugins[i] || [];
       
   242 				proto.plugins[i].push([option, set[i]]);
       
   243 			}
       
   244 		},
       
   245 		call: function(instance, name, args) {
       
   246 			var set = instance.plugins[name];
       
   247 			if(!set) { return; }
       
   248 			
       
   249 			for (var i = 0; i < set.length; i++) {
       
   250 				if (instance.options[set[i][0]]) {
       
   251 					set[i][1].apply(instance.element, args);
       
   252 				}
       
   253 			}
       
   254 		}	
       
   255 	},
       
   256 	cssCache: {},
       
   257 	css: function(name) {
       
   258 		if ($.ui.cssCache[name]) { return $.ui.cssCache[name]; }
       
   259 		var tmp = $('<div class="ui-gen">').addClass(name).css({position:'absolute', top:'-5000px', left:'-5000px', display:'block'}).appendTo('body');
       
   260 		
       
   261 		//if (!$.browser.safari)
       
   262 			//tmp.appendTo('body'); 
       
   263 		
       
   264 		//Opera and Safari set width and height to 0px instead of auto
       
   265 		//Safari returns rgba(0,0,0,0) when bgcolor is not set
       
   266 		$.ui.cssCache[name] = !!(
       
   267 			(!(/auto|default/).test(tmp.css('cursor')) || (/^[1-9]/).test(tmp.css('height')) || (/^[1-9]/).test(tmp.css('width')) || 
       
   268 			!(/none/).test(tmp.css('backgroundImage')) || !(/transparent|rgba\(0, 0, 0, 0\)/).test(tmp.css('backgroundColor')))
       
   269 		);
       
   270 		try { $('body').get(0).removeChild(tmp.get(0));	} catch(e){}
       
   271 		return $.ui.cssCache[name];
       
   272 	},
       
   273 	disableSelection: function(el) {
       
   274 		$(el)
       
   275 			.attr('unselectable', 'on')
       
   276 			.css('MozUserSelect', 'none')
       
   277 			.bind('selectstart.ui', function() { return false; });
       
   278 	},
       
   279 	enableSelection: function(el) {
       
   280 		$(el)
       
   281 			.attr('unselectable', 'off')
       
   282 			.css('MozUserSelect', '')
       
   283 			.unbind('selectstart.ui');
       
   284 	},
       
   285 	hasScroll: function(e, a) {
       
   286 		var scroll = (a && a == 'left') ? 'scrollLeft' : 'scrollTop',
       
   287 			has = false;
       
   288 		
       
   289 		if (e[scroll] > 0) { return true; }
       
   290 		
       
   291 		// TODO: determine which cases actually cause this to happen
       
   292 		// if the element doesn't have the scroll set, see if it's possible to
       
   293 		// set the scroll
       
   294 		e[scroll] = 1;
       
   295 		has = (e[scroll] > 0);
       
   296 		e[scroll] = 0;
       
   297 		return has;
       
   298 	}
       
   299 };
       
   300 
       
   301 
       
   302 /** Mouse Interaction Plugin **/
       
   303 
       
   304 $.ui.mouse = {
       
   305 	_mouseInit: function() {
       
   306 		var self = this;
       
   307 	
       
   308 		this.element.bind('mousedown.'+this.widgetName, function(e) {
       
   309 			return self._mouseDown(e);
       
   310 		});
       
   311 		
       
   312 		// Prevent text selection in IE
       
   313 		if ($.browser.msie) {
       
   314 			this._mouseUnselectable = this.element.attr('unselectable');
       
   315 			this.element.attr('unselectable', 'on');
       
   316 		}
       
   317 		
       
   318 		this.started = false;
       
   319 	},
       
   320 	
       
   321 	// TODO: make sure destroying one instance of mouse doesn't mess with
       
   322 	// other instances of mouse
       
   323 	_mouseDestroy: function() {
       
   324 		this.element.unbind('.'+this.widgetName);
       
   325 		
       
   326 		// Restore text selection in IE
       
   327 		($.browser.msie
       
   328 			&& this.element.attr('unselectable', this._mouseUnselectable));
       
   329 	},
       
   330 	
       
   331 	_mouseDown: function(e) {
       
   332 		// we may have missed mouseup (out of window)
       
   333 		(this._mouseStarted && this._mouseUp(e));
       
   334 		
       
   335 		this._mouseDownEvent = e;
       
   336 		
       
   337 		var self = this,
       
   338 			btnIsLeft = (e.which == 1),
       
   339 			elIsCancel = (typeof this.options.cancel == "string" ? $(e.target).parents().add(e.target).filter(this.options.cancel).length : false);
       
   340 		if (!btnIsLeft || elIsCancel || !this._mouseCapture(e)) {
       
   341 			return true;
       
   342 		}
       
   343 		
       
   344 		this.mouseDelayMet = !this.options.delay;
       
   345 		if (!this.mouseDelayMet) {
       
   346 			this._mouseDelayTimer = setTimeout(function() {
       
   347 				self.mouseDelayMet = true;
       
   348 			}, this.options.delay);
       
   349 		}
       
   350 		
       
   351 		if (this._mouseDistanceMet(e) && this._mouseDelayMet(e)) {
       
   352 			this._mouseStarted = (this._mouseStart(e) !== false);
       
   353 			if (!this._mouseStarted) {
       
   354 				e.preventDefault();
       
   355 				return true;
       
   356 			}
       
   357 		}
       
   358 		
       
   359 		// these delegates are required to keep context
       
   360 		this._mouseMoveDelegate = function(e) {
       
   361 			return self._mouseMove(e);
       
   362 		};
       
   363 		this._mouseUpDelegate = function(e) {
       
   364 			return self._mouseUp(e);
       
   365 		};
       
   366 		$(document)
       
   367 			.bind('mousemove.'+this.widgetName, this._mouseMoveDelegate)
       
   368 			.bind('mouseup.'+this.widgetName, this._mouseUpDelegate);
       
   369 		
       
   370 		return false;
       
   371 	},
       
   372 	
       
   373 	_mouseMove: function(e) {
       
   374 		// IE mouseup check - mouseup happened when mouse was out of window
       
   375 		if ($.browser.msie && !e.button) {
       
   376 			return this._mouseUp(e);
       
   377 		}
       
   378 		
       
   379 		if (this._mouseStarted) {
       
   380 			this._mouseDrag(e);
       
   381 			return false;
       
   382 		}
       
   383 		
       
   384 		if (this._mouseDistanceMet(e) && this._mouseDelayMet(e)) {
       
   385 			this._mouseStarted =
       
   386 				(this._mouseStart(this._mouseDownEvent, e) !== false);
       
   387 			(this._mouseStarted ? this._mouseDrag(e) : this._mouseUp(e));
       
   388 		}
       
   389 		
       
   390 		return !this._mouseStarted;
       
   391 	},
       
   392 	
       
   393 	_mouseUp: function(e) {
       
   394 		$(document)
       
   395 			.unbind('mousemove.'+this.widgetName, this._mouseMoveDelegate)
       
   396 			.unbind('mouseup.'+this.widgetName, this._mouseUpDelegate);
       
   397 		
       
   398 		if (this._mouseStarted) {
       
   399 			this._mouseStarted = false;
       
   400 			this._mouseStop(e);
       
   401 		}
       
   402 		
       
   403 		return false;
       
   404 	},
       
   405 	
       
   406 	_mouseDistanceMet: function(e) {
       
   407 		return (Math.max(
       
   408 				Math.abs(this._mouseDownEvent.pageX - e.pageX),
       
   409 				Math.abs(this._mouseDownEvent.pageY - e.pageY)
       
   410 			) >= this.options.distance
       
   411 		);
       
   412 	},
       
   413 	
       
   414 	_mouseDelayMet: function(e) {
       
   415 		return this.mouseDelayMet;
       
   416 	},
       
   417 	
       
   418 	// These are placeholder methods, to be overriden by extending plugin
       
   419 	_mouseStart: function(e) {},
       
   420 	_mouseDrag: function(e) {},
       
   421 	_mouseStop: function(e) {},
       
   422 	_mouseCapture: function(e) { return true; }
       
   423 };
       
   424 
       
   425 $.ui.mouse.defaults = {
       
   426 	cancel: null,
       
   427 	distance: 1,
       
   428 	delay: 0
       
   429 };
       
   430 
       
   431 })(jQuery);