2011-04-23 16:45:50 +00:00
|
|
|
// vim: et:ts=4:sw=4:sts=4:ft=js
|
2011-04-22 22:03:10 +00:00
|
|
|
// check out http://ejohn.org/apps/learn/#36 (-#38, making fns work w/o new)
|
|
|
|
|
2011-05-05 18:02:56 +00:00
|
|
|
/*@
|
|
|
|
Ox.Element <function:Ox.JQueryElement> Basic UI element object
|
|
|
|
# Usage --------------------------------------------------------------------
|
|
|
|
([options[, self]]) -> <object> UI element
|
|
|
|
# Arguments ----------------------------------------------------------------
|
|
|
|
options <object> the options of the element
|
|
|
|
# Properties
|
|
|
|
element <string> tagname or CSS selector
|
|
|
|
tooltip <object> tooltip (not implemented)
|
|
|
|
options <string> tagname or CSS selector
|
|
|
|
self <object> shared private variable
|
|
|
|
# Events -------------------------------------------------------------------
|
|
|
|
anyclick <event> anyclick
|
|
|
|
fires on mouseup, but not on any subsequent mouseup within 250 ms
|
|
|
|
* <*> original event properties
|
|
|
|
doubleclick <event> doubleclick
|
|
|
|
fires on the second mousedown within 250 ms
|
|
|
|
* <*> original event properties
|
|
|
|
drag <event> drag
|
|
|
|
fires on mousemove after dragstart, stops firing on mouseup
|
|
|
|
clientDX <number> horizontal drag delta in px
|
|
|
|
clientDY <number> vertical drag delta in px
|
|
|
|
* <*> original event properties
|
|
|
|
dragend <event> dragpause
|
|
|
|
Fires on mouseup after dragstart
|
|
|
|
clientDX <number> horizontal drag delta in px
|
|
|
|
clientDY <number> vertical drag delta in px
|
|
|
|
* <*> original event properties
|
|
|
|
dragpause <event> dragpause
|
|
|
|
Fires once when the mouse doesn't move for 250 ms after drag
|
|
|
|
clientDX <number> horizontal drag delta in px
|
|
|
|
clientDY <number> vertical drag delta in px
|
|
|
|
* <*> original event properties
|
|
|
|
dragstart <event> dragstart
|
|
|
|
fires when the mouse is down for 250 ms
|
|
|
|
* <*> original event properties
|
|
|
|
mouserepeat <event> mouserepeat
|
|
|
|
fires every 50 ms after the mouse was down for 250 ms, stops firing on
|
|
|
|
mouseleave or mouseup
|
|
|
|
* <*> original event properties
|
|
|
|
singleclick <event> singleclick
|
|
|
|
fires 250 ms after mouseup, if there was no subsequent mousedown
|
|
|
|
* <*> original event properties
|
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-05-05 18:02:56 +00:00
|
|
|
Ox.Element = function() {
|
2011-04-22 22:03:10 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
tooltip option can be any of the following:
|
|
|
|
string
|
|
|
|
function(e), returns string
|
|
|
|
{mousemove: true, title: function(e)}
|
|
|
|
*/
|
|
|
|
|
|
|
|
return function(options, self) {
|
|
|
|
|
2011-04-29 12:40:51 +00:00
|
|
|
/*
|
|
|
|
// allow for 'Ox.Element()' instead of 'new Ox.Element()'
|
2011-04-22 22:03:10 +00:00
|
|
|
if (!(this instanceof arguments.callee)) {
|
|
|
|
return new arguments.callee(options, self);
|
|
|
|
}
|
2011-04-29 12:40:51 +00:00
|
|
|
*/
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-04-29 12:40:51 +00:00
|
|
|
// create private object
|
2011-04-22 22:03:10 +00:00
|
|
|
self = self || {};
|
2011-04-29 12:40:51 +00:00
|
|
|
// create defaults and options objects
|
|
|
|
self.defaults = {};
|
2011-04-22 22:03:10 +00:00
|
|
|
self.options = options || {};
|
2011-04-29 12:40:51 +00:00
|
|
|
// allow for Ox.TestElement('<tagname>')
|
|
|
|
// or Ox.TestElement('cssSelector')
|
|
|
|
if (Ox.isString(self.options)) {
|
2011-04-22 22:03:10 +00:00
|
|
|
self.options = {
|
|
|
|
element: self.options
|
2011-04-29 12:40:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
// create event handler
|
2011-04-22 22:03:10 +00:00
|
|
|
if (!self.$eventHandler) {
|
|
|
|
self.$eventHandler = $('<div>');
|
|
|
|
}
|
|
|
|
|
2011-04-29 12:40:51 +00:00
|
|
|
// create public object
|
2011-04-22 22:03:10 +00:00
|
|
|
var that = new Ox.JQueryElement(
|
2011-04-29 12:40:51 +00:00
|
|
|
$(self.options.element || '<div>')
|
2011-04-22 22:03:10 +00:00
|
|
|
)
|
|
|
|
.mousedown(mousedown);
|
|
|
|
|
|
|
|
/*
|
|
|
|
self.options.tooltip && that.bind(Ox.extend({
|
|
|
|
mouseenter: mouseenter,
|
|
|
|
mouseleave: mouseleave
|
|
|
|
}, self.options.tooltip.mousemove ? {
|
|
|
|
mousemove: mousemove
|
|
|
|
} : {}));
|
|
|
|
*/
|
|
|
|
|
2011-04-29 22:07:23 +00:00
|
|
|
function bind(action, event, fn) {
|
|
|
|
self.$eventHandler[action]('ox_' + event, function(event, data) {
|
|
|
|
// fixme: remove second parameter
|
|
|
|
fn(Ox.extend({_event: event}, data), data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-04-22 22:03:10 +00:00
|
|
|
function mousedown(e) {
|
|
|
|
/*
|
|
|
|
better mouse events
|
|
|
|
mousedown:
|
|
|
|
trigger mousedown
|
|
|
|
within 250 msec:
|
|
|
|
mouseup: trigger anyclick ("click" would collide with click events of certain widgets)
|
|
|
|
mouseup + mousedown: trigger doubleclick
|
|
|
|
after 250 msec:
|
|
|
|
mouseup + no mousedown within 250 msec: trigger singleclick
|
|
|
|
no mouseup within 250 msec:
|
|
|
|
trigger mouserepeat every 50 msec
|
|
|
|
trigger dragstart
|
|
|
|
mousemove: trigger drag
|
|
|
|
no mousemove within 250 msec:
|
|
|
|
trigger dragpause
|
|
|
|
mouseup: trigger dragend
|
|
|
|
*/
|
|
|
|
var clientX, clientY,
|
|
|
|
dragTimeout = 0,
|
|
|
|
mouseInterval = 0;
|
|
|
|
if (!self.mouseTimeout) {
|
|
|
|
// first mousedown
|
|
|
|
that.triggerEvent('mousedown', e);
|
|
|
|
self.mouseup = false;
|
|
|
|
self.mouseTimeout = setTimeout(function() {
|
|
|
|
self.mouseTimeout = 0;
|
|
|
|
if (self.mouseup) {
|
|
|
|
// singleclick
|
|
|
|
that.triggerEvent('singleclick', e);
|
|
|
|
} else {
|
|
|
|
// mouserepeat, drag
|
|
|
|
clientX = e.clientX;
|
|
|
|
clientY = e.clientY;
|
|
|
|
that.triggerEvent('dragstart', e);
|
|
|
|
mouserepeat();
|
|
|
|
mouseInterval = setInterval(mouserepeat, 50);
|
|
|
|
Ox.UI.$window.unbind('mouseup', mouseup)
|
|
|
|
.mousemove(mousemove)
|
|
|
|
.one('mouseup', function(e) {
|
|
|
|
clearInterval(mouseInterval);
|
|
|
|
clearTimeout(dragTimeout);
|
|
|
|
Ox.UI.$window.unbind('mousemove', mousemove);
|
|
|
|
that.triggerEvent('dragend', extend(e));
|
|
|
|
});
|
|
|
|
that.one('mouseleave', function() {
|
|
|
|
clearInterval(mouseInterval);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, 250);
|
|
|
|
} else {
|
|
|
|
// second mousedown
|
|
|
|
clearTimeout(self.mouseTimeout);
|
|
|
|
self.mouseTimeout = 0;
|
|
|
|
that.triggerEvent('doubleclick', e);
|
|
|
|
}
|
|
|
|
Ox.UI.$window.one('mouseup', mouseup);
|
|
|
|
function extend(e) {
|
|
|
|
return Ox.extend({
|
|
|
|
clientDX: e.clientX - clientX,
|
|
|
|
clientDY: e.clientY - clientY
|
|
|
|
}, e);
|
|
|
|
}
|
|
|
|
function mousemove(e) {
|
|
|
|
e = extend(e);
|
|
|
|
clearTimeout(dragTimeout);
|
|
|
|
dragTimeout = setTimeout(function() {
|
|
|
|
that.triggerEvent('dragpause', e);
|
|
|
|
}, 250);
|
|
|
|
that.triggerEvent('drag', e);
|
|
|
|
}
|
|
|
|
function mouserepeat() {
|
|
|
|
that.triggerEvent('mouserepeat');
|
|
|
|
}
|
|
|
|
function mouseup(e) {
|
2011-05-05 18:02:56 +00:00
|
|
|
// only trigger on first mouseup
|
2011-04-22 22:03:10 +00:00
|
|
|
if (!self.mouseup) {
|
|
|
|
that.triggerEvent('anyclick', e);
|
|
|
|
self.mouseup = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
function mouseenter(e) {
|
|
|
|
self.$tooltip = new Ox.Tooltip({
|
|
|
|
title: Ox.isString(self.options.tooltip) ?
|
|
|
|
self.options.tooltip : Ox.isFunction(self.options.tooltip) ?
|
|
|
|
self.options.tooltip(e) : self.options.tooltip.title(e)
|
|
|
|
}).show();
|
|
|
|
}
|
|
|
|
|
|
|
|
function mouseleave(e) {
|
|
|
|
self.$tooltip.hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
function mousemove(e) {
|
|
|
|
self.$tooltip.options({
|
|
|
|
title: self.options.tooltip.title(e)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2011-04-29 12:40:51 +00:00
|
|
|
self.setOption = function() {
|
|
|
|
// self.setOptions(key, value)
|
2011-04-22 22:03:10 +00:00
|
|
|
// is called when an option changes
|
|
|
|
// (to be implemented by widget)
|
|
|
|
};
|
|
|
|
|
2011-04-29 12:40:51 +00:00
|
|
|
that._self = self; // fixme: remove
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-05-05 18:02:56 +00:00
|
|
|
/*@
|
|
|
|
bindEvent <function> Binds a function to an event
|
2011-05-07 21:07:53 +00:00
|
|
|
(event, callback) -> <o> This element
|
|
|
|
({event: callback, ...}) -> <o> This element
|
2011-05-05 18:02:56 +00:00
|
|
|
callback <f> Callback function
|
2011-05-07 21:07:53 +00:00
|
|
|
data <o> event data (key/value pairs)
|
|
|
|
event <s> Event name
|
|
|
|
Event names can be namespaced, like <code>'click.foo'</code>
|
|
|
|
|
2011-05-05 18:02:56 +00:00
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
that.bindEvent = function() {
|
2011-04-29 22:07:23 +00:00
|
|
|
Ox.forEach(Ox.makeObject(arguments), function(fn, event) {
|
|
|
|
bind('bind', event, fn);
|
|
|
|
});
|
2011-04-22 22:03:10 +00:00
|
|
|
return that;
|
|
|
|
}
|
|
|
|
|
2011-05-05 18:02:56 +00:00
|
|
|
/*@
|
|
|
|
bindEventOnce <function> Binds a function to an event, once
|
2011-05-07 21:07:53 +00:00
|
|
|
(event, callback) -> <obj> This element object
|
|
|
|
({event: callback, ...}) -> <obj> This element object
|
2011-05-05 18:02:56 +00:00
|
|
|
callback <f> Callback function
|
2011-05-07 21:07:53 +00:00
|
|
|
data <o> event data (key/value pairs)
|
|
|
|
event <s> Event name
|
|
|
|
Event names can be namespaced, like <code>'click.foo'</code>
|
2011-05-05 18:02:56 +00:00
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
that.bindEventOnce = function() {
|
2011-04-29 22:07:23 +00:00
|
|
|
Ox.forEach(Ox.makeObject(arguments), function(fn, event) {
|
|
|
|
bind('one', event, fn);
|
|
|
|
});
|
2011-04-22 22:03:10 +00:00
|
|
|
return that;
|
|
|
|
};
|
|
|
|
|
2011-05-05 18:02:56 +00:00
|
|
|
/*@
|
2011-05-07 21:07:53 +00:00
|
|
|
defaults <function> Sets the default options for an element object
|
|
|
|
({key: value, ...}) -> <obj> This element object
|
|
|
|
key <str> The name of the default option
|
|
|
|
value <val> The value of the default option
|
2011-05-05 18:02:56 +00:00
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
that.defaults = function(defaults) {
|
2011-04-29 12:40:51 +00:00
|
|
|
// sets the default options
|
2011-04-22 22:03:10 +00:00
|
|
|
self.defaults = defaults;
|
2011-04-29 12:40:51 +00:00
|
|
|
self.options = defaults;
|
2011-04-22 22:03:10 +00:00
|
|
|
return that;
|
|
|
|
};
|
|
|
|
|
2011-05-05 18:02:56 +00:00
|
|
|
/*@
|
2011-05-07 21:07:53 +00:00
|
|
|
gainFocus <function> Makes an element object gain focus
|
|
|
|
() -> <obj> This element object
|
2011-05-05 18:02:56 +00:00
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
that.gainFocus = function() {
|
|
|
|
Ox.Focus.focus(that.id);
|
|
|
|
return that;
|
|
|
|
};
|
|
|
|
|
2011-05-05 18:02:56 +00:00
|
|
|
/*@
|
2011-05-07 21:07:53 +00:00
|
|
|
hasFocus <function> Returns true if an element object has focus
|
|
|
|
() -> <boolean> True if the element has focus
|
2011-05-05 18:02:56 +00:00
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
that.hasFocus = function() {
|
|
|
|
return Ox.Focus.focused() == that.id;
|
|
|
|
};
|
|
|
|
|
2011-05-05 18:02:56 +00:00
|
|
|
/*@
|
2011-05-07 21:07:53 +00:00
|
|
|
loseFocus <function> Makes an element object lose focus
|
|
|
|
() -> <object> This element object
|
2011-05-05 18:02:56 +00:00
|
|
|
@*/
|
|
|
|
|
2011-04-22 22:03:10 +00:00
|
|
|
that.loseFocus = function() {
|
|
|
|
Ox.Focus.blur(that.id);
|
|
|
|
return that;
|
|
|
|
};
|
|
|
|
|
2011-05-05 18:02:56 +00:00
|
|
|
/*@
|
2011-05-07 21:07:53 +00:00
|
|
|
options <function> Gets or sets the options of an element object
|
2011-05-05 18:02:56 +00:00
|
|
|
# Usage
|
|
|
|
() -> <obj> all options
|
|
|
|
(key) -> <val> the value of option[key]
|
|
|
|
(key, value) -> <obj> this element
|
|
|
|
sets options[key] to value and calls self.setOption(key, value)
|
|
|
|
if the key/value pair was added or modified
|
|
|
|
({key: value, ...}) -> <obj> this element
|
|
|
|
sets multiple options and calls self.setOption(key, value)
|
|
|
|
for every key/value pair that was added or modified
|
|
|
|
# Arguments
|
|
|
|
key <str> the name of the option
|
|
|
|
value <val> the value of the option
|
|
|
|
@*/
|
|
|
|
|
2011-04-29 12:40:51 +00:00
|
|
|
that.options = function() {
|
|
|
|
return Ox.getset(self.options, arguments, self.setOption, that);
|
2011-04-22 22:03:10 +00:00
|
|
|
};
|
|
|
|
|
2011-05-07 21:07:53 +00:00
|
|
|
/*@
|
|
|
|
removeElement <function> Removes an element object and its event handler
|
|
|
|
() -> <obj> This element
|
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
that.removeElement = function() {
|
|
|
|
that.loseFocus();
|
|
|
|
delete self.$eventHandler;
|
|
|
|
that.remove();
|
|
|
|
delete Ox.UI.elements[that.id];
|
|
|
|
return that;
|
|
|
|
};
|
|
|
|
|
2011-05-07 21:07:53 +00:00
|
|
|
/*@
|
|
|
|
triggerEvent <function> Triggers an event
|
|
|
|
(event) -> <object> This element object
|
|
|
|
(event, data) -> <object> This element object
|
|
|
|
({event: data, ...}) -> <object> This element object
|
|
|
|
event <string> Event name
|
|
|
|
data <object> Event data (key/value pairs)
|
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
that.triggerEvent = function() {
|
2011-04-29 22:07:23 +00:00
|
|
|
Ox.forEach(Ox.makeObject(arguments), function(data, event) {
|
|
|
|
if ([
|
|
|
|
'mousedown', 'mouserepeat', 'anyclick', 'singleclick', 'doubleclick',
|
|
|
|
'dragstart', 'drag', 'dragpause', 'dragend', 'playing'
|
|
|
|
].indexOf(event) == -1) {
|
|
|
|
Ox.print(that.id, self.options.id, 'trigger', event, data);
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
2011-04-29 22:07:23 +00:00
|
|
|
self.$eventHandler.trigger('ox_' + event, data);
|
|
|
|
});
|
2011-04-22 22:03:10 +00:00
|
|
|
return that;
|
|
|
|
};
|
|
|
|
|
2011-05-07 21:07:53 +00:00
|
|
|
/*@
|
|
|
|
unbindEvent <function> Unbinds all callbacks from an event
|
|
|
|
To unbind a specific handler, use namespaced events, like
|
|
|
|
<code>bindEvent('click.foo', callback)</code>, and then
|
|
|
|
<code>unbindEvent('click.foo')</code>.
|
|
|
|
() -> <object> This element object
|
|
|
|
Unbinds all events
|
|
|
|
(event) -> <object> This element object
|
|
|
|
Unbinds one event
|
|
|
|
(event, event, ...) -> <object> This element object
|
|
|
|
Unbinds multiple events
|
|
|
|
([event, event, ...]) -> <object> This element object
|
|
|
|
Unbinds multiple events
|
|
|
|
event <string> Event name
|
|
|
|
@*/
|
2011-04-22 22:03:10 +00:00
|
|
|
that.unbindEvent = function() {
|
2011-04-29 22:07:23 +00:00
|
|
|
if (arguments.length == 0) {
|
|
|
|
self.$eventHandler.unbind();
|
2011-04-22 22:03:10 +00:00
|
|
|
} else {
|
2011-04-29 22:07:23 +00:00
|
|
|
Ox.makeArray(arguments).forEach(function(event) {
|
|
|
|
self.$eventHandler.unbind('ox_' + event);
|
|
|
|
});
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
|
|
|
return that;
|
|
|
|
};
|
|
|
|
|
|
|
|
return that;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}();
|