2011-11-05 16:46:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2011-05-05 18:02:56 +00:00
|
|
|
/*@
|
2012-06-02 12:10:47 +00:00
|
|
|
Ox.Element <f> Basic UI element object
|
2011-05-05 18:02:56 +00:00
|
|
|
# Arguments ----------------------------------------------------------------
|
2012-06-02 12:10:47 +00:00
|
|
|
options <o|s> Options of the element, or just the `element` option
|
|
|
|
element <s> Tagname or CSS selector
|
|
|
|
tooltip <s|f> Tooltip title, or a function that returns one
|
|
|
|
(e) -> <s> Tooltip title
|
|
|
|
e <o> Mouse event
|
|
|
|
self <o> Shared private variable
|
|
|
|
# Usage --------------------------------------------------------------------
|
|
|
|
([options[, self]]) -> <o:Ox.JQueryElement> Element object
|
|
|
|
# Events ---------------------------------------------------------------
|
|
|
|
anyclick <!> anyclick
|
|
|
|
Fires on mouseup, but not on any subsequent mouseup within 250 ms
|
|
|
|
(this is useful if one wants to listen for singleclicks, but not
|
|
|
|
doubleclicks, since it will fire immediately, and won't fire again
|
|
|
|
in case of a doubleclick)
|
|
|
|
* <*> Original event properties
|
|
|
|
doubleclick <!> doubleclick
|
|
|
|
Fires on the second mousedown within 250 ms (this is useful if one
|
|
|
|
wants to listen for both singleclicks and doubleclicks, since it
|
|
|
|
will not trigger a singleclick event)
|
|
|
|
* <*> Original event properties
|
|
|
|
drag <!> drag
|
|
|
|
Fires on mousemove after dragstart, stops firing on mouseup
|
|
|
|
clientDX <n> Horizontal drag delta in px
|
|
|
|
clientDY <n> Vertical drag delta in px
|
|
|
|
* <*> Original event properties
|
|
|
|
dragend <!> dragpause
|
|
|
|
Fires on mouseup after dragstart
|
|
|
|
clientDX <n> Horizontal drag delta in px
|
|
|
|
clientDY <n> Vertical drag delta in px
|
|
|
|
* <*> Original event properties
|
|
|
|
dragenter <!> dragenter
|
|
|
|
Fires when entering an element during drag (this fires on the
|
|
|
|
element being dragged -- the target element is the event's target
|
|
|
|
property)
|
|
|
|
clientDX <n> Horizontal drag delta in px
|
|
|
|
clientDY <n> Vertical drag delta in px
|
|
|
|
* <*> Original event properties
|
|
|
|
dragleave <!> dragleave
|
|
|
|
Fires when leaving an element during drag (this fires on the element
|
|
|
|
being dragged -- the target element is the event's target property)
|
|
|
|
clientDX <n> Horizontal drag delta in px
|
|
|
|
clientDY <n> Vertical drag delta in px
|
|
|
|
* <*> Original event properties
|
|
|
|
dragpause <!> dragpause
|
|
|
|
Fires once when the mouse doesn't move for 250 ms during drag (this
|
|
|
|
is useful in order to execute operations that are too expensive to
|
|
|
|
be attached to the drag event)
|
|
|
|
clientDX <n> Horizontal drag delta in px
|
|
|
|
clientDY <n> Vertical drag delta in px
|
|
|
|
* <*> Original event properties
|
2013-11-29 20:07:37 +00:00
|
|
|
dragstart <!> dragstart
|
|
|
|
Fires when the mouse is down for 250 ms
|
|
|
|
* <*> Original event properties
|
2012-06-02 12:10:47 +00:00
|
|
|
mousedown <!> mousedown
|
|
|
|
Fires on mousedown (this is useful if one wants to listen for
|
|
|
|
singleclicks, but not doubleclicks or drag events, and wants the
|
|
|
|
event to fire as early as possible)
|
|
|
|
* <*> Original event properties
|
|
|
|
mouserepeat <!> mouserepeat
|
|
|
|
Fires every 50 ms after the mouse was down for 250 ms, stops firing
|
|
|
|
on mouseleave or mouseup (this fires like a key that is being
|
|
|
|
pressed and held, and is useful for buttons like scrollbars arrows
|
|
|
|
that need to react to both clicking and holding)
|
2013-12-04 22:44:07 +00:00
|
|
|
mousewheel <!> mousewheel
|
|
|
|
Fires on mousewheel scroll or trackpad swipe
|
|
|
|
deltaFactor <n> Original delta = normalized delta * delta factor
|
|
|
|
deltaX <n> Normalized horizontal scroll delta in px
|
2013-12-04 23:14:10 +00:00
|
|
|
deltaY <n> Normalized vertical scroll delta in px
|
2013-12-04 22:44:07 +00:00
|
|
|
* <*> Original event properties
|
2012-06-02 12:10:47 +00:00
|
|
|
singleclick <!> singleclick
|
|
|
|
Fires 250 ms after mouseup, if there was no subsequent mousedown
|
|
|
|
(this is useful if one wants to listen for both singleclicks and
|
|
|
|
doubleclicks, since it will not fire for doubleclicks)
|
|
|
|
* <*> Original event properties
|
|
|
|
*/
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-06-19 17:48:32 +00:00
|
|
|
Ox.Element = function(options, self) {
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-10-01 02:21:55 +00:00
|
|
|
// create private object
|
|
|
|
self = self || {};
|
|
|
|
// create defaults and options objects
|
|
|
|
self.defaults = {};
|
2012-06-26 12:54:30 +00:00
|
|
|
// allow for Ox.TestElement('<tagname>') or Ox.TestElement('cssSelector')
|
|
|
|
self.options = Ox.isString(options) ? {element: options} : options || {};
|
2012-06-26 13:12:09 +00:00
|
|
|
// stack of callbacks bound to option updates
|
2012-06-26 12:54:30 +00:00
|
|
|
self.updateCallbacks = self.updateCallbacks || [];
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-10-01 02:21:55 +00:00
|
|
|
// create public object
|
2012-05-28 14:02:25 +00:00
|
|
|
var that = new Ox.JQueryElement($(self.options.element || '<div>'))
|
2012-06-26 12:54:30 +00:00
|
|
|
.addClass('OxElement')
|
2013-12-06 19:56:33 +00:00
|
|
|
.on(Ox.extend({
|
2013-12-04 22:44:07 +00:00
|
|
|
mousedown: mousedown,
|
|
|
|
mousewheel: mousewheel
|
2013-12-06 19:56:33 +00:00
|
|
|
}, self.options.element == '<iframe>' ? {
|
|
|
|
load: function() {
|
|
|
|
Ox.Message.post(that, 'init', {id: that.oxid});
|
|
|
|
}
|
|
|
|
} : {}));
|
2013-02-24 10:57:37 +00:00
|
|
|
|
2011-11-04 22:14:30 +00:00
|
|
|
setTooltip();
|
2011-04-29 22:07:23 +00:00
|
|
|
|
2011-10-01 02:21:55 +00:00
|
|
|
function mousedown(e) {
|
|
|
|
/*
|
|
|
|
better mouse events
|
|
|
|
mousedown:
|
|
|
|
trigger mousedown
|
|
|
|
within 250 msec:
|
|
|
|
mouseup: trigger anyclick
|
|
|
|
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 for 250 msec:
|
|
|
|
trigger dragpause
|
|
|
|
mouseup: trigger dragend
|
|
|
|
"anyclick" is not called "click" since this would collide with the click
|
|
|
|
events of some widgets
|
|
|
|
*/
|
|
|
|
var clientX, clientY,
|
|
|
|
dragTimeout = 0,
|
|
|
|
mouseInterval = 0;
|
2013-12-01 14:41:51 +00:00
|
|
|
that.triggerEvent('mousedown', e);
|
2011-10-06 04:06:49 +00:00
|
|
|
if (!self._mouseTimeout) {
|
2011-10-01 02:21:55 +00:00
|
|
|
// first mousedown
|
2011-10-06 04:06:49 +00:00
|
|
|
self._drag = false;
|
|
|
|
self._mouseup = false;
|
|
|
|
self._mouseTimeout = setTimeout(function() {
|
2011-10-01 02:21:55 +00:00
|
|
|
// 250 ms later, no subsequent click
|
2011-10-06 04:06:49 +00:00
|
|
|
self._mouseTimeout = 0;
|
|
|
|
if (self._mouseup) {
|
2011-10-01 02:21:55 +00:00
|
|
|
// mouse went up, trigger singleclick
|
|
|
|
that.triggerEvent('singleclick', e);
|
|
|
|
} else {
|
|
|
|
// mouse is still down, trigger mouserepeat
|
|
|
|
// every 50 ms until mouseleave or mouseup
|
|
|
|
mouserepeat();
|
|
|
|
mouseInterval = setInterval(mouserepeat, 50);
|
|
|
|
that.one('mouseleave', function() {
|
|
|
|
clearInterval(mouseInterval);
|
|
|
|
});
|
|
|
|
// trigger dragstart, set up drag events
|
|
|
|
that.triggerEvent('dragstart', e);
|
2013-07-15 11:32:28 +00:00
|
|
|
$('.OxElement').live({
|
2011-10-01 02:21:55 +00:00
|
|
|
mouseenter: dragenter,
|
|
|
|
mouseleave: dragleave
|
|
|
|
});
|
|
|
|
clientX = e.clientX;
|
|
|
|
clientY = e.clientY;
|
|
|
|
Ox.UI.$window
|
2012-05-28 14:02:25 +00:00
|
|
|
.off('mouseup', mouseup)
|
2013-12-06 20:43:00 +00:00
|
|
|
.on({mousemove: mousemove})
|
2011-10-01 02:21:55 +00:00
|
|
|
.one('mouseup', function(e) {
|
|
|
|
// stop checking for mouserepeat
|
2011-04-22 22:03:10 +00:00
|
|
|
clearInterval(mouseInterval);
|
2011-10-01 02:21:55 +00:00
|
|
|
// stop checking for dragpause
|
|
|
|
clearTimeout(dragTimeout);
|
|
|
|
// stop checking for drag
|
2012-06-26 14:33:26 +00:00
|
|
|
Ox.UI.$window.off({mousemove: mousemove});
|
2011-10-01 02:21:55 +00:00
|
|
|
// stop checking for dragenter and dragleave
|
2012-06-16 16:10:19 +00:00
|
|
|
$('.OxElement').off({
|
2011-10-01 02:21:55 +00:00
|
|
|
mouseenter: dragenter,
|
|
|
|
mouseleave: dragleave
|
|
|
|
});
|
|
|
|
// trigger dragend
|
|
|
|
that.triggerEvent('dragend', extend(e));
|
2011-04-22 22:03:10 +00:00
|
|
|
});
|
2011-10-06 04:06:49 +00:00
|
|
|
self._drag = true;
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
2011-10-01 02:21:55 +00:00
|
|
|
}, 250);
|
|
|
|
} else {
|
|
|
|
// second mousedown within 250 ms, trigger doubleclick
|
2011-10-06 04:06:49 +00:00
|
|
|
clearTimeout(self._mouseTimeout);
|
|
|
|
self._mouseTimeout = 0;
|
2011-10-01 02:21:55 +00:00
|
|
|
that.triggerEvent('doubleclick', e);
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
2012-06-26 12:54:30 +00:00
|
|
|
Ox.UI.$window.one({mouseup: mouseup});
|
2011-10-01 02:21:55 +00:00
|
|
|
function dragenter(e) {
|
|
|
|
that.triggerEvent('dragenter', extend(e));
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
2011-10-01 02:21:55 +00:00
|
|
|
function dragleave(e) {
|
|
|
|
that.triggerEvent('dragleave', extend(e));
|
|
|
|
}
|
|
|
|
function extend(e) {
|
|
|
|
return Ox.extend({
|
|
|
|
clientDX: e.clientX - clientX,
|
|
|
|
clientDY: e.clientY - clientY
|
|
|
|
}, e);
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
|
|
|
function mousemove(e) {
|
2011-10-01 02:21:55 +00:00
|
|
|
e = extend(e);
|
2011-11-03 15:42:41 +00:00
|
|
|
clearTimeout(dragTimeout);
|
2011-10-01 02:21:55 +00:00
|
|
|
dragTimeout = setTimeout(function() {
|
|
|
|
// mouse did not move for 250 ms, trigger dragpause
|
|
|
|
that.triggerEvent('dragpause', e);
|
|
|
|
}, 250);
|
|
|
|
that.triggerEvent('drag', e);
|
|
|
|
}
|
|
|
|
function mouserepeat(e) {
|
|
|
|
that.triggerEvent('mouserepeat', e);
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
2011-10-01 02:21:55 +00:00
|
|
|
function mouseup(e) {
|
2011-10-06 04:06:49 +00:00
|
|
|
if (!self._mouseup && !self._drag) {
|
2011-10-01 02:21:55 +00:00
|
|
|
// mouse went up for the first time, trigger anyclick
|
|
|
|
that.triggerEvent('anyclick', e);
|
2011-10-06 04:06:49 +00:00
|
|
|
self._mouseup = true;
|
2011-10-01 02:21:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-10-01 02:21:55 +00:00
|
|
|
function mouseenter(e) {
|
|
|
|
that.$tooltip.show(e);
|
|
|
|
}
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-10-01 02:21:55 +00:00
|
|
|
function mouseleave(e) {
|
|
|
|
that.$tooltip.hide();
|
|
|
|
}
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-10-01 02:21:55 +00:00
|
|
|
function mousemove(e) {
|
2012-06-26 12:54:30 +00:00
|
|
|
that.$tooltip.options({title: self.options.tooltip(e)}).show(e);
|
2011-10-01 02:21:55 +00:00
|
|
|
}
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2013-12-04 22:44:07 +00:00
|
|
|
function mousewheel(e) {
|
|
|
|
// see https://github.com/brandonaaron/jquery-mousewheel/blob/master/jquery.mousewheel.js
|
|
|
|
e = e.originalEvent;
|
|
|
|
var absDelta,
|
|
|
|
deltaX = 'deltaX' in e ? e.deltaX
|
|
|
|
: 'wheelDeltaX' in e ? -e.wheelDeltaX
|
|
|
|
: 0,
|
|
|
|
deltaY = 'deltaY' in e ? -e.deltaY
|
|
|
|
: 'wheelDeltaY' in e ? e.wheelDeltaY
|
|
|
|
: 'wheelDelta' in e ? e.wheelDelta
|
|
|
|
: 0;
|
|
|
|
// Firefox < 17
|
|
|
|
if ('axis' in e && e.axis === e.HORIZONTAL_AXIS) {
|
|
|
|
deltaX = -deltaY;
|
|
|
|
deltaY = 0;
|
|
|
|
}
|
|
|
|
if (deltaX || deltaY) {
|
|
|
|
absDelta = Math.max(Math.abs(deltaY), Math.abs(deltaX));
|
|
|
|
if (!self._deltaFactor || self._deltaFactor > absDelta) {
|
|
|
|
self._deltaFactor = absDelta;
|
|
|
|
}
|
|
|
|
that.triggerEvent('mousewheel', Ox.extend(e, {
|
|
|
|
deltaFactor: self._deltaFactor,
|
|
|
|
deltaX: Ox.trunc(deltaX / self._deltaFactor),
|
|
|
|
deltaY: Ox.trunc(deltaY / self._deltaFactor)
|
|
|
|
}));
|
|
|
|
clearTimeout(self._deltaTimeout)
|
|
|
|
self._deltaTimeout = setTimeout(function() {
|
|
|
|
self._deltaFactor = null;
|
|
|
|
}, 200);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-26 12:54:30 +00:00
|
|
|
// TODO: in other widgets, use this,
|
2011-11-04 22:14:30 +00:00
|
|
|
// rather than some self.$tooltip that
|
|
|
|
// will not get garbage collected
|
|
|
|
function setTooltip() {
|
|
|
|
if (self.options.tooltip) {
|
|
|
|
if (Ox.isString(self.options.tooltip)) {
|
2012-06-26 12:54:30 +00:00
|
|
|
that.$tooltip = Ox.Tooltip({title: self.options.tooltip});
|
|
|
|
that.on({mouseenter: mouseenter}).off({mousemove: mousemove});
|
2011-11-04 22:14:30 +00:00
|
|
|
} else {
|
2012-06-26 12:54:30 +00:00
|
|
|
that.$tooltip = Ox.Tooltip({animate: false});
|
|
|
|
that.on({mousemove: mousemove}).off({mouseenter: mouseenter});
|
2011-11-04 22:14:30 +00:00
|
|
|
}
|
2012-06-26 12:54:30 +00:00
|
|
|
that.on({mouseleave: mouseleave});
|
2011-11-04 22:14:30 +00:00
|
|
|
} else {
|
2011-11-05 14:29:31 +00:00
|
|
|
if (that.$tooltip) {
|
|
|
|
that.$tooltip.remove();
|
2012-05-28 14:02:25 +00:00
|
|
|
that.off({
|
2011-11-05 14:29:31 +00:00
|
|
|
mouseenter: mouseenter,
|
|
|
|
mousemove: mousemove,
|
|
|
|
mouseleave: mouseleave
|
|
|
|
});
|
|
|
|
}
|
2011-11-04 22:14:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-28 16:16:23 +00:00
|
|
|
function update(key, value) {
|
|
|
|
// update is called whenever an option is modified or added
|
|
|
|
Ox.loop(self.updateCallbacks.length - 1, -1, -1, function(i) {
|
2012-07-05 08:58:08 +00:00
|
|
|
// break if the callback returns false
|
|
|
|
return self.updateCallbacks[i](key, value) !== false;
|
2012-05-28 16:16:23 +00:00
|
|
|
});
|
|
|
|
}
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-10-01 02:21:55 +00:00
|
|
|
/*@
|
2012-06-30 10:29:06 +00:00
|
|
|
bindEvent <f> Adds event handler(s)
|
|
|
|
(callback) -> <o> This element
|
|
|
|
Adds a catch-all handler
|
|
|
|
(event, callback) -> <o> This element
|
|
|
|
Adds a handler for a single event
|
2011-10-01 02:21:55 +00:00
|
|
|
({event: callback, ...}) -> <o> This element
|
2012-06-30 10:29:06 +00:00
|
|
|
Adds handlers for multiple events
|
2011-10-01 02:21:55 +00:00
|
|
|
callback <f> Callback function
|
|
|
|
data <o> event data (key/value pairs)
|
|
|
|
event <s> Event name
|
2012-06-02 11:06:44 +00:00
|
|
|
Event names can be namespaced, like `'click.foo'`
|
2011-10-01 02:21:55 +00:00
|
|
|
@*/
|
|
|
|
that.bindEvent = function() {
|
2012-06-30 09:24:38 +00:00
|
|
|
Ox.Event.bind.apply(null, [self].concat(Ox.slice(arguments)));
|
2011-10-01 02:21:55 +00:00
|
|
|
return that;
|
2012-01-19 12:29:29 +00:00
|
|
|
};
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-10-01 02:21:55 +00:00
|
|
|
/*@
|
2012-06-30 10:29:06 +00:00
|
|
|
bindEventOnce <f> Adds event handler(s) that run(s) only once
|
|
|
|
(callback) -> <o> This element
|
|
|
|
Adds a catch-all handler
|
|
|
|
(event, callback) -> <o> This element
|
|
|
|
Adds a handler for a single event
|
|
|
|
({event: callback, ...}) -> <o> This element
|
|
|
|
Adds handlers for multiple events
|
2011-10-01 02:21:55 +00:00
|
|
|
callback <f> Callback function
|
|
|
|
data <o> event data (key/value pairs)
|
|
|
|
event <s> Event name
|
2012-06-02 11:06:44 +00:00
|
|
|
Event names can be namespaced, like `'click.foo'`
|
2011-10-01 02:21:55 +00:00
|
|
|
@*/
|
|
|
|
that.bindEventOnce = function() {
|
2012-06-30 09:24:38 +00:00
|
|
|
Ox.Event.bindOnce.apply(null, [self].concat(Ox.slice(arguments)));
|
2011-10-01 02:21:55 +00:00
|
|
|
return that;
|
|
|
|
};
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2012-05-21 10:38:18 +00:00
|
|
|
/*@
|
|
|
|
bindKeyboard <f> bind keyboard
|
|
|
|
() -> <o> object
|
|
|
|
@*/
|
2011-11-01 11:49:46 +00:00
|
|
|
that.bindKeyboard = function() {
|
2012-05-22 14:08:09 +00:00
|
|
|
Ox.Keyboard.bind(that.oxid);
|
2011-11-01 11:49:46 +00:00
|
|
|
return that;
|
|
|
|
};
|
|
|
|
|
2013-12-06 19:51:59 +00:00
|
|
|
that.childrenElements = function() {
|
|
|
|
return that.children().filter(Ox.UI.isOxElement).map(Ox.UI.getOxElement);
|
|
|
|
};
|
|
|
|
|
2011-10-01 02:21:55 +00:00
|
|
|
/*@
|
2011-12-21 13:42:47 +00:00
|
|
|
defaults <function> Gets or sets the default options for an element object
|
2012-06-22 23:54:33 +00:00
|
|
|
({key: value, ...}) -> <obj> This element object
|
|
|
|
key <str> The name of the default option
|
|
|
|
value <*> The value of the default option
|
2011-10-01 02:21:55 +00:00
|
|
|
@*/
|
2011-12-21 13:42:47 +00:00
|
|
|
that.defaults = function() {
|
|
|
|
var ret;
|
|
|
|
if (arguments.length == 0) {
|
|
|
|
ret = self.defaults;
|
|
|
|
} else if (Ox.isString(arguments[0])) {
|
|
|
|
ret = self.defaults[arguments[0]];
|
|
|
|
} else {
|
|
|
|
self.defaults = arguments[0];
|
|
|
|
self.options = Ox.clone(self.defaults);
|
|
|
|
ret = that;
|
|
|
|
}
|
|
|
|
return ret;
|
2011-10-01 02:21:55 +00:00
|
|
|
};
|
2011-05-05 18:02:56 +00:00
|
|
|
|
2013-12-06 19:51:59 +00:00
|
|
|
that.findElements = function() {
|
|
|
|
return Ox.map(that.find('.OxElement'), Ox.UI.getOxElement);
|
|
|
|
};
|
|
|
|
|
2011-10-01 02:21:55 +00:00
|
|
|
/*@
|
|
|
|
gainFocus <function> Makes an element object gain focus
|
|
|
|
() -> <obj> This element object
|
|
|
|
@*/
|
|
|
|
that.gainFocus = function() {
|
2012-05-22 14:08:09 +00:00
|
|
|
Ox.Focus.focus(that.oxid);
|
2011-10-01 02:21:55 +00:00
|
|
|
return that;
|
|
|
|
};
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-10-01 02:21:55 +00:00
|
|
|
/*@
|
|
|
|
hasFocus <function> Returns true if an element object has focus
|
|
|
|
() -> <boolean> True if the element has focus
|
|
|
|
@*/
|
|
|
|
that.hasFocus = function() {
|
2012-05-22 14:08:09 +00:00
|
|
|
return Ox.Focus.focused() == that.oxid;
|
2011-10-01 02:21:55 +00:00
|
|
|
};
|
2011-05-05 18:02:56 +00:00
|
|
|
|
2011-10-01 02:21:55 +00:00
|
|
|
/*@
|
|
|
|
loseFocus <function> Makes an element object lose focus
|
|
|
|
() -> <object> This element object
|
|
|
|
@*/
|
|
|
|
that.loseFocus = function() {
|
2012-05-22 14:08:09 +00:00
|
|
|
Ox.Focus.blur(that.oxid);
|
2011-10-01 02:21:55 +00:00
|
|
|
return that;
|
|
|
|
};
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2013-12-06 19:51:59 +00:00
|
|
|
that.nextElement = function() {
|
|
|
|
return that.nextElements()[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
that.nextElements = function() {
|
|
|
|
return that.nextAll().filter(Ox.UI.isOxElement).map(Ox.UI.getOxElement);
|
|
|
|
};
|
|
|
|
|
2013-02-24 10:57:37 +00:00
|
|
|
/*@
|
|
|
|
onMessage <f> Adds message handlers (if the element is an iframe)
|
|
|
|
(callback) -> <o> This element
|
|
|
|
Adds a catch-all handler
|
|
|
|
(event, callback) -> <o> This element
|
|
|
|
Adds a handler for a single event
|
|
|
|
({event: callback, ...}) -> <o> This element
|
|
|
|
Adds handlers for multiple events
|
|
|
|
callback <f> Callback function
|
|
|
|
data <o> event data (key/value pairs)
|
|
|
|
event <s> Event name
|
|
|
|
@*/
|
|
|
|
that.onMessage = function() {
|
|
|
|
// FIXME: Implement catch-all handler
|
|
|
|
var callback;
|
|
|
|
if (self.options.element == '<iframe>') {
|
|
|
|
if (Ox.isObject(arguments[0])) {
|
|
|
|
Ox.forEach(arguments[0], function(callback, event) {
|
|
|
|
Ox.Message.bind(arguments[0], function(event_, data, oxid) {
|
|
|
|
if (event_ == event && oxid == that.oxid) {
|
2013-02-25 07:14:22 +00:00
|
|
|
callback(data || {});
|
2013-02-24 10:57:37 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback = arguments[0];
|
|
|
|
Ox.Message.bind(function(event, data, oxid) {
|
|
|
|
if (that.oxid == oxid) {
|
2013-02-25 07:14:22 +00:00
|
|
|
callback(event, data || {});
|
2013-02-24 10:57:37 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return that;
|
|
|
|
};
|
|
|
|
|
2011-10-01 02:21:55 +00:00
|
|
|
/*@
|
2012-06-02 10:37:19 +00:00
|
|
|
options <f> Gets or sets the options of an element object
|
2012-07-02 11:28:42 +00:00
|
|
|
() -> <o> All options
|
|
|
|
(key) -> <*> The value of option[key]
|
|
|
|
(key, value) -> <o> This element
|
2012-05-28 19:35:41 +00:00
|
|
|
Sets options[key] to value and calls update(key, value)
|
2011-10-01 02:21:55 +00:00
|
|
|
if the key/value pair was added or modified
|
2012-07-02 11:28:42 +00:00
|
|
|
({key: value, ...}) -> <o> This element
|
2012-05-28 19:35:41 +00:00
|
|
|
Sets multiple options and calls update(key, value)
|
2011-10-01 02:21:55 +00:00
|
|
|
for every key/value pair that was added or modified
|
2012-07-02 11:28:42 +00:00
|
|
|
key <s> The name of the option
|
|
|
|
value <*> The value of the option
|
2011-10-01 02:21:55 +00:00
|
|
|
@*/
|
|
|
|
that.options = function() {
|
2012-05-28 16:16:23 +00:00
|
|
|
return Ox.getset(self.options, arguments, update, that);
|
2011-10-01 02:21:55 +00:00
|
|
|
};
|
|
|
|
|
2013-12-06 19:51:59 +00:00
|
|
|
that.parentElement = function() {
|
|
|
|
return Ox.last(that.parentElements());
|
|
|
|
};
|
|
|
|
|
|
|
|
that.parentElements = function() {
|
|
|
|
return that.parents().filter(Ox.UI.isOxElement).map(Ox.UI.getOxElement);
|
|
|
|
};
|
|
|
|
|
2013-02-24 10:57:37 +00:00
|
|
|
/*@
|
|
|
|
postMessage <f> Sends a message (if the element is an iframe)
|
|
|
|
(event, data) -> This element
|
|
|
|
event <s> Event name
|
|
|
|
data <o> Event data
|
|
|
|
@*/
|
|
|
|
that.postMessage = function(event, data) {
|
|
|
|
if (self.options.element == '<iframe>') {
|
|
|
|
Ox.Message.post(that, event, data);
|
|
|
|
}
|
2013-12-03 17:14:11 +00:00
|
|
|
return that;
|
2013-02-24 10:57:37 +00:00
|
|
|
};
|
|
|
|
|
2013-12-06 19:51:59 +00:00
|
|
|
that.prevElement = function() {
|
|
|
|
return Ox.last(that.prevElements());
|
|
|
|
};
|
|
|
|
|
|
|
|
that.prevElements = function() {
|
|
|
|
return that.prevAll().filter(Ox.UI.isOxElement).map(Ox.UI.getOxElement);
|
|
|
|
};
|
|
|
|
|
2011-10-01 02:21:55 +00:00
|
|
|
/*@
|
2013-12-06 19:51:59 +00:00
|
|
|
remove <f> Removes an element object and its event handler
|
2012-06-02 10:37:19 +00:00
|
|
|
() -> <o> This element
|
2011-10-01 02:21:55 +00:00
|
|
|
@*/
|
2011-11-04 22:14:30 +00:00
|
|
|
that.remove = function(remove) {
|
2012-05-22 13:14:40 +00:00
|
|
|
remove !== false && that.find('.OxElement').each(function() {
|
2012-06-30 09:24:38 +00:00
|
|
|
var element = Ox.UI.elements[$(this).data('oxid')];
|
2011-11-04 22:14:30 +00:00
|
|
|
element && element.remove(false);
|
2011-11-01 19:56:11 +00:00
|
|
|
});
|
2012-05-22 14:08:09 +00:00
|
|
|
Ox.Focus.remove(that.oxid);
|
|
|
|
Ox.Keyboard.unbind(that.oxid);
|
|
|
|
delete Ox.UI.elements[that.oxid];
|
2011-11-01 23:14:29 +00:00
|
|
|
that.$tooltip && that.$tooltip.remove();
|
|
|
|
remove !== false && that.$element.remove();
|
2011-10-01 02:21:55 +00:00
|
|
|
return that;
|
|
|
|
};
|
|
|
|
|
2013-12-06 19:51:59 +00:00
|
|
|
/*
|
|
|
|
that.remove = function() {
|
|
|
|
[that].concat(that.find('.OxElement'))
|
|
|
|
.map(Ox.UI.getOxElement).forEach(function($element) {
|
|
|
|
$element.removeElement();
|
|
|
|
});
|
|
|
|
that[0].parentNode.removeChild(this[0]);
|
|
|
|
return that;
|
|
|
|
}
|
|
|
|
|
|
|
|
that.removeElement = function() {
|
|
|
|
delete Ox.$elements[that.oxid];
|
|
|
|
Ox.Focus.remove(that);
|
|
|
|
self.unbindKeyboard();
|
|
|
|
return that;
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
2012-05-21 10:38:18 +00:00
|
|
|
/*@
|
|
|
|
setElement <f> set $element
|
2012-06-02 10:00:52 +00:00
|
|
|
($element) -> <o> This element
|
2012-05-21 10:38:18 +00:00
|
|
|
@*/
|
2011-12-21 13:42:47 +00:00
|
|
|
that.setElement = function($element) {
|
2012-05-22 14:08:09 +00:00
|
|
|
$element.addClass('OxElement').data({oxid: that.oxid});
|
2011-12-21 13:42:47 +00:00
|
|
|
that.$element.replaceWith($element);
|
|
|
|
that.$element = $element;
|
2012-06-26 16:01:13 +00:00
|
|
|
that[0] = $element[0];
|
2012-06-02 10:00:52 +00:00
|
|
|
return that;
|
2011-12-21 13:42:47 +00:00
|
|
|
};
|
|
|
|
|
2012-05-21 10:38:18 +00:00
|
|
|
/*@
|
2012-06-02 10:00:52 +00:00
|
|
|
toggleOption <f> Toggle boolean option(s)
|
|
|
|
(key[, key[, ...]]) -> <o> This element
|
2012-05-21 10:38:18 +00:00
|
|
|
@*/
|
2011-12-21 13:42:47 +00:00
|
|
|
that.toggleOption = function() {
|
|
|
|
var options = {};
|
2013-12-01 13:57:52 +00:00
|
|
|
Ox.slice(arguments).forEach(function(key) {
|
2011-12-21 13:42:47 +00:00
|
|
|
options[key] == !self.options[key];
|
|
|
|
});
|
|
|
|
that.options(options);
|
2012-06-02 10:00:52 +00:00
|
|
|
return that;
|
2011-12-21 13:42:47 +00:00
|
|
|
};
|
|
|
|
|
2011-10-01 02:21:55 +00:00
|
|
|
/*@
|
2012-06-30 10:29:06 +00:00
|
|
|
triggerEvent <f> Triggers one or more events
|
|
|
|
(event) -> <o> This element object
|
|
|
|
Triggers an event
|
|
|
|
(event, data) -> <o> This element object
|
|
|
|
Triggers an event with data
|
|
|
|
({event: data, ...}) -> <o> This element object
|
|
|
|
Triggers multiple events with data
|
2011-10-01 02:21:55 +00:00
|
|
|
event <string> Event name
|
|
|
|
data <object> Event data (key/value pairs)
|
|
|
|
@*/
|
|
|
|
that.triggerEvent = function() {
|
2012-06-30 09:24:38 +00:00
|
|
|
Ox.Event.trigger.apply(that, [self].concat(Ox.slice(arguments)));
|
2011-10-01 02:21:55 +00:00
|
|
|
return that;
|
|
|
|
};
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-10-01 02:21:55 +00:00
|
|
|
/*@
|
2012-06-30 10:29:06 +00:00
|
|
|
unbindEvent <f> Removes event handler(s)
|
|
|
|
() -> <o> This element
|
|
|
|
Removes all handlers.
|
2012-05-28 14:02:25 +00:00
|
|
|
(callback) -> <o> This element
|
2012-06-30 10:29:06 +00:00
|
|
|
Removes a specific catch-all handler
|
2012-05-28 14:02:25 +00:00
|
|
|
(event) -> <o> This element
|
2012-06-30 10:29:06 +00:00
|
|
|
Removes all handlers for a single event (to remove all catch-all
|
|
|
|
handlers, pass '*' as event)
|
2012-05-28 14:02:25 +00:00
|
|
|
(event, callback) -> <o> This element
|
2012-06-30 10:29:06 +00:00
|
|
|
Removes a specific handler for a single event
|
2012-05-28 14:02:25 +00:00
|
|
|
({event: callback}, ...) -> <o> This element
|
2012-06-30 10:29:06 +00:00
|
|
|
Removes specific handlers for multiple events
|
2011-10-01 02:21:55 +00:00
|
|
|
event <string> Event name
|
|
|
|
@*/
|
|
|
|
that.unbindEvent = function() {
|
2012-06-30 09:24:38 +00:00
|
|
|
Ox.Event.unbind.apply(null, [self].concat(Ox.slice(arguments)));
|
2011-04-22 22:03:10 +00:00
|
|
|
return that;
|
2011-10-01 02:21:55 +00:00
|
|
|
};
|
|
|
|
|
2012-05-21 10:38:18 +00:00
|
|
|
/*@
|
|
|
|
unbindKeyboard <f> unbind keyboard
|
|
|
|
() -> <o> object
|
|
|
|
@*/
|
2011-11-01 11:49:46 +00:00
|
|
|
that.unbindKeyboard = function() {
|
2012-05-22 14:08:09 +00:00
|
|
|
Ox.Keyboard.unbind(that.oxid);
|
2011-11-01 11:49:46 +00:00
|
|
|
return that;
|
|
|
|
};
|
|
|
|
|
2012-05-28 16:16:23 +00:00
|
|
|
/*@
|
|
|
|
update <f> Adds one or more handlers for options updates
|
|
|
|
(callback) -> <o> that
|
|
|
|
(key, callback) -> <o> that
|
|
|
|
({key: callback, ...}) -> <o> that
|
|
|
|
@*/
|
|
|
|
that.update = function() {
|
|
|
|
var callbacks;
|
2012-06-25 14:32:16 +00:00
|
|
|
if (Ox.isFunction(arguments[0])) {
|
2012-05-28 16:16:23 +00:00
|
|
|
self.updateCallbacks.push(arguments[0]);
|
|
|
|
} else {
|
|
|
|
callbacks = Ox.makeObject(arguments);
|
|
|
|
self.updateCallbacks.push(function(key) {
|
|
|
|
if (callbacks[key]) {
|
|
|
|
return callbacks[key]();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return that;
|
|
|
|
};
|
|
|
|
|
2011-12-21 15:33:52 +00:00
|
|
|
/*@
|
|
|
|
value <f> Shortcut to get or set self.options.value
|
|
|
|
@*/
|
|
|
|
that.value = function() {
|
|
|
|
return that.options(
|
|
|
|
arguments.length == 0 ? 'value' : {value: arguments[0]}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2012-06-26 12:54:30 +00:00
|
|
|
that.update({tooltip: setTooltip});
|
2012-05-28 16:16:23 +00:00
|
|
|
|
2011-10-01 02:21:55 +00:00
|
|
|
return that;
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-06-19 17:48:32 +00:00
|
|
|
};
|