oxjs/source/UI/js/Core/Element.js

905 lines
34 KiB
JavaScript
Raw Permalink Normal View History

2011-11-05 16:46:53 +00:00
'use strict';
2014-09-22 14:56:54 +00:00
(function(_) {
var noTooltipEvents = {};
var supportsPassive = false;
try {
var opts = Object.defineProperty({}, 'passive', {
get: function() {
supportsPassive = true;
}
});
window.addEventListener("test", null, opts);
} catch (e) {}
2013-02-24 10:57:37 +00:00
2014-09-22 14:56:54 +00:00
/*@
Ox.Element <f> Basic UI element object
# Arguments -----------------------------------------------------------
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 ---------------------------------------------------------------
2014-09-22 15:04:42 +00:00
([options[, self]]) -> <o> Element object
2014-09-22 14:56:54 +00:00
# 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
dragstart <!> dragstart
Fires when the mouse is down for 250 ms
* <*> Original event properties
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
scrollbar arrows that need to react to both clicking and
holding)
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
deltaY <n> Normalized vertical scroll delta in px
* <*> Original event properties
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
2017-11-13 14:53:19 +00:00
touchend <!> touchend
normalized version of touchend event
* <*> Original event properties
2017-11-13 14:53:19 +00:00
touchmove <!> touchmove
normalized version of touchmove event
* <*> Original event properties
touchstart <!> touchstart
normalized version of touchstart event
* <*> Original event properties
2014-09-22 14:56:54 +00:00
*/
2011-04-29 22:07:23 +00:00
2014-09-22 14:56:54 +00:00
Ox.Element = function Element(options, self) {
// create private object
self = self || {};
self.boundTooltipEvents = noTooltipEvents;
2014-09-22 14:56:54 +00:00
self.defaults = {};
2014-09-23 19:16:25 +00:00
self.eventCallbacks = self.eventCallbacks || {};
2014-09-22 14:56:54 +00:00
// allow for Ox.Element('<tagname>') or Ox.Element('cssSelector')
self.options = Ox.isString(options) ? {element: options} : options || {};
2014-09-23 19:16:25 +00:00
self.unbindKeyboard = function unbindKeyboard() {
Object.keys(self.eventCallbacks).filter(function(event) {
return /^key([\._][\w\.]+)?$/.test(event);
}).forEach(function(event) {
that.unbindEvent(event);
});
};
2014-09-22 14:56:54 +00:00
self.update = function update(key, value) {
// update is called whenever an option is modified or added
Ox.loop(self.updateCallbacks.length - 1, -1, -1, function(index) {
// break if the callback returns false
return self.updateCallbacks[index](key, value) !== false;
});
};
2014-09-23 19:16:25 +00:00
self.updateCallbacks = self.updateCallbacks || [];
2014-09-22 14:56:54 +00:00
// create public object
var that = Object.create(Ox.Element.prototype);
that.oxid = Ox.uid();
that.$element = $(self.options.element || '<div>')
.addClass('OxElement')
.data({oxid: that.oxid})
.on({
mousedown: onMousedown,
mousewheel: onMousewheel,
//touchend: onTouchend,
//touchmove: onTouchmove,
//touchstart: onTouchstart
2014-09-22 14:56:54 +00:00
});
that.$element[0].addEventListener('touchend', onTouchend, supportsPassive ? { passive: true } : false );
that.$element[0].addEventListener('touchmove', onTouchmove, supportsPassive ? { passive: true } : false );
that.$element[0].addEventListener('touchstart', onTouchstart, supportsPassive ? { passive: true } : false );
2014-09-22 14:56:54 +00:00
that[0] = that.$element[0];
that.length = 1;
2014-09-23 19:16:25 +00:00
that.self = function _self() {
2014-09-22 15:10:43 +00:00
return arguments[0] === _ ? self : {};
};
2014-09-25 16:47:29 +00:00
Ox.$elements[that.oxid] = that;
2014-02-02 11:05:41 +00:00
2014-09-22 14:56:54 +00:00
if (self.options.element == '<iframe>') {
2014-09-23 19:16:25 +00:00
self.messageCallbacks = self.messageCallbacks || {};
2014-09-22 14:56:54 +00:00
that.on({
2014-09-23 19:16:25 +00:00
load: function init() {
2014-09-23 21:58:53 +00:00
if (that.attr('src')) {
// send oxid to iframe
that.postMessage({init: {oxid: that.oxid}});
self.initTime = self.initTime || +new Date();
if (+new Date() < self.initTime + 60000) {
self.initTimeout = setTimeout(init, 250);
}
2014-09-23 21:58:53 +00:00
}
2014-09-23 19:16:25 +00:00
}
}).bindEvent({
init: function() {
// iframe has received oxid
clearTimeout(self.initTimeout);
2011-04-22 22:03:10 +00:00
}
2014-09-22 14:56:54 +00:00
});
}
2014-09-22 14:56:54 +00:00
var constantTooltipEvents = {
mouseenter: onMouseenter,
mouseleave: onMouseleave
},
dynamicTooltipEvents = {
mousemove: onMousemove,
mouseleave: onMouseleave
};
2014-09-22 14:56:54 +00:00
setTooltip();
function bindTooltipEvents(events) {
if (self.boundTooltipEvents !== events) {
that.off(self.boundTooltipEvents).on(events);
self.boundTooltipEvents = events;
}
2011-04-22 22:03:10 +00:00
}
2014-09-22 14:56:54 +00:00
function getTouchData(e) {
var data = {};
2017-11-13 14:53:19 +00:00
if (e && e.changedTouches && e.changedTouches.length) {
data.clientX = e.changedTouches[0].clientX;
data.clientY = e.changedTouches[0].clientY;
} else {
data.clientX = e.pageX;
data.clientY = e.pageY;
}
return data;
}
2014-09-22 14:56:54 +00:00
function onMousedown(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;
that.triggerEvent('mousedown', e);
if (!self._mouseTimeout) {
// first mousedown
self._drag = false;
self._mouseup = false;
self._mouseTimeout = setTimeout(function() {
// 250 ms later, no subsequent click
self._mouseTimeout = 0;
if (self._mouseup) {
// 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);
$('.OxElement').live({
mouseenter: dragenter,
mouseleave: dragleave
});
clientX = e.clientX;
clientY = e.clientY;
2014-09-23 19:16:25 +00:00
Ox.$window
2014-09-22 14:56:54 +00:00
.off('mouseup', mouseup)
.on({mousemove: mousemove})
.one('mouseup', function(e) {
// stop checking for mouserepeat
clearInterval(mouseInterval);
// stop checking for dragpause
clearTimeout(dragTimeout);
// stop checking for drag
2014-09-23 19:16:25 +00:00
Ox.$window.off({mousemove: mousemove});
2014-09-22 14:56:54 +00:00
// stop checking for dragenter and dragleave
$('.OxElement').off({
mouseenter: dragenter,
mouseleave: dragleave
});
// trigger dragend
that.triggerEvent('dragend', extend(e));
});
self._drag = true;
}
}, 250);
} else {
// second mousedown within 250 ms, trigger doubleclick
clearTimeout(self._mouseTimeout);
self._mouseTimeout = 0;
that.triggerEvent('doubleclick', e);
}
2014-09-23 19:16:25 +00:00
Ox.$window.one({mouseup: mouseup});
2014-09-22 14:56:54 +00:00
function dragenter(e) {
that.triggerEvent('dragenter', extend(e));
}
function dragleave(e) {
that.triggerEvent('dragleave', extend(e));
}
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() {
// 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);
}
function mouseup(e) {
if (!self._mouseup && !self._drag) {
// mouse went up for the first time, trigger anyclick
that.triggerEvent('anyclick', e);
self._mouseup = true;
}
}
}
2011-04-22 22:03:10 +00:00
2014-09-22 14:56:54 +00:00
function onMouseenter(e) {
if (!that.$tooltip) {
that.$tooltip = Ox.Tooltip({title: self.options.tooltip});
}
that.$tooltip.show(e);
2014-02-02 11:05:41 +00:00
}
2011-04-22 22:03:10 +00:00
2014-09-22 14:56:54 +00:00
function onMouseleave(e) {
that.$tooltip && that.$tooltip.hide();
}
2011-04-22 22:03:10 +00:00
2014-09-22 14:56:54 +00:00
function onMousemove(e) {
that.$tooltip.options({title: self.options.tooltip(e)}).show(e);
}
2014-09-22 14:56:54 +00:00
function onMousewheel(e) {
// see https://github.com/brandonaaron/jquery-mousewheel/blob/master/jquery.mousewheel.js
2014-09-22 15:12:17 +00:00
e = e.originalEvent;
2014-09-22 14:56:54 +00:00
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($.event.fix(e), {
2014-09-22 14:56:54 +00:00
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);
}
}
function onTouchend(e) {
//var data = getTouchData(e.originalEvent);
2017-11-13 14:53:19 +00:00
var data = getTouchData(e);
that.triggerEvent('touchend', Ox.extend(e, data));
}
function onTouchmove(e) {
//var data = getTouchData(e.originalEvent);
2017-11-13 14:53:19 +00:00
var data = getTouchData(e);
that.triggerEvent('touchmove', Ox.extend(e, data));
}
function onTouchstart(e) {
//var data = getTouchData(e.originalEvent);
2017-11-13 14:53:19 +00:00
var data = getTouchData(e);
that.triggerEvent('touchstart', Ox.extend(e, data));
}
2014-09-22 14:56:54 +00:00
// TODO: in other widgets, use this,
// rather than some self.$tooltip that
// will not get garbage collected
function setTooltip() {
if (self.options.tooltip) {
if (Ox.isString(self.options.tooltip)) {
bindTooltipEvents(constantTooltipEvents);
2014-09-22 14:56:54 +00:00
that.$tooltip && that.$tooltip.options({
title: self.options.tooltip,
animate: true
});
} else {
that.$tooltip = Ox.Tooltip({animate: false});
bindTooltipEvents(dynamicTooltipEvents);
2014-09-22 14:56:54 +00:00
}
} else {
2014-09-22 14:56:54 +00:00
if (that.$tooltip) {
that.$tooltip.remove();
}
bindTooltipEvents(noTooltipEvents);
}
}
2014-09-22 14:56:54 +00:00
that.update({tooltip: setTooltip});
return that;
};
// add all jQuery methods to the prototype of Ox.Element
Ox.methods($('<div>'), true).forEach(function(method) {
Ox.Element.prototype[method] = function() {
2014-09-23 19:16:25 +00:00
var ret = this.$element[method].apply(this.$element, arguments),
2014-09-22 14:56:54 +00:00
oxid;
2014-09-22 15:38:37 +00:00
// If exactly one $element of an Ox Element was returned, then
// return the Ox Element instead, so that we can do
// oxObj.jqFn().oxFn()
2014-09-23 19:16:25 +00:00
return ret && ret.jquery && ret.length == 1
2014-09-25 16:47:29 +00:00
&& Ox.$elements[oxid = ret.data('oxid')]
? Ox.$elements[oxid] : ret;
2014-09-22 14:56:54 +00:00
};
});
2011-04-22 22:03:10 +00:00
/*@
2012-06-30 10:29:06 +00:00
bindEvent <f> Adds event handler(s)
2014-09-24 10:17:46 +00:00
(callback) -> <o> This element object
2012-06-30 10:29:06 +00:00
Adds a catch-all handler
2014-09-24 10:17:46 +00:00
(event, callback) -> <o> This element object
2012-06-30 10:29:06 +00:00
Adds a handler for a single event
2014-09-24 10:17:46 +00:00
({event: callback, ...}) -> <o> This element object
Adds handlers for one or more events
callback <f> Callback function
data <o> event data (key/value pairs)
event <s> Event name
Event names can be namespaced, like `'click.foo'`
callback <f> Callback function
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.bindEvent = function bindEvent() {
2014-09-23 19:16:25 +00:00
Ox.Event.bind.apply(this, [this.self(_)].concat(Ox.slice(arguments)));
2014-09-22 14:56:54 +00:00
return this;
};
2011-04-22 22:03:10 +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 one or more events
callback <f> Callback function
data <o> event data (key/value pairs)
event <s> Event name
Event names can be namespaced, like `'click.foo'`
callback <f> Callback function
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.bindEventOnce = function bindEventOnce() {
2014-09-23 19:16:25 +00:00
Ox.Event.bindOnce.apply(
this, [this.self(_)].concat(Ox.slice(arguments))
);
2014-09-22 14:56:54 +00:00
return this;
};
2011-04-22 22:03:10 +00:00
/*@
2014-09-24 10:17:46 +00:00
bindMessage <f> Adds message handler(s) (if the element is an iframe)
(callback) -> <o> This element object
Adds a catch-all handler
2014-09-24 10:17:46 +00:00
(message, callback) -> <o> This element object
Adds a handler for a single message
({message: callback, ...}) -> <o> This element object
Adds handlers for one or more messages
message <s> Message name
callback <f> Callback function
2014-09-24 10:17:46 +00:00
data <o> Message data (key/value pairs)
event <s> Event name
element <o> Element object
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.bindMessage = Ox.Element.prototype.onMessage = function bindMessage() {
var self = this.self(_);
if (self.options.element == '<iframe>') {
2014-09-23 19:16:25 +00:00
Ox.Message.bind.apply(this, [self].concat(Ox.slice(arguments)));
}
2014-09-22 14:56:54 +00:00
return this;
};
2014-09-24 10:17:46 +00:00
/*@
bindMessageOnce <f> Adds message handler(s) that run only once
(callback) -> <o> This element object
Adds a catch-all handler
(message, callback) -> <o> This element object
Adds a handler for a single message
({message: callback, ...}) -> <o> This element object
Adds handlers for one or more messages
event <s> Message name
2014-09-24 10:17:46 +00:00
callback <f> Callback function
data <o> Message data (key/value pairs)
event <s> Event name
element <o> Element object
2014-09-24 10:17:46 +00:00
@*/
2014-09-23 19:16:25 +00:00
Ox.Element.prototype.bindMessageOnce = Ox.Element.prototype.onMessageOnce = function bindMessageOnce() {
var self = this.self(_);
if (self.options.element == '<iframe>') {
Ox.Message.bindOnce.apply(
this, [self].concat(Ox.slice(arguments))
);
}
2014-09-22 14:56:54 +00:00
return this;
};
2014-09-24 10:17:46 +00:00
/*@
childrenElements <f> Gets all direct children element objects
() -> <[o]> Array of element objects
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.childrenElements = function childrenElements() {
return Ox.compact(
Ox.slice(this.children())
2014-09-26 12:18:11 +00:00
.filter(Ox.UI.isElement)
.map(Ox.UI.getElement)
);
};
/*@
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
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.defaults = function defaults() {
var self = this.self(_);
2011-12-21 13:42:47 +00:00
var ret;
if (arguments.length == 0) {
ret = self.defaults;
} else if (Ox.isString(arguments[0])) {
ret = self.defaults[arguments[0]];
} else {
2014-09-23 19:16:25 +00:00
self.defaults = Ox.makeObject(arguments);
2011-12-21 13:42:47 +00:00
self.options = Ox.clone(self.defaults);
2014-09-22 14:56:54 +00:00
ret = this;
2011-12-21 13:42:47 +00:00
}
return ret;
};
2011-05-05 18:02:56 +00:00
2014-09-23 19:16:25 +00:00
Ox.Element.prototype.empty = function empty() {
this.childrenElements().forEach(function($element) {
$element.removeElement();
});
this.$element.empty.apply(this, arguments);
return this;
};
2014-09-24 10:17:46 +00:00
/*@
findElements <f> Gets all descendant element objects
() -> <[o]> Array of element objects
@*/
2014-09-23 19:16:25 +00:00
Ox.Element.prototype.findElements = function findElements() {
return Ox.compact(
2014-09-26 12:18:11 +00:00
Ox.slice(this.find('.OxElement')).map(Ox.UI.getElement)
);
};
/*@
gainFocus <function> Makes an element object gain focus
2014-09-24 10:17:46 +00:00
() -> <obj> This element object
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.gainFocus = function gainFocus() {
2014-09-23 19:16:25 +00:00
Ox.Focus.gainFocus(this);
2014-09-22 14:56:54 +00:00
return this;
};
2011-04-22 22:03:10 +00:00
/*@
hasFocus <function> Returns true if an element object has focus
2014-09-24 10:17:46 +00:00
() -> <boolean> True if the element has focus
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.hasFocus = function hasFocus() {
2014-09-23 19:16:25 +00:00
return Ox.Focus.focusedElement() === this;
};
Ox.Element.prototype.html = function html() {
var ret;
this.childrenElements().forEach(function($element) {
$element.removeElement();
});
ret = this.$element.html.apply(this, arguments);
return arguments.length == 0 ? ret : this;
};
2011-05-05 18:02:56 +00:00
/*@
loseFocus <function> Makes an element object lose focus
2014-09-24 10:17:46 +00:00
() -> <object> This element object
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.loseFocus = function loseFocus() {
2014-09-23 19:16:25 +00:00
Ox.Focus.loseFocus(this);
2014-09-22 14:56:54 +00:00
return this;
};
2011-04-22 22:03:10 +00:00
2014-09-24 10:17:46 +00:00
/*@
nextElement <f> Gets the closest following sibling element object
() -> <o> Element object
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.nextElement = function nextElement() {
return this.nextElements()[0];
};
2014-09-24 10:17:46 +00:00
/*@
nextElements <f> Gets all following sibling element objects
() -> <[o]> Array of element objects
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.nextElements = function nextElements() {
return Ox.compact(
2014-09-26 12:18:11 +00:00
this.nextAll().filter(Ox.UI.isElement).map(Ox.UI.getElement)
);
};
/*@
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)
if the key/value pair was added or modified
2012-07-02 11:28:42 +00:00
({key: value, ...}) -> <o> This element
Sets one or more options and calls update(key, value)
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
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.options = function options() {
var self = this.self(_);
return Ox.getset(self.options, arguments, self.update, this);
};
2014-09-24 10:17:46 +00:00
/*@
parentElement <f> Gets the closest parent element object
() -> <o> Element object
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.parentElement = function parentElement() {
return Ox.last(this.parentElements());
};
2014-09-24 10:17:46 +00:00
/*@
parentElements <f> Gets all parent element objects
() -> <[o]> Array of element objects
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.parentElements = function parentElements() {
return Ox.compact(
Ox.slice(this.parents())
2014-09-26 12:18:11 +00:00
.filter(Ox.UI.isElement)
.map(Ox.UI.getElement)
);
};
2013-02-24 10:57:37 +00:00
/*@
postMessage <f> Sends a message (if the element is an iframe)
2014-09-24 10:17:46 +00:00
(event, data) -> This element object
2013-02-24 10:57:37 +00:00
event <s> Event name
data <o> Event data
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.postMessage = function postMessage(event, data) {
2014-09-23 19:16:25 +00:00
if (this.self(_).options.element == '<iframe>') {
Ox.Message.post.apply(this, Ox.slice(arguments));
2013-02-24 10:57:37 +00:00
}
2014-09-22 14:56:54 +00:00
return this;
2013-02-24 10:57:37 +00:00
};
2014-09-24 10:17:46 +00:00
/*@
prevElement <f> Gets the closest preceding sibling element object
() -> <[o]> Array of element objects
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.prevElement = function prevElement() {
return Ox.last(this.prevElements());
};
2014-09-24 10:17:46 +00:00
/*@
prevElements <f> Gets all preceding sibling element objects
() -> <[o]> Array of element objects
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.prevElements = function prevElements() {
return Ox.compact(
2014-09-26 12:18:11 +00:00
this.prevAll().filter(Ox.UI.isElement).map(Ox.UI.getElement)
);
};
2014-09-23 19:16:25 +00:00
Ox.Element.prototype.remove = function remove() {
var parent = this[0].parentNode;
this.removeElement();
parent && parent.removeChild(this[0]);
return this;
};
2014-09-24 10:17:46 +00:00
/*@
removeElement <f> Clean up after removal from DOM
This gets invoked on .remove()
@*/
2014-09-23 19:16:25 +00:00
Ox.Element.prototype.removeElement = function removeElement(includeChildren) {
if (includeChildren !== false) {
this.findElements().forEach(function($element) {
if (!$element) {
Ox.print(
'*** Found undefined descendant element,'
+ ' this should never happen. ***'
);
return;
}
$element.removeElement(false);
2014-09-22 14:56:54 +00:00
});
}
2014-09-23 19:16:25 +00:00
Ox.Focus.removeElement(this.oxid);
this.self(_).unbindKeyboard();
2014-09-22 14:56:54 +00:00
this.$tooltip && this.$tooltip.remove();
2023-08-06 09:49:55 +00:00
jQuery.cleanData(this.$element);
2014-09-25 16:47:29 +00:00
delete Ox.$elements[this.oxid];
2014-09-23 19:16:25 +00:00
// If setElement($element) was used, delete $element too
2014-09-25 16:47:29 +00:00
delete Ox.$elements[this.$element.oxid];
2014-09-22 14:56:54 +00:00
return this;
};
2014-09-23 19:16:25 +00:00
Ox.Element.prototype.replace = function replace() {
arguments[0].removeElement();
this.$element.replace.apply(this.$element, arguments);
return this;
};
2014-09-23 19:16:25 +00:00
Ox.Element.prototype.replaceWith = function replaceWith() {
this.removeElement();
this.$element.replaceWith.apply(this.$element, arguments);
return this;
};
2012-05-21 10:38:18 +00:00
/*@
2014-09-24 10:17:46 +00:00
setElement <f> Sets the element to the element of another element object
This is useful if an element has specific options, but uses another
generic element as its DOM representation
($element) -> <o> This element object
2012-05-21 10:38:18 +00:00
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.setElement = function setElement($element) {
this.findElements().forEach(function($element) {
$element.removeElement(false);
});
2023-08-06 09:49:55 +00:00
jQuery.cleanData(this.$element);
this.$element.replaceWith($element);
if ($element.$element) { // $element is Ox.Element
this.$element = $element.$element;
this.$element.oxid = $element.oxid;
2015-03-24 13:37:23 +00:00
this.$newElement = $element;
} else { // $element is jQuery Element
this.$element = $element;
}
this.$element.addClass('OxElement').data({oxid: this.oxid});
2014-09-22 14:56:54 +00:00
this[0] = $element[0];
return this;
2011-12-21 13:42:47 +00:00
};
2014-09-24 10:17:46 +00:00
/*@
siblingElements <f> Gets all sibling element objects
() -> <[o]> Array of element objects
@*/
2014-09-23 19:16:25 +00:00
Ox.Element.prototype.siblingElements = function siblingElements() {
return Ox.compact(
Ox.slice(this.siblings())
2014-09-26 12:18:11 +00:00
.filter(Ox.UI.isElement)
.map(Ox.UI.getElement)
);
2014-09-23 19:16:25 +00:00
};
Ox.Element.prototype.text = function text() {
var ret;
this.childrenElements().forEach(function($element) {
$element.removeElement();
});
ret = this.$element.text.apply(this, arguments);
return arguments.length == 0 ? ret : this;
};
2012-05-21 10:38:18 +00:00
/*@
2012-06-02 10:00:52 +00:00
toggleOption <f> Toggle boolean option(s)
2014-09-24 10:17:46 +00:00
(key[, key[, ...]]) -> <o> This element object
2012-05-21 10:38:18 +00:00
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.toggleOption = function toggleOption() {
2014-09-23 19:16:25 +00:00
var options = {}, self = this.self(_);
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];
});
2014-09-23 19:16:25 +00:00
return this.options(options);
2011-12-21 13:42:47 +00:00
};
/*@
triggerEvent <f> Triggers all handlers for one or more events
2012-06-30 10:29:06 +00:00
(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 one or more events with data
2014-09-24 10:17:46 +00:00
event <s> Event name
data <o> Event data (key/value pairs)
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.triggerEvent = function triggerEvent() {
2014-09-23 19:16:25 +00:00
Ox.Event.trigger.apply(
this, [this.self(_)].concat(Ox.slice(arguments))
);
2014-09-22 14:56:54 +00:00
return this;
};
2011-04-22 22:03:10 +00:00
2014-09-24 10:17:46 +00:00
/*@
triggerMessage <f> Triggers all handlers for one or more messages
2014-09-24 10:17:46 +00:00
(message) -> <o> This element object
Triggers an event
(message, data) -> <o> This element object
Triggers a message with data
({message: data, ...}) -> <o> This element object
Triggers one or more messages with data
message <s> Message name
data <o> Message data (key/value pairs)
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.triggerMessage = function triggerMessage() {
2014-09-23 19:16:25 +00:00
var self = this.self(_);
if (self.options.element == '<iframe>') {
Ox.Message.trigger.apply(this, [self].concat(Ox.slice(arguments)));
}
return this;
};
/*@
2012-06-30 10:29:06 +00:00
unbindEvent <f> Removes event handler(s)
2014-09-24 10:17:46 +00:00
() -> <o> This element object
2012-06-30 10:29:06 +00:00
Removes all handlers.
2014-09-24 10:17:46 +00:00
(callback) -> <o> This element object
2012-06-30 10:29:06 +00:00
Removes a specific catch-all handler
2014-09-24 10:17:46 +00:00
(event) -> <o> This element object
2012-06-30 10:29:06 +00:00
Removes all handlers for a single event (to remove all catch-all
handlers, pass '*' as event)
2014-09-24 10:17:46 +00:00
(event, callback) -> <o> This element object
2012-06-30 10:29:06 +00:00
Removes a specific handler for a single event
2014-09-24 10:17:46 +00:00
({event: callback}, ...) -> <o> This element object
Removes specific handlers for one or more events
event <s> Event name
callback <f> Event handler
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.unbindEvent = function unbindEvent() {
2014-09-23 19:16:25 +00:00
Ox.Event.unbind.apply(this, [this.self(_)].concat(Ox.slice(arguments)));
2014-09-22 14:56:54 +00:00
return this;
};
2014-09-24 10:17:46 +00:00
/*@
unbindMessage <f> Removes message handler(s)
() -> <o> This element
Removes all handlers.
(callback) -> <o> This element object
Removes a specific catch-all handler
(message) -> <o> This element object
Removes all handlers for a single message (to remove all catch-all
handlers, pass '*' as message)
(message, callback) -> <o> This element object
Removes a specific handler for a single event
({message: callback}, ...) -> <o> This element object
Removes specific handlers for one or more messages
message <s> Message name
callback <f> Message handler
2014-09-24 10:17:46 +00:00
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.unbindMessage = function unbindMessage() {
2014-09-23 19:16:25 +00:00
var self = this.self(_);
if (self.options.element == '<iframe>') {
Ox.Message.unbind.apply(this, [self].concat(Ox.slice(arguments)));
}
2014-09-22 14:56:54 +00:00
return this;
};
2012-05-28 16:16:23 +00:00
/*@
update <f> Adds one or more handlers for options updates
2014-09-24 10:17:46 +00:00
(callback) -> <o> This element object
(key, callback) -> <o> This element object
({key: callback, ...}) -> <o> This element object
2012-05-28 16:16:23 +00:00
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.update = function update() {
2014-09-23 19:16:25 +00:00
var callbacks, self = this.self(_);
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);
2014-09-23 19:16:25 +00:00
self.updateCallbacks.push(function(key, value) {
2012-05-28 16:16:23 +00:00
if (callbacks[key]) {
2014-09-23 19:16:25 +00:00
return callbacks[key](value);
2012-05-28 16:16:23 +00:00
}
});
}
2014-09-22 14:56:54 +00:00
return this;
2012-05-28 16:16:23 +00:00
};
2011-12-21 15:33:52 +00:00
/*@
value <f> Shortcut to get or set self.options.value
2014-09-24 10:17:46 +00:00
() -> <*> Value
(value) -> <o> This element object
value <*> Value
2011-12-21 15:33:52 +00:00
@*/
2014-09-22 14:56:54 +00:00
Ox.Element.prototype.value = function value() {
return this.options(
2011-12-21 15:33:52 +00:00
arguments.length == 0 ? 'value' : {value: arguments[0]}
);
};
2014-09-22 14:56:54 +00:00
}({}));