source: trunk/template-common/lib/ui/ui.core.js @ 3282

Last change on this file since 3282 was 3282, checked in by plg, 15 years ago

change: according to topic:15067, svn:keywords property was removed

  • Property svn:eol-style set to LF
File size: 7.5 KB
Line 
1/*
2 * jQuery UI 1.5.3
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;(function($) {
11
12$.ui = {
13        plugin: {
14                add: function(module, option, set) {
15                        var proto = $.ui[module].prototype;
16                        for(var i in set) {
17                                proto.plugins[i] = proto.plugins[i] || [];
18                                proto.plugins[i].push([option, set[i]]);
19                        }
20                },
21                call: function(instance, name, args) {
22                        var set = instance.plugins[name];
23                        if(!set) { return; }
24                       
25                        for (var i = 0; i < set.length; i++) {
26                                if (instance.options[set[i][0]]) {
27                                        set[i][1].apply(instance.element, args);
28                                }
29                        }
30                }       
31        },
32        cssCache: {},
33        css: function(name) {
34                if ($.ui.cssCache[name]) { return $.ui.cssCache[name]; }
35                var tmp = $('<div class="ui-gen">').addClass(name).css({position:'absolute', top:'-5000px', left:'-5000px', display:'block'}).appendTo('body');
36               
37                //if (!$.browser.safari)
38                        //tmp.appendTo('body');
39               
40                //Opera and Safari set width and height to 0px instead of auto
41                //Safari returns rgba(0,0,0,0) when bgcolor is not set
42                $.ui.cssCache[name] = !!(
43                        (!(/auto|default/).test(tmp.css('cursor')) || (/^[1-9]/).test(tmp.css('height')) || (/^[1-9]/).test(tmp.css('width')) || 
44                        !(/none/).test(tmp.css('backgroundImage')) || !(/transparent|rgba\(0, 0, 0, 0\)/).test(tmp.css('backgroundColor')))
45                );
46                try { $('body').get(0).removeChild(tmp.get(0)); } catch(e){}
47                return $.ui.cssCache[name];
48        },
49        disableSelection: function(el) {
50                $(el).attr('unselectable', 'on').css('MozUserSelect', 'none');
51        },
52        enableSelection: function(el) {
53                $(el).attr('unselectable', 'off').css('MozUserSelect', '');
54        },
55        hasScroll: function(e, a) {
56                var scroll = /top/.test(a||"top") ? 'scrollTop' : 'scrollLeft', has = false;
57                if (e[scroll] > 0) return true; e[scroll] = 1;
58                has = e[scroll] > 0 ? true : false; e[scroll] = 0;
59                return has;
60        }
61};
62
63
64/** jQuery core modifications and additions **/
65
66var _remove = $.fn.remove;
67$.fn.remove = function() {
68        $("*", this).add(this).triggerHandler("remove");
69        return _remove.apply(this, arguments );
70};
71
72// $.widget is a factory to create jQuery plugins
73// taking some boilerplate code out of the plugin code
74// created by Scott González and Jörn Zaefferer
75function getter(namespace, plugin, method) {
76        var methods = $[namespace][plugin].getter || [];
77        methods = (typeof methods == "string" ? methods.split(/,?\s+/) : methods);
78        return ($.inArray(method, methods) != -1);
79}
80
81$.widget = function(name, prototype) {
82        var namespace = name.split(".")[0];
83        name = name.split(".")[1];
84       
85        // create plugin method
86        $.fn[name] = function(options) {
87                var isMethodCall = (typeof options == 'string'),
88                        args = Array.prototype.slice.call(arguments, 1);
89               
90                if (isMethodCall && getter(namespace, name, options)) {
91                        var instance = $.data(this[0], name);
92                        return (instance ? instance[options].apply(instance, args)
93                                : undefined);
94                }
95               
96                return this.each(function() {
97                        var instance = $.data(this, name);
98                        if (isMethodCall && instance && $.isFunction(instance[options])) {
99                                instance[options].apply(instance, args);
100                        } else if (!isMethodCall) {
101                                $.data(this, name, new $[namespace][name](this, options));
102                        }
103                });
104        };
105       
106        // create widget constructor
107        $[namespace][name] = function(element, options) {
108                var self = this;
109               
110                this.widgetName = name;
111                this.widgetBaseClass = namespace + '-' + name;
112               
113                this.options = $.extend({}, $.widget.defaults, $[namespace][name].defaults, options);
114                this.element = $(element)
115                        .bind('setData.' + name, function(e, key, value) {
116                                return self.setData(key, value);
117                        })
118                        .bind('getData.' + name, function(e, key) {
119                                return self.getData(key);
120                        })
121                        .bind('remove', function() {
122                                return self.destroy();
123                        });
124                this.init();
125        };
126       
127        // add widget prototype
128        $[namespace][name].prototype = $.extend({}, $.widget.prototype, prototype);
129};
130
131$.widget.prototype = {
132        init: function() {},
133        destroy: function() {
134                this.element.removeData(this.widgetName);
135        },
136       
137        getData: function(key) {
138                return this.options[key];
139        },
140        setData: function(key, value) {
141                this.options[key] = value;
142               
143                if (key == 'disabled') {
144                        this.element[value ? 'addClass' : 'removeClass'](
145                                this.widgetBaseClass + '-disabled');
146                }
147        },
148       
149        enable: function() {
150                this.setData('disabled', false);
151        },
152        disable: function() {
153                this.setData('disabled', true);
154        }
155};
156
157$.widget.defaults = {
158        disabled: false
159};
160
161
162/** Mouse Interaction Plugin **/
163
164$.ui.mouse = {
165        mouseInit: function() {
166                var self = this;
167       
168                this.element.bind('mousedown.'+this.widgetName, function(e) {
169                        return self.mouseDown(e);
170                });
171               
172                // Prevent text selection in IE
173                if ($.browser.msie) {
174                        this._mouseUnselectable = this.element.attr('unselectable');
175                        this.element.attr('unselectable', 'on');
176                }
177               
178                this.started = false;
179        },
180       
181        // TODO: make sure destroying one instance of mouse doesn't mess with
182        // other instances of mouse
183        mouseDestroy: function() {
184                this.element.unbind('.'+this.widgetName);
185               
186                // Restore text selection in IE
187                ($.browser.msie
188                        && this.element.attr('unselectable', this._mouseUnselectable));
189        },
190       
191        mouseDown: function(e) {
192                // we may have missed mouseup (out of window)
193                (this._mouseStarted && this.mouseUp(e));
194               
195                this._mouseDownEvent = e;
196               
197                var self = this,
198                        btnIsLeft = (e.which == 1),
199                        elIsCancel = (typeof this.options.cancel == "string" ? $(e.target).parents().add(e.target).filter(this.options.cancel).length : false);
200                if (!btnIsLeft || elIsCancel || !this.mouseCapture(e)) {
201                        return true;
202                }
203               
204                this._mouseDelayMet = !this.options.delay;
205                if (!this._mouseDelayMet) {
206                        this._mouseDelayTimer = setTimeout(function() {
207                                self._mouseDelayMet = true;
208                        }, this.options.delay);
209                }
210               
211                if (this.mouseDistanceMet(e) && this.mouseDelayMet(e)) {
212                        this._mouseStarted = (this.mouseStart(e) !== false);
213                        if (!this._mouseStarted) {
214                                e.preventDefault();
215                                return true;
216                        }
217                }
218               
219                // these delegates are required to keep context
220                this._mouseMoveDelegate = function(e) {
221                        return self.mouseMove(e);
222                };
223                this._mouseUpDelegate = function(e) {
224                        return self.mouseUp(e);
225                };
226                $(document)
227                        .bind('mousemove.'+this.widgetName, this._mouseMoveDelegate)
228                        .bind('mouseup.'+this.widgetName, this._mouseUpDelegate);
229               
230                return false;
231        },
232       
233        mouseMove: function(e) {
234                // IE mouseup check - mouseup happened when mouse was out of window
235                if ($.browser.msie && !e.button) {
236                        return this.mouseUp(e);
237                }
238               
239                if (this._mouseStarted) {
240                        this.mouseDrag(e);
241                        return false;
242                }
243               
244                if (this.mouseDistanceMet(e) && this.mouseDelayMet(e)) {
245                        this._mouseStarted =
246                                (this.mouseStart(this._mouseDownEvent, e) !== false);
247                        (this._mouseStarted ? this.mouseDrag(e) : this.mouseUp(e));
248                }
249               
250                return !this._mouseStarted;
251        },
252       
253        mouseUp: function(e) {
254                $(document)
255                        .unbind('mousemove.'+this.widgetName, this._mouseMoveDelegate)
256                        .unbind('mouseup.'+this.widgetName, this._mouseUpDelegate);
257               
258                if (this._mouseStarted) {
259                        this._mouseStarted = false;
260                        this.mouseStop(e);
261                }
262               
263                return false;
264        },
265       
266        mouseDistanceMet: function(e) {
267                return (Math.max(
268                                Math.abs(this._mouseDownEvent.pageX - e.pageX),
269                                Math.abs(this._mouseDownEvent.pageY - e.pageY)
270                        ) >= this.options.distance
271                );
272        },
273       
274        mouseDelayMet: function(e) {
275                return this._mouseDelayMet;
276        },
277       
278        // These are placeholder methods, to be overriden by extending plugin
279        mouseStart: function(e) {},
280        mouseDrag: function(e) {},
281        mouseStop: function(e) {},
282        mouseCapture: function(e) { return true; }
283};
284
285$.ui.mouse.defaults = {
286        cancel: null,
287        distance: 1,
288        delay: 0
289};
290
291})(jQuery);
Note: See TracBrowser for help on using the repository browser.