oxjstmp/build/js/ox.ui.js

10812 lines
379 KiB
JavaScript
Raw Normal View History

2010-01-07 20:21:07 +00:00
/*
################################################################################
ox.ui.js
requires
2010-01-25 11:42:28 +00:00
jquery-1.4.js
2010-01-07 20:21:07 +00:00
ox.js
################################################################################
*/
// also see test.js, in demos ...
(function() {
var oxui = {
2010-09-03 20:54:40 +00:00
defaultTheme: 'classic',
2010-01-07 20:21:07 +00:00
elements: {},
getDimensions: function(orientation) {
2010-09-03 20:54:40 +00:00
return orientation == 'horizontal' ?
['width', 'height'] : ['height', 'width'];
2010-01-07 20:21:07 +00:00
},
getEdges: function(orientation) {
2010-09-03 20:54:40 +00:00
return orientation == 'horizontal' ?
['left', 'right', 'top', 'bottom'] :
['top', 'bottom', 'left', 'right'];
2010-01-07 20:21:07 +00:00
},
getBarSize: function(size) {
var sizes = {
2010-02-20 03:58:46 +00:00
small: 20,
medium: 24,
large: 28,
2010-01-07 20:21:07 +00:00
};
return sizes[size];
},
2010-01-25 11:42:28 +00:00
jQueryFunctions: function() {
2010-01-07 20:21:07 +00:00
var functions = [],
2010-09-03 20:54:40 +00:00
$element = $('<div>');
2010-01-25 11:42:28 +00:00
//delete $element.length;
Ox.each($element, function(k, v) {
2010-09-03 20:54:40 +00:00
if (typeof v == 'function') {
2010-01-07 20:21:07 +00:00
functions.push(k);
}
});
return functions.sort();
}(),
2010-09-05 14:24:22 +00:00
path: $('script[src*=ox.ui.js]').attr('src').replace('js/ox.ui.js', ''),
2010-06-30 18:47:10 +00:00
scrollbarSize: $.browser.mozilla ? 16 : 12,
2010-06-30 09:27:02 +00:00
symbols: {
2010-09-03 20:54:40 +00:00
alt: '\u2325',
apple: '\uF8FF',
arrow_down: '\u2193',
arrow_left: '\u2190',
arrow_right: '\u2192',
arrow_up: '\u2191',
backspace: '\u232B',
backup: '\u2707',
ballot: '\u2717',
black_star: '\u2605',
burn: '\u2622',
caps_lock: '\u21EA',
check: '\u2713',
//clear: '\u2327',
clear: '\u00D7',
click: '\uF803',
close: '\u2715',
command: '\u2318',
control: '\u2303',
cut: '\u2702',
'delete': '\u2326',
diamond: '\u25C6',
edit: '\uF802',
eject: '\u23CF',
escape: '\u238B',
end: '\u2198',
enter: '\u2324',
fly: '\u2708',
gear: '\u2699',
home: '\u2196',
info: '\u24D8',
navigate: '\u2388',
option: '\u2387',
page_up: '\u21DE',
page_down: '\u21DF',
redo: '\u21BA',
'return': '\u21A9',
//select: '\u21D5',
select: '\u25BE',
shift: '\u21E7',
sound: '\u266B',
space: '\u2423',
tab: '\u21E5',
trash: '\u267A',
triangle_down: '\u25BC',
triangle_left: '\u25C0',
triangle_right: '\u25BA',
triangle_up: '\u25B2',
undo: '\u21BB',
voltage: '\u26A1',
warning: '\u26A0',
white_star: '\u2606'
2010-02-03 12:12:21 +00:00
}
2010-01-07 20:21:07 +00:00
},
2010-12-26 20:16:35 +00:00
$elements = {},
2010-01-07 20:21:07 +00:00
$window, $document, $body;
2011-01-02 10:01:55 +00:00
_$elements = $elements;
2010-12-31 11:01:35 +00:00
2010-01-07 20:21:07 +00:00
$(function() {
$window = $(window),
$document = $(document),
2010-09-03 20:54:40 +00:00
$body = $('body'),
2010-01-07 20:21:07 +00:00
Ox.theme(oxui.defaultTheme);
2010-02-04 09:50:45 +00:00
});
2010-01-07 20:21:07 +00:00
/*
============================================================================
Application
============================================================================
*/
2010-12-06 17:42:45 +00:00
/**
Creates an App instance.
2010-01-07 20:21:07 +00:00
*/
2010-01-27 12:30:00 +00:00
Ox.App = function() {
return function(options) {
options = options || {};
var self = {},
that = this;
2010-09-05 14:24:22 +00:00
self.time = +new Date();
2010-01-27 12:30:00 +00:00
self.options = $.extend({
2010-09-14 13:50:51 +00:00
apiTimeout: 15000,
apiType: 'POST',
apiURL: '',
config: '',
init: ''
2010-01-27 12:30:00 +00:00
}, options);
2010-09-05 14:24:22 +00:00
function getUserAgent() {
var userAgent = '';
$.each(['Chrome', 'Firefox', 'Internet Explorer', 'Opera', 'Safari'], function(i, v) {
if (navigator.userAgent.indexOf(v) > -1) {
userAgent = v;
return false;
}
});
return userAgent;
}
2010-09-05 00:31:58 +00:00
function getUserData() {
//return {};
2011-01-02 10:01:55 +00:00
return {};
2010-09-05 00:31:58 +00:00
return {
navigator: {
cookieEnabled: navigator.cookieEnabled,
plugins: $.map(navigator.plugins, function(plugin, i) {
return plugin.name;
}),
userAgent: navigator.userAgent
},
screen: screen,
2010-09-05 14:24:22 +00:00
time: (+new Date() - self.time) / 1000,
2010-09-05 00:31:58 +00:00
window: {
innerHeight: window.innerHeight,
innerWidth: window.innerWidth,
outerHeight: window.outerHeight,
outerWidth: window.outerWidth,
screenLeft: window.screenLeft,
screenTop: window.screenTop
}
};
}
function loadImages(callback) {
$.getJSON(oxui.path + 'json/ox.ui.images.json', function(data) {
var counter = 0,
length = data.length;
data.forEach(function(src, i) {
image = new Image()
image.src = oxui.path + src;
image.onload = function() {
(++counter == length) && callback();
}
});
});
}
2010-07-07 12:36:12 +00:00
self.change = function(key, value) {
2010-01-27 12:30:00 +00:00
};
that.api = {
api: function(callback) {
Ox.Request.send({
2010-09-14 13:50:51 +00:00
url: self.options.apiURL,
data: {
action: 'api'
},
callback: callback
});
},
cancel: function(id) {
Ox.Request.cancel(id);
}
};
2010-09-05 00:31:58 +00:00
that.launch = function(callback) {
2010-09-05 14:24:22 +00:00
var time = +new Date(),
userAgent = getUserAgent(),
2011-01-02 10:01:55 +00:00
userAgents = ['Chrome', 'Firefox', 'Opera', 'Safari'];
2010-01-27 12:30:00 +00:00
$.ajaxSetup({
2010-09-14 13:50:51 +00:00
timeout: self.options.apiTimeout,
type: self.options.apiType,
url: self.options.apiURL
2010-01-27 12:30:00 +00:00
});
2010-09-05 14:24:22 +00:00
userAgents.indexOf(userAgent) > -1 ? start() : stop();
function start() {
2010-09-14 13:50:51 +00:00
$.getJSON(self.options.config, function(data) {
var config = data;
Ox.print('config', config);
document.title = config.site.name;
2010-12-22 17:58:39 +00:00
loadImages(function() {
that.api.api(function(result) {
$.each(result.data.actions, function(i, action) {
that.api[action] = function(data, callback) {
if (arguments.length == 1) {
callback = data;
data = {};
}
return Ox.Request.send({
url: self.options.apiURL,
data: {
action: action,
data: JSON.stringify(data)
},
callback: callback
});
};
});
that.api[self.options.init](getUserData(), function(data) {
var user = data.data.user;
$(function() {
var $div = $body.find('div');
$body.find('img').remove();
$div.animate({
opacity: 0
}, 1000, function() {
$div.remove();
});
callback({
config: config,
user: user
});
2010-09-05 14:24:22 +00:00
});
2010-09-05 00:31:58 +00:00
});
});
});
});
2010-09-05 14:24:22 +00:00
}
function stop() {
that.request.send(self.options.init, getUserData(), function() {});
2010-09-05 14:24:22 +00:00
}
2010-09-05 00:31:58 +00:00
return that;
2010-01-27 12:30:00 +00:00
};
that.options = function() {
return Ox.getset(self.options, Array.prototype.slice.call(arguments), self.change, that);
2010-01-27 12:30:00 +00:00
};
return that;
};
}();
2010-12-06 17:42:45 +00:00
/**
2010-12-06 18:23:04 +00:00
Ox.Event event object
2010-01-07 20:21:07 +00:00
*/
2010-12-26 20:16:35 +00:00
/*
2010-01-07 20:21:07 +00:00
Ox.Event = function() {
2010-09-03 20:54:40 +00:00
var $eventHandler = $('<div>'),
2010-09-03 08:47:40 +00:00
events = {};
2010-12-26 20:16:35 +00:00
function bindEvent(id, event, callback) {
Ox.print('bind', id, event);
2010-09-03 08:47:40 +00:00
events[id] = events[id] || {};
2010-12-26 20:16:35 +00:00
events[id][event] = events[id][event] || [];
if ($.map(events[id][event], function(fn) {
return fn.toString();
}).indexOf(callback.toString()) == -1) {
events[id][event].push(callback);
if (!isKeyboardEvent(event) || Ox.Focus.focused() == id) {
$eventHandler.bind(event + '_' + id, callback); // requestStart/requestStop currently have '' as id
}
}
}
function unbindEvent(id, event, callback) {
Ox.print('unbind', id, event);
var toString = (callback || '').toString(),
unbind = !event || (event && !isKeyboardEvent(event)) || Ox.Focus.focused() == id;
if (events[id] && (!event || events[id][event])) {
$.each(events[id], function(e, fns) {
2010-09-03 08:47:40 +00:00
if (!event || event == e) {
2010-12-26 20:16:35 +00:00
events[id][e] = $.map(events[id][e], function(fn, i) {
2010-09-03 08:47:40 +00:00
if (!callback || toString == fn.toString()) {
2010-12-26 20:16:35 +00:00
Ox.print(unbind, 'eventHandler.unbind', e, id)
unbind && $eventHandler.unbind(e + '_' + id, fn);
2010-09-03 08:47:40 +00:00
return null;
} else {
return fn;
}
});
}
});
2010-12-26 20:16:35 +00:00
Ox.print('id/events', id, events, events[id])
if (!callback || (event && events[id][event].length == 0)) {
delete events[id][event];
2010-07-05 16:52:12 +00:00
}
2010-12-26 20:16:35 +00:00
if (!event || Ox.length(events[id]) == 0) {
2010-09-03 08:47:40 +00:00
delete events[id];
2010-02-05 09:13:03 +00:00
}
2010-09-03 08:47:40 +00:00
}
}
2010-12-26 20:16:35 +00:00
function isKeyboardEvent(event) {
2010-09-03 20:54:40 +00:00
return event.substr(0, 4) == 'key_';
2010-09-03 08:47:40 +00:00
}
return {
_print: function() {
Ox.print(events);
},
bind: function(id, event, callback) {
// bind event
2010-12-26 20:16:35 +00:00
bindEvent(id, event, callback);
2010-02-04 08:02:23 +00:00
},
2010-02-05 09:13:03 +00:00
bindKeyboard: function(id) {
2010-09-03 08:47:40 +00:00
// bind all keyboard events
2010-09-03 20:54:40 +00:00
//Ox.print('binding', 'id', id, 'events', events[id], Ox.length(events[id]), 'keyboardevents', events[id]['keyboard'])
2010-09-03 08:47:40 +00:00
$.each(events[id], function(k, v) {
2010-09-03 20:54:40 +00:00
Ox.print('|' + k + '|');
2010-09-03 08:47:40 +00:00
})
2010-12-26 20:16:35 +00:00
events[id] && $.each(events[id], function(event, callbacks) {
Ox.print('BIND ID EVENT', id, event)
isKeyboardEvent(event) && $.each(callbacks, function(i, callback) {
2010-09-03 20:54:40 +00:00
Ox.print('bind', id, event);
2010-09-03 08:47:40 +00:00
$eventHandler.bind(event, callback);
});
2010-02-05 09:13:03 +00:00
});
2010-02-04 09:50:45 +00:00
},
2010-09-03 08:47:40 +00:00
changeId: function(oldId, newId) {
2010-12-26 20:16:35 +00:00
// we need to reference events by options('id'),
// not by that.id, since textlist events fire on list,
// new lists have new that.ids, etc.
// so this renaming is necessary
2010-09-03 20:54:40 +00:00
Ox.print('changeId', oldId, newId, events[oldId]);
2010-12-26 20:16:35 +00:00
$.each($.extend({}, events[oldId] || {}), function(event, callbacks) {
$.each(callbacks, function(i, callback) {
Ox.Event.unbind(oldId, event, callback);
Ox.Event.bind(newId, event, callback);
2010-09-03 08:47:40 +00:00
});
});
},
2010-02-05 09:13:03 +00:00
unbind: function(id, event, callback) {
2010-09-03 08:47:40 +00:00
// unbind event
// event and callback are optional
2010-12-26 20:16:35 +00:00
unbindEvent(id, event, callback);
},
unbindAll: function() {
Ox.print('beginUnbindAll')
$.each(events, function(id, eventsById) {
$.each(eventsById, function(event, callbacks) {
$.each(callbacks, function(i, callback) {
Ox.Event.unbind(id, event, callback);
});
});
});
Ox.print('endUnbindAll')
2010-09-03 08:47:40 +00:00
},
trigger: function(id, event, data) {
// trigger event
// data is optional
2010-09-03 20:54:40 +00:00
// keyboard handler will call this with '' as id
Ox.print('trigger', id, event, data || {});
$eventHandler.trigger(event + (id ? '_' + id : ''), data || {});
2010-02-05 09:13:03 +00:00
},
unbindKeyboard: function(id) {
2010-09-03 08:47:40 +00:00
// unbind all keyboard events
2010-12-26 20:16:35 +00:00
Ox.print('unbinding', id)
events[id] && $.each(events[id], function(event, callbacks) {
Ox.print('UNBIND ID EVENT', id, event)
isKeyboardEvent(event) && $.each(callbacks, function(i, callback) {
2010-09-03 20:54:40 +00:00
Ox.print('unbind', event)
2010-09-03 08:47:40 +00:00
$eventHandler.unbind(event, callback);
2010-01-07 20:21:07 +00:00
});
});
}
2010-09-03 08:47:40 +00:00
}
2010-01-07 20:21:07 +00:00
}();
2010-12-26 20:16:35 +00:00
*/
2010-01-07 20:21:07 +00:00
2010-12-06 17:42:45 +00:00
/**
Ox.Focus
2010-01-07 20:21:07 +00:00
*/
Ox.Focus = function() {
var stack = [];
return {
2010-12-26 20:16:35 +00:00
_print: function() {
Ox.print(stack);
},
2010-06-30 09:27:02 +00:00
blur: function(id) {
2010-12-26 20:16:35 +00:00
Ox.print('BLUR', id, stack)
2010-09-03 08:47:40 +00:00
var index = stack.indexOf(id);
if (index == stack.length - 1) {
stack.length == 1 ? stack.pop() :
stack.splice(stack.length - 2, 0, stack.pop());
2010-12-26 20:16:35 +00:00
// fix later: Ox.Event.unbindKeyboard($elements[id].options('id'));
// fix later: stack.length && Ox.Event.bindKeyboard($elements[stack[stack.length - 1]].options('id'));
2010-09-04 14:28:40 +00:00
//$elements[id].removeClass('OxFocus');
$('.OxFocus').removeClass('OxFocus'); // fixme: the above is better, and should work
stack.length && $elements[stack[stack.length - 1]].addClass('OxFocus');
2010-09-03 20:54:40 +00:00
Ox.print('blur', id, stack);
2010-06-30 09:27:02 +00:00
}
},
2010-01-07 20:21:07 +00:00
focus: function(id) {
2010-02-07 15:01:22 +00:00
var index = stack.indexOf(id);
2010-09-03 08:47:40 +00:00
if (index == -1 || index < stack.length - 1) {
2010-12-26 20:16:35 +00:00
// fix later: stack.length && Ox.Event.unbindKeyboard($elements[stack[stack.length - 1]].options('id'));
2010-09-03 08:47:40 +00:00
index > -1 && stack.splice(index, 1);
stack.push(id);
2010-12-26 20:16:35 +00:00
// fix later: Ox.Event.bindKeyboard($elements[id].options('id'));
2010-09-04 14:28:40 +00:00
$('.OxFocus').removeClass('OxFocus'); // fixme: see above
$elements[id].addClass('OxFocus');
2010-09-03 20:54:40 +00:00
Ox.print('focus', id, stack);
2010-01-07 20:21:07 +00:00
}
2010-02-05 09:13:03 +00:00
},
focused: function() {
2010-12-27 05:01:24 +00:00
return stack.length ? stack[stack.length - 1] : null;
2010-01-07 20:21:07 +00:00
}
};
}();
2010-12-06 17:42:45 +00:00
/**
Ox.History
2010-01-07 20:21:07 +00:00
*/
2010-12-06 17:42:45 +00:00
/**
Ox.Keyboard
2010-01-07 20:21:07 +00:00
*/
(function() {
2010-01-25 11:42:28 +00:00
2010-09-03 20:54:40 +00:00
var buffer = '',
2010-01-07 20:21:07 +00:00
bufferTime = 0,
bufferTimeout = 1000,
keyNames = function() {
return {
2010-09-03 20:54:40 +00:00
0: 'section',
8: 'backspace',
9: 'tab',
12: 'clear',
13: 'enter',
16: 'shift',
17: 'control',
18: 'alt',
20: 'capslock',
27: 'escape',
32: 'space',
33: 'pageup',
34: 'pagedown',
35: 'end',
36: 'home',
37: 'left',
38: 'up',
39: 'right',
40: 'down',
45: 'insert',
46: 'delete',
47: 'help',
48: '0',
49: '1',
50: '2',
51: '3',
52: '4',
53: '5',
54: '6',
55: '7',
56: '8',
57: '9',
65: 'a',
66: 'b',
67: 'c',
68: 'd',
69: 'e',
70: 'f',
71: 'g',
72: 'h',
73: 'i',
74: 'j',
75: 'k',
76: 'l',
77: 'm',
78: 'n',
79: 'o',
80: 'p',
81: 'q',
82: 'r',
83: 's',
84: 't',
85: 'u',
86: 'v',
87: 'w',
88: 'x',
89: 'y',
90: 'z',
91: 'meta.left',
92: 'meta.right',
93: 'select',
96: '0.numpad',
97: '1.numpad',
98: '2.numpad',
99: '3.numpad',
100: '4.numpad',
101: '5.numpad',
102: '6.numpad',
103: '7.numpad',
104: '8.numpad',
105: '9.numpad',
106: 'asterisk.numpad',
107: 'plus.numpad',
109: 'minus.numpad',
108: 'enter.numpad',
110: 'dot.numpad',
111: 'slash.numpad',
112: 'f1',
113: 'f2',
114: 'f3',
115: 'f4',
116: 'f5',
117: 'f6',
118: 'f7',
119: 'f8',
120: 'f9',
121: 'f10',
122: 'f11',
123: 'f12',
124: 'f13',
125: 'f14',
126: 'f15',
127: 'f16',
144: 'numlock',
145: 'scrolllock',
186: 'semicolon',
187: 'equal',
188: 'comma',
189: 'minus',
190: 'dot',
191: 'slash',
192: 'backtick',
219: 'openbracket',
220: 'backslash',
221: 'closebracket',
222: 'quote'
2010-01-07 20:21:07 +00:00
// see dojo, for ex.
};
}(),
modifierNames = {
2010-09-03 20:54:40 +00:00
altKey: 'alt', // mac: option
ctrlKey: 'control',
// metaKey: 'meta', // mac: command
shiftKey: 'shift'
2010-01-07 20:21:07 +00:00
};
2010-02-05 15:42:52 +00:00
2010-02-04 09:50:45 +00:00
$(function() {
2010-02-05 15:42:52 +00:00
// fixme: how to do this better?
2010-02-19 14:19:48 +00:00
// in firefox on mac, keypress doesn't fire for up/down
2010-02-19 11:47:13 +00:00
// if the cursor is at the start/end of an input element
2010-02-19 14:19:48 +00:00
// on linux, it doesn't seem to fire if the input element has focus
2010-02-19 11:47:13 +00:00
if ($.browser.mozilla) {
2010-02-18 07:27:32 +00:00
$document.keypress(keypress);
2010-02-19 11:47:13 +00:00
$document.keydown(function(event) {
2010-09-03 20:54:40 +00:00
var $element = $('input:focus');
2010-02-19 11:47:13 +00:00
if ($element.length) {
if (
(
2010-09-03 20:54:40 +00:00
keyNames[event.keyCode] == 'up' &&
2010-02-19 11:47:13 +00:00
$element[0].selectionStart + $element[0].selectionEnd == 0
) || (
2010-09-03 20:54:40 +00:00
keyNames[event.keyCode] == 'down' &&
2010-02-19 11:47:13 +00:00
$element[0].selectionStart == $element.val().length &&
$element[0].selectionEnd == $element.val().length
)
) {
keypress(event);
}
}
});
2010-02-19 11:47:13 +00:00
} else {
$document.keydown(keypress);
2010-02-05 15:31:19 +00:00
}
2010-02-04 09:50:45 +00:00
});
2010-02-18 07:27:32 +00:00
function keypress(event) {
2010-12-27 05:01:24 +00:00
var focused = Ox.Focus.focused(),
key,
2010-02-04 09:50:45 +00:00
keys = [],
2010-02-20 03:58:46 +00:00
//ret = true,
2010-02-04 09:50:45 +00:00
time;
$.each(modifierNames, function(k, v) {
if (event[k]) {
keys.push(v);
2010-01-07 20:21:07 +00:00
}
2010-02-04 09:50:45 +00:00
});
// avoid pushing modifier twice
2010-09-03 20:54:40 +00:00
Ox.print('keys', keys)
2010-02-04 09:50:45 +00:00
if (keyNames[event.keyCode] && keys.indexOf(keyNames[event.keyCode]) == -1) {
keys.push(keyNames[event.keyCode]);
}
2010-09-03 20:54:40 +00:00
key = keys.join('_');
2010-02-04 09:50:45 +00:00
if (key.match(/^[\w\d-]$|SPACE/)) {
time = Ox.getTime();
if (time - bufferTime > bufferTimeout) {
2010-09-03 20:54:40 +00:00
buffer = '';
2010-01-07 20:21:07 +00:00
}
2010-09-03 20:54:40 +00:00
buffer += key == 'SPACE' ? ' ' : key;
bufferTime = time;
2010-01-07 20:21:07 +00:00
}
2011-01-02 10:01:55 +00:00
focused && $elements[focused].triggerEvent('key_' + key);
2010-02-18 14:24:17 +00:00
//return false;
2010-02-04 09:50:45 +00:00
/*
$.each(stack, function(i, v) {
// fixme: we dont get the return value!
2010-09-03 20:54:40 +00:00
ret = Ox.event.trigger(keyboard + Ox.toCamelCase(key) + '.' + v);
2010-02-04 09:50:45 +00:00
return ret;
});
*/
2010-02-04 09:50:45 +00:00
}
2010-01-07 20:21:07 +00:00
})();
2010-12-06 17:42:45 +00:00
/**
2010-01-07 20:21:07 +00:00
Ox.Mouse (??)
*/
2010-12-06 17:42:45 +00:00
/**
2010-12-06 18:23:04 +00:00
Ox.Request request object
2010-01-27 12:30:00 +00:00
*/
Ox.Request = function() {
var cache = {},
pending = {},
requests = {},
self = {
options: {
timeout: 15000,
2010-09-03 20:54:40 +00:00
type: 'POST',
url: 'api'
2010-01-27 12:30:00 +00:00
}
};
return {
cancel: function() {
var index;
if (arguments.length == 0) {
requests = {};
} else if (Ox.isFunction(arguments[0])) {
// cancel with function
$.each(requests, function(id, req) {
if (arguments[0](req)) {
delete requests[id];
}
})
} else {
// cancel by id
delete requests[arguments[0]]
}
},
2010-01-27 12:34:27 +00:00
emptyCache: function() {
cache = {};
2010-01-27 12:35:37 +00:00
},
2010-01-27 12:34:27 +00:00
2010-01-27 12:30:00 +00:00
options: function(options) {
2010-01-27 12:34:27 +00:00
return Ox.getset(self.options, options, $.noop(), this);
2010-01-27 12:30:00 +00:00
},
send: function(options) {
2010-07-01 23:51:08 +00:00
var options = $.extend({
age: -1,
callback: function() {},
id: Ox.uid(),
timeout: self.options.timeout,
type: self.options.type,
url: self.options.url
}, options),
req = JSON.stringify({
2010-01-27 12:30:00 +00:00
url: options.url,
data: options.data
});
2010-01-31 10:06:52 +00:00
function callback(data) {
2010-01-27 12:30:00 +00:00
delete requests[options.id];
2010-12-26 20:16:35 +00:00
Ox.length(requests) == 0 && $body.trigger('requestStop');
2010-09-05 14:24:22 +00:00
options.callback(data); // fixme: doesn't work if callback hasn't been passed
2010-01-27 12:30:00 +00:00
}
2010-01-31 09:32:41 +00:00
function debug(request) {
2010-09-03 20:54:40 +00:00
var $iframe = $('<iframe>')
2010-01-31 09:32:41 +00:00
.css({ // fixme: should go into a class
width: 768,
height: 384
}),
$dialog = new Ox.Dialog({
2010-09-03 20:54:40 +00:00
title: 'Application Error',
2010-01-31 09:32:41 +00:00
buttons: [
2010-12-24 17:13:18 +00:00
new Ox.Button({
title: 'Close'
})
.bindEvent({
2010-12-26 20:16:35 +00:00
click: function() {
$dialog.close();
}
2010-12-24 17:13:18 +00:00
})
2010-01-31 09:32:41 +00:00
],
2010-12-24 17:13:18 +00:00
content: $iframe,
2010-01-31 09:32:41 +00:00
width: 800,
height: 400
})
.open(),
iframe = $iframe[0].contentDocument || $iframe[0].contentWindow.document;
iframe.open();
iframe.write(request.responseText);
iframe.close();
}
2010-01-27 12:30:00 +00:00
function error(request, status, error) {
2010-01-31 10:06:52 +00:00
var data;
2010-02-01 06:11:35 +00:00
if (arguments.length == 1) {
data = arguments[0]
} else {
try {
data = JSON.parse(request.responseText);
} catch (err) {
2011-01-02 10:01:55 +00:00
try {
data = {
status: {
code: request.status,
text: request.statusText
}
};
} catch (err) {
data = {
status: {
code: '500',
text: 'Unknown Error'
}
};
}
2010-02-01 06:11:35 +00:00
}
2010-01-27 13:25:37 +00:00
}
2010-01-31 10:06:52 +00:00
if (data.status.code < 500) {
callback(data);
} else {
2010-01-27 13:25:37 +00:00
var $dialog = new Ox.Dialog({
2010-09-03 20:54:40 +00:00
title: 'Application Error',
2010-01-31 09:32:41 +00:00
buttons: [
2010-12-24 17:13:18 +00:00
new Ox.Button({
title: 'Details'
})
.bindEvent({
2010-01-31 09:32:41 +00:00
click: function() {
$dialog.close(function() {
debug(request);
2010-12-24 17:13:18 +00:00
})
2010-01-31 09:32:41 +00:00
}
2010-12-24 17:13:18 +00:00
}),
new Ox.Button({
title: 'Close'
})
.bindEvent({
2010-01-31 09:32:41 +00:00
click: function() {
2010-12-24 17:13:18 +00:00
$dialog.close();
2010-01-31 09:32:41 +00:00
}
2010-12-24 17:13:18 +00:00
})
2010-01-31 09:32:41 +00:00
],
2010-12-24 17:13:18 +00:00
content: 'Sorry, we have encountered an application error while handling your request. To help us find out what went wrong, you may want to report this error to an administrator. Otherwise, please try again later.',
2010-01-31 09:32:41 +00:00
width: 400,
2010-06-30 18:47:10 +00:00
height: 200
2010-01-31 09:32:41 +00:00
})
.open();
2010-02-01 06:11:35 +00:00
// fixme: change this to Send / Don't Send
2010-01-27 13:25:37 +00:00
Ox.print({
request: request,
status: status,
error: error
});
}
2010-01-27 12:30:00 +00:00
pending[options.id] = false;
}
function success(data) {
pending[options.id] = false;
cache[req] = {
data: data,
time: Ox.getTime()
};
2010-01-31 10:06:52 +00:00
callback(data);
2010-01-27 12:30:00 +00:00
}
if (pending[options.id]) {
setTimeout(function() {
Ox.Request.send(options);
}, 0);
} else {
requests[options.id] = {
url: options.url,
data: options.data
};
if (cache[req] && (options.age == -1 || options.age > Ox.getTime() - cache[req].time)) {
setTimeout(function() {
2010-01-31 10:06:52 +00:00
callback(cache[req].data);
2010-01-27 12:30:00 +00:00
}, 0);
} else {
pending[options.id] = true;
$.ajax({
data: options.data,
2010-09-03 20:54:40 +00:00
dataType: 'json',
2010-01-27 12:30:00 +00:00
error: error,
success: success,
timeout: options.timeout,
type: options.type,
url: options.url
});
2010-09-03 20:54:40 +00:00
Ox.print('request', options.data, Ox.length(requests));
2010-12-26 20:16:35 +00:00
Ox.length(requests) == 1 && $body.trigger('requestStart');
2010-01-27 12:30:00 +00:00
}
}
2010-07-01 23:51:08 +00:00
2010-01-27 12:30:00 +00:00
return options.id;
2010-07-01 23:51:08 +00:00
},
requests: function() {
return Ox.length(requests);
2010-01-27 12:30:00 +00:00
}
};
}();
2010-12-06 17:42:45 +00:00
/**
not implemented
2010-09-05 15:51:11 +00:00
*/
Ox.Theme = function() {
};
2010-12-06 17:42:45 +00:00
/**
2010-01-07 20:21:07 +00:00
Ox.URL
*/
/*
============================================================================
Core
============================================================================
*/
// fixme: wouldn't it be better to let the elements be,
// rather then $element, $content, and potentially others,
// 0, 1, 2, etc, so that append would append 0, and appendTo
// would append (length - 1)?
2010-12-06 17:42:45 +00:00
/**
*/
2010-06-28 11:19:04 +00:00
Ox.Container = function(options, self) {
2010-09-03 20:54:40 +00:00
var that = new Ox.Element('div', self)
2010-09-03 08:47:40 +00:00
.options(options || {})
2010-09-03 20:54:40 +00:00
.addClass('OxContainer');
that.$content = new Ox.Element('div', self)
2010-09-03 08:47:40 +00:00
.options(options || {})
2010-09-03 20:54:40 +00:00
.addClass('OxContent')
2010-01-07 20:21:07 +00:00
.appendTo(that);
return that;
2010-02-07 15:01:22 +00:00
};
2010-01-07 20:21:07 +00:00
// check out http://ejohn.org/apps/learn/#36 (-#38, making fns work w/o new)
2010-12-06 17:42:45 +00:00
/**
Ox.Element
*/
2010-01-07 20:21:07 +00:00
Ox.Element = function() {
return function(options, self) {
// construct
self = self || {};
2011-01-02 10:01:55 +00:00
self.options = options || {};
if (!self.$eventHandler) {
self.$eventHandler = $('<div>');
}
2010-01-07 20:21:07 +00:00
var that = this;
// init
(function() {
2011-01-02 10:01:55 +00:00
// allow for Ox.Element('tagname', self)
if (typeof self.options == 'string') {
self.options = {
element: self.options
2010-01-07 20:21:07 +00:00
};
}
that.ox = Ox.version;
that.id = Ox.uid();
2011-01-02 10:01:55 +00:00
that.$element = $('<' + (self.options.element || 'div') + '/>', {
2010-01-25 11:42:28 +00:00
data: {
ox: that.id
}
});
2010-06-30 09:27:02 +00:00
$elements[that.id] = that;
2010-01-07 20:21:07 +00:00
wrapjQuery();
})();
// private
function wrapjQuery() {
2010-02-10 18:42:59 +00:00
$.each(oxui.jQueryFunctions, function(i, fn) {
2010-02-10 18:52:46 +00:00
that[fn] = function() {
2010-01-07 20:21:07 +00:00
var args = arguments,
length = args.length,
2010-02-10 19:50:10 +00:00
id, ret;
2010-02-10 18:42:59 +00:00
$.each(args, function(i, arg) {
2010-07-20 20:04:13 +00:00
if (Ox.isUndefined(arg)) {
2010-09-03 20:54:40 +00:00
Ox.print('fn', fn, 'undefined argument')
2010-07-20 20:04:13 +00:00
}
2010-01-07 20:21:07 +00:00
// if an ox object was passed
// then pass its $element instead
// so we can do oxObj.jqFn(oxObj)
2010-12-24 17:13:18 +00:00
if (arg && arg.ox) {
2010-02-10 19:59:10 +00:00
args[i] = arg.$element;
}
2010-02-10 19:41:25 +00:00
/*
2010-02-10 18:52:46 +00:00
if (arg.ox) { // fixme: or is this too much magic?
2010-09-03 20:54:40 +00:00
if (fn == 'appendTo' && arg.$content) {
2010-02-10 18:52:46 +00:00
args[i] = arg.$content
2010-02-10 18:42:59 +00:00
} else {
2010-02-10 18:52:46 +00:00
args[i] = arg.$element;
2010-02-10 18:42:59 +00:00
}
2010-01-07 20:21:07 +00:00
}
2010-02-10 19:41:25 +00:00
*/
2010-01-07 20:21:07 +00:00
});
2010-02-10 19:41:25 +00:00
/*
2010-09-03 20:54:40 +00:00
if (fn == 'html' && that.$content) { // fixme: or is this too much magic?
2010-02-10 18:42:59 +00:00
$element = that.$content;
} else {
$element = that.$element;
}
2010-02-10 19:41:25 +00:00
*/
2010-07-17 08:46:27 +00:00
// why does this not work? (that?)
// ret = that.$element[fn].apply(this, arguments);
2010-01-07 20:21:07 +00:00
if (length == 0) {
2010-02-10 19:50:10 +00:00
ret = that.$element[fn]();
2010-01-07 20:21:07 +00:00
} else if (length == 1) {
2010-02-10 19:50:10 +00:00
ret = that.$element[fn](args[0]);
2010-01-07 20:21:07 +00:00
} else if (length == 2) {
2010-02-10 19:50:10 +00:00
ret = that.$element[fn](args[0], args[1]);
2010-01-07 20:21:07 +00:00
} else if (length == 3) {
2010-02-10 19:50:10 +00:00
ret = that.$element[fn](args[0], args[1], args[2]);
2010-01-07 20:21:07 +00:00
} else if (length == 4) {
2010-02-10 19:50:10 +00:00
ret = that.$element[fn](args[0], args[1], args[2], args[3]);
2010-01-07 20:21:07 +00:00
}
2010-09-03 20:54:40 +00:00
if (fn == 'data') {
// Ox.print('data ret', ret, $(ret))
2010-02-07 15:01:22 +00:00
}
2010-01-07 20:21:07 +00:00
// if the $element of an ox object was returned
// then return the ox object instead
// so we can do oxObj.jqFn().oxFn()
2010-09-03 20:54:40 +00:00
return ret.jquery && $elements[id = ret.data('ox')] ?
2010-06-30 09:27:02 +00:00
$elements[id] : ret;
2010-01-07 20:21:07 +00:00
}
});
}
// shared
self.onChange = function() {
2010-02-07 15:01:22 +00:00
// self.onChange(key, value)
2010-01-07 20:21:07 +00:00
// is called when an option changes
// (to be implemented by widget)
};
// public
2010-09-03 08:47:40 +00:00
2010-02-05 09:13:03 +00:00
that.bindEvent = function() {
/*
2010-09-03 08:47:40 +00:00
binds a function to an event triggered by this object
2010-02-05 09:13:03 +00:00
bindEvent(event, fn) or bindEvent({event0: fn0, event1: fn1, ...})
*/
if (arguments.length == 1) {
$.each(arguments[0], function(event, fn) {
2010-12-26 20:16:35 +00:00
Ox.print(that.id, 'bind', event);
2010-12-31 11:01:35 +00:00
self.$eventHandler.bind('ox_' + event, fn);
2010-02-10 16:37:26 +00:00
});
2010-02-05 09:13:03 +00:00
} else {
2010-12-26 20:16:35 +00:00
Ox.print(that.id, 'bind', arguments[0]);
2010-12-31 11:01:35 +00:00
self.$eventHandler.bind('ox_' + arguments[0], arguments[1]);
2010-02-05 09:13:03 +00:00
}
return that;
2010-09-03 08:47:40 +00:00
}
2010-01-07 20:21:07 +00:00
that.defaults = function(defaults) {
/*
that.defaults({foo: x}) sets self.defaults
*/
self.defaults = defaults;
delete self.options; // fixme: hackish fix for that = Ox.Foo({...}, self).defaults({...}).options({...})
2010-01-07 20:21:07 +00:00
return that;
2010-02-05 09:13:03 +00:00
};
2010-09-03 08:47:40 +00:00
2010-02-05 09:13:03 +00:00
that.gainFocus = function() {
Ox.Focus.focus(that.id);
2010-02-09 05:43:36 +00:00
return that;
2010-02-05 09:13:03 +00:00
};
2010-09-03 08:47:40 +00:00
2010-02-05 09:13:03 +00:00
that.hasFocus = function() {
return Ox.Focus.focused() == that.id;
};
2010-09-03 08:47:40 +00:00
2010-02-05 09:13:03 +00:00
that.loseFocus = function() {
Ox.Focus.blur(that.id);
2010-02-09 05:43:36 +00:00
return that;
2010-02-05 09:13:03 +00:00
};
2010-09-03 08:47:40 +00:00
2010-02-05 09:13:03 +00:00
that.options = function() { // fixme: use Ox.getset
2010-01-07 20:21:07 +00:00
/*
that.options() returns self.options
2010-09-03 20:54:40 +00:00
that.options('foo') returns self.options.foo
that.options('foo', x) sets self.options.foo,
2010-01-07 20:21:07 +00:00
returns that
that.options({foo: x, bar: y}) sets self.options.foo
and self.options.bar,
returns that
*/
2010-12-24 17:13:18 +00:00
var args,
length = arguments.length,
oldOptions,
ret;
2010-01-07 20:21:07 +00:00
if (length == 0) {
// options()
2011-01-02 10:01:55 +00:00
ret = self.options;
2010-09-03 20:54:40 +00:00
} else if (length == 1 && typeof arguments[0] == 'string') {
2010-01-07 20:21:07 +00:00
// options(str)
2010-07-06 18:28:58 +00:00
ret = self.options ? self.options[arguments[0]] : options[arguments[0]];
2010-01-07 20:21:07 +00:00
} else {
// options (str, val) or options({str: val, ...})
// translate (str, val) to ({str: val})
2011-01-02 10:01:55 +00:00
args = Ox.makeObject.apply(that, arguments || {});
2010-12-24 17:13:18 +00:00
oldOptions = $.extend({}, self.options);
// if options have not been set, extend defaults,
// otherwise, extend options
2011-01-02 10:01:55 +00:00
//self.options = $.extend(self.options, self.options ? {} : self.defaults, args);
self.options = $.extend({}, self.defaults, self.options, args);
//self.options = $.extend(self.options || self.defaults, args);
$.each(args, function(key, value) {
2010-12-26 20:16:35 +00:00
// key == 'id' && id && Ox.Event.changeId(id, value);
/*!Ox.equals(value, oldOptions[key]) &&*/ self.onChange(key, value);
2010-01-07 20:21:07 +00:00
});
ret = that;
2010-01-07 20:21:07 +00:00
}
return ret;
2010-02-10 16:37:26 +00:00
};
2010-09-03 08:47:40 +00:00
2010-12-27 05:01:24 +00:00
that.remove = function() { // fixme: clashes with jquery, should be removeElement
2011-01-03 23:38:43 +00:00
that.loseFocus();
delete self.$eventHandler;
2010-01-07 20:21:07 +00:00
that.$element.remove();
2010-06-30 09:27:02 +00:00
delete $elements[that.ox];
return that;
2010-02-10 16:37:26 +00:00
};
2010-09-03 08:47:40 +00:00
2010-12-26 20:16:35 +00:00
that.triggerEvent = function() {
2010-09-03 08:47:40 +00:00
/*
2010-12-26 20:16:35 +00:00
triggers an event
triggerEvent(event) or triggerEvent(event, data) or triggerEvent({event0: data, event1: data, ...})
2010-09-03 08:47:40 +00:00
*/
2010-12-26 20:16:35 +00:00
if (Ox.isObject(arguments[0])) {
$.each(arguments[0], function(event, data) {
2010-12-29 06:50:40 +00:00
Ox.print(that.id, self.options.id, 'trigger', event, data);
2010-12-31 11:01:35 +00:00
self.$eventHandler.trigger('ox_' + event, data);
2010-09-03 08:47:40 +00:00
});
} else {
2011-01-02 10:01:55 +00:00
Ox.print('??', that.id, self.options ? self.options.id : '', 'trigger', arguments[0], arguments[1] || {});
2010-12-31 11:01:35 +00:00
self.$eventHandler.trigger('ox_' + arguments[0], arguments[1] || {});
2010-09-03 08:47:40 +00:00
}
return that;
};
2010-02-05 09:13:03 +00:00
that.unbindEvent = function() {
2010-12-26 20:16:35 +00:00
/*
unbinds an event
unbindEvent(event, fn) or unbindEvent({event0: fn0, event1: fn1, ...})
*/
if (arguments.length == 1) {
$.each(arguments[0], function(event, fn) {
Ox.print(that.id, 'unbind', arguments[0]);
2010-12-31 11:01:35 +00:00
self.$eventHandler.unbind('ox_' + event, fn);
2010-12-26 20:16:35 +00:00
});
} else {
Ox.print(that.id, 'unbind', arguments[0]);
2010-12-31 11:01:35 +00:00
self.$eventHandler.unbind('ox_' + arguments[0], arguments[1]);
2010-02-05 09:13:03 +00:00
}
return that;
2010-02-10 16:37:26 +00:00
};
2010-01-07 20:21:07 +00:00
// return
return that;
}
}();
Ox._Element = function(element) {
var that = this;
that.def = {};
that.opt = {};
that.ox = Ox.version;
that.id = Ox.uid();
2010-09-03 20:54:40 +00:00
//Ox.print('that.id', that.id)
that.$element = $('<' + (element || 'div') + '/>')
//.addClass('OxElement')
.data('ox', that.id);
2010-01-07 20:21:07 +00:00
oxui.elements[that.id] = that;
2010-09-03 20:54:40 +00:00
// Ox.print('oxui.elements', oxui.elements)
2010-01-07 20:21:07 +00:00
//function setOption() {};
that.setOption = function() {};
/*
*/
that.destroy = function() {
that.$element.remove();
delete oxui.elements[that.ox];
}
/*
*/
that.disable = function() {
}
/*
*/
that.enable = function() {
}
/*
*/
///*
that.defaults = function() {
var length = arguments.length,
ret;
if (length == 0) {
ret = that.def
2010-09-03 20:54:40 +00:00
} else if (length == 1 && typeof arguments[0] == 'string') {
2010-01-07 20:21:07 +00:00
ret = that.def[arguments[0]];
} else {
2010-09-03 20:54:40 +00:00
// translate ('key', 'value') to {'key': 'value'}
2010-01-07 20:21:07 +00:00
that.def = $.extend(
that.def, Ox.makeObject.apply(that, arguments)
);
ret = that;
}
return ret;
}
//*/
/*
Ox.Element.options()
get options
2010-09-03 20:54:40 +00:00
Ox.Element.options('foo')
2010-01-07 20:21:07 +00:00
get options.foo
2010-09-03 20:54:40 +00:00
Ox.Element.options('foo', 0)
2010-01-07 20:21:07 +00:00
set options.foo
Ox.Element.options({foo: 0, bar: 1})
set options.foo and options.bar
*/
///*
that.options = function() {
var length = arguments.length,
args, ret;
if (length == 0) {
2010-09-03 20:54:40 +00:00
//Ox.print('getting all options', options);
2010-01-07 20:21:07 +00:00
ret = that.opt;
2010-09-03 20:54:40 +00:00
} else if (length == 1 && typeof arguments[0] == 'string') {
//Ox.print('getting one option', options, arguments[0], options[arguments[0]]);
2010-01-07 20:21:07 +00:00
ret = that.opt[arguments[0]];
} else {
2010-09-03 20:54:40 +00:00
// translate ('key', 'value') to {'key': 'value'}
2010-01-07 20:21:07 +00:00
args = Ox.makeObject.apply(that, arguments);
// if options have been set then extend options,
// otherwise extend defaults
that.opt = $.extend(Ox.length(that.opt) ?
that.opt : that.def, args);
2010-09-03 20:54:40 +00:00
// that.trigger('OxElement' + that.id + 'SetOptions', args);
2010-01-07 20:21:07 +00:00
$.each(args, function(k, v) {
that.setOption(k, v);
2010-09-03 20:54:40 +00:00
//Ox.print('triggering', 'OxElement' + that.id + 'SetOption', {k: v})
//that.trigger('OxElement' + that.id + 'SetOption', {k: v});
2010-01-07 20:21:07 +00:00
})
ret = that;
}
return ret;
}
// should become self.publish
that.publish = function(event, data) {
Ox.Event.publish(event + that.id, data);
return that;
}
that.subscribe = function(event, callback) {
Ox.Event.subscribe(event, callback);
return that;
}
//that.setOptions = function() {};
//*/
// wrap jquery functions
// so we can do oxObj.jqFn()
$.each(oxui.jqueryFunctions, function(i, v) {
that[v] = function() {
var args = arguments,
length = args.length,
$element, id, ret;
$.each(args, function(i, v) {
// if an oxui object was passed
// then pass its $element instead
// so we can do jqObj.append(oxObj)
if (v.ox) {
args[i] = v.$element;
}
});
2010-09-03 20:54:40 +00:00
if (v == 'html' && that.$content) {
2010-01-07 20:21:07 +00:00
$element = that.$content;
} else {
$element = that.$element;
}
// why does this not work?
// ret = that.$element[v].apply(this, arguments);
// maybe because we pass this, and not that.$element[v] ... ?
// ret = that.$element[v].apply(that.$element[v], arguments);
// doesn't work either ...
if (length == 0) {
ret = $element[v]();
} else if (length == 1) {
ret = $element[v](args[0]);
} else if (length == 2) {
ret = $element[v](args[0], args[1]);
} else if (length == 3) {
ret = $element[v](args[0], args[1], args[2]);
} else if (length == 4) {
ret = $element[v](args[0], args[1], args[2], args[3]);
}
// if the $element of an oxui object was returned
// then return the oxui object instead
// so we can do oxObj.jqFn().oxFn()
2010-09-03 20:54:40 +00:00
//Ox.print('v', v, 'arguments', arguments)
2010-01-07 20:21:07 +00:00
if (ret.jquery) {
2010-09-03 20:54:40 +00:00
//Ox.print('ret', ret, 'ret.data('id')', ret.data('ox'))
2010-01-07 20:21:07 +00:00
}
2010-09-03 20:54:40 +00:00
return ret.jquery && oxui.elements[id = ret.data('ox')] ?
2010-01-07 20:21:07 +00:00
oxui.elements[id] : ret;
}
});
return that;
};
2010-12-06 17:42:45 +00:00
/**
2010-07-24 01:32:08 +00:00
Ox.Window
*/
Ox.Window = function(options, self) {
self = self || {},
2010-09-03 20:54:40 +00:00
that = new Ox.Element('div', self)
2010-07-24 01:32:08 +00:00
.defaults({
draggable: true,
fullscreenable: true, // fixme: silly name
height: 225,
resizeable: true,
scaleable: true,
width: 400
})
.options(options || {})
self.center = function() {
};
self.drag = function() {
};
self.fullscreen = function() {
};
self.onChange = function() {
};
self.reset = function() {
};
self.resize = function() {
};
self.scale = function() {
};
that.close = function() {
};
that.open = function() {
};
return that;
};
2010-01-07 20:21:07 +00:00
2010-09-03 22:12:25 +00:00
// fixme: this should be Ox.Theme, and provide Ox.Theme.set(), Ox.Theme.load, etc.
2010-12-06 17:42:45 +00:00
/**
if name is given as argument, switch to this theme.
return current theme otherwise.
Ox.theme()
get theme
Ox.theme('foo')
set theme to 'foo'
*/
2010-01-07 20:21:07 +00:00
Ox.theme = function() {
var length = arguments.length,
2010-09-03 20:54:40 +00:00
classes = $body.attr('class').split(' '),
2010-01-07 20:21:07 +00:00
arg, theme;
$.each(classes, function(i, v) {
2010-09-03 20:54:40 +00:00
if (Ox.startsWith(v, 'OxTheme')) {
theme = v.replace('OxTheme', '').toLowerCase();
2010-01-07 20:21:07 +00:00
if (length == 1) {
$body.removeClass(v);
}
return false;
}
});
if (length == 1) {
arg = arguments[0]
2010-09-03 20:54:40 +00:00
$body.addClass('OxTheme' + Ox.toTitleCase(arg));
2010-01-07 20:21:07 +00:00
if (theme) {
2010-09-03 20:54:40 +00:00
$('input[type=image]').each(function() {
2010-01-07 20:21:07 +00:00
var $this = $(this);
$this.attr({
2010-09-03 20:54:40 +00:00
src: $this.attr('src').replace(
'/ox.ui.' + theme + '/', '/ox.ui.' + arg + '/'
2010-01-07 20:21:07 +00:00
)
});
});
2010-09-03 20:54:40 +00:00
$('.OxLoadingIcon').each(function() {
2010-02-20 10:34:50 +00:00
var $this = $(this);
$this.attr({
2010-09-03 20:54:40 +00:00
src: $this.attr('src').replace(
'/ox.ui.' + theme + '/', '/ox.ui.' + arg + '/'
2010-02-20 10:34:50 +00:00
)
});
})
2010-01-07 20:21:07 +00:00
}
}
return theme;
};
2010-12-06 17:42:45 +00:00
/**
2010-01-07 20:21:07 +00:00
*/
Ox.Bar = function(options, self) {
var self = self || {},
that = new Ox.Element({}, self)
.defaults({
2010-09-03 20:54:40 +00:00
orientation: 'horizontal',
size: 'medium' // can be int
2010-01-07 20:21:07 +00:00
})
2010-02-18 07:27:32 +00:00
.options(options || {})
2010-09-03 20:54:40 +00:00
.addClass('OxBar Ox' + Ox.toTitleCase(self.options.orientation)),
2010-01-07 20:21:07 +00:00
dimensions = oxui.getDimensions(self.options.orientation);
2010-02-20 03:58:46 +00:00
self.options.size = Ox.isString(self.options.size) ?
oxui.getBarSize(self.options.size) : self.options.size;
2010-09-03 20:54:40 +00:00
that.css(dimensions[0], '100%')
.css(dimensions[1], self.options.size + 'px');
2010-01-07 20:21:07 +00:00
return that;
};
2010-12-06 17:42:45 +00:00
/**
2010-07-06 18:28:58 +00:00
*/
Ox.Resizebar = function(options, self) {
var self = self || {},
that = new Ox.Element({}, self)
.defaults({
2010-07-07 07:18:38 +00:00
collapsed: false,
2010-07-06 18:28:58 +00:00
collapsible: true,
2010-09-03 20:54:40 +00:00
edge: 'left',
2010-07-06 18:28:58 +00:00
elements: [],
2010-09-03 20:54:40 +00:00
orientation: 'horizontal',
2010-07-06 18:28:58 +00:00
parent: null,
resizable: true,
resize: [],
size: 0
})
.options(options || {}) // fixme: options function should be able to handle undefined, no need for || {}
2010-09-03 20:54:40 +00:00
.addClass('OxResizebar Ox' + Ox.toTitleCase(self.options.orientation))
2010-07-06 18:28:58 +00:00
/*
.attr({
2010-09-03 20:54:40 +00:00
draggable: 'true'
2010-07-06 18:28:58 +00:00
})
2010-09-03 20:54:40 +00:00
.bind('dragstart', function(e) {
// e.originalEvent.dataTransfer.setDragImage($('<div>')[0], 0, 0);
2010-07-06 18:28:58 +00:00
})
2010-09-03 20:54:40 +00:00
.bind('drag', function(e) {
Ox.print('dragging', e)
2010-07-06 18:28:58 +00:00
})
*/
.mousedown(dragStart)
.dblclick(toggle)
2010-09-03 20:54:40 +00:00
.append($('<div>').addClass('OxSpace'))
.append($('<div>').addClass('OxLine'))
.append($('<div>').addClass('OxSpace'));
2010-07-06 18:28:58 +00:00
$.extend(self, {
2010-09-03 20:54:40 +00:00
clientXY: self.options.orientation == 'horizontal' ? 'clientY' : 'clientX',
2010-07-15 19:04:47 +00:00
dimensions: oxui.getDimensions(self.options.orientation), // fixme: should orientation be the opposite orientation here?
edges: oxui.getEdges(self.options.orientation),
2010-09-17 16:37:11 +00:00
leftOrTop: self.options.edge == 'left' || self.options.edge == 'top',
2010-07-06 18:28:58 +00:00
startPos: 0,
startSize: 0
});
function drag(e) {
var d = e[self.clientXY] - self.startPos
size = self.options.size;
2010-09-17 16:37:11 +00:00
self.options.size = Ox.limit(
self.startSize + d * (self.leftOrTop ? 1 : -1),
self.options.resize[0],
self.options.resize[self.options.resize.length - 1]
);
2010-07-06 18:28:58 +00:00
$.each(self.options.resize, function(i, v) {
if (self.options.size >= v - 8 && self.options.size <= v + 8) {
2010-07-06 18:28:58 +00:00
self.options.size = v;
return false;
}
});
if (self.options.size != size) {
that.css(self.edges[self.leftOrTop ? 2 : 3], self.options.size + 'px');
// fixme: send {size: x}, not x
if (self.leftOrTop) {
2010-12-27 05:01:24 +00:00
self.options.elements[0]
.css(self.dimensions[1], self.options.size + 'px')
.triggerEvent('resize', self.options.size);
self.options.elements[1]
.css(self.edges[2], (self.options.size + 1) + 'px')
.triggerEvent('resize', self.options.elements[1][self.dimensions[1]]());
self.options.parent.updateSize(0, self.options.size);
} else {
2010-12-27 05:01:24 +00:00
self.options.elements[0]
.css(self.edges[3], (self.options.size + 1) + 'px')
.triggerEvent('resize', self.options.elements[0][self.dimensions[1]]());
self.options.elements[1]
.css(self.dimensions[1], self.options.size + 'px')
.triggerEvent('resize', self.options.size);
self.options.parent.updateSize(1, self.options.size); // fixme: listen to event instead?
}
2010-09-17 16:37:11 +00:00
}
2010-07-06 18:28:58 +00:00
}
function dragStart(e) {
if (self.options.resizable && !self.options.collapsed) {
2010-11-25 10:05:50 +00:00
self.startPos = e[self.clientXY];
self.startSize = self.options.size;
Ox.print('startSize', self.startSize)
$window.mousemove(drag);
$window.one('mouseup', dragStop);
}
2010-07-06 18:28:58 +00:00
}
function dragStop() {
2010-09-03 20:54:40 +00:00
$window.unbind('mousemove');
2010-07-06 18:28:58 +00:00
}
function toggle() {
if (self.options.collapsible) {
self.options.parent.toggle(self.leftOrTop ? 0 : self.options.elements.length - 1);
self.options.collapsed = !self.options.collapsed;
}
2010-11-28 15:06:47 +00:00
/*
2010-09-03 20:54:40 +00:00
Ox.print('toggle');
if (Ox.isUndefined(self.options.position)) {
self.options.position = parseInt(self.options.parent.css(self.options.edge)) +
2010-11-28 15:06:47 +00:00
(self.options.collapsed ? self.options.size : 0);
}
var size = self.options.position -
(self.options.collapsed ? 0 : self.options.size),
2010-07-07 07:18:38 +00:00
animate = {};
2010-09-03 20:54:40 +00:00
Ox.print('s.o.e', self.options.edge);
2010-07-07 07:18:38 +00:00
animate[self.options.edge] = size;
self.options.parent.animate(animate, 200, function() {
2010-11-28 15:06:47 +00:00
var i = (self.options.edge == 'left' || self.options.edge == 'top') ? 0 : 1;
2010-07-07 07:18:38 +00:00
self.options.collapsed = !self.options.collapsed;
2010-11-28 15:06:47 +00:00
Ox.Event.trigger(self.ids[i], 'toggle', self.options.collapsed);
Ox.Event.trigger(self.ids[1 - i], 'resize', self.options.elements[1 - i][self.dimensions[1]]());
2010-07-07 07:18:38 +00:00
});
2010-11-28 15:06:47 +00:00
*/
2010-07-06 18:28:58 +00:00
}
return that;
};
2010-12-06 17:42:45 +00:00
/**
2010-01-07 20:21:07 +00:00
*/
Ox.Tabbar = function(options, self) {
var self = self || {},
that = new Ox.Bar({
size: 20
}, self)
.defaults({
selected: 0,
2010-02-10 09:59:59 +00:00
tabs: []
2010-01-07 20:21:07 +00:00
})
.options(options || {})
2010-09-03 20:54:40 +00:00
.addClass('OxTabbar');
2010-01-07 20:21:07 +00:00
2010-02-08 09:35:24 +00:00
Ox.ButtonGroup({
2010-02-10 09:59:59 +00:00
buttons: self.options.tabs,
2010-02-08 09:35:24 +00:00
group: true,
selectable: true,
selected: self.options.selected,
2010-09-03 20:54:40 +00:00
size: 'medium',
style: 'tab',
2010-02-08 09:35:24 +00:00
}).appendTo(that);
2010-01-07 20:21:07 +00:00
return that;
};
2010-12-06 17:42:45 +00:00
/**
fixme: no need for this
*/
2010-01-07 20:21:07 +00:00
Ox.Toolbar = function(options, self) {
var self = self || {},
that = new Ox.Bar({
2010-02-07 15:01:22 +00:00
size: oxui.getBarSize(options.size)
}, self);
2010-01-07 20:21:07 +00:00
return that;
};
2010-12-06 17:42:45 +00:00
/**
2010-01-27 12:30:00 +00:00
*/
Ox.Dialog = function(options, self) {
2010-02-20 07:46:31 +00:00
2010-02-18 07:27:32 +00:00
// fixme: dialog should be derived from a generic draggable
2010-09-04 14:28:40 +00:00
// fixme: buttons should have a close attribute, or the dialog a close id
2010-01-27 12:30:00 +00:00
var self = self || {},
2010-09-03 20:54:40 +00:00
that = new Ox.Element('div', self)
2010-02-20 03:42:03 +00:00
.defaults({
2010-09-03 20:54:40 +00:00
title: '',
2010-02-20 03:42:03 +00:00
buttons: [],
2010-12-24 17:13:18 +00:00
content: null,
2010-02-21 06:47:18 +00:00
height: 216,
2010-12-26 20:16:35 +00:00
keys: {},
2010-02-21 06:47:18 +00:00
minHeight: 144,
2010-02-21 05:40:11 +00:00
minWidth: 256,
2010-09-13 11:53:31 +00:00
movable: true,
2010-07-24 01:32:08 +00:00
padding: 16,
2010-09-13 11:53:31 +00:00
resizable: true,
2010-02-21 05:20:39 +00:00
width: 384
2010-02-20 03:42:03 +00:00
})
.options(options || {})
2010-09-04 14:28:40 +00:00
.addClass('OxDialog')
2010-12-26 20:16:35 +00:00
.bindEvent({
key_enter: function() {
keypress('enter');
},
2010-09-04 14:28:40 +00:00
key_escape: function() {
2010-12-26 20:16:35 +00:00
Ox.print('KEY ESCAPE')
keypress('escape');
2010-09-04 14:28:40 +00:00
}
});
2010-02-20 07:46:31 +00:00
2010-09-13 11:53:31 +00:00
$.extend(self, {
initialWidth: self.options.width,
initialHeight: self.options.height
})
2010-01-31 08:03:22 +00:00
that.$titlebar = new Ox.Bar({
2010-09-03 20:54:40 +00:00
size: 'medium'
2010-01-31 08:03:22 +00:00
})
2010-09-03 20:54:40 +00:00
.addClass('OxTitleBar')
2010-01-27 12:30:00 +00:00
.appendTo(that);
2010-09-13 11:53:31 +00:00
self.options.movable && that.$titlebar
.mousedown(drag)
.dblclick(center);
2010-09-04 14:28:40 +00:00
2010-02-09 05:43:36 +00:00
that.$title = new Ox.Element()
2010-09-03 20:54:40 +00:00
.addClass('OxTitle')
2010-02-20 03:42:03 +00:00
.html(self.options.title)
2010-02-09 05:43:36 +00:00
.appendTo(that.$titlebar);
2010-09-04 14:28:40 +00:00
2010-07-24 01:32:08 +00:00
// fixme: should the following be a container?
that.$content = new Ox.Element()
2010-09-03 20:54:40 +00:00
.addClass('OxContent')
2010-07-24 01:32:08 +00:00
.css({
2010-09-03 20:54:40 +00:00
padding: self.options.padding + 'px',
overflow: 'auto'
2010-07-24 01:32:08 +00:00
})
2010-12-24 17:13:18 +00:00
.append(self.options.content)
2010-01-27 12:30:00 +00:00
.appendTo(that);
2010-09-04 14:28:40 +00:00
2010-02-20 04:26:53 +00:00
that.$buttonsbar = new Ox.Bar({})
2010-09-03 20:54:40 +00:00
.addClass('OxButtonsBar')
2010-01-27 12:30:00 +00:00
.appendTo(that);
2010-12-24 17:13:18 +00:00
loadButtons();
2010-09-04 14:28:40 +00:00
2010-12-26 20:16:35 +00:00
//that.$buttons[0].focus();
2010-09-04 14:28:40 +00:00
that.$layer = new Ox.Element() // fixme: Layer widget that would handle click?
2010-09-03 20:54:40 +00:00
.addClass('OxLayer')
2010-02-21 07:09:32 +00:00
.mousedown(mousedownLayer)
.mouseup(mouseupLayer);
2010-02-20 07:46:31 +00:00
function center() {
2010-07-20 20:04:13 +00:00
var documentHeight = $document.height();
that.css({
left: 0,
2010-09-03 20:54:40 +00:00
top: Math.max(parseInt(-documentHeight / 10), self.options.height - documentHeight + 40) + 'px',
right: 0,
bottom: 0,
2010-09-03 20:54:40 +00:00
margin: 'auto'
});
}
2010-02-21 05:20:39 +00:00
function drag(event) {
var bodyWidth = $body.width(),
bodyHeight = $document.height(),
2010-02-21 06:47:18 +00:00
elementWidth = that.width(),
2010-02-21 05:20:39 +00:00
offset = that.offset(),
x = event.clientX,
y = event.clientY;
2010-02-21 05:20:39 +00:00
$window.mousemove(function(event) {
that.css({
margin: 0
});
2010-02-21 05:20:39 +00:00
var left = Ox.limit(
offset.left - x + event.clientX,
24 - elementWidth, bodyWidth - 24
//0, documentWidth - elementWidth
2010-02-21 05:20:39 +00:00
),
top = Ox.limit(
offset.top - y + event.clientY,
24, bodyHeight - 24
//24, documentHeight - elementHeight
2010-02-21 05:20:39 +00:00
);
that.css({
2010-09-03 20:54:40 +00:00
left: left + 'px',
top: top + 'px'
2010-02-21 05:20:39 +00:00
});
});
2010-09-03 20:54:40 +00:00
$window.one('mouseup', function() {
$window.unbind('mousemove');
});
2010-02-21 05:20:39 +00:00
}
2010-07-24 01:32:08 +00:00
function getButtonById(id) {
2010-12-26 20:16:35 +00:00
var ret = null;
Ox.print('that.$buttons', that.$buttons, id)
2010-07-24 01:32:08 +00:00
$.each(that.$buttons, function(i, button) {
2010-09-03 20:54:40 +00:00
if (button.options('id') == id) {
2010-07-24 01:32:08 +00:00
ret = button;
return false;
}
});
return ret;
}
2010-12-26 20:16:35 +00:00
function keypress(key) {
var id = self.options.keys[key];
Ox.print('X', key, self.options.keys)
id && getButtonById(id).$element.trigger('click');
}
2010-12-24 17:13:18 +00:00
function loadButtons() {
2010-12-26 20:16:35 +00:00
Ox.print('loadButtons', $.map(self.options.buttons, function(v) {
return v;
}));
2010-12-24 17:13:18 +00:00
if (that.$buttons) {
that.$buttons.forEach(function($button) {
$button.remove();
});
that.$resize.remove();
2010-12-26 20:16:35 +00:00
// that.$buttonsbar.empty();
2010-12-24 17:13:18 +00:00
}
that.$buttons = [];
if (!Ox.isArray(self.options.buttons[0])) {
self.options.buttons = [[], self.options.buttons];
}
Ox.print('--- one', self.options.buttons[0]);
$.each(self.options.buttons[0], function(i, button) {
// Ox.print('---', button, self.options.buttons)
that.$buttons[i] = button
.addClass('OxLeft')
.appendTo(that.$buttonsbar);
});
if (self.options.resizable) {
that.$resize = new Ox.Element()
.addClass('OxResize')
.mousedown(resize)
.dblclick(reset)
.appendTo(that.$buttonsbar);
}
Ox.print('--- two', self.options.buttons[1]);
$.each(self.options.buttons[1].reverse(), function(i, button) {
//Ox.print('---', button, self.options.buttons)
that.$buttons[that.$buttons.length] = button
.addClass('OxRight')
.appendTo(that.$buttonsbar);
});
}
2010-02-21 07:09:32 +00:00
function mousedownLayer() {
that.$layer.stop().animate({
opacity: 0.5
}, 0);
2010-02-21 07:09:32 +00:00
}
function mouseupLayer() {
that.$layer.stop().animate({
opacity: 0
}, 0);
2010-02-21 07:09:32 +00:00
}
2010-02-21 06:47:18 +00:00
function reset() {
2010-09-13 11:53:31 +00:00
$.extend(self.options, {
height: self.initialHeight,
width: self.initialWidth
});
2010-09-04 14:28:40 +00:00
that/*.css({
2010-07-24 01:32:08 +00:00
left: Math.max(that.offset().left, 24 - that.width())
2010-09-04 14:28:40 +00:00
})*/
.width(self.options.width)
.height(self.options.height);
2010-07-24 01:32:08 +00:00
that.$content.height(self.options.height - 48 - 2 * self.options.padding); // fixme: this should happen automatically
2010-09-13 11:53:31 +00:00
triggerResizeEvent();
2010-02-21 06:47:18 +00:00
}
2010-11-25 10:05:50 +00:00
function resize(event) { // fixme: reserved jquery string?
2010-07-24 01:32:08 +00:00
var documentWidth = $document.width(),
2010-02-21 05:20:39 +00:00
documentHeight = $document.height(),
elementWidth = that.width(),
elementHeight = that.height(),
2010-02-21 05:40:11 +00:00
offset = that.offset(),
2010-02-21 05:20:39 +00:00
x = event.clientX,
y = event.clientY;
$window.mousemove(function(event) {
2010-02-21 05:34:17 +00:00
that.css({
2010-02-21 05:40:11 +00:00
left: offset.left,
top: offset.top,
2010-02-21 05:34:17 +00:00
margin: 0
});
2010-09-13 11:53:31 +00:00
self.options.width = Ox.limit(
elementWidth - x + event.clientX,
self.options.minWidth, Math.min(documentWidth, documentWidth - offset.left)
);
self.options.height = Ox.limit(
elementHeight - y + event.clientY,
self.options.minHeight, documentHeight - offset.top
);
that.width(self.options.width);
that.height(self.options.height);
that.$content.height(self.options.height - 48 - 2 * self.options.padding); // fixme: this should happen automatically
triggerResizeEvent();
2010-02-21 05:20:39 +00:00
});
2010-09-03 20:54:40 +00:00
$window.one('mouseup', function() {
$window.unbind('mousemove');
});
2010-02-21 05:20:39 +00:00
}
2010-09-13 11:53:31 +00:00
function triggerResizeEvent() {
that.triggerEvent('resize', {
width: self.options.width,
height: self.options.height
});
}
2010-01-31 08:03:22 +00:00
self.onChange = function(key, value) {
2010-12-24 17:13:18 +00:00
if (key == 'buttons') {
loadButtons();
/*
that.$buttonsbar.children().animate({
opacity: 0
}, 100, function() {
loadButtons();
that.$buttonsbar.children().animate({
opacity: 1
}, 100);
});
*/
} else if (key == 'content') {
that.$content.html(value);
} else if (key == 'height' || key == 'width') {
2010-07-24 01:32:08 +00:00
that.animate({
2010-09-03 20:54:40 +00:00
height: self.options.height + 'px',
width: self.options.width + 'px'
2010-09-13 11:53:31 +00:00
}, 100);
2010-07-24 01:32:08 +00:00
that.$content.height(self.options.height - 48 - 2 * self.options.padding); // fixme: this should happen automatically
2010-09-03 20:54:40 +00:00
} else if (key == 'title') {
2010-07-20 20:04:13 +00:00
that.$title.animate({
opacity: 0
2010-09-13 11:53:31 +00:00
}, 100, function() {
2010-07-20 20:04:13 +00:00
that.$title.html(value).animate({
opacity: 1
2010-09-13 11:53:31 +00:00
}, 100);
2010-07-20 20:04:13 +00:00
});
2010-01-31 08:03:22 +00:00
}
}
2010-02-20 07:46:31 +00:00
2010-09-13 11:53:31 +00:00
that.center = function() {
};
2010-02-20 07:46:31 +00:00
2010-01-31 09:32:41 +00:00
that.close = function(callback) {
callback = callback || function() {};
2010-01-27 12:30:00 +00:00
that.animate({
opacity: 0
}, 200, function() {
2010-12-26 20:16:35 +00:00
that.$buttons.forEach(function($button) {
$button.remove();
});
that.loseFocus();
2010-01-27 12:30:00 +00:00
that.$layer.remove();
2010-12-26 20:16:35 +00:00
that.remove();
2010-01-31 09:32:41 +00:00
callback();
});
2010-09-03 20:54:40 +00:00
$window.unbind('mouseup', mouseupLayer)
return that;
2010-09-13 11:53:31 +00:00
};
2010-02-20 07:46:31 +00:00
2010-12-24 17:13:18 +00:00
that.content = function($element) {
that.$content.empty().append($element);
return that;
}
2010-02-20 07:46:31 +00:00
that.disable = function() {
2010-02-10 16:37:26 +00:00
// to be used on submit of form, like login
2010-09-03 20:54:40 +00:00
that.$layer.addClass('OxFront');
2010-07-20 20:04:13 +00:00
return that;
2010-02-10 16:37:26 +00:00
};
2010-02-20 07:46:31 +00:00
2010-07-24 01:32:08 +00:00
that.disableButton = function(id) {
getButtonById(id).options({
disabled: true
});
2010-09-04 14:28:40 +00:00
return that;
2010-07-24 01:32:08 +00:00
};
2010-02-20 07:46:31 +00:00
that.enable = function() {
2010-09-03 20:54:40 +00:00
that.$layer.removeClass('OxFront');
2010-07-20 20:04:13 +00:00
return that;
2010-07-24 01:32:08 +00:00
};
that.enableButton = function(id) {
getButtonById(id).options({
disabled: false
});
2010-09-04 14:28:40 +00:00
return that;
2010-07-24 01:32:08 +00:00
};
2010-02-20 07:46:31 +00:00
2010-01-27 12:30:00 +00:00
that.open = function() {
2010-12-26 20:16:35 +00:00
Ox.print('before open')
2010-02-20 07:46:31 +00:00
that.$layer.appendTo($body);
2010-01-27 12:30:00 +00:00
that.css({
opacity: 0
2010-05-05 18:27:09 +00:00
}).appendTo($body).animate({
2010-01-27 12:30:00 +00:00
opacity: 1
}, 200);
2010-09-04 14:28:40 +00:00
center();
reset();
2010-12-26 20:16:35 +00:00
// fixme: the following line prevents preview-style dialog
that.gainFocus();
2010-09-03 20:54:40 +00:00
$window.bind('mouseup', mouseupLayer)
2010-12-26 20:16:35 +00:00
Ox.print('after open')
2010-01-27 12:30:00 +00:00
return that;
2010-07-24 01:32:08 +00:00
};
2010-02-20 07:46:31 +00:00
2010-11-25 10:05:50 +00:00
that.size = function(width, height, callback) {
2010-09-13 11:53:31 +00:00
$.extend(self, {
initialWidth: width,
initialHeight: height
});
$.extend(self.options, {
width: width,
height: height
});
// fixme: duplicated
that.animate({
height: self.options.height + 'px',
width: self.options.width + 'px'
}, 100, function() {
that.$content.height(self.options.height - 48 - 2 * self.options.padding); // fixme: this should happen automatically
callback();
});
}
2010-01-27 12:30:00 +00:00
return that;
2010-02-20 07:46:31 +00:00
2010-01-27 12:30:00 +00:00
}
2010-01-07 20:21:07 +00:00
/*
============================================================================
Forms
============================================================================
*/
2010-12-06 17:42:45 +00:00
/**
2010-07-24 01:32:08 +00:00
*/
Ox.Filter = function(options, self) {
var self = self || {}
that = new Ox.Element()
.defaults({
})
.options(options || {});
return that;
};
2010-12-06 17:42:45 +00:00
/**
2010-07-24 01:32:08 +00:00
*/
2010-02-10 16:37:26 +00:00
Ox.Form = function(options, self) {
var self = self || {},
2010-09-03 20:54:40 +00:00
that = new Ox.Element('div', self)
.defaults({
2010-09-03 20:54:40 +00:00
error: '',
id: '',
2010-07-24 01:32:08 +00:00
items: [],
submit: null
})
2010-12-24 17:13:18 +00:00
.options(options || {}) // fixme: the || {} can be done once, in the options function
.addClass('OxForm');
2010-07-20 20:04:13 +00:00
$.extend(self, {
2010-07-24 01:32:08 +00:00
$items: [],
$messages: [],
2010-07-20 20:04:13 +00:00
formIsValid: false,
2010-07-24 01:32:08 +00:00
itemIds: [],
itemIsValid: []
});
// fixme: form isn't necessarily empty/invalid
$.each(self.options.items, function(i, item) {
2010-12-26 20:16:35 +00:00
self.itemIds[i] = item.options('id') || item.id;
self.itemIsValid[i] = !!item.value().length;
that.append(self.$items[i] = new Ox.FormItem({element: item}))
2010-09-03 20:54:40 +00:00
.append(self.$messages[i] = new Ox.Element().addClass('OxFormMessage'));
2010-12-26 20:16:35 +00:00
item.bindEvent({
2010-12-24 17:13:18 +00:00
/*
2010-12-06 17:42:05 +00:00
blur: function(event, data) {
validate(i, data.valid);
if (data.valid) {
self.$messages[i].html('').hide();
} else {
self.$messages[i].html(data.message).show();
}
},
2010-12-24 17:13:18 +00:00
*/
2010-12-26 20:16:35 +00:00
autovalidate: function(event, data) {
data.valid = !!data.value.length;
validate(i, data.valid);
if (data.valid) {
self.$messages[i].html('').hide();
} else {
//self.$messages[i].html(data.message).show();
}
},
2010-12-06 17:42:05 +00:00
submit: function(event, data) {
self.formIsValid && that.submit();
2010-12-24 17:13:18 +00:00
},
validate: function(event, data) {
validate(i, data.valid);
if (data.valid) {
self.$messages[i].html('').hide();
} else {
self.$messages[i].html(data.message).show();
}
2010-12-06 17:42:05 +00:00
}
});
});
2010-07-24 01:32:08 +00:00
function getItemPositionById(id) {
return self.itemIds.indexOf(id);
}
function setMessage(id, message) {
2010-09-03 20:54:40 +00:00
self.$messages[getItemPositionById(id)].html(message)[message !== '' ? 'show' : 'hide']();
2010-07-24 01:32:08 +00:00
}
function submitCallback(data) {
$.each(data, function(i, v) {
setMessage(v.id, v.message);
});
}
function validate(pos, valid) {
2010-12-26 20:16:35 +00:00
Ox.print('FORM validate', pos, valid)
2010-07-24 01:32:08 +00:00
self.itemIsValid[pos] = valid;
2010-07-20 20:04:13 +00:00
if (Ox.every(self.itemIsValid) != self.formIsValid) {
self.formIsValid = !self.formIsValid;
2010-09-03 20:54:40 +00:00
that.triggerEvent('validate', {
2010-07-24 01:32:08 +00:00
valid: self.formIsValid
2010-07-20 20:04:13 +00:00
});
}
}
2010-07-24 01:32:08 +00:00
that.submit = function() {
2010-12-24 17:13:18 +00:00
Ox.print('---- that.values()', that.values())
2010-07-24 01:32:08 +00:00
self.options.submit(that.values(), submitCallback);
};
that.values = function() { // fixme: can this be private?
2010-12-06 17:42:45 +00:00
/*
get/set form values
call without arguments to get current form values
pass values as array to set values (not implemented)
*/
var values = {};
if (arguments.length == 0) {
2010-07-24 01:32:08 +00:00
$.each(self.$items, function(i, $item) {
values[self.itemIds[i]] = self.$items[i].value();
});
2010-12-26 20:16:35 +00:00
Ox.print('VALUES', values)
return values;
} else {
$.each(arguments[0], function(key, value) {
});
return that;
}
};
return that;
2010-02-10 16:37:26 +00:00
};
2010-12-06 17:42:45 +00:00
/**
*/
Ox.FormItem = function(options, self) {
var self = self || {},
2010-09-03 20:54:40 +00:00
that = new Ox.Element('div', self)
.defaults({
2010-07-20 20:04:13 +00:00
element: null,
2010-09-03 20:54:40 +00:00
error: '',
})
2010-07-20 20:04:13 +00:00
.options(options || {})
2010-09-03 20:54:40 +00:00
.addClass('OxFormItem')
2010-07-20 20:04:13 +00:00
.append(self.options.element);
2010-07-24 01:32:08 +00:00
that.value = function() {
2010-12-24 17:13:18 +00:00
return self.options.element.value();
2010-07-24 01:32:08 +00:00
};
return that;
}
2010-12-06 17:42:45 +00:00
/**
2010-09-03 08:47:40 +00:00
Form Elements
2010-01-07 20:21:07 +00:00
*/
2010-12-06 17:42:45 +00:00
/**
methods:
toggleDisabled enable/disable button
toggleSelected select/unselect button
toggleTitle if more than one title was provided,
toggle to next title.
2010-02-10 09:59:59 +00:00
events:
2010-12-06 17:42:45 +00:00
click non-selectable button was clicked
deselect selectable button was deselected
select selectable button was selected
*/
Ox.Button = function(options, self) {
2010-01-07 20:21:07 +00:00
var self = self || {},
2010-09-03 20:54:40 +00:00
that = new Ox.Element('input', self)
2010-01-07 20:21:07 +00:00
.defaults({
disabled: false,
2010-09-03 08:47:40 +00:00
group: false,
2010-09-03 20:54:40 +00:00
id: '',
overlap: 'none',
2010-01-07 20:21:07 +00:00
selectable: false,
selected: false,
2010-09-03 20:54:40 +00:00
size: 'medium',
// fixme: 'default' or ''?
style: 'default', // can be default, checkbox, symbol, or tab
title: '',
tooltip: '',
type: 'text',
width: 'auto'
2010-01-07 20:21:07 +00:00
})
2010-09-03 08:47:40 +00:00
.options(options || {})
2010-02-06 08:23:42 +00:00
.attr({
2010-09-03 20:54:40 +00:00
disabled: self.options.disabled ? 'disabled' : '',
type: self.options.type == 'text' ? 'button' : 'image'
})
2010-09-03 20:54:40 +00:00
.addClass('OxButton Ox' + Ox.toTitleCase(self.options.size) +
(self.options.disabled ? ' OxDisabled': '') +
(self.options.selected ? ' OxSelected': '') +
(self.options.style != 'default' ? ' Ox' + Ox.toTitleCase(self.options.style) : '') +
(self.options.overlap != 'none' ? ' OxOverlap' + Ox.toTitleCase(self.options.overlap) : ''))
.css(self.options.width == 'auto' ? {} : {
width: (self.options.width - 14) + 'px'
2010-09-03 08:47:40 +00:00
})
.mousedown(mousedown)
.click(click);
2010-09-03 08:47:40 +00:00
$.extend(self, Ox.isArray(self.options.title) ? {
2010-09-03 20:54:40 +00:00
selectedTitle: Ox.setPropertyOnce(self.options.title, 'selected'),
2010-09-03 08:47:40 +00:00
titles: self.options.title
} : {
selectedTitle: 0,
titles: [{
2010-09-03 20:54:40 +00:00
id: '',
2010-09-03 08:47:40 +00:00
title: self.options.title
}]
});
setTitle(self.titles[self.selectedTitle].title);
if (self.options.tooltip) {
self.tooltips = Ox.isArray(self.options.tooltip) ? self.options.tooltip : [self.options.tooltip];
self.$tooltip = new Ox.Tooltip({
title: self.tooltips[self.selectedTitle]
});
that.mouseenter(mouseenter)
.mouseleave(mouseleave);
2010-01-07 20:21:07 +00:00
}
2010-09-03 08:47:40 +00:00
2010-01-07 20:21:07 +00:00
function click() {
2010-12-26 20:16:35 +00:00
if (!self.options.disabled) {
var data = self.titles[self.selectedTitle];
if (!self.options.selectable) {
that.triggerEvent('click', data);
2010-02-10 09:59:59 +00:00
} else {
2010-12-26 20:16:35 +00:00
if (self.options.group) {
that.triggerEvent('select', data);
} else {
that.toggleSelected();
}
}
if (self.titles.length == 2) {
that.toggleTitle();
2010-02-10 09:59:59 +00:00
}
2010-09-03 08:47:40 +00:00
}
}
function mousedown(event) {
2010-09-03 20:54:40 +00:00
if (self.options.type == 'image' && $.browser.safari) {
2010-09-03 08:47:40 +00:00
// keep image from being draggable
event.preventDefault();
}
}
function mouseenter(event) {
self.$tooltip.show(event.clientX, event.clientY);
}
2010-09-03 08:47:40 +00:00
function mouseleave(event) {
self.$tooltip.hide();
}
function setTitle(title) {
self.title = title;
2010-09-03 20:54:40 +00:00
if (self.options.type == 'image') {
2010-09-03 08:47:40 +00:00
that.attr({
2010-09-03 20:54:40 +00:00
src: oxui.path + 'png/ox.ui.' + Ox.theme() +
'/symbol' + Ox.toTitleCase(title) + '.png'
2010-09-03 08:47:40 +00:00
});
} else {
that.val(title);
2010-01-07 20:21:07 +00:00
}
}
2010-09-03 08:47:40 +00:00
self.onChange = function(key, value) {
2010-09-03 20:54:40 +00:00
if (key == 'disabled') {
2010-07-24 01:32:08 +00:00
that.attr({
2010-09-03 20:54:40 +00:00
disabled: value ? 'disabled' : ''
2010-07-24 01:32:08 +00:00
})
2010-09-03 20:54:40 +00:00
.toggleClass('OxDisabled');
} else if (key == 'selected') {
if (value != that.hasClass('OxSelected')) { // fixme: neccessary?
that.toggleClass('OxSelected');
2010-01-07 20:21:07 +00:00
}
2010-09-03 20:54:40 +00:00
that.triggerEvent('change');
} else if (key == 'title') {
2010-09-03 08:47:40 +00:00
setTitle(value);
2010-09-03 20:54:40 +00:00
} else if (key == 'width') {
2010-09-03 08:47:40 +00:00
that.$element.css({
2010-09-03 20:54:40 +00:00
width: (value - 14) + 'px'
2010-09-03 08:47:40 +00:00
});
2010-01-07 20:21:07 +00:00
}
}
2010-09-03 08:47:40 +00:00
2010-01-31 08:03:22 +00:00
that.toggleDisabled = function() {
that.options({
enabled: !self.options.disabled
});
}
2010-09-03 08:47:40 +00:00
2010-01-07 20:21:07 +00:00
that.toggleSelected = function() {
that.options({
selected: !self.options.selected
});
}
2010-09-03 08:47:40 +00:00
that.toggleTitle = function() {
self.selectedTitle = 1 - self.selectedTitle;
setTitle(self.titles[self.selectedTitle].title);
2010-09-03 20:54:40 +00:00
self.$tooltip && self.$tooltip.options({
2010-09-03 08:47:40 +00:00
title: self.tooltips[self.selectedTitle]
});
}
2010-01-07 20:21:07 +00:00
return that;
2010-09-03 08:47:40 +00:00
};
2010-01-07 20:21:07 +00:00
2010-12-06 17:42:45 +00:00
/**
options
2010-09-03 08:47:40 +00:00
buttons array of buttons
max integer, maximum number of selected buttons, 0 for all
min integer, minimum number of selected buttons, 0 for none
selectable if true, buttons are selectable
2010-09-03 20:54:40 +00:00
type string, 'image' or 'text'
2010-12-06 17:42:45 +00:00
methods:
2010-02-10 09:59:59 +00:00
events:
2010-09-03 08:47:40 +00:00
change {id, value} selection within a group changed
2010-12-06 17:42:45 +00:00
*/
Ox.ButtonGroup = function(options, self) {
2010-01-07 20:21:07 +00:00
var self = self || {},
that = new Ox.Element({}, self)
.defaults({
2010-02-10 09:59:59 +00:00
buttons: [],
2010-09-03 08:47:40 +00:00
max: 1,
min: 1,
2010-01-07 20:21:07 +00:00
selectable: false,
2010-09-03 20:54:40 +00:00
size: 'medium',
style: '',
type: 'text',
2010-01-07 20:21:07 +00:00
})
.options(options || {})
2010-09-03 20:54:40 +00:00
.addClass('OxButtonGroup');
2010-02-08 09:35:24 +00:00
2010-09-03 08:47:40 +00:00
if (self.options.selectable) {
self.optionGroup = new Ox.OptionGroup(
self.options.buttons,
self.options.min,
self.options.max,
2010-09-03 20:54:40 +00:00
'selected'
2010-09-03 08:47:40 +00:00
);
self.options.buttons = self.optionGroup.init();
}
self.$buttons = [];
2010-02-10 09:59:59 +00:00
$.each(self.options.buttons, function(position, button) {
2010-09-03 08:47:40 +00:00
var id = self.options.id + Ox.toTitleCase(button.id)
self.$buttons[position] = Ox.Button({
disabled: button.disabled,
group: true,
id: id,
selectable: self.options.selectable,
selected: button.selected,
size: self.options.size,
style: self.options.style,
title: button.title,
type: self.options.type
})
2010-09-03 20:54:40 +00:00
.bindEvent('select', function() {
2010-09-03 08:47:40 +00:00
selectButton(position);
})
.appendTo(that);
2010-02-08 09:35:24 +00:00
});
2010-09-03 08:47:40 +00:00
function selectButton(pos) {
var toggled = self.optionGroup.toggle(pos);
if (toggled.length) {
$.each(toggled, function(i, pos) {
self.$buttons[pos].toggleSelected();
});
2010-09-03 20:54:40 +00:00
that.triggerEvent('change', {
2010-09-03 08:47:40 +00:00
selected: $.map(self.optionGroup.selected(), function(v, i) {
return self.options.buttons[v].id;
})
});
2010-02-08 09:35:24 +00:00
}
}
2010-02-10 09:59:59 +00:00
2010-01-07 20:21:07 +00:00
return that;
};
2010-12-06 17:42:45 +00:00
/**
options
2010-09-03 08:47:40 +00:00
disabled boolean, if true, checkbox is disabled
id element id
group boolean, if true, checkbox is part of a group
checked boolean, if true, checkbox is checked
title string, text on label
width integer, width in px
2010-12-06 17:42:45 +00:00
methods:
2010-09-03 08:47:40 +00:00
toggleChecked function()
toggles checked property
returns that
2010-12-06 17:42:45 +00:00
events:
2010-09-03 08:47:40 +00:00
change triggered when checked property changes
passes {checked, id, title}
2010-12-06 17:42:45 +00:00
*/
Ox.Checkbox = function(options, self) {
2010-02-19 10:24:02 +00:00
2010-01-07 20:21:07 +00:00
var self = self || {},
2010-09-03 20:54:40 +00:00
that = new Ox.Element('div', self)
2010-01-07 20:21:07 +00:00
.defaults({
2010-09-03 08:47:40 +00:00
disabled: false,
2010-09-03 20:54:40 +00:00
id: '',
2010-09-03 08:47:40 +00:00
group: false,
checked: false,
2010-09-03 20:54:40 +00:00
title: '',
width: 'auto'
2010-01-07 20:21:07 +00:00
})
2010-02-18 07:27:32 +00:00
.options(options || {})
2010-09-03 20:54:40 +00:00
.addClass('OxCheckbox')
2010-09-03 08:47:40 +00:00
.attr(self.options.disabled ? {
2010-09-03 20:54:40 +00:00
disabled: 'disabled'
2010-09-03 08:47:40 +00:00
} : {});
2010-02-19 10:24:02 +00:00
2010-09-03 08:47:40 +00:00
if (self.options.title) {
2010-09-03 20:54:40 +00:00
self.options.width != 'auto' && that.css({
width: self.options.width + 'px'
2010-09-03 08:47:40 +00:00
});
self.$title = new Ox.Label({
disabled: self.options.disabled,
2010-09-03 20:54:40 +00:00
id: self.options.id + 'Label',
overlap: 'left',
2010-09-03 08:47:40 +00:00
title: self.options.title,
width: self.options.width - 16
})
.css({
2010-09-03 20:54:40 +00:00
float: 'right'
2010-02-19 10:24:02 +00:00
})
2010-09-03 08:47:40 +00:00
.click(clickTitle)
2010-02-19 10:24:02 +00:00
.appendTo(that);
}
2010-09-03 08:47:40 +00:00
self.$button = new Ox.Button({
disabled: self.options.disabled,
2010-09-03 20:54:40 +00:00
id: self.options.id + 'Button',
2010-09-03 08:47:40 +00:00
title: [
2010-09-03 20:54:40 +00:00
{id: 'none', title: 'none', selected: !self.options.checked},
{id: 'check', title: 'check', selected: self.options.checked}
2010-09-03 08:47:40 +00:00
],
2010-09-03 20:54:40 +00:00
type: 'image'
2010-02-19 10:24:02 +00:00
})
2010-09-03 20:54:40 +00:00
.addClass('OxCheckbox')
2010-09-03 08:47:40 +00:00
.click(clickButton)
2010-02-19 10:24:02 +00:00
.appendTo(that);
2010-09-03 08:47:40 +00:00
function clickButton() {
self.options.checked = !self.options.checked;
// click will have toggled the button,
// if it is part of a group, we have to revert that
self.options.group && that.toggleChecked();
2010-09-03 20:54:40 +00:00
that.triggerEvent('change', {
2010-09-03 08:47:40 +00:00
checked: self.options.checked,
id: self.options.id,
title: self.options.title
2010-02-18 07:27:32 +00:00
});
}
2010-02-19 10:24:02 +00:00
2010-09-03 08:47:40 +00:00
function clickTitle() {
2010-09-03 20:54:40 +00:00
!self.options.disabled && self.$button.trigger('click');
2010-02-26 13:46:14 +00:00
}
2010-07-24 01:32:08 +00:00
2010-09-03 08:47:40 +00:00
self.onChange = function(key, value) {
2010-09-03 20:54:40 +00:00
if (key == 'checked') {
2010-09-03 08:47:40 +00:00
that.toggleChecked();
}
};
2010-12-26 20:16:35 +00:00
that.checked = function() {
return self.options.checked;
}
2010-09-03 08:47:40 +00:00
that.toggleChecked = function() {
self.$button.toggleTitle();
return that;
2010-07-24 01:32:08 +00:00
}
2010-09-03 08:47:40 +00:00
return that;
2010-02-19 10:24:02 +00:00
2010-09-03 08:47:40 +00:00
};
2010-12-06 17:42:45 +00:00
/**
options
checkboxes [] array of checkboxes
max 1 integer
min 1 integer
width integer, width in px
events:
change triggered when checked property changes
passes {checked, id, title}
*/
2010-09-03 08:47:40 +00:00
Ox.CheckboxGroup = function(options, self) {
var self = self || {},
2010-09-03 20:54:40 +00:00
that = new Ox.Element('div', self)
2010-09-03 08:47:40 +00:00
.defaults({
checkboxes: [],
max: 1,
min: 1,
width: 256
})
.options(options || {})
2010-09-03 20:54:40 +00:00
.addClass('OxCheckboxGroup');
2010-09-03 08:47:40 +00:00
self.optionGroup = new Ox.OptionGroup(
self.options.checkboxes,
self.options.min,
self.options.max);
self.options.checkboxes = self.optionGroup.init();
$.extend(self, {
$checkboxes: [],
checkboxWidth: $.map(Ox.divideInt(
self.options.width + (self.options.checkboxes.length - 1) * 6,
self.options.checkboxes.length
), function(v, i) {
return v + (i < self.options.checkboxes.length - 1 ? 10 : 0);
})
2010-09-05 00:31:58 +00:00
});
2010-09-03 08:47:40 +00:00
$.each(self.options.checkboxes, function(position, checkbox) {
var id = self.options.id + Ox.toTitleCase(checkbox.id)
self.$checkboxes[position] = new Ox.Checkbox($.extend(checkbox, {
group: true,
id: id,
width: self.checkboxWidth[position]
}))
2010-09-03 20:54:40 +00:00
.bindEvent('change', function() {
2010-09-03 08:47:40 +00:00
change(position);
})
.appendTo(that);
});
function change(pos) {
var toggled = self.optionGroup.toggle(pos);
2010-09-03 20:54:40 +00:00
//Ox.print('change', pos, 'toggled', toggled)
2010-09-03 08:47:40 +00:00
if (toggled.length) {
$.each(toggled, function(i, pos) {
self.$checkboxes[pos].toggleChecked();
});
2010-09-03 20:54:40 +00:00
that.triggerEvent('change', {
2010-09-03 08:47:40 +00:00
checked: $.map(self.optionGroup.checked(), function(v, i) {
return self.options.checkboxes[v].id;
})
});
}
}
return that;
};
2010-12-06 17:42:45 +00:00
/**
options
2010-09-03 20:54:40 +00:00
arrows boolearn, if true, and type is 'float' or 'integer', display arrows
2010-09-03 08:47:40 +00:00
arrowStep number, step when clicking arrows
autocomplete array of possible values, or
function(key, value, callback), returns one or more values
autocompleteReplace boolean, if true, value is replaced
autocompleteReplaceCorrect boolean, if true, only valid values can be entered
autocompleteSelect boolean, if true, menu is displayed
autocompleteSelectHighlight boolean, if true, value in menu is highlighted
autocompleteSelectSubmit boolean, if true, submit input on menu selection
2010-09-04 14:28:40 +00:00
autocorrect string ('email', 'float', 'integer', 'phone', 'url'), or
2010-09-03 08:47:40 +00:00
regexp(value), or
function(key, value, blur, callback), returns value
2010-09-04 14:28:40 +00:00
auto validate --remote validation--
2010-09-03 08:47:40 +00:00
clear boolean, if true, has clear button
disabled boolean, if true, is disabled
2010-09-03 20:54:40 +00:00
height integer, px (for type='textarea' and type='range' with orientation='horizontal')
2010-09-03 08:47:40 +00:00
id string, element id
key string, to be passed to autocomplete and autovalidate functions
2010-09-03 20:54:40 +00:00
max number, max value if type is 'integer' or 'float'
min number, min value if type is 'integer' or 'float'
name string, will be displayed by autovalidate function ('invalid ' + name)
overlap string, '', 'left' or 'right', will cause padding and negative margin
2010-09-03 08:47:40 +00:00
picker
//rangeOptions
arrows boolean, if true, display arrows
//arrowStep number, step when clicking arrows
//arrowSymbols array of two strings
max number, maximum value
min number, minimum value
2010-09-03 20:54:40 +00:00
orientation 'horizontal' or 'vertical'
2010-09-03 08:47:40 +00:00
step number, step
thumbValue boolean, if true, value is displayed on thumb, or
array of strings per value, or
function(value), returns string
thumbSize integer, px
trackGradient string, css gradient for track
trackImage string, image url, or
array of image urls
2010-09-03 20:54:40 +00:00
//trackStep number, 0 for 'scroll here', positive for step
2010-09-03 08:47:40 +00:00
trackValues boolean
serialize
2010-09-03 20:54:40 +00:00
textAlign 'left', 'center' or 'right'
type 'float', 'integer', 'password', 'text'
2010-09-03 08:47:40 +00:00
value string
2010-12-24 17:13:18 +00:00
validate function, remote validation
2010-09-03 08:47:40 +00:00
width integer, px
methods:
events:
change
submit
2010-12-06 17:42:45 +00:00
*/
Ox.Input = function(options, self) {
2010-09-03 08:47:40 +00:00
var self = self || {},
2010-09-03 20:54:40 +00:00
that = new Ox.Element('div', self)
2010-09-03 08:47:40 +00:00
.defaults({
arrows: false,
arrowStep: 1,
autocomplete: null,
autocompleteReplace: false,
autocompleteReplaceCorrect: false,
autocompleteSelect: false,
autocompleteSelectHighlight: false,
autocompleteSelectSubmit: false,
autovalidate: null,
clear: false,
2010-11-28 15:06:47 +00:00
disabled: false,
2010-09-03 20:54:40 +00:00
key: '',
2010-09-03 08:47:40 +00:00
min: 0,
max: 100,
2010-09-03 20:54:40 +00:00
label: '',
2010-09-03 08:47:40 +00:00
labelWidth: 64,
2010-09-03 20:54:40 +00:00
overlap: 'none',
placeholder: '',
2010-09-03 08:47:40 +00:00
serialize: null,
2010-09-03 20:54:40 +00:00
textAlign: 'left',
type: 'text',
2010-12-24 17:13:18 +00:00
validate: null,
2010-09-03 20:54:40 +00:00
value: '',
2010-09-03 08:47:40 +00:00
width: 128
})
.options(options)
2010-09-03 20:54:40 +00:00
.addClass('OxInput OxMedium')
2010-12-26 20:16:35 +00:00
.bindEvent($.extend(self.options.type == 'textarea' ? {} : {
2010-09-03 08:47:40 +00:00
key_enter: submit
}, {
key_escape: cancel
}));
if (
Ox.isArray(self.options.autocomplete) &&
self.options.autocompleteReplace &&
self.options.autocompleteReplaceCorrect &&
2010-09-03 20:54:40 +00:00
self.options.value === ''
2010-09-03 08:47:40 +00:00
) {
self.options.value = self.options.autocomplete[0]
}
// fixme: set to min, not 0
2010-09-03 20:54:40 +00:00
if (self.options.type == 'float') {
2010-09-03 08:47:40 +00:00
$.extend(self.options, {
2010-09-03 20:54:40 +00:00
autovalidate: 'float',
textAlign: 'right',
value: self.options.value || '0.0'
2010-09-03 08:47:40 +00:00
});
2010-09-03 20:54:40 +00:00
} else if (self.options.type == 'integer') {
2010-09-03 08:47:40 +00:00
$.extend(self.options, {
2010-09-03 20:54:40 +00:00
autovalidate: 'integer',
textAlign: 'right',
value: self.options.value || '0'
2010-09-03 08:47:40 +00:00
});
}
if (self.options.label) {
self.$label = new Ox.Label({
2010-09-03 20:54:40 +00:00
overlap: 'right',
textAlign: 'right',
2010-09-03 08:47:40 +00:00
title: self.options.label,
width: self.options.labelWidth
})
.css({
2010-09-03 20:54:40 +00:00
float: 'left', // fixme: use css rule
2010-09-03 08:47:40 +00:00
})
.click(function() {
that.focus();
})
.appendTo(that);
}
if (self.options.arrows) {
self.arrows = [];
self.arrows[0] = [
new Ox.Button({
2010-09-03 20:54:40 +00:00
overlap: 'right',
title: 'previous',
type: 'image'
2010-09-03 08:47:40 +00:00
})
.css({
2010-09-03 20:54:40 +00:00
float: 'left'
2010-09-03 08:47:40 +00:00
})
.click(function() {
clickArrow(0);
})
.appendTo(that),
new Ox.Button({
2010-09-03 20:54:40 +00:00
overlap: 'left',
title: 'next',
type: 'image'
2010-09-03 08:47:40 +00:00
})
.css({
2010-09-03 20:54:40 +00:00
float: 'right'
2010-09-03 08:47:40 +00:00
})
.click(function() {
clickArrow(1);
})
.appendTo(that)
]
}
$.extend(self, {
bindKeyboard: self.options.autocomplete || self.options.autovalidate,
2010-09-03 20:54:40 +00:00
hasPasswordPlaceholder: self.options.type == 'password' && self.options.placeholder,
2010-09-03 08:47:40 +00:00
inputWidth: getInputWidth()
});
if (self.options.clear) {
self.$button = new Ox.Button({
2010-09-03 20:54:40 +00:00
overlap: 'left',
title: 'clear',
type: 'image'
2010-09-03 08:47:40 +00:00
})
.css({
2010-09-03 20:54:40 +00:00
float: 'right' // fixme: use css rule
2010-09-03 08:47:40 +00:00
})
.click(clear)
.appendTo(that);
}
2010-09-03 20:54:40 +00:00
self.$input = $('<input>')
.addClass('OxInput OxMedium')
2010-09-03 08:47:40 +00:00
.attr({
2010-09-03 20:54:40 +00:00
disabled: self.options.disabled ? 'disabled' : '',
type: self.options.type == 'password' ? 'password' : 'text'
2010-09-03 08:47:40 +00:00
})
.css({
2010-09-03 20:54:40 +00:00
width: self.inputWidth + 'px',
2010-09-03 08:47:40 +00:00
textAlign: self.options.textAlign
})
.val(self.options.value)
.blur(blur)
.change(change)
.focus(focus)
.appendTo(that.$element);
if (self.hasPasswordPlaceholder) {
self.$input.hide();
2010-09-03 20:54:40 +00:00
self.$placeholder = $('<input>')
.addClass('OxInput OxMedium OxPlaceholder')
2010-09-03 08:47:40 +00:00
.attr({
2010-09-03 20:54:40 +00:00
type: 'text'
2010-09-03 08:47:40 +00:00
})
.css({
2010-09-03 20:54:40 +00:00
//float: 'left',
width: self.inputWidth + 'px'
2010-09-03 08:47:40 +00:00
})
.val(self.options.placeholder)
.focus(focus)
.appendTo(that.$element);
}
if (self.options.autocomplete && self.options.autocompleteSelect) {
2011-01-03 12:01:38 +00:00
self.$autocompleteMenu = constructAutocompleteMenu();
2010-09-03 08:47:40 +00:00
}
self.options.placeholder && setPlaceholder();
function autocomplete(oldValue, oldCursor) {
if (self.options.value || self.options.autocompleteReplaceCorrect) {
Ox.isFunction(self.options.autocomplete) ?
(self.options.key ? self.options.autocomplete(
self.options.key,
self.options.value,
autocompleteCallback
) : self.options.autocomplete(
self.options.value,
autocompleteCallback
)) : autocompleteCallback(autocompleteFunction(self.options.value));
}
if (!self.options.value) {
self.options.autocompleteSelect && self.$autocompleteMenu.hideMenu();
}
function autocompleteFunction() {
var values = Ox.find(self.options.autocomplete, self.options.value);
return self.options.autocompleteReplace ? values[0] :
$.merge(values[0], values[1]);
}
function autocompleteCallback(values) {
2010-09-03 20:54:40 +00:00
Ox.print('autocompleteCallback', values[0], self.options.value, self.options.value.length, oldValue, oldCursor)
2010-09-03 08:47:40 +00:00
var length = self.options.value.length,
deleted = length <= oldValue.length - (oldCursor[1] - oldCursor[0]),
newValue = values[0] ?
((self.options.autocompleteReplaceCorrect || !deleted) ?
values[0] : self.options.value) :
(self.options.autocompleteReplaceCorrect ? oldValue : self.options.value),
newLength = newValue.length,
pos = cursor(),
selected = -1,
selectEnd = length == 0 || (values[0] && values[0].length),
value;
2010-09-03 20:54:40 +00:00
Ox.print('selectEnd', selectEnd)
2010-09-03 08:47:40 +00:00
if (self.options.autocompleteReplace) {
self.options.value = newValue;
self.$input.val(self.options.value);
if (selectEnd) {
cursor(length, newLength);
} else if (self.options.autocompleteReplaceCorrect) {
cursor(oldCursor);
} else {
cursor(pos);
}
selected = 0;
}
if (self.options.autocompleteSelect) {
value = self.options.value.toLowerCase();
if (values.length) {
self.oldCursor = cursor();
self.oldValue = self.options.value;
self.$autocompleteMenu.options({
items: $.map(values, function(v, i) {
if (value == v.toLowerCase()) {
selected = i;
}
return {
2010-09-03 20:54:40 +00:00
id: v.toLowerCase().replace(/ /g, '_'), // fixme: need function to do lowercase, underscores etc?
2010-09-03 08:47:40 +00:00
title: self.options.autocompleteSelectHighlight ? v.replace(
2010-09-03 20:54:40 +00:00
new RegExp('(' + value + ')', 'ig'),
'<span class="OxHighlight">$1</span>'
2010-09-03 08:47:40 +00:00
) : v
};
}),
selected: selected
}).showMenu();
} else {
self.$autocompleteMenu.hideMenu();
}
}
2010-09-03 20:54:40 +00:00
that.triggerEvent('autocomplete', {
2010-09-03 08:47:40 +00:00
value: newValue
});
}
}
2011-01-03 12:01:38 +00:00
function constructAutocompleteMenu() {
var menu = new Ox.Menu({
element: self.$input,
id: self.options.id + 'Menu', // fixme: we do this in other places ... are we doing it the same way? var name?,
offset: {
left: 4,
top: 0
},
size: self.options.size
})
.bindEvent('click', clickMenu);
if (self.options.autocompleteReplace) {
menu.bindEvent({
deselect: deselectMenu,
select: selectMenu,
});
}
return menu;
}
2010-09-03 08:47:40 +00:00
function autovalidate() {
var blur, oldCursor, oldValue;
if (arguments.length == 1) {
blur = arguments[0];
} else {
blur = false;
oldValue = arguments[0];
oldCursor = arguments[1];
}
Ox.isFunction(self.options.autovalidate) ?
(self.options.key ? self.options.autovalidate(
self.options.key,
self.options.value,
blur,
autovalidateCallback
) : self.options.autovalidate(
self.options.value,
blur,
autovalidateCallback
2010-12-24 17:13:18 +00:00
)) : (Ox.isRegExp(self.options.autovalidate) ?
2010-09-03 08:47:40 +00:00
autovalidateCallback(autovalidateFunction(self.options.value)) :
2010-12-24 17:13:18 +00:00
autovalidateTypeFunction(self.options.type, self.options.value)
);
2010-09-03 08:47:40 +00:00
function autovalidateFunction(value) {
var regexp = new RegExp(self.options.autovalidate);
2010-12-26 20:16:35 +00:00
return $.map(value.split(''), function(v) {
return regexp(v) ? v : null;
2010-09-03 20:54:40 +00:00
}).join('');
2010-09-03 08:47:40 +00:00
}
function autovalidateTypeFunction(type, value) {
2010-12-26 20:16:35 +00:00
// fixme: remove trailing zeroes on blur
2010-09-03 08:47:40 +00:00
var cursor,
2010-09-03 20:54:40 +00:00
regexp = type == 'float' ? /[\d\.]/ : /\d/;
if (type == 'float') {
if (value.indexOf('.') != value.lastIndexOf('.')) {
2010-09-03 08:47:40 +00:00
value = oldValue;
} else {
if (self.autovalidateFloatFlag) {
2010-09-03 20:54:40 +00:00
if (Ox.endsWith(value, '.')) {
2010-09-03 08:47:40 +00:00
value = value.substr(0, value.length - 1);
}
self.autovalidateFloatFlag = false;
}
2010-12-26 20:16:35 +00:00
while (value[0] == '0' && value[1] != '.') {
value = value.substr(1);
}
2010-09-03 20:54:40 +00:00
while (Ox.startsWith(value, '.')) {
if (Ox.startsWith(value, '..')) {
2010-09-03 08:47:40 +00:00
value = value.substr(1);
} else {
2010-09-03 20:54:40 +00:00
value = '0' + value;
2010-09-03 08:47:40 +00:00
}
}
2010-09-03 20:54:40 +00:00
if (Ox.endsWith(value, '.')) {
value += '0';
2010-09-03 08:47:40 +00:00
cursor = [value.length - 1, value.length];
self.autovalidateFloatFlag = true;
}
}
}
2010-12-26 20:16:35 +00:00
value = $.map(value.split(''), function(v) {
return regexp(v) ? v : null;
2010-09-03 20:54:40 +00:00
}).join('');
if (type == 'integer') {
while (value.length > 1 && Ox.startsWith(value, '0')) {
2010-09-03 08:47:40 +00:00
value = value.substr(1);
}
}
2010-09-03 20:54:40 +00:00
if (value === '') {
value = type == 'float' ? '0.0' : '0';
2010-09-03 08:47:40 +00:00
cursor = [0, value.length];
} else if (value > self.options.max) {
value = oldValue;
}
autovalidateCallback(value, cursor);
}
function autovalidateCallback(newValue, newCursor) {
2010-09-03 20:54:40 +00:00
Ox.print('autovalidateCallback', newValue, oldCursor)
2010-09-03 08:47:40 +00:00
self.options.value = newValue;
self.$input.val(self.options.value);
!blur && cursor(
newCursor || (oldCursor[1] + newValue.length - oldValue.length)
);
2010-09-03 20:54:40 +00:00
that.triggerEvent('autovalidate', {
2010-09-03 08:47:40 +00:00
value: self.options.value
});
}
}
/*
function autovalidate(blur) {
2010-09-03 20:54:40 +00:00
Ox.print('autovalidate', self.options.value, blur || false)
2010-09-03 08:47:40 +00:00
self.autocorrectBlur = blur || false;
self.autocorrectCursor = cursor();
Ox.isFunction(self.options.autocorrect) ?
(self.options.key ? self.options.autocorrect(
self.options.key,
self.options.value,
self.autocorrectBlur,
autocorrectCallback
) : self.options.autocorrect(
self.options.value,
self.autocorrectBlur,
autocorrectCallback
)) : autocorrectCallback(autocorrect(self.options.value));
}
function autovalidateFunction(value) {
var length = value.length;
2010-09-03 20:54:40 +00:00
return $.map(value.toLowerCase().split(''), function(v, i) {
2010-09-03 08:47:40 +00:00
if (new RegExp(self.options.autocorrect)(v)) {
return v;
} else {
return null;
}
2010-09-03 20:54:40 +00:00
}).join('');
2010-09-03 08:47:40 +00:00
}
*/
function blur() {
2010-09-03 20:54:40 +00:00
Ox.print('blur')
2010-09-03 08:47:40 +00:00
that.loseFocus();
2010-09-03 20:54:40 +00:00
//that.removeClass('OxFocus');
2010-09-03 08:47:40 +00:00
self.options.value = self.$input.val();
self.options.autovalidate && autovalidate(true);
self.options.placeholder && setPlaceholder();
2010-12-24 17:13:18 +00:00
self.options.validate && validate();
2010-09-03 08:47:40 +00:00
if (self.bindKeyboard) {
2010-09-03 20:54:40 +00:00
$document.unbind('keydown', keypress);
$document.unbind('keypress', keypress);
2010-09-03 08:47:40 +00:00
}
}
function cancel() {
self.$input.blur();
}
function change() {
self.options.value = self.$input.val();
2010-09-03 20:54:40 +00:00
that.triggerEvent('change', {
2010-09-03 08:47:40 +00:00
value: self.options.value
});
}
function clear() {
// fixme: set to min, not zero
// fixme: make this work for password
2010-09-03 20:54:40 +00:00
var value = '';
if (self.options.type == 'float') {
value = '0.0';
} else if (self.options.type == 'integer') {
value = '0'
2010-09-03 08:47:40 +00:00
}
self.$input.val(value);
cursor(0, value.length);
}
function clickArrow(i) {
self.options.value = Ox.limit(
parseFloat(self.options.value) + (i == 0 ? -1 : 1) * self.options.arrowStep,
self.options.min,
self.options.max
2010-12-26 20:16:35 +00:00
).toString();
2010-09-03 08:47:40 +00:00
self.$input.val(self.options.value);//.focus();
}
function clickMenu(event, data) {
2010-09-03 20:54:40 +00:00
Ox.print('clickMenu', data);
2010-09-03 08:47:40 +00:00
self.options.value = data.title;
self.$input.val(self.options.value).focus();
that.gainFocus();
self.options.autocompleteSelectSubmit && submit();
}
function cursor(start, end) {
/*
cursor() returns [start, end]
cursor(start) sets start
cursor([start, end]) sets start and end
cursor(start, end) sets start and end
*/
var isArray = Ox.isArray(start);
if (arguments.length == 0) {
return [self.$input[0].selectionStart, self.$input[0].selectionEnd];
} else {
end = isArray ? start[1] : (end ? end : start);
start = isArray ? start[0] : start;
self.$input[0].setSelectionRange(start, end);
}
}
function deselectMenu() {
self.options.value = self.oldValue;
self.$input.val(self.options.value);
cursor(self.oldCursor);
}
function focus() {
2010-09-03 20:54:40 +00:00
Ox.print('focus()')
2010-09-03 08:47:40 +00:00
if (
2010-09-03 20:54:40 +00:00
that.hasClass('OxFocus') || // fixme: this is just a workaround, since for some reason, focus() gets called twice on focus
(self.$autocompleteMenu && self.$autocompleteMenu.is(':visible')) ||
(self.hasPasswordPlaceholder && self.$input.is(':visible'))
2010-09-03 08:47:40 +00:00
) {
return;
}
that.gainFocus();
self.options.placeholder && setPlaceholder();
if (self.bindKeyboard) {
2010-09-03 20:54:40 +00:00
Ox.print('binding...')
2010-09-03 08:47:40 +00:00
// fixme: different in webkit and firefox (?), see keyboard handler, need generic function
$document.keydown(keypress);
$document.keypress(keypress);
self.options.autocompleteSelect && setTimeout(autocomplete, 0); // fixme: why is the timeout needed?
}
}
function getInputWidth() {
return self.options.width - 14 -
(self.options.arrows ? 32 : 0) -
(self.options.clear ? 16 : 0) -
(self.options.label ? self.options.labelWidth : 0);
}
function keypress(event) {
var oldCursor = cursor(),
oldValue = self.options.value,
newValue = oldValue.substr(0, oldCursor[0] - 1),
hasDeletedSelectedEnd = (event.keyCode == 8 || event.keyCode == 46) &&
oldCursor[0] < oldCursor[1] && oldCursor[1] == oldValue.length;
2010-09-03 20:54:40 +00:00
Ox.print('keypress', event.keyCode)
2010-09-03 08:47:40 +00:00
if (event.keyCode != 9 && event.keyCode != 13 && event.keyCode != 27) { // fixme: can't 13 and 27 return false?
setTimeout(function() { // wait for val to be set
var value = self.$input.val();
if (self.options.autocompleteReplaceCorrect && hasDeletedSelectedEnd) {
2010-09-03 20:54:40 +00:00
Ox.print(value, '->', newValue);
2010-09-03 08:47:40 +00:00
value = newValue; // value.substr(0, value.length - 1);
self.$input.val(value);
}
if (value != self.options.value) {
self.options.value = value;
self.options.autocomplete && autocomplete(oldValue, oldCursor);
self.options.autovalidate && autovalidate(oldValue, oldCursor);
}
}, 0);
}
2010-09-03 20:54:40 +00:00
if ((event.keyCode == 38 || event.keyCode == 40) && self.options.autocompleteSelect && self.$autocompleteMenu.is(':visible')) {
2010-09-03 08:47:40 +00:00
return false;
}
}
function selectMenu(event, data) {
var pos = cursor();
2010-09-03 20:54:40 +00:00
Ox.print('selectMenu', pos)
2010-09-03 08:47:40 +00:00
self.options.value = data.title
self.$input.val(self.options.value);
cursor(pos[0], self.options.value.length)
}
function setPlaceholder() {
if (self.options.placeholder) {
2010-09-03 20:54:40 +00:00
if (that.hasClass('OxFocus')) {
if (self.options.value === '') {
if (self.options.type == 'password') {
2010-09-03 08:47:40 +00:00
self.$placeholder.hide();
self.$input.show().focus();
} else {
self.$input
2010-09-03 20:54:40 +00:00
.removeClass('OxPlaceholder')
.val('');
2010-09-03 08:47:40 +00:00
}
}
} else {
2010-09-03 20:54:40 +00:00
if (self.options.value === '') {
if (self.options.type == 'password') {
2010-09-03 08:47:40 +00:00
self.$input.hide();
self.$placeholder.show();
} else {
self.$input
2010-09-03 20:54:40 +00:00
.addClass('OxPlaceholder')
2010-09-03 08:47:40 +00:00
.val(self.options.placeholder)
}
} else {
self.$input
2010-09-03 20:54:40 +00:00
.removeClass('OxPlaceholder')
2010-09-03 08:47:40 +00:00
.val(self.options.value)
}
}
}
}
function setWidth() {
}
function submit() {
self.$input.blur();
2010-09-03 20:54:40 +00:00
that.triggerEvent('submit', {
2010-09-03 08:47:40 +00:00
value: self.options.value
});
}
2010-12-24 17:13:18 +00:00
function validate() {
self.options.validate(self.options.value, function(data) {
that.triggerEvent('validate', data);
});
}
2010-09-03 08:47:40 +00:00
self.onChange = function(key, value) {
var inputWidth, val;
2011-01-03 12:01:38 +00:00
if (['autocomplete', 'autocompleteReplace', 'autocompleteSelect', 'autovalidate'].indexOf(key) > -1) {
if (self.options.autocomplete && self.options.autocompleteSelect) {
self.$autocompleteMenu = constructAutocompleteMenu();
}
self.bindKeyboard = self.options.autocomplete || self.options.autovalidate;
} else if (key == 'disabled') {
2010-11-28 15:06:47 +00:00
self.$input.attr({
disabled: value ? 'disabled' : ''
});
} else if (key == 'placeholder') {
2010-09-04 14:28:40 +00:00
setPlaceholder();
} else if (key == 'value') {
2010-09-03 08:47:40 +00:00
val = self.$input.val();
self.$input.val(value);
setPlaceholder();
2010-09-03 20:54:40 +00:00
} else if (key == 'width') {
2010-09-03 08:47:40 +00:00
inputWidth = getInputWidth();
self.$input.css({
2010-09-03 20:54:40 +00:00
width: inputWidth + 'px'
2010-09-03 08:47:40 +00:00
});
self.hasPasswordPlaceholder && self.$placeholder.css({
2010-09-03 20:54:40 +00:00
width: inputWidth + 'px'
2010-09-03 08:47:40 +00:00
});
}
};
that.focus = function() {
self.$input.focus();
cursor(0, self.$input.val().length);
};
2010-12-24 17:13:18 +00:00
that.value = function() {
2011-01-03 23:38:43 +00:00
return self.$input.hasClass('OxPlaceholder') ? '' : self.$input.val();
2010-12-24 17:13:18 +00:00
};
2010-09-03 08:47:40 +00:00
return that;
};
Ox.AutocorrectIntFunction = function(min, max, pad, year) {
var pad = pad || false,
year = year || false,
maxLength = max.toString().length,
ret = null,
values = [];
$.each(Ox.range(min, max + 1), function(i, v) {
2010-09-03 20:54:40 +00:00
values.push(v + '');
2010-09-03 08:47:40 +00:00
pad && v.toString().length < maxLength && values.push(Ox.pad(v, maxLength));
});
return function(value, blur, callback) {
var results;
2010-09-03 20:54:40 +00:00
if (year && value == '1') {
value = '1900';
2010-09-03 08:47:40 +00:00
} else {
results = Ox.find(values, value);
value = results[0].length == 1 && results[0][0].length < maxLength ?
(pad ? Ox.pad(results[0][0], maxLength) : results[0][0]) :
(results[0].length ? results[0][0] : null);
}
callback(value);
};
};
Ox.InputGroup = function(options, self) {
/***
Ox.InputGroup
Options:
Methods:
Events:
***/
var self = self || {},
2010-09-03 20:54:40 +00:00
that = new Ox.Element('div', self)
2010-09-03 08:47:40 +00:00
.defaults({
2010-09-03 20:54:40 +00:00
id: '',
2010-09-03 08:47:40 +00:00
inputs: [],
separators: [],
width: 0
})
.options(options || {})
2010-09-03 20:54:40 +00:00
.addClass('OxInputGroup')
2010-09-03 08:47:40 +00:00
.click(click);
if (self.options.width) {
setWidths();
} else {
self.options.width = getWidth();
}
that.css({
2010-09-03 20:54:40 +00:00
width: self.options.width + 'px'
2010-09-03 08:47:40 +00:00
});
$.extend(self, {
//$input: [],
$separator: []
});
$.each(self.options.separators, function(i, v) {
self.$separator[i] = new Ox.Label({
2010-09-03 20:54:40 +00:00
textAlign: 'center',
2010-09-03 08:47:40 +00:00
title: v.title,
width: v.width + 32
})
2010-09-03 20:54:40 +00:00
.addClass('OxSeparator')
2010-09-03 08:47:40 +00:00
.css({
2010-09-03 20:54:40 +00:00
marginLeft: (self.options.inputs[i].options('width') - (i == 0 ? 16 : 32)) + 'px'
2010-09-03 08:47:40 +00:00
})
.appendTo(that);
});
$.each(self.options.inputs, function(i, $input) {
$input.options({
2010-09-03 20:54:40 +00:00
id: self.options.id + Ox.toTitleCase($input.options('id')),
2010-09-03 08:47:40 +00:00
parent: that
})
.css({
marginLeft: -Ox.sum($.map(self.options.inputs, function(v_, i_) {
2010-09-03 20:54:40 +00:00
return i_ > i ? self.options.inputs[i_ - 1].options('width') +
2010-09-03 08:47:40 +00:00
self.options.separators[i_ - 1].width : (i_ == i ? 16 : 0);
2010-09-03 20:54:40 +00:00
})) + 'px'
2010-09-03 08:47:40 +00:00
})
.bindEvent({
change: change,
2010-12-26 20:16:35 +00:00
submit: change,
validate: validate
2010-09-03 08:47:40 +00:00
})
.appendTo(that);
});
function change(event, data) {
2010-09-03 20:54:40 +00:00
Ox.print('InputGroup change')
2010-09-03 08:47:40 +00:00
// fixme: would be good to pass a value here
2010-09-03 20:54:40 +00:00
that.triggerEvent('change');
2010-09-03 08:47:40 +00:00
}
function click(event) {
2010-09-03 20:54:40 +00:00
if ($(event.target).hasClass('OxSeparator')) {
2010-09-03 08:47:40 +00:00
self.options.inputs[0].focus();
}
}
function getWidth() {
return Ox.sum($.map(self.options.inputs, function(v, i) {
2010-09-03 20:54:40 +00:00
return v.options('width');
2010-09-03 08:47:40 +00:00
})) + Ox.sum($.map(self.options.separators, function(v, i) {
return v.width;
}));
}
function setWidths() {
var length = self.options.inputs.length,
inputWidths = Ox.divideInt(
self.options.width - Ox.sum($.map(self.options.separators, function(v, i) {
return v.width;
})), length
);
$.each(self.options.inputs, function(i, v) {
v.options({
width: inputWidths[1]
});
});
}
2010-12-26 20:16:35 +00:00
function validate(event, data) {
Ox.print('INPUTGROUP TRIGGER VALIDATE')
that.triggerEvent('validate', data);
}
2010-09-03 08:47:40 +00:00
// fixme: is this used?
that.getInputById = function(id) {
var input = null;
$.each(self.options.inputs, function(i, v) {
2010-09-03 20:54:40 +00:00
Ox.print(v, v.options('id'), id)
if (v.options('id') == self.options.id + Ox.toTitleCase(id)) {
2010-09-03 08:47:40 +00:00
input = v;
return false;
}
});
return input;
};
2010-12-26 20:16:35 +00:00
that.value = function() {
return $.map(self.options.inputs, function(input) {
var ret = null;
['checked', 'selected', 'value'].forEach(function(v) {
input[v] && (ret = input[v]());
});
return ret;
});
};
2010-09-03 08:47:40 +00:00
return that;
};
Ox.ColorInput = function(options, self) {
var self = $.extend(self || {}, {
options: $.extend({
2010-09-03 20:54:40 +00:00
id: '',
value: '0, 0, 0'
2010-09-03 08:47:40 +00:00
}, options)
}),
that;
2010-09-03 20:54:40 +00:00
self.values = self.options.value.split(', ');
2010-09-03 08:47:40 +00:00
self.$inputs = [];
2010-09-03 20:54:40 +00:00
$.each(['red', 'green', 'blue'], function(i, v) {
2010-09-03 08:47:40 +00:00
self.$inputs[i] = new Ox.Input({
id: v,
max: 255,
2010-09-03 20:54:40 +00:00
type: 'integer',
2010-09-03 08:47:40 +00:00
value: self.values[i],
width: 36
})
2010-09-03 20:54:40 +00:00
.bindEvent('autovalidate', change);
2010-09-03 08:47:40 +00:00
});
self.$inputs[3] = new Ox.Label({
2010-09-03 20:54:40 +00:00
id: 'color',
2010-09-03 08:47:40 +00:00
width: 36
})
.css({
2010-09-03 20:54:40 +00:00
background: 'rgb(' + self.options.value + ')'
2010-09-03 08:47:40 +00:00
});
self.$inputs[4] = new Ox.ColorPicker({
2010-09-03 20:54:40 +00:00
id: 'picker'
2010-09-03 08:47:40 +00:00
})
2010-09-03 20:54:40 +00:00
.bindEvent('change', function(event, data) {
Ox.print('change function called');
2010-09-03 08:47:40 +00:00
self.options.value = data.value;
2010-09-03 20:54:40 +00:00
self.values = data.value.split(', ');
2010-09-03 08:47:40 +00:00
$.each(Ox.range(3), function(i) {
self.$inputs[i].options({
value: self.values[i]
});
});
})
.options({
width: 16 // this is just a hack to make the InputGroup layout work
});
that = new Ox.InputGroup({
id: self.options.id,
inputs: self.$inputs,
separators: [
2010-09-03 20:54:40 +00:00
{title: ',', width: 8},
{title: ',', width: 8},
{title: '', width: 8},
{title: '', width: 8}
2010-09-03 08:47:40 +00:00
],
value: self.options.value // fixme: it'd be nicer if this would be taken care of by passing self
}, self)
2010-09-03 20:54:40 +00:00
.bindEvent('change', change);
2010-09-03 08:47:40 +00:00
function change() {
self.options.value = $.map(self.$inputs, function(v, i) {
2010-09-03 20:54:40 +00:00
return v.options('value');
}).join(', ');
2010-09-03 08:47:40 +00:00
self.$inputs[3].css({
2010-09-03 20:54:40 +00:00
background: 'rgb(' + self.options.value + ')'
2010-09-03 08:47:40 +00:00
});
}
return that;
};
2010-12-06 18:23:04 +00:00
/**
options:
format: 'short'
value: date value
weekday: false
width: {
day: 32,
month: options.format == 'long' ? 80 : (options.format == 'medium' ? 40 : 32),
weekday: options.format == 'long' ? 80 : 40,
year: 48
}
*/
2010-09-03 08:47:40 +00:00
Ox.DateInput = function(options, self) {
var self = $.extend(self || {}, {
options: $.extend({
2010-09-03 20:54:40 +00:00
format: 'short',
value: Ox.formatDate(new Date(), '%F'),
2010-09-03 08:47:40 +00:00
weekday: false,
width: {
day: 32,
2010-09-03 20:54:40 +00:00
month: options.format == 'long' ? 80 : (options.format == 'medium' ? 40 : 32),
weekday: options.format == 'long' ? 80 : 40,
2010-09-03 08:47:40 +00:00
year: 48
}
}, options)
}),
that;
$.extend(self, {
2010-09-03 20:54:40 +00:00
date: new Date(self.options.value.replace(/-/g, '/')),
2010-09-03 08:47:40 +00:00
formats: {
2010-09-03 20:54:40 +00:00
day: '%d',
month: self.options.format == 'short' ? '%m' :
(self.options.format == 'medium' ? '%b' : '%B'),
weekday: self.options.format == 'long' ? '%A' : '%a',
year: '%Y'
2010-09-03 08:47:40 +00:00
},
2010-09-03 20:54:40 +00:00
months: self.options.format == 'long' ? Ox.MONTHS : $.map(Ox.MONTHS, function(v, i) {
2010-09-03 08:47:40 +00:00
return v.substr(0, 3);
}),
2010-09-03 20:54:40 +00:00
weekdays: self.options.format == 'long' ? Ox.WEEKDAYS : $.map(Ox.WEEKDAYS, function(v, i) {
2010-09-03 08:47:40 +00:00
return v.substr(0, 3);
})
});
self.$input = $.extend(self.options.weekday ? {
weekday: new Ox.Input({
autocomplete: self.weekdays,
autocompleteReplace: true,
autocompleteReplaceCorrect: true,
2010-09-03 20:54:40 +00:00
id: 'weekday',
2010-09-03 08:47:40 +00:00
value: Ox.formatDate(self.date, self.formats.weekday),
width: self.options.width.weekday
})
2010-09-03 20:54:40 +00:00
.bindEvent('autocomplete', changeWeekday),
2010-09-03 08:47:40 +00:00
} : {}, {
day: new Ox.Input({
autocomplete: $.map(Ox.range(1, Ox.getDaysInMonth(
2010-09-03 20:54:40 +00:00
parseInt(Ox.formatDate(self.date, '%Y'), 10),
parseInt(Ox.formatDate(self.date, '%m'), 10)
2010-09-03 08:47:40 +00:00
) + 1), function(v, i) {
2010-09-03 20:54:40 +00:00
return self.options.format == 'short' ? Ox.pad(v, 2) : v.toString();
2010-09-03 08:47:40 +00:00
}),
autocompleteReplace: true,
autocompleteReplaceCorrect: true,
2010-09-03 20:54:40 +00:00
id: 'day',
2010-09-03 08:47:40 +00:00
value: Ox.formatDate(self.date, self.formats.day),
2010-09-03 20:54:40 +00:00
textAlign: 'right',
2010-09-03 08:47:40 +00:00
width: self.options.width.day
})
2010-09-03 20:54:40 +00:00
.bindEvent('autocomplete', changeDay),
2010-09-03 08:47:40 +00:00
month: new Ox.Input({
2010-09-03 20:54:40 +00:00
autocomplete: self.options.format == 'short' ? $.map(Ox.range(1, 13), function(v, i) {
2010-09-03 08:47:40 +00:00
return Ox.pad(v, 2);
}) : self.months,
autocompleteReplace: true,
autocompleteReplaceCorrect: true,
2010-09-03 20:54:40 +00:00
id: 'month',
2010-09-03 08:47:40 +00:00
value: Ox.formatDate(self.date, self.formats.month),
2010-09-03 20:54:40 +00:00
textAlign: self.options.format == 'short' ? 'right' : 'left',
2010-09-03 08:47:40 +00:00
width: self.options.width.month
})
2010-09-03 20:54:40 +00:00
.bindEvent('autocomplete', changeMonthOrYear),
2010-09-03 08:47:40 +00:00
year: new Ox.Input({
autocomplete: $.map($.merge(Ox.range(1900, 3000), Ox.range(1000, 1900)), function(v, i) {
return v.toString();
}),
autocompleteReplace: true,
autocompleteReplaceCorrect: true,
2010-09-03 20:54:40 +00:00
id: 'year',
2010-09-03 08:47:40 +00:00
value: Ox.formatDate(self.date, self.formats.year),
2010-09-03 20:54:40 +00:00
textAlign: 'right',
2010-09-03 08:47:40 +00:00
width: self.options.width.year
})
2010-09-03 20:54:40 +00:00
.bindEvent('autocomplete', changeMonthOrYear)
2010-09-03 08:47:40 +00:00
});
that = new Ox.InputGroup($.extend(self.options, {
id: self.options.id,
inputs: $.merge(self.options.weekday ? [
self.$input.weekday
2010-09-03 20:54:40 +00:00
] : [], self.options.format == 'short' ? [
2010-09-03 08:47:40 +00:00
self.$input.year, self.$input.month, self.$input.day
] : [
self.$input.month, self.$input.day, self.$input.year
]),
separators: $.merge(self.options.weekday ? [
2010-09-03 20:54:40 +00:00
{title: self.options.format == 'short' ? '' : ',', width: 8},
] : [], self.options.format == 'short' ? [
{title: '-', width: 8}, {title: '-', width: 8}
2010-09-03 08:47:40 +00:00
] : [
2010-09-03 20:54:40 +00:00
{title: '', width: 8}, {title: ',', width: 8}
2010-09-03 08:47:40 +00:00
]),
width: 0
}), self);
2010-09-03 20:54:40 +00:00
Ox.print('SELF', self)
2010-09-03 08:47:40 +00:00
function changeDay() {
self.options.weekday && self.$input.weekday.options({
value: Ox.formatDate(new Date([
2010-09-03 20:54:40 +00:00
self.$input.month.options('value'),
self.$input.day.options('value'),
self.$input.year.options('value')
].join(' ')), self.formats.weekday)
2010-09-03 08:47:40 +00:00
});
setValue();
}
function changeMonthOrYear() {
2010-09-03 20:54:40 +00:00
var day = self.$input.day.options('value'),
month = self.$input.month.options('value'),
year = self.$input.year.options('value'),
days = Ox.getDaysInMonth(year, self.options.format == 'short' ? parseInt(month, 10) : month);
2010-09-03 08:47:40 +00:00
day = day <= days ? day : days;
2010-09-03 20:54:40 +00:00
Ox.print(year, month, 'day days', day, days)
2010-09-03 08:47:40 +00:00
self.options.weekday && self.$input.weekday.options({
2010-09-03 20:54:40 +00:00
value: Ox.formatDate(new Date([month, day, year].join(' ')), self.formats.weekday)
2010-09-03 08:47:40 +00:00
});
self.$input.day.options({
autocomplete: $.map(Ox.range(1, days + 1), function(v, i) {
2010-09-03 20:54:40 +00:00
return self.options.format == 'short' ? Ox.pad(v, 2) : v.toString();
2010-09-03 08:47:40 +00:00
}),
2010-09-03 20:54:40 +00:00
value: self.options.format == 'short' ? Ox.pad(day, 2) : day.toString()
2010-09-03 08:47:40 +00:00
});
setValue();
}
function changeWeekday() {
var date = getDateInWeek(
2010-09-03 20:54:40 +00:00
self.$input.weekday.options('value'),
self.$input.month.options('value'),
self.$input.day.options('value'),
self.$input.year.options('value')
2010-09-03 08:47:40 +00:00
);
self.$input.month.options({value: date.month});
self.$input.day.options({
autocomplete: $.map(Ox.range(1, Ox.getDaysInMonth(date.year, date.month) + 1), function(v, i) {
2010-09-03 20:54:40 +00:00
return self.options.format == 'short' ? Ox.pad(v, 2) : v.toString();
2010-09-03 08:47:40 +00:00
}),
value: date.day
});
self.$input.year.options({value: date.year});
setValue();
}
function getDateInWeek(weekday, month, day, year) {
2010-09-03 20:54:40 +00:00
Ox.print([month, day, year].join(' '))
var date = new Date([month, day, year].join(' '));
2010-09-03 08:47:40 +00:00
date = Ox.getDateInWeek(date, weekday);
return {
day: Ox.formatDate(date, self.formats.day),
month: Ox.formatDate(date, self.formats.month),
year: Ox.formatDate(date, self.formats.year)
};
}
function setValue() {
2010-09-03 20:54:40 +00:00
self.options.value = Ox.formatDate(new Date(self.options.format == 'short' ? [
self.$input.year.options('value'),
self.$input.month.options('value'),
self.$input.day.options('value')
].join('/') : [
self.$input.month.options('value'),
self.$input.day.options('value'),
self.$input.year.options('value')
].join(' ')), '%F');
2010-09-03 08:47:40 +00:00
}
/*
function normalize() {
2010-09-03 20:54:40 +00:00
var year = that.getInputById('year').options('value'),
month = that.getInputById('month').options('value'),
day = that.getInputById('day').options('value')
2010-09-03 08:47:40 +00:00
return {
year: year,
2010-09-03 20:54:40 +00:00
month: self.options.format == 'short' ? month :
Ox.pad((format == 'medium' ? Ox.WEEKDAYS.map(function(v, i) {
2010-09-03 08:47:40 +00:00
return v.substr(0, 3);
}) : Ox.WEEKDAYS).indexOf(month), 2),
day: Ox.pad(day, 2)
}
}
*/
/*
that.serialize = function() {
var normal = normalize();
2010-09-03 20:54:40 +00:00
return [normal.year, normal.month, normal.day].join('-');
2010-09-03 08:47:40 +00:00
}
*/
return that;
};
Ox.DateTimeInput = function(options, self) {
var self = self || {},
that = new Ox.Element({}, self)
.defaults({
ampm: false,
2010-09-03 20:54:40 +00:00
format: 'short',
2010-09-03 08:47:40 +00:00
seconds: false,
2010-09-03 20:54:40 +00:00
value: Ox.formatDate(new Date(), '%F %T'),
2010-09-03 08:47:40 +00:00
weekday: false
})
.options(options || {});
2010-09-03 20:54:40 +00:00
self.values = self.options.value.split(' ');
2010-09-03 08:47:40 +00:00
Ox.print(self.values)
that = new Ox.InputGroup({
inputs: [
new Ox.DateInput({
format: self.options.format,
2010-09-03 20:54:40 +00:00
id: 'date',
2010-09-03 08:47:40 +00:00
value: self.values[0],
weekday: self.options.weekday
}),
new Ox.TimeInput({
ampm: self.options.ampm,
2010-09-03 20:54:40 +00:00
id: 'time',
2010-09-03 08:47:40 +00:00
value: self.values[1],
seconds: self.options.seconds
})
],
separators: [
2010-09-03 20:54:40 +00:00
{title: '', width: 8}
2010-09-03 08:47:40 +00:00
],
value: self.options.value
})
2010-09-03 20:54:40 +00:00
.bindEvent('change', setValue);
2010-09-03 08:47:40 +00:00
function setValue() {
self.options.value = [
2010-09-03 20:54:40 +00:00
self.options('inputs')[0].options('value'),
self.options('inputs')[1].options('value')
].join(' ');
2010-09-03 08:47:40 +00:00
}
return that;
};
Ox.PlaceInput = function(options, self) {
var self = $.extend(self || {}, {
options: $.extend({
2010-09-03 20:54:40 +00:00
id: '',
value: 'United States'
2010-09-03 08:47:40 +00:00
}, options)
}),
that;
that = new Ox.FormElementGroup({
id: self.options.id,
elements: [
new Ox.Input({
2010-09-03 20:54:40 +00:00
id: 'input',
2010-09-03 08:47:40 +00:00
value: self.options.value
}),
new Ox.PlacePicker({
2010-09-03 20:54:40 +00:00
id: 'picker',
overlap: 'left',
2010-09-03 08:47:40 +00:00
value: self.options.value
})
],
2010-09-03 20:54:40 +00:00
float: 'right'
2010-09-03 08:47:40 +00:00
}, self)
2010-09-03 20:54:40 +00:00
.bindEvent('change', change);
2010-09-03 08:47:40 +00:00
function change() {
}
return that;
};
Ox.TimeInput = function(options, self) {
// fixme: seconds get set even if options.seconds is false
var self = self || {},
that = new Ox.Element({}, self)
.defaults({
ampm: false,
seconds: false,
milliseconds: false,
2010-09-03 20:54:40 +00:00
value: Ox.formatDate(new Date(), '%T'),
2010-09-03 08:47:40 +00:00
})
.options(options || {});
if (self.options.milliseconds) {
self.options.seconds = true;
2010-09-03 20:54:40 +00:00
if (self.options.value.indexOf('.') == -1) {
self.options.value += '.000';
2010-09-03 08:47:40 +00:00
}
}
self.date = getDate();
self.values = getValues();
self.$input = {
hours: Ox.Input({
autocomplete: $.map(self.options.ampm ? Ox.range(1, 13) : Ox.range(0, 24), function(v) {
return Ox.pad(v, 2);
}),
autocompleteReplace: true,
autocompleteReplaceCorrect: true,
2010-09-03 20:54:40 +00:00
id: 'hours',
textAlign: 'right',
2010-09-03 08:47:40 +00:00
value: self.values.hours,
width: 32
}),
minutes: Ox.Input({
autocomplete: $.map(Ox.range(0, 60), function(v) {
return Ox.pad(v, 2);
}),
autocompleteReplace: true,
autocompleteReplaceCorrect: true,
2010-09-03 20:54:40 +00:00
id: 'minutes',
textAlign: 'right',
2010-09-03 08:47:40 +00:00
value: self.values.minutes,
width: 32
}),
seconds: Ox.Input({
autocomplete: $.map(Ox.range(0, 60), function(v) {
return Ox.pad(v, 2);
}),
autocompleteReplace: true,
autocompleteReplaceCorrect: true,
2010-09-03 20:54:40 +00:00
id: 'seconds',
textAlign: 'right',
2010-09-03 08:47:40 +00:00
value: self.values.seconds,
width: 32
}),
milliseconds: Ox.Input({
autocomplete: $.map(Ox.range(0, 1000), function(v) {
return Ox.pad(v, 3);
}),
autocompleteReplace: true,
autocompleteReplaceCorrect: true,
2010-09-03 20:54:40 +00:00
id: 'milliseconds',
textAlign: 'right',
2010-09-03 08:47:40 +00:00
value: self.values.milliseconds,
width: 40
}),
ampm: Ox.Input({
2010-09-03 20:54:40 +00:00
autocomplete: ['AM', 'PM'],
2010-09-03 08:47:40 +00:00
autocompleteReplace: true,
autocompleteReplaceCorrect: true,
2010-09-03 20:54:40 +00:00
id: 'ampm',
2010-09-03 08:47:40 +00:00
value: self.values.ampm,
width: 32
})
};
that = new Ox.InputGroup($.extend(self.options, {
inputs: $.merge($.merge($.merge([
self.$input.hours,
self.$input.minutes,
], self.options.seconds ? [
self.$input.seconds
] : []), self.options.milliseconds ? [
self.$input.milliseconds
] : []), self.options.ampm ? [
self.$input.ampm
] : []),
separators: $.merge($.merge($.merge([
2010-09-03 20:54:40 +00:00
{title: ':', width: 8},
2010-09-03 08:47:40 +00:00
], self.options.seconds ? [
2010-09-03 20:54:40 +00:00
{title: ':', width: 8}
2010-09-03 08:47:40 +00:00
] : []), self.options.milliseconds ? [
2010-09-03 20:54:40 +00:00
{title: '.', width: 8}
2010-09-03 08:47:40 +00:00
] : []), self.options.ampm ? [
2010-09-03 20:54:40 +00:00
{title: '', width: 8}
2010-09-03 08:47:40 +00:00
] : []),
//width: self.options.width || 128
}), self)
2010-09-03 20:54:40 +00:00
.bindEvent('change', setValue);
2010-09-03 08:47:40 +00:00
setValue();
function getDate() {
2010-09-03 20:54:40 +00:00
return new Date('1970/01/01 ' + (
2010-09-03 08:47:40 +00:00
self.options.milliseconds ?
self.options.value.substr(0, self.options.value.length - 4) :
self.options.value
));
}
function getValues() {
self.date = getDate();
return {
2010-09-03 20:54:40 +00:00
ampm: Ox.formatDate(self.date, '%p'),
hours: Ox.formatDate(self.date, self.options.ampm ? '%I' : '%H'),
milliseconds: self.options.milliseconds ? self.options.value.substr(-3) : '000',
minutes: Ox.formatDate(self.date, '%M'),
seconds: Ox.formatDate(self.date, '%S')
2010-09-03 08:47:40 +00:00
};
}
function setValue() {
2010-09-03 20:54:40 +00:00
self.options.value = Ox.formatDate(new Date('1970/01/01 ' + [
self.$input.hours.options('value'),
self.$input.minutes.options('value'),
self.options.seconds ? self.$input.seconds.options('value') : '00'
].join(':') + (self.options.ampm ? ' ' + self.$input.ampm.options('value') : '')),
(self.options.seconds? '%T' : '%H:%M')) +
(self.options.milliseconds ? '.' + self.$input.milliseconds.options('value') : '');
Ox.print('SETVALUE', self.options.value);
2010-09-03 08:47:40 +00:00
}
function setValues() {
self.values = getValues();
$.each(self.$input, function(k, v) {
self.$input[k].options({
value: self.values[k]
});
});
}
self.onChange = function(key, value) {
2010-09-03 20:54:40 +00:00
if (key == 'value') {
2010-09-03 08:47:40 +00:00
setValues();
}
}
return that;
};
Ox.Label = function(options, self) {
var self = self || {},
2010-09-03 20:54:40 +00:00
that = new Ox.Element('div', self)
2010-09-03 08:47:40 +00:00
.defaults({
disabled: false,
2010-09-03 20:54:40 +00:00
id: '',
overlap: 'none',
textAlign: 'left',
title: '',
width: 'auto'
2010-09-03 08:47:40 +00:00
})
.options(options)
.addClass(
2010-09-03 20:54:40 +00:00
'OxLabel' + (self.options.disabled ? ' OxDisabled' : '') +
(self.options.overlap != 'none' ?
' OxOverlap' + Ox.toTitleCase(self.options.overlap) : '')
2010-09-03 08:47:40 +00:00
)
2010-09-03 20:54:40 +00:00
.css($.extend(self.options.width == 'auto' ? {} : {
width: (self.options.width - 14) + 'px'
2010-09-03 08:47:40 +00:00
}, {
textAlign: self.options.textAlign
}))
.html(self.options.title);
2010-11-28 15:06:47 +00:00
self.onChange = function(key, value) {
if (key == 'title') {
that.html(value);
}
}
2010-09-03 08:47:40 +00:00
return that;
};
Ox.OptionGroup = function(items, min, max, property) {
/*
to be used by ButtonGroup, CheckboxGroup, Select and Menu
*/
2010-09-03 20:54:40 +00:00
var property = property || 'checked'
2010-09-03 08:47:40 +00:00
length = items.length,
max = max == -1 ? length : max;
function getLastBefore(pos) {
// returns the position of the last checked item before position pos
var last = -1;
Ox.print(items, items.length, length, $.merge(
pos > 0 ? Ox.range(pos - 1, -1, -1) : [],
2010-09-03 20:54:40 +00:00
pos < items.length - 1 ? Ox.range(items.length - 1, pos, -1) : []
2010-09-03 08:47:40 +00:00
))
2010-09-03 20:54:40 +00:00
// fixme: why is length not == items.length here?
2010-09-03 08:47:40 +00:00
$.each($.merge(
pos > 0 ? Ox.range(pos - 1, -1, -1) : [],
2010-09-03 20:54:40 +00:00
pos < items.length - 1 ? Ox.range(items.length - 1, pos, -1) : []
2010-09-03 08:47:40 +00:00
), function(i, v) {
Ox.print(pos, v)
if (items[v][property]) {
last = v;
return false;
}
});
return last;
}
function getNumber() {
// returns the number of checked items
var num = 0;
$.each(items, function(i, item) {
if (item[property]) {
num++;
}
})
return num;
}
this[property] = function() {
// returns an array with the positions of all checked item
var checked = [];
$.each(items, function(i, item) {
if (item[property]) {
checked.push(i);
}
})
return checked;
};
this.init = function() {
var num = getNumber(),
count = 0;
//if (num < min || num > max) {
$.each(items, function(i, item) {
if (Ox.isUndefined(item[property])) {
item[property] = false;
}
if (item[property]) {
count++;
if (count > max) {
item[property] = false;
}
} else {
if (num < min) {
item[property] = true;
num++;
}
}
});
//}
return items;
};
this.toggle = function(pos) {
var last,
num = getNumber(),
toggled = [];
if (!items[pos][property]) { // check
if (num >= max) {
last = getLastBefore(pos);
items[last][property] = false;
toggled.push(last);
}
if (!items[pos][property]) {
items[pos][property] = true;
toggled.push(pos);
}
} else { // uncheck
if (num > min) {
items[pos][property] = false;
toggled.push(pos);
}
}
return toggled;
}
return this;
}
2010-12-06 17:42:45 +00:00
/**
options
2010-09-03 08:47:40 +00:00
arrows boolean if true, show arrows
arrowStep number step when clicking arrows
2010-09-03 20:54:40 +00:00
arrowSymbols array arrow symbols, like ['minus', 'plus']
2010-09-03 08:47:40 +00:00
max number maximum value
min number minimum value
2010-09-03 20:54:40 +00:00
orientation string 'horizontal' or 'vertical'
2010-09-03 08:47:40 +00:00
step number step between values
size number width or height, in px
thumbSize number minimum width or height of thumb, in px
thumbValue boolean if true, display value on thumb
trackGradient array colors
trackImages string or array one or multiple track background image URLs
trackStep number 0 (scroll here) or step when clicking track
value number initial value
valueNames array value names to display on thumb
2010-12-06 17:42:45 +00:00
*/
Ox.Range = function(options, self) {
2010-09-03 08:47:40 +00:00
var self = self || {},
that = new Ox.Element({}, self)
.defaults({
arrows: false,
arrowStep: 1,
2010-09-03 20:54:40 +00:00
arrowSymbols: ['previous', 'next'],
2010-09-03 08:47:40 +00:00
max: 100,
min: 0,
2010-09-03 20:54:40 +00:00
orientation: 'horizontal',
2010-09-03 08:47:40 +00:00
step: 1,
size: 128,
thumbSize: 16,
thumbValue: false,
trackColors: [],
trackImages: [],
trackStep: 0,
value: 0,
valueNames: null,
})
.options($.extend(options, {
arrowStep: options.arrowStep ?
options.arrowStep : options.step,
trackImages: $.makeArray(options.trackImages || [])
}))
2010-09-03 20:54:40 +00:00
.addClass('OxRange')
2010-09-03 08:47:40 +00:00
.css({
2010-09-03 20:54:40 +00:00
width: self.options.size + 'px'
2010-09-03 08:47:40 +00:00
});
$.extend(self, {
trackColors: self.options.trackColors.length,
trackImages: self.options.trackImages.length,
trackSize: self.options.size - self.options.arrows * 32,
values: (self.options.max - self.options.min + self.options.step) /
self.options.step
});
$.extend(self, {
thumbSize: Math.max(self.trackSize / self.values, self.options.thumbSize),
trackImageWidths: self.trackImages == 1 ? [self.trackSize - 16] :
Ox.divideInt(self.trackSize - 2, self.trackImages)
});
$.extend(self, {
trackColorsStart: self.thumbSize / 2 / self.options.size,
trackColorsStep: (self.options.size - self.thumbSize) / (self.trackColors - 1) / self.options.size
});
if (self.options.arrows) {
self.$arrows = [];
$.each(Ox.range(0, 2), function(i) {
self.$arrows[i] = Ox.Button({
2010-09-03 20:54:40 +00:00
overlap: i == 0 ? 'right' : 'left',
2010-09-03 08:47:40 +00:00
title: self.options.arrowSymbols[i],
2010-09-03 20:54:40 +00:00
type: 'image'
2010-09-03 08:47:40 +00:00
})
2010-09-03 20:54:40 +00:00
.addClass('OxArrow')
2010-09-03 08:47:40 +00:00
.mousedown(function(e) {
clickArrow(e, i);
})
.appendTo(that.$element);
});
}
2010-09-03 20:54:40 +00:00
self.$track = $('<div>')
.addClass('OxTrack')
2010-09-03 08:47:40 +00:00
.css($.extend({
2010-09-03 20:54:40 +00:00
width: (self.trackSize - 2) + 'px'
2010-09-03 08:47:40 +00:00
}, self.trackImages == 1 ? {
2010-09-03 20:54:40 +00:00
background: 'rgb(0, 0, 0)'
2010-09-03 08:47:40 +00:00
} : {}))
.mousedown(clickTrack)
.appendTo(that.$element);
self.trackColors && setTrackColors();
if (self.trackImages) {
2010-09-03 20:54:40 +00:00
self.$trackImages = $('<div>')
2010-09-03 08:47:40 +00:00
.css({
2010-09-03 20:54:40 +00:00
width: self.trackSize + 'px',
marginRight: (-self.trackSize - 1) + 'px'
2010-09-03 08:47:40 +00:00
})
.appendTo(self.$track);
$.each(self.options.trackImages, function(i, v) {
Ox.print(self.trackImageWidths[i])
2010-09-03 20:54:40 +00:00
$('<img>')
2010-09-03 08:47:40 +00:00
.attr({
src: v
})
2010-09-03 20:54:40 +00:00
.addClass(i == 0 ? 'OxFirstChild' : '')
.addClass(i == self.trackImages - 1 ? 'OxLastChild' : '')
2010-09-03 08:47:40 +00:00
.css({
2010-09-03 20:54:40 +00:00
width: self.trackImageWidths[i] + 'px'
2010-09-03 08:47:40 +00:00
})
.mousedown(function(e) {
e.preventDefault(); // prevent drag
})
.appendTo(self.$trackImages);
//left += self.trackImageWidths[i];
});
}
self.$thumb = Ox.Button({
2010-09-03 20:54:40 +00:00
id: self.options.id + 'Thumb',
2010-09-03 08:47:40 +00:00
title: self.options.thumbValue ? (self.options.valueNames ?
self.options.valueNames[self.options.value] :
2010-09-03 20:54:40 +00:00
self.options.value) : '',
2010-09-03 08:47:40 +00:00
width: self.thumbSize
})
2010-09-03 20:54:40 +00:00
.addClass('OxThumb')
2010-09-03 08:47:40 +00:00
/*
.css({
2010-09-03 20:54:40 +00:00
border: '1px solid rgb(255, 255, 255)',
background: 'rgba(0, 0, 0, 0)'
2010-09-03 08:47:40 +00:00
})
*/
.appendTo(self.$track);
setThumb();
function clickArrow(e, i) {
// fixme: shift doesn't work, see menu scrolling
var interval,
timeout = setTimeout(function() {
interval = setInterval(function() {
setValue(self.options.value + self.options.arrowStep * (i == 0 ? -1 : 1));
}, 50);
}, 500);
setValue(self.options.value + self.options.arrowStep * (i == 0 ? -1 : 1) * (e.shiftKey ? 2 : 1), true);
2010-09-03 20:54:40 +00:00
$window.one('mouseup', function() {
2010-09-03 08:47:40 +00:00
clearInterval(interval);
clearTimeout(timeout);
});
}
function clickTrack(e) {
//Ox.Focus.focus();
2010-09-03 20:54:40 +00:00
var isThumb = $(e.target).hasClass('OxThumb'),
2010-09-03 08:47:40 +00:00
left = self.$track.offset().left,
offset = isThumb ? e.clientX - self.$thumb.offset().left - 8 /*self.thumbSize / 2*/ : 0;
setValue(val(e), !isThumb);
$window.mousemove(function(e) {
setValue(val(e));
});
2010-09-03 20:54:40 +00:00
$window.one('mouseup', function() {
$window.unbind('mousemove');
2010-09-03 08:47:40 +00:00
});
function val(e) {
return getVal(e.clientX - left - offset);
}
}
function getPx(val) {
var pxPerVal = (self.trackSize - self.thumbSize) /
(self.options.max - self.options.min);
return Math.ceil((val - self.options.min) * pxPerVal);
}
/*
function getTime(oldValue, newValue) {
return self.animationTime * Math.abs(oldValue - newValue) / (self.options.max - self.options.min);
}
*/
function getVal(px) {
var px = self.trackSize / self.values >= 16 ? px : px - 8,
valPerPx = (self.options.max - self.options.min) /
(self.trackSize - self.thumbSize);
2010-09-03 08:47:40 +00:00
return Ox.limit(self.options.min +
Math.floor(px * valPerPx / self.options.step) * self.options.step,
self.options.min, self.options.max);
}
function setThumb(animate) {
self.$thumb.stop().animate({
2010-09-03 20:54:40 +00:00
marginLeft: (getPx(self.options.value) - 1) + 'px',
//width: self.thumbSize + 'px'
2010-09-03 08:47:40 +00:00
}, animate ? 200 : 0, function() {
if (self.options.thumbValue) {
self.$thumb.options({
title: self.options.valueNames ?
self.options.valueNames[self.options.value] :
self.options.value
});
}
});
}
function setTrackColors() {
self.$track.css({
backgroundImage: $.browser.mozilla ?
2010-09-03 20:54:40 +00:00
('-moz-linear-gradient(left, ' +
self.options.trackColors[0] + ' 0%, ' + $.map(self.options.trackColors, function(v, i) {
return v + ' ' + ((self.trackColorsStart + self.trackColorsStep * i) * 100) + '%';
}).join(', ') + ', ' + self.options.trackColors[self.trackColors - 1] + ' 100%)') :
('-webkit-gradient(linear, left top, right top, color-stop(0, ' +
self.options.trackColors[0] + '), ' + $.map(self.options.trackColors, function(v, i) {
return 'color-stop(' + (self.trackColorsStart + self.trackColorsStep * i) + ', ' + v + ')';
}).join(', ') + ', color-stop(1, ' + self.options.trackColors[self.trackColors - 1] + '))')
2010-09-03 08:47:40 +00:00
});
}
function setValue(value, animate) {
var value = Ox.limit(value, self.options.min, self.options.max);
if (value != self.options.value) {
//time = getTime(self.options.value, value);
self.options.value = value;
setThumb(animate);
2010-09-03 20:54:40 +00:00
that.triggerEvent('change', {
2010-09-03 08:47:40 +00:00
value: value
});
}
}
self.onChange = function(key, value) {
2010-09-03 20:54:40 +00:00
if (key == 'trackColors') {
2010-09-03 08:47:40 +00:00
setTrackColors();
2010-09-03 20:54:40 +00:00
} else if (key == 'value') {
2010-09-03 08:47:40 +00:00
setThumb();
}
}
return that;
};
Ox.Select = function(options, self) {
var self = self || {},
2010-09-03 20:54:40 +00:00
that = new Ox.Element('div', self) // fixme: do we use 'div', or {}, or '', by default?
2010-09-03 08:47:40 +00:00
.defaults({
2010-09-03 20:54:40 +00:00
id: '',
2010-09-03 08:47:40 +00:00
items: [],
max: 1,
min: 1,
2010-09-03 20:54:40 +00:00
overlap: 'none', // can be none, left or right
2010-09-03 08:47:40 +00:00
selectable: true,
2010-09-03 20:54:40 +00:00
size: 'medium',
title: '',
type: 'text', // can be 'text' or 'image'
width: 'auto'
2010-09-03 08:47:40 +00:00
})
.options(options)
.addClass(
2010-09-03 20:54:40 +00:00
'OxSelect Ox' + Ox.toTitleCase(self.options.size) +
(self.options.overlap == 'none' ? '' : ' OxOverlap' +
2010-09-03 08:47:40 +00:00
Ox.toTitleCase(self.options.overlap))
)
2010-09-03 20:54:40 +00:00
.css(self.options.width == 'auto' ? {} : {
width: self.options.width + 'px'
2010-09-04 14:28:40 +00:00
})
2010-12-26 20:16:35 +00:00
.bindEvent({
2010-09-04 14:28:40 +00:00
key_escape: loseFocus,
key_down: showMenu
2010-09-03 08:47:40 +00:00
});
$.extend(self, {
2010-09-03 20:54:40 +00:00
buttonId: self.options.id + 'Button',
groupId: self.options.id + 'Group',
menuId: self.options.id + 'Menu'
2010-09-03 08:47:40 +00:00
});
if (self.options.selectable) {
self.optionGroup = new Ox.OptionGroup(
self.options.items,
self.options.min,
self.options.max
);
self.options.items = self.optionGroup.init();
self.checked = self.optionGroup.checked();
}
2010-09-03 20:54:40 +00:00
if (self.options.type == 'text') {
self.$title = $('<div>')
.addClass('OxTitle')
2010-09-03 08:47:40 +00:00
.css({
2010-09-03 20:54:40 +00:00
width: (self.options.width - 22) + 'px'
2010-09-03 08:47:40 +00:00
})
.html(
self.options.title ? self.options.title :
self.options.items[self.checked[0]].title
)
2010-09-04 14:28:40 +00:00
.click(showMenu)
2010-09-03 08:47:40 +00:00
.appendTo(that.$element);
}
self.$button = new Ox.Button({
id: self.buttonId,
2010-09-03 20:54:40 +00:00
style: 'symbol',
title: 'select',
type: 'image'
2010-09-03 08:47:40 +00:00
})
2010-09-04 14:28:40 +00:00
.bindEvent('click', showMenu)
2010-09-03 08:47:40 +00:00
.appendTo(that);
self.$menu = new Ox.Menu({
element: self.$title || self.$button,
id: self.menuId,
items: [self.options.selectable ? {
group: self.groupId,
items: self.options.items,
max: self.options.max,
min: self.options.min
} : self.options.items],
2010-09-03 20:54:40 +00:00
side: 'bottom',
2010-09-03 08:47:40 +00:00
size: self.options.size
})
2010-09-03 20:54:40 +00:00
.bindEvent({
change: changeMenu,
click: clickMenu,
hide: hideMenu
});
2010-09-03 08:47:40 +00:00
2010-12-27 05:01:24 +00:00
self.options.type == 'image' && self.$menu.addClass('OxRight');
2010-09-03 08:47:40 +00:00
function clickMenu(event, data) {
2010-12-26 20:16:35 +00:00
Ox.print('%% clickMenu')
2010-09-03 20:54:40 +00:00
}
function changeMenu(event, data) {
Ox.print('clickMenu: ', self.options.id, data)
self.checked = self.optionGroup.checked();
2010-09-03 08:47:40 +00:00
self.$title && self.$title.html(
self.options.title ? self.options.title :
2010-09-03 20:54:40 +00:00
data.checked[0].title
2010-09-03 08:47:40 +00:00
);
2010-09-04 14:28:40 +00:00
that.triggerEvent('change', {
selected: data.checked
});
2010-09-03 08:47:40 +00:00
}
function hideMenu() {
2010-12-26 20:16:35 +00:00
Ox.print('%% hideMenu that', that, 'self', self)
2010-09-03 20:54:40 +00:00
that.removeClass('OxSelected');
2010-12-26 20:16:35 +00:00
// self.$button.removeClass('OxSelected');
Ox.print('%% hideMenu end')
2010-09-03 08:47:40 +00:00
}
2010-09-04 14:28:40 +00:00
function loseFocus() {
that.loseFocus();
}
function showMenu() {
that.gainFocus();
that.addClass('OxSelected');
self.$menu.showMenu();
}
2010-09-03 08:47:40 +00:00
self.onChange = function(key, value) {
};
2010-09-03 20:54:40 +00:00
that.selected = function() {
return $.map(/*self.checked*/self.optionGroup.checked(), function(v, i) {
return {
id: self.options.items[i].id,
title: self.options.items[i].title
};
});
};
2010-09-03 08:47:40 +00:00
that.selectItem = function(id) {
2010-09-03 20:54:40 +00:00
Ox.print('selectItem', id, Ox.getObjectById(self.options.items, id).title)
self.options.type == 'text' && self.$title.html(
Ox.getObjectById(self.options.items, id).title[0] // fixme: title should not have become an array
2010-09-03 08:47:40 +00:00
);
self.$menu.checkItem(id);
2010-09-03 20:54:40 +00:00
self.checked = self.optionGroup.checked();
2010-09-03 08:47:40 +00:00
};
/*
that.width = function(val) {
// fixme: silly hack, and won't work for css() ... remove!
that.$element.width(val + 16);
that.$button.width(val);
//that.$symbol.width(val);
return that;
};
*/
return that;
2010-12-23 17:05:46 +00:00
};
2010-09-03 08:47:40 +00:00
Ox.FormElementGroup = function(options, self) {
var self = self || {},
2010-09-03 20:54:40 +00:00
that = new Ox.Element('div', self)
2010-09-03 08:47:40 +00:00
.defaults({
2010-09-03 20:54:40 +00:00
id: '',
2010-09-03 08:47:40 +00:00
elements: [],
2010-09-03 20:54:40 +00:00
float: 'left',
2010-09-03 08:47:40 +00:00
separators: [],
width: 0
})
.options(options || {})
2010-09-03 20:54:40 +00:00
.addClass('OxInputGroup');
2010-09-03 08:47:40 +00:00
2010-09-03 20:54:40 +00:00
$.each(self.options.float == 'left' ? self.options.elements : self.options.elements.reverse(), function(i, $element) {
2010-12-26 20:16:35 +00:00
$element.css({
2010-09-03 08:47:40 +00:00
float: self.options.float // fixme: make this a class
})
2010-12-26 20:16:35 +00:00
.bindEvent({
validate: function(event, data) {
that.triggerEvent({
validate: data
});
}
})
2010-09-03 08:47:40 +00:00
.appendTo(that);
});
/*
if (self.options.width) {
setWidths();
} else {
self.options.width = getWidth();
}
that.css({
2010-09-03 20:54:40 +00:00
width: self.options.width + 'px'
2010-09-03 08:47:40 +00:00
});
*/
function getWidth() {
}
function setWidth() {
}
self.onChange = function(key, value) {
2010-09-03 20:54:40 +00:00
if (key == 'trackColors') {
2010-09-03 08:47:40 +00:00
}
}
2010-12-26 20:16:35 +00:00
that.value = function() {
return $.map(self.options.elements, function(input) {
var ret = null;
['checked', 'selected', 'value'].forEach(function(v) {
input[v] && (ret = input[v]());
});
return ret;
});
};
2010-09-03 08:47:40 +00:00
return that;
2010-12-23 17:05:46 +00:00
};
2010-09-03 08:47:40 +00:00
Ox.Picker = function(options, self) {
var self = self || {},
2010-09-03 20:54:40 +00:00
that = new Ox.Element('div', self)
2010-09-03 08:47:40 +00:00
.defaults({
element: null,
elementHeight: 128,
elementWidth: 256,
2010-09-03 20:54:40 +00:00
id: '',
overlap: 'none'
2010-09-03 08:47:40 +00:00
})
.options(options || {});
self.$selectButton = new Ox.Button({
overlap: self.options.overlap,
2010-09-03 20:54:40 +00:00
title: 'select',
type: 'image'
2010-09-03 08:47:40 +00:00
})
.click(showMenu)
.appendTo(that);
2010-09-03 20:54:40 +00:00
self.$menu = new Ox.Element('div')
.addClass('OxPicker')
2010-09-03 08:47:40 +00:00
.css({
2010-09-03 20:54:40 +00:00
width: self.options.elementWidth + 'px',
height: (self.options.elementHeight + 24) + 'px'
2010-09-03 08:47:40 +00:00
});
self.options.element
.css({
2010-09-03 20:54:40 +00:00
width: self.options.elementWidth + 'px',
height: self.options.elementHeight + 'px'
2010-09-03 08:47:40 +00:00
})
.appendTo(self.$menu);
self.$bar = new Ox.Bar({
2010-09-03 20:54:40 +00:00
orientation: 'horizontal',
2010-09-03 08:47:40 +00:00
size: 24
})
.appendTo(self.$menu);
that.$label = new Ox.Label({
width: self.options.elementWidth - 60
})
.appendTo(self.$bar);
self.$doneButton = new Ox.Button({
2010-09-03 20:54:40 +00:00
title: 'Done',
2010-09-03 08:47:40 +00:00
width: 48
})
.click(hideMenu)
.appendTo(self.$bar);
2010-09-03 20:54:40 +00:00
self.$layer = $('<div>')
.addClass('OxLayer')
2010-09-03 08:47:40 +00:00
.click(hideMenu);
function hideMenu() {
self.$menu.detach();
self.$layer.detach();
self.$selectButton
2010-09-03 20:54:40 +00:00
.removeClass('OxSelected')
2010-09-03 08:47:40 +00:00
.css({
2010-09-03 20:54:40 +00:00
MozBorderRadius: '8px',
WebkitBorderRadius: '8px'
2010-09-03 08:47:40 +00:00
});
2010-09-03 20:54:40 +00:00
that.triggerEvent('hide');
2010-09-03 08:47:40 +00:00
};
function showMenu() {
var offset = that.offset(),
left = offset.left,
top = offset.top + 15;
self.$selectButton
2010-09-03 20:54:40 +00:00
.addClass('OxSelected')
2010-09-03 08:47:40 +00:00
.css({
2010-09-03 20:54:40 +00:00
MozBorderRadius: '8px 8px 0 0',
WebkitBorderRadius: '8px 8px 0 0'
2010-09-03 08:47:40 +00:00
});
self.$layer.appendTo($body);
self.$menu
.css({
2010-09-03 20:54:40 +00:00
left: left + 'px',
top: top + 'px'
2010-09-03 08:47:40 +00:00
})
.appendTo($body);
2010-09-03 20:54:40 +00:00
that.triggerEvent('show');
2010-09-03 08:47:40 +00:00
};
return that;
};
Ox.ColorPicker = function(options, self) {
var self = self || {},
2010-09-03 20:54:40 +00:00
that = new Ox.Element('div', self)
2010-09-03 08:47:40 +00:00
.defaults({
2010-09-03 20:54:40 +00:00
id: '',
value: '0, 0, 0'
2010-09-03 08:47:40 +00:00
})
.options(options || {});
Ox.print(self)
self.$ranges = [];
2010-09-03 20:54:40 +00:00
self.rgb = ['red', 'green', 'blue'];
self.values = self.options.value.split(', ');
2010-09-03 08:47:40 +00:00
$.each(Ox.range(3), function(i) {
self.$ranges[i] = new Ox.Range({
arrows: true,
id: self.options.id + Ox.toTitleCase(self.rgb[i]),
max: 255,
size: 328, // 256 + 16 + 40 + 16
thumbSize: 40,
thumbValue: true,
trackColors: getColors(i),
value: self.values[i]
})
.css({
2010-09-03 20:54:40 +00:00
position: 'absolute',
top: (i * 15) + 'px'
2010-09-03 08:47:40 +00:00
})
2010-09-03 20:54:40 +00:00
.bindEvent('change', function(event, data) {
2010-09-03 08:47:40 +00:00
change(i, data.value);
})
.appendTo(that);
// fixme: make self.$ranges[i].children() work
if (i == 0) {
2010-09-03 20:54:40 +00:00
self.$ranges[i].$element.children('input.OxOverlapRight').css({
2010-09-03 08:47:40 +00:00
MozBorderRadius: 0,
WebkitBorderRadius: 0
});
2010-09-03 20:54:40 +00:00
self.$ranges[i].$element.children('input.OxOverlapLeft').css({
MozBorderRadius: '0 8px 0 0',
WebkitBorderRadius: '0 8px 0 0'
2010-09-03 08:47:40 +00:00
});
} else {
2010-09-03 20:54:40 +00:00
self.$ranges[i].$element.children('input').css({
2010-09-03 08:47:40 +00:00
MozBorderRadius: 0,
WebkitBorderRadius: 0
});
}
});
that = new Ox.Picker({
element: that,
elementHeight: 46,
elementWidth: 328,
id: self.options.id
});
function change(index, value) {
self.values[index] = value;
2010-09-03 20:54:40 +00:00
self.options.value = self.values.join(', ');
2010-09-03 08:47:40 +00:00
that.$label.css({
2010-09-03 20:54:40 +00:00
background: 'rgb(' + self.options.value + ')'
2010-09-03 08:47:40 +00:00
});
$.each(Ox.range(3), function(i) {
if (i != index) {
self.$ranges[i].options({
trackColors: getColors(i)
});
}
});
2010-09-03 20:54:40 +00:00
that.triggerEvent('change', {
2010-09-03 08:47:40 +00:00
value: self.options.value
});
}
function getColors(index) {
return [
2010-09-03 20:54:40 +00:00
'rgb(' + $.map(Ox.range(3), function(v) {
2010-09-03 08:47:40 +00:00
return v == index ? 0 : self.values[v];
2010-09-03 20:54:40 +00:00
}).join(', ') + ')',
'rgb(' + $.map(Ox.range(3), function(v) {
2010-09-03 08:47:40 +00:00
return v == index ? 255 : self.values[v];
2010-09-03 20:54:40 +00:00
}).join(', ') + ')'
2010-09-03 08:47:40 +00:00
]
}
return that;
};
Ox.PlacePicker = function(options, self) {
var self = $.extend(self || {}, {
options: $.extend({
2010-09-03 20:54:40 +00:00
id: '',
value: 'United States'
2010-09-03 08:47:40 +00:00
}, options)
}),
that;
2010-09-03 20:54:40 +00:00
self.$element = new Ox.Element('div')
2010-09-03 08:47:40 +00:00
.css({
2010-09-03 20:54:40 +00:00
width: '256px',
height: '192px'
2010-09-03 08:47:40 +00:00
})
.append(
self.$topBar = new Ox.Bar({
size: 16
})
.css({
2010-09-03 20:54:40 +00:00
MozBorderRadius: '0 8px 0 0',
WebkitBorderRadius: '0 8px 0 0'
2010-09-03 08:47:40 +00:00
})
.append(
self.$input = new Ox.Input({
clear: true,
2010-09-03 20:54:40 +00:00
id: self.options.id + 'Input',
placeholder: 'Find',
2010-09-03 08:47:40 +00:00
width: 256
})
2010-09-03 20:54:40 +00:00
.bindEvent('submit', findPlace)
2010-09-03 08:47:40 +00:00
)
)
.append(
2010-09-03 20:54:40 +00:00
self.$container = new Ox.Element('div')
2010-09-03 08:47:40 +00:00
.css({
2010-09-03 20:54:40 +00:00
width: '256px',
height: '160px'
2010-09-03 08:47:40 +00:00
})
)
.append(
self.$bottomBar = new Ox.Bar({
size: 16
})
.append(
self.$range = new Ox.Range({
arrows: true,
2010-09-03 20:54:40 +00:00
id: self.options.id + 'Range',
2010-09-03 08:47:40 +00:00
max: 22,
size: 256,
thumbSize: 32,
thumbValue: true
})
2010-09-03 20:54:40 +00:00
.bindEvent('change', changeZoom)
2010-09-03 08:47:40 +00:00
)
);
2010-09-03 20:54:40 +00:00
self.$input.$element.children('input[type=text]').css({
width: '230px',
paddingLeft: '2px',
MozBorderRadius: '0 8px 8px 0',
WebkitBorderRadius: '0 8px 8px 0'
2010-09-03 08:47:40 +00:00
});
2010-09-03 20:54:40 +00:00
self.$input.$element.children('input[type=image]').css({
MozBorderRadius: '0 8px 0 0',
WebkitBorderRadius: '0 8px 0 0'
2010-09-03 08:47:40 +00:00
});
2010-09-03 20:54:40 +00:00
self.$range.$element.children('input').css({
2010-09-03 08:47:40 +00:00
MozBorderRadius: 0,
WebkitBorderRadius: 0
});
that = new Ox.Picker({
element: self.$element,
elementHeight: 192,
elementWidth: 256,
id: self.options.id,
overlap: self.options.overlap,
value: self.options.value
}, self)
2010-09-03 20:54:40 +00:00
.bindEvent('show', showPicker);
2010-09-03 08:47:40 +00:00
2010-09-03 20:54:40 +00:00
that.$label.bind('click', clickLabel)
2010-09-03 08:47:40 +00:00
self.map = false;
function changeZoom(event, data) {
2010-09-03 20:54:40 +00:00
Ox.print('changeZoom')
2010-09-03 08:47:40 +00:00
self.$map.zoom(data.value);
}
function clickLabel() {
var name = that.$label.html();
if (name) {
self.$input.options({
value: name
})
2010-09-03 20:54:40 +00:00
.triggerEvent('submit', {
2010-09-03 08:47:40 +00:00
value: name
});
}
}
function findPlace(event, data) {
2010-09-03 20:54:40 +00:00
Ox.print('findPlace', data);
2010-12-06 17:42:05 +00:00
self.$map.find(data.value, function(place) {
place && that.$label.html(place.geoname);
2010-09-03 08:47:40 +00:00
})
}
function onSelect(event, data) {
2010-12-06 17:42:05 +00:00
that.$label.html(data.geoname);
2010-09-03 08:47:40 +00:00
}
function onZoom(event, data) {
self.$range.options({
value: data.value
});
}
function showPicker() {
if (!self.map) {
self.$map = new Ox.Map({
2010-09-03 20:54:40 +00:00
id: self.options.id + 'Map',
2010-09-03 08:47:40 +00:00
places: [self.options.value]
})
.css({
2010-09-03 20:54:40 +00:00
width: '256px',
height: '160px'
2010-09-03 08:47:40 +00:00
})
.bindEvent({
select: onSelect,
zoom: onZoom
})
.appendTo(self.$container);
self.map = true;
}
}
return that;
};
2010-12-06 17:42:45 +00:00
/**
2010-09-03 08:47:40 +00:00
delete below
*/
Ox.Input_ = function(options, self) {
/*
options:
clear boolean, clear button, or not
disabled boolean, disabled, or not
2010-09-03 20:54:40 +00:00
height height (px), if type is 'textarea'
2010-09-03 08:47:40 +00:00
id
label string, or
array [{ id, title, checked }] (selectable label) or
array [{ id, label: [{ id, title, checked }], width }] (multiple selectable labels)
label and placeholder are mutually exclusive
labelWidth integer (px)
placeholder string, or
array [{ id, title, checked }] (selectable placeholder)
label and placeholder are mutually exclusive
separator string, or
array of strings
to separate multiple values
separatorWidth integer (px), or
array of integers
serialize function
2010-09-03 20:54:40 +00:00
size 'large', 'medium' or 'small'
type 'password', 'select' or 'text'
unit string, or
2010-09-03 08:47:40 +00:00
array [{ id, title, checked }] (selectable unit)
unitWidth integer (px)
value string, or
array [{ id, value, width }] (multiple values)
width integer (px)
methods:
events:
*/
var self = self || {},
2010-09-03 20:54:40 +00:00
that = new Ox.Element('div', self)
2010-09-03 08:47:40 +00:00
.defaults({
autocomplete: null,
autocorrect: null,
autosuggest: null,
autosuggestHighlight: false,
autosuggestSubmit: false,
autovalidate: null,
2010-09-03 20:54:40 +00:00
autovalidateName: 'Value',
2010-09-03 08:47:40 +00:00
clear: false,
disabled: false,
height: 128,
2010-09-03 20:54:40 +00:00
id: '',
key: '',
label: '',
2010-09-03 08:47:40 +00:00
labelWidth: 64,
2010-09-03 20:54:40 +00:00
placeholder: '',
separator: '',
2010-09-03 08:47:40 +00:00
separatorWidth: 16,
serialize: null,
2010-09-03 20:54:40 +00:00
size: 'medium',
type: 'text',
unit: '',
2010-09-03 08:47:40 +00:00
unitWidth: 64,
2010-09-03 20:54:40 +00:00
value: '',
2010-09-03 08:47:40 +00:00
width: 128
})
.options(options || {})
2010-09-03 20:54:40 +00:00
.addClass('OxInput Ox' + Ox.toTitleCase(self.options.size))
2010-09-03 08:47:40 +00:00
.css({
2010-09-03 20:54:40 +00:00
width: self.options.width + 'px'
2010-09-03 08:47:40 +00:00
});
$.extend(self, {
clearWidth: 16,
2010-09-03 20:54:40 +00:00
hasMultipleKeys: Ox.isArray(self.options.label) && 'label' in self.options.label[0],
2010-09-03 08:47:40 +00:00
hasMultipleValues: Ox.isArray(self.options.value) &&
2010-09-03 20:54:40 +00:00
(self.options.type != 'select' || 'items' in self.options.value[0]),
2010-09-03 08:47:40 +00:00
hasSelectableKeys: Ox.isArray(self.options.label) || Ox.isArray(self.options.placeholder),
hasSelectableUnits: Ox.isArray(self.options.unit),
2010-09-03 20:54:40 +00:00
keyName: self.options.label ? 'label' : (self.options.placeholder ? 'placeholder' : ''),
2010-09-03 08:47:40 +00:00
placeholderWidth: 16,
selectedKey: [0], // fixme: only set on demand?
selectedValue: 0,
selectedUnit: 0,
/* valid: autovalidateCall(true) */
});
2010-09-03 20:54:40 +00:00
$.each(['autocomplete', 'autocorrect', 'autosuggest', 'autovalidate'], function(i, v) {
2010-09-03 08:47:40 +00:00
//if (!Ox.isFunction(self.options[v])) {
self.options[v] = {
2010-09-03 20:54:40 +00:00
'': self.options[v]
2010-09-03 08:47:40 +00:00
};
//}
});
if (self.keyName && !self.hasMultipleKeys) {
self.options[self.keyName] = [$.extend({
2010-09-03 20:54:40 +00:00
id: '',
2010-09-03 08:47:40 +00:00
label: self.options[self.keyName],
2010-09-03 20:54:40 +00:00
}, self.keyName == 'label' ? {
id: '',
2010-09-03 08:47:40 +00:00
width: self.options.labelWidth
} : {})];
if (!self.hasSelectableKeys) {
self.options[self.keyName][0].label = [{
2010-09-03 20:54:40 +00:00
id: '',
2010-09-03 08:47:40 +00:00
title: self.options[self.keyName][0].label
}];
}
}
if (self.hasSelectableKeys) {
$.each(self.options[self.keyName], function(keyPos, key) {
if (key.width) {
self.options.labelWidth = (keyPos == 0 ? 0 : self.options.labelWidth) + key.width;
}
self.selectedKey[keyPos] = 0;
$.each(key, function(valuePos, value) {
if (value.checked) {
self.selectedKey[keyPos] = valuePos;
return false;
}
});
});
}
self.valueWidth = self.options.width -
(self.options.label ? self.options.labelWidth : 0) -
((self.options.placeholder && self.options.placeholder[0].label.length > 1) ? self.placeholderWidth : 0) -
(self.options.unit ? self.options.unitWidth : 0) -
(self.options.clear ? self.clearWidth : 0);
/*
if (self.hasMultipleValues) {
self.valueWidth -= Ox.isArray(self.options.separatorWidth) ?
Ox.sum(self.options.separatorWidth) :
(self.options.value.length - 1) * self.options.separatorWidth;
}
*/
2010-09-03 20:54:40 +00:00
Ox.print('self.hasMulVal', self.hasMultipleValues);
Ox.print('self.options.value', self.options.value)
2010-09-03 08:47:40 +00:00
if (!self.hasMultipleValues) {
2010-09-03 20:54:40 +00:00
if (self.options.type == 'select') {
2010-09-03 08:47:40 +00:00
self.options.value = [{
2010-09-03 20:54:40 +00:00
id: '',
2010-09-03 08:47:40 +00:00
items: self.options.value,
width: self.valueWidth
}];
2010-09-03 20:54:40 +00:00
} else if (self.options.type == 'range') {
2010-09-03 08:47:40 +00:00
self.options.value = [$.extend({
2010-09-03 20:54:40 +00:00
id: '',
2010-09-03 08:47:40 +00:00
size: self.valueWidth
}, self.options.value)];
2010-02-19 10:24:02 +00:00
} else {
2010-09-03 08:47:40 +00:00
self.options.value = [{
2010-09-03 20:54:40 +00:00
id: '',
2010-09-03 08:47:40 +00:00
value: self.options.value,
width: self.valueWidth
}]
}
}
2010-09-03 20:54:40 +00:00
Ox.print('self.options.value', self.options.value)
2010-09-03 08:47:40 +00:00
self.values = self.options.value.length;
2010-09-03 20:54:40 +00:00
Ox.print(self.options.id, 'self.values', self.values)
2010-09-03 08:47:40 +00:00
if (Ox.isString(self.options.separator)) {
self.options.separator = $.map(new Array(self.values - 1), function(v, i) {
return self.options.separator;
});
}
if (Ox.isNumber(self.options.separatorWidth)) {
self.options.separatorWidth = $.map(new Array(self.values - 1), function(v, i) {
return self.options.separatorWidth;
});
}
if (self.options.unit) {
if (self.hasSelectableUnits) {
$.each(self.options.unit, function(pos, unit) {
if (unit.checked) {
self.selectedUnit = pos;
return false;
2010-02-19 10:24:02 +00:00
}
});
2010-09-03 08:47:40 +00:00
} else {
self.options.unit = [{
2010-09-03 20:54:40 +00:00
id: '',
2010-09-03 08:47:40 +00:00
title: self.options.unit
}];
2010-02-19 10:24:02 +00:00
}
2010-02-20 11:05:58 +00:00
}
2010-09-03 20:54:40 +00:00
Ox.print('self', self);
2010-09-03 08:47:40 +00:00
if (self.keyName) {
that.$key = [];
$.each(self.options[self.keyName], function(keyPos, key) {
2010-09-03 20:54:40 +00:00
Ox.print('keyPos key', keyPos, key)
if (self.keyName == 'label' && key.label.length == 1) {
2010-09-03 08:47:40 +00:00
that.$key[keyPos] = new Ox.Label({
2010-09-03 20:54:40 +00:00
overlap: 'right',
2010-09-03 08:47:40 +00:00
title: key.label[0].title,
width: self.options.labelWidth
})
.css({
2010-09-03 20:54:40 +00:00
float: 'left'
2010-09-03 08:47:40 +00:00
})
.click(function() {
that.$input[0].focus();
})
.appendTo(that);
} else if (key.label.length > 1) {
2010-09-03 20:54:40 +00:00
Ox.print('key.length > 1')
2010-09-03 08:47:40 +00:00
self.selectKeyId = self.options.id + Ox.toTitleCase(self.keyName) +
2010-09-03 20:54:40 +00:00
(self.options[self.keyName].length == 1 ? '' : keyPos);
Ox.print('three', self.selectedKey, keyPos, self.selectedKey[keyPos]);
2010-09-03 08:47:40 +00:00
that.$key[keyPos] = new Ox.Select({
id: self.selectKeyId,
items: $.map(key.label, function(value, valuePos) {
return {
checked: valuePos == self.selectedKey[keyPos],
id: value.id,
group: self.selectKeyId, // fixme: same id, works here, but should be different
title: value.title
};
}),
2010-09-03 20:54:40 +00:00
overlap: 'right',
type: self.options.label ? 'text' : 'image',
2010-09-03 08:47:40 +00:00
width: self.options.label ? (self.options.label.length == 1 ? self.options.labelWidth : key.width) : self.placeholderWidth
})
.css({
2010-09-03 20:54:40 +00:00
float: 'left'
2010-09-03 08:47:40 +00:00
})
.appendTo(that);
2010-09-03 20:54:40 +00:00
that.bindEvent('change_' + self.selectKeyId, changeKey);
2010-09-03 08:47:40 +00:00
}
});
}
if (self.options.clear) {
that.$clear = new Ox.Button({
2010-09-03 20:54:40 +00:00
overlap: 'left',
type: 'image',
value: 'clear'
2010-09-03 08:47:40 +00:00
})
.css({
2010-09-03 20:54:40 +00:00
float: 'right'
2010-09-03 08:47:40 +00:00
})
.click(clear)
.appendTo(that);
}
if (self.options.unit.length == 1) {
that.$unit = new Ox.Label({
2010-09-03 20:54:40 +00:00
overlap: 'left',
2010-09-03 08:47:40 +00:00
title: self.options.unit[0].title,
width: self.options.unitWidth
})
.css({
2010-09-03 20:54:40 +00:00
float: 'right'
2010-09-03 08:47:40 +00:00
})
.click(function() {
that.$input[0].focus();
})
.appendTo(that);
} else if (self.options.unit.length > 1) {
2010-09-03 20:54:40 +00:00
self.selectUnitId = self.options.id + 'Unit';
2010-09-03 08:47:40 +00:00
that.$unit = new Ox.Select({
id: self.selectUnitId,
items: $.map(self.options.unit, function(unit, i) {
2010-09-03 20:54:40 +00:00
Ox.print('unit', unit)
2010-09-03 08:47:40 +00:00
return {
checked: i == 0,
id: unit.id,
group: self.selectUnitId, // fixme: same id, works here, but should be different
title: unit.title
};
}),
2010-09-03 20:54:40 +00:00
overlap: 'left',
2010-09-03 08:47:40 +00:00
size: self.options.size,
width: self.options.unitWidth
})
.css({
2010-09-03 20:54:40 +00:00
float: 'right'
2010-09-03 08:47:40 +00:00
})
.appendTo(that);
}
if (self.values) {
that.$separator = [];
$.each(self.options.value, function(i, v) {
if (i < self.values - 1) {
that.$separator[i] = new Ox.Label({
2010-09-03 20:54:40 +00:00
textAlign: 'center',
2010-09-03 08:47:40 +00:00
title: self.options.separator[i],
width: self.options.separatorWidth[i] + 32
})
.css({
2010-09-03 20:54:40 +00:00
float: 'left',
marginLeft: (v.width - (i == 0 ? 16 : 32)) + 'px'
2010-09-03 08:47:40 +00:00
})
.click(function() {
that.$input[0].focus();
})
.appendTo(that);
}
});
}
that.$input = [];
//self.margin = 0;
$.each(self.options.value, function(i, v) {
2010-09-03 20:54:40 +00:00
Ox.print('o k i', self.options, self.keyName, i);
2010-09-03 08:47:40 +00:00
var id = self.keyName ? $.map(self.selectedKey, function(v, i) {
return self.options[self.keyName][i].id;
2010-09-03 20:54:40 +00:00
}).join('.') : '';
2010-09-03 08:47:40 +00:00
//self.margin -= (i == 0 ? 16 : self.options.value[i - 1].width)
2010-09-03 20:54:40 +00:00
Ox.print('v:', v, 'id:', id)
if (self.options.type == 'select') {
2010-09-03 08:47:40 +00:00
that.$input[i] = new Ox.Select({
id: v.id,
items: v.items,
width: v.width
}).
css({
2010-09-03 20:54:40 +00:00
float: 'left'
2010-09-03 08:47:40 +00:00
});
2010-09-03 20:54:40 +00:00
} else if (self.options.type == 'range') {
2010-09-03 08:47:40 +00:00
that.$input[i] = new Ox.Range(v)
.css({
2010-09-03 20:54:40 +00:00
float: 'left'
2010-09-03 08:47:40 +00:00
});
} else {
that.$input[i] = new Ox.InputElement({
autocomplete: self.options.autocomplete[id],
autocorrect: self.options.autocorrect[id],
autosuggest: self.options.autosuggest[id],
autosuggestHighlight: self.options.autosuggestHighlight,
autosuggestSubmit: self.options.autosuggestSubmit,
autovalidate: self.options.autovalidate[id],
autovalidateName: self.options.autovalidateName,
disabled: self.options.disabled,
height: self.options.height,
2010-09-03 20:54:40 +00:00
id: self.options.id + 'Input' + Ox.toTitleCase(v.id),
key: self.hasSelectableKeys ? self.options[self.keyName][0].label[self.selectedKey[0]].id : '',
2010-09-03 08:47:40 +00:00
parent: that,
2010-09-03 20:54:40 +00:00
placeholder: self.options.placeholder ? self.options.placeholder[0].label[0].title : '',
2010-09-03 08:47:40 +00:00
size: self.options.size,
type: self.options.type,
value: v.value,
width: v.width
});
}
that.$input[i]
.css($.extend({}, self.options.value.length > 1 ? {
2010-09-03 20:54:40 +00:00
float: 'left',
2010-09-03 08:47:40 +00:00
marginLeft: -Ox.sum($.map(self.options.value, function(v_, i_) {
return i_ > i ? self.options.value[i_ - 1].width + self.options.separatorWidth[i_ - 1] : (i_ == i ? 16 : 0);
}))
} : {}))
.appendTo(that);
});
//width(self.options.width);
function changeKey(event, data) {
2010-09-03 20:54:40 +00:00
Ox.print('changeKey', data);
2010-09-03 08:47:40 +00:00
if (data) { // fixme: necessary?
self.key = {
2010-09-03 08:47:40 +00:00
id: data.id,
title: data.value // fixme: should be data.title
};
that.$input[0].options({
key: data.id
});
}
if (self.options.label) {
//that.$label.html(self.option.title);
that.$input[0].focus();
//autocompleteCall();
} else {
that.$input[0].options({
placeholder: data.value // fixme: should be data.title
});
/*
2010-09-03 20:54:40 +00:00
if (that.$input.hasClass('OxPlaceholder')) {
2010-09-03 08:47:40 +00:00
that.$input.val(self.key.title);
//that.$input.focus();
2010-07-02 12:33:45 +00:00
} else {
2010-09-03 08:47:40 +00:00
that.$input.focus();
self.options.autosuggest && autosuggestCall();
2010-07-02 12:33:45 +00:00
}
2010-09-03 08:47:40 +00:00
*/
}
}
function changeUnit() {
that.$input[0].focus();
}
function clear() {
$.each(that.$input, function(i, v) {
2010-09-03 20:54:40 +00:00
v.val('');
2010-09-03 08:47:40 +00:00
});
that.$input[0].focus();
}
function height(value) {
var stop = 8 / value;
2010-09-03 20:54:40 +00:00
if (self.options.type == 'textarea') {
2010-09-03 08:47:40 +00:00
that.$element
.height(value)
.css({
2010-09-03 20:54:40 +00:00
background: '-moz-linear-gradient(top, rgb(224, 224, 224), rgb(208, 208, 208) ' + (stop * 100) + '%, rgb(208, 208, 208) ' + (100 - stop * 100) + '%, rgb(192, 192, 192))'
2010-09-03 08:47:40 +00:00
})
.css({
2010-09-03 20:54:40 +00:00
background: '-webkit-gradient(linear, left top, left bottom, from(rgb(224, 224, 224)), color-stop(' + stop + ', rgb(208, 208, 208)), color-stop(' + (1 - stop) + ', rgb(208, 208, 208)), to(rgb(192, 192, 192)))'
2010-09-03 08:47:40 +00:00
});
that.$input
.height(value)
.css({
2010-09-03 20:54:40 +00:00
background: '-moz-linear-gradient(top, rgb(224, 224, 224), rgb(240, 240, 240) ' + (stop * 100) + '%, rgb(240, 240, 240) ' + (100 - stop * 100) + '%, rgb(255, 255, 255))'
2010-09-03 08:47:40 +00:00
})
.css({
2010-09-03 20:54:40 +00:00
background: '-webkit-gradient(linear, left top, left bottom, from(rgb(224, 224, 224)), color-stop(' + stop + ', rgb(240, 240, 240)), color-stop(' + (1 - stop) + ', rgb(240, 240, 240)), to(rgb(255, 255, 255)))'
2010-09-03 08:47:40 +00:00
});
}
}
function selectUnit() {
self.$selectUnitMenu.show();
}
function submit() {
2010-09-03 20:54:40 +00:00
Ox.print('submit')
2010-09-03 08:47:40 +00:00
var value = that.$input.val();
that.$input.blur();
2010-09-03 20:54:40 +00:00
that.triggerEvent('submit', self.options.key ? {
2010-09-03 08:47:40 +00:00
key: self.options.key,
value: value
} : value);
}
function width(value) {
that.$element.width(value);
that.$input.width(
2010-09-03 20:54:40 +00:00
value - (self.options.type == 'textarea' ? 0 : 12) -
2010-09-03 08:47:40 +00:00
(self.options.label ? self.options.labelWidth : 0) -
(self.options.placeholder.length > 1 ? 16 : 0) -
(self.options.unit ? self.options.unitWidth : 0) -
(self.options.clear ? 16 : 0)
);
}
self.onChange = function(key, value) {
2010-09-03 20:54:40 +00:00
if (key == 'height') {
2010-09-03 08:47:40 +00:00
height(value);
2010-09-03 20:54:40 +00:00
} else if (key == 'width') {
2010-09-03 08:47:40 +00:00
width(value);
}
};
that.changeLabel = function(id) {
that.$key.html(Ox.getObjectById(self.options.label, id).title);
self.selectMenu.checkItem(id);
};
return that;
}
Ox.InputElement_ = function(options, self) {
var self = self || {},
that = new Ox.Element(
2010-09-03 20:54:40 +00:00
options.type == 'textarea' ? 'textarea' : 'input', self
2010-09-03 08:47:40 +00:00
)
.defaults({
autocomplete: null,
autocorrect: null,
autosuggest: null,
autosuggestHighlight: false,
autosuggestSubmit: false,
autovalidate: null,
disabled: false,
height: 128,
2010-09-03 20:54:40 +00:00
id: '',
key: '',
2010-09-03 08:47:40 +00:00
parent: null,
2010-09-03 20:54:40 +00:00
placeholder: '',
size: 'medium',
type: 'text',
value: '',
width: 128
2010-09-03 08:47:40 +00:00
})
.options(options || {})
2010-09-03 20:54:40 +00:00
.addClass('OxInput Ox' + Ox.toTitleCase(self.options.size) + (
(self.options.placeholder && self.options.value === '') ?
' OxPlaceholder' : ''
2010-09-03 08:47:40 +00:00
))
2010-09-03 20:54:40 +00:00
.attr(self.options.type == 'textarea' ? {} : {
2010-09-03 08:47:40 +00:00
type: self.options.type
})
.css({
2010-09-03 20:54:40 +00:00
float: 'left',
width: (self.options.width - 14) + 'px'
2010-09-03 08:47:40 +00:00
})
.val(
2010-09-03 20:54:40 +00:00
(self.options.placeholder && self.options.value === '') ?
2010-09-03 08:47:40 +00:00
self.options.placeholder : self.options.value
)
.blur(blur)
.change(change)
.focus(focus);
2010-09-03 20:54:40 +00:00
Ox.print('InputElement self.options', self.options)
2010-09-03 08:47:40 +00:00
self.bindKeyboard = self.options.autocomplete || self.options.autocorrect ||
self.options.autosuggest || self.options.autovalidate;
if (self.options.autosuggest) {
2010-09-03 20:54:40 +00:00
self.autosuggestId = self.options.id + 'Menu'; // fixme: we do this in other places ... are we doing it the same way? var name?
2010-09-03 08:47:40 +00:00
self.$autosuggestMenu = new Ox.Menu({
element: that.$element,
id: self.autosuggestId,
offset: {
left: 4,
top: 0
},
size: self.options.size
});
2010-09-03 20:54:40 +00:00
that.bindEvent('click_' + self.autosuggestId, clickMenu);
2010-09-03 08:47:40 +00:00
}
2010-09-03 20:54:40 +00:00
that.bindEvent($.extend(self.options.type == 'textarea' ? {} : {
2010-09-03 08:47:40 +00:00
key_enter: submit
}, {
key_escape: cancel
}));
function autocomplete(value) {
var value = value.toLowerCase(),
2010-09-03 20:54:40 +00:00
ret = '';
if (value !== '') {
2010-09-03 08:47:40 +00:00
$.each(self.options.autocomplete, function(i, v) {
if (v.toLowerCase().indexOf(value) == 0) {
ret = v;
return false;
}
});
}
return ret;
}
function autocompleteCall() {
var value = that.$element.val();
Ox.isFunction(self.options.autocomplete) ?
self.options.autocomplete(self.options.key ? {
key: self.options.key,
value: value
} : value, autocompleteCallback) :
autocompleteCallback(autocomplete(value));
}
function autocompleteCallback(value) {
var pos = cursor()[0];
if (value) {
that.$element.val(value);
cursor(pos, value.length);
2010-02-19 10:24:02 +00:00
}
2010-02-18 07:27:32 +00:00
}
2010-02-19 10:24:02 +00:00
2010-09-03 08:47:40 +00:00
function autocorrect(value) {
var length = value.length;
2010-09-03 20:54:40 +00:00
return $.map(value.toLowerCase().split(''), function(v, i) {
2010-09-03 08:47:40 +00:00
if (new RegExp(self.options.autocorrect)(v)) {
return v
} else {
return null;
}
2010-09-03 20:54:40 +00:00
}).join('');
2010-09-03 08:47:40 +00:00
}
function autocorrectCall(blur) {
var blur = blur || false,
value = that.$element.val(),
pos = cursor()[0];
Ox.isFunction(self.options.autocorrect) ?
self.options.autocorrect(value, blur, autocorrectCallback) :
autocorrectCallback(autocorrect(value), blue);
}
function autocorrectCallback(value, blur) {
var length = that.$element.val().length;
that.$element.val(self.options.value);
!blur && cursor(pos + value.length - length);
}
function autosuggest(value) {
var value = value.toLowerCase(),
values = [[], []];
2010-09-03 20:54:40 +00:00
if (value !== '') {
2010-09-03 08:47:40 +00:00
$.each(self.options.key ? self.options.autosuggest[self.options.key] : self.options.autosuggest, function(i, v) {
2010-09-03 20:54:40 +00:00
//Ox.print('v...', v)
2010-09-03 08:47:40 +00:00
var index = v.toLowerCase().indexOf(value);
index > -1 && values[index == 0 ? 0 : 1].push(v);
});
}
return $.merge(values[0], values[1]);
}
function autosuggestCall() {
var value = that.$element.val();
Ox.isFunction(self.options.autosuggest) ?
self.options.autosuggest(self.options.key ? {
key: self.options.key,
value: value
} : value, autosuggestCallback) :
autosuggestCallback(autosuggest(value));
}
function autosuggestCallback(values) {
var values = values || [],
selected = values.length == 1 ? 0 : -1,
value = that.$element.val().toLowerCase();
2010-09-03 20:54:40 +00:00
//Ox.print('values', values);
2010-09-03 08:47:40 +00:00
if (values.length) {
values = $.map(values, function(v, i) {
if (value == v.toLowerCase()) {
selected = i;
2010-02-18 15:11:14 +00:00
}
2010-02-18 07:56:37 +00:00
return {
2010-09-03 20:54:40 +00:00
id: v.toLowerCase().replace(/ /g, '_'), // fixme: need function to do lowercase, underscores etc?
2010-09-03 08:47:40 +00:00
title: self.options.autosuggestHighlight ? v.replace(
2010-09-03 20:54:40 +00:00
new RegExp('(' + value + ')', 'ig'),
'<span class="OxHighlight">$1</span>'
2010-09-03 08:47:40 +00:00
) : v
2010-02-18 09:11:47 +00:00
};
2010-02-18 07:56:37 +00:00
});
2010-09-03 08:47:40 +00:00
// self.selectMenu && self.selectMenu.hideMenu(); // fixme: need event
self.$autosuggestMenu.options({
items: values,
2010-02-18 15:11:14 +00:00
selected: selected
2010-02-18 07:56:37 +00:00
}).showMenu();
} else {
2010-09-03 08:47:40 +00:00
self.$autosuggestMenu.hideMenu();
2010-02-18 07:56:37 +00:00
}
2010-02-10 15:49:33 +00:00
}
2010-02-19 10:24:02 +00:00
2010-09-03 08:47:40 +00:00
function autovalidate(value) {
return {
valid: self.options.autovalidate(value) != null,
2010-09-03 20:54:40 +00:00
message: 'Invalid ' + self.options.name
2010-09-03 08:47:40 +00:00
};
}
function autovalidateCall(blur) {
2010-07-24 01:32:08 +00:00
var blur = blur || false,
2010-09-03 08:47:40 +00:00
value = that.$element.val();
2010-09-03 20:54:40 +00:00
if (value !== '') {
2010-09-03 08:47:40 +00:00
Ox.isFunction(self.options.autovalidate) ?
self.options.autovalidate(value, autovalidateCallback) :
autovalidateCallback(autovalidate(value), blur);
} else {
autovalidateCallback({
blur: blur,
valid: false,
2010-09-03 20:54:40 +00:00
message: 'Empty ' + self.options.name
2010-09-03 08:47:40 +00:00
});
}
}
function autovalidateCallback(data, blur) {
if (data.valid != self.valid) {
self.valid = data.valid;
2010-09-03 20:54:40 +00:00
that.triggerEvent('validate', $.extend(data, {
2010-09-03 08:47:40 +00:00
blur: blur
}));
2010-07-24 01:32:08 +00:00
}
}
function blur() {
2010-09-03 20:54:40 +00:00
if (!self.options.autosuggest || self.$autosuggestMenu.is(':hidden')) {
Ox.print('losing focus...')
2010-09-03 08:47:40 +00:00
that.loseFocus();
2010-09-03 20:54:40 +00:00
self.options.parent.removeClass('OxFocus');
2010-09-03 08:47:40 +00:00
self.options.autocorrect && autocorrectCall(true);
// self.options.autosuggest && self.$autosuggestMenu.hideMenu();
self.options.autovalidate && autovalidateCall(true);
2010-09-03 20:54:40 +00:00
if (self.options.placeholder && that.$element.val() === '') {
that.$element.addClass('OxPlaceholder').val(self.options.placeholder);
2010-09-03 08:47:40 +00:00
}
2010-07-24 01:32:08 +00:00
}
2010-09-03 08:47:40 +00:00
if (self.bindKeyboard) {
2010-09-03 20:54:40 +00:00
$document.unbind('keydown', keypress);
$document.unbind('keypress', keypress);
2010-07-24 01:32:08 +00:00
}
}
2010-02-19 10:24:02 +00:00
function cancel() {
2010-09-03 08:47:40 +00:00
that.$element.blur();
2010-02-19 10:24:02 +00:00
}
2010-09-03 08:47:40 +00:00
function change() {
2010-02-19 10:24:02 +00:00
}
2010-02-20 11:05:58 +00:00
function clear() {
2010-09-03 20:54:40 +00:00
that.$element.val('').focus();
2010-07-24 01:32:08 +00:00
}
2010-09-03 08:47:40 +00:00
function clickMenu(event, data) {
2010-09-03 20:54:40 +00:00
Ox.print('clickMenu', data);
2010-09-03 08:47:40 +00:00
that.$element.val(data.title);
//self.$autosuggestMenu.hideMenu();
self.options.autosuggestSubmit && submit();
}
function cursor(start, end) {
/*
cursor() returns [start, end]
cursor(start) sets start
cursor([start, end]) sets start and end
cursor(start, end) sets start and end
*/
var isArray = Ox.isArray(start);
2010-07-24 01:32:08 +00:00
if (arguments.length == 0) {
2010-09-03 08:47:40 +00:00
return [that.$element[0].selectionStart, that.$element[0].selectionEnd];
2010-07-24 01:32:08 +00:00
} else {
2010-09-03 08:47:40 +00:00
start = isArray ? start[0] : start;
end = isArray ? start[1] : (end ? end : start);
that.$element[0].setSelectionRange(start, end);
2010-07-24 01:32:08 +00:00
}
2010-01-07 20:21:07 +00:00
}
2010-02-19 10:24:02 +00:00
2010-01-07 20:21:07 +00:00
function focus() {
2010-09-03 08:47:40 +00:00
var val = that.$element.val();
2010-02-19 10:24:02 +00:00
that.gainFocus();
2010-09-03 20:54:40 +00:00
self.options.parent.addClass('OxFocus');
if (that.$element.hasClass('OxPlaceholder')) {
that.$element.val('').removeClass('OxPlaceholder');
2010-01-07 20:21:07 +00:00
}
2010-09-03 08:47:40 +00:00
if (self.bindKeyboard) {
2010-02-18 07:27:32 +00:00
// fixme: different in webkit and firefox (?), see keyboard handler, need generic function
2010-09-03 08:47:40 +00:00
$document.keydown(keypress);
$document.keypress(keypress);
2010-09-03 20:54:40 +00:00
Ox.print('calling autosuggest...')
2010-09-03 08:47:40 +00:00
self.options.autosuggest && setTimeout(autosuggestCall, 0); // fixme: why is the timeout needed?
2010-02-18 07:27:32 +00:00
}
2010-01-07 20:21:07 +00:00
}
2010-02-19 10:24:02 +00:00
2010-02-18 14:24:17 +00:00
function keypress(event) {
2010-09-03 20:54:40 +00:00
Ox.print('keyCode', event.keyCode)
2010-09-03 08:47:40 +00:00
if (event.keyCode != 9 && event.keyCode != 13 && event.keyCode != 27) { // fixme: can't 13 and 27 return false?
setTimeout(function() { // fixme: document what this timeout is for
var value = that.$element.val();
if (value != self.options.value) {
self.options.value = value;
2010-07-24 01:32:08 +00:00
self.options.autocomplete && autocompleteCall();
2010-09-03 08:47:40 +00:00
self.options.autocorrect && autocorrectCall();
self.options.autosuggest && autosuggestCall();
self.options.autovalidate && autovalidateCall();
2010-02-18 15:15:23 +00:00
}
2010-02-18 15:19:36 +00:00
}, 25);
2010-02-18 15:15:23 +00:00
}
2010-02-18 07:56:37 +00:00
}
2010-02-19 10:24:02 +00:00
function submit() {
2010-09-03 08:47:40 +00:00
2010-07-24 01:32:08 +00:00
}
2010-09-03 08:47:40 +00:00
self.onChange = function(key, value) {
2010-09-03 20:54:40 +00:00
if (key == 'placeholder') {
that.$element.hasClass('OxPlaceholder') && that.$element.val(value);
} else if (key == 'value') {
2010-09-03 08:47:40 +00:00
if (self.options.placeholder) {
2010-09-03 20:54:40 +00:00
if (value === '') {
that.$element.addClass('OxPlaceholder').val(self.options.placeholder);
2010-09-03 08:47:40 +00:00
} else {
2010-09-03 20:54:40 +00:00
that.$element.removeClass('OxPlaceholder');
2010-09-03 08:47:40 +00:00
}
2010-07-24 01:32:08 +00:00
}
2010-09-03 08:47:40 +00:00
change(); // fixme: keypress too
2010-07-24 01:32:08 +00:00
}
}
2010-02-27 08:46:49 +00:00
return that;
2010-09-03 08:47:40 +00:00
}
2010-01-07 20:21:07 +00:00
2010-09-03 08:47:40 +00:00
Ox.Range_ = function(options, self) {
2010-01-07 20:21:07 +00:00
/*
init
*/
var self = self || {},
that = new Ox.Element({}, self)
.defaults({
animate: false,
arrows: false,
arrowStep: 1,
2010-09-03 20:54:40 +00:00
arrowSymbols: ['previous', 'next'],
2010-01-07 20:21:07 +00:00
max: 100,
min: 0,
2010-09-03 20:54:40 +00:00
orientation: 'horizontal',
2010-01-07 20:21:07 +00:00
step: 1,
size: 128,
thumbSize: 16,
thumbValue: false,
trackImages: [],
trackStep: 0,
2010-09-03 08:47:40 +00:00
value: 0,
valueNames: null
2010-01-07 20:21:07 +00:00
})
.options($.extend(options, {
arrowStep: options.arrowStep ?
options.arrowStep : options.step,
trackImages: $.makeArray(options.trackImages || [])
}))
2010-09-03 20:54:40 +00:00
.addClass('OxRange')
2010-09-03 08:47:40 +00:00
.css({
2010-09-03 20:54:40 +00:00
width: self.options.size + 'px'
2010-09-03 08:47:40 +00:00
});
2010-01-07 20:21:07 +00:00
// fixme: self. ... ?
var trackImages = self.options.trackImages.length,
values = (self.options.max - self.options.min + self.options.step) /
self.options.step;
/*
construct
*/
that.$element
.css({
2010-09-03 20:54:40 +00:00
width: self.options.size + 'px'
2010-01-07 20:21:07 +00:00
});
if (self.options.arrows) {
var $arrowDec = Ox.Button({
2010-09-03 20:54:40 +00:00
style: 'symbol',
type: 'image',
2010-03-06 09:54:30 +00:00
value: self.options.arrowSymbols[0]
2010-01-07 20:21:07 +00:00
})
2010-09-03 20:54:40 +00:00
.addClass('OxArrow')
2010-01-07 20:21:07 +00:00
.mousedown(mousedownArrow)
.click(clickArrowDec)
.appendTo(that.$element);
}
var $track = new Ox.Element()
2010-09-03 20:54:40 +00:00
.addClass('OxTrack')
2010-01-07 20:21:07 +00:00
.mousedown(clickTrack)
2010-09-03 08:47:40 +00:00
.appendTo(that.$element);
2010-01-07 20:21:07 +00:00
if (trackImages) {
var width = parseFloat(screen.width / trackImages),
2010-09-03 20:54:40 +00:00
$image = $('<canvas>')
2010-01-07 20:21:07 +00:00
.attr({
width: width * trackImages,
height: 14
})
2010-09-03 20:54:40 +00:00
.addClass('OxImage')
2010-09-03 08:47:40 +00:00
.appendTo($track.$element),
2010-01-07 20:21:07 +00:00
c = $image[0].getContext('2d');
2010-01-25 15:10:44 +00:00
c.mozImageSmoothingEnabled = false; // we may want to remove this later
2010-01-07 20:21:07 +00:00
$.each(self.options.trackImages, function(i, v) {
2010-09-03 08:47:40 +00:00
var left = 0;
2010-09-03 20:54:40 +00:00
$('<img/>')
2010-01-07 20:21:07 +00:00
.attr({
src: v
})
.load(function() {
2010-09-03 08:47:40 +00:00
c.drawImage(this, left, 0, self.trackImageWidth[i], 14);
2010-01-07 20:21:07 +00:00
});
2010-09-03 08:47:40 +00:00
left += self.trackImageWidth[i];
2010-01-07 20:21:07 +00:00
});
}
var $thumb = Ox.Button({})
2010-09-03 20:54:40 +00:00
.addClass('OxThumb')
2010-01-07 20:21:07 +00:00
.appendTo($track);
2010-09-03 20:54:40 +00:00
Ox.print('----')
2010-01-07 20:21:07 +00:00
if (self.options.arrows) {
var $arrowInc = Ox.Button({
2010-09-03 20:54:40 +00:00
style: 'symbol',
type: 'image',
2010-03-06 09:54:30 +00:00
value: self.options.arrowSymbols[1]
2010-01-07 20:21:07 +00:00
})
2010-09-03 20:54:40 +00:00
.addClass('OxArrow')
2010-01-07 20:21:07 +00:00
.mousedown(mousedownArrow)
.click(clickArrowInc)
.appendTo(that.$element);
}
var rangeWidth, trackWidth, imageWidth, thumbWidth;
setWidth(self.options.size);
/*
private functions
*/
function clickArrowDec() {
2010-09-03 20:54:40 +00:00
that.removeClass('OxActive');
2010-01-07 20:21:07 +00:00
setValue(self.options.value - self.options.arrowStep, 200)
}
function clickArrowInc() {
2010-09-03 20:54:40 +00:00
that.removeClass('OxActive');
2010-01-07 20:21:07 +00:00
setValue(self.options.value + self.options.arrowStep, 200);
}
function clickTrack(e) {
2010-09-03 08:47:40 +00:00
//Ox.Focus.focus();
2010-01-07 20:21:07 +00:00
var left = $track.offset().left,
2010-09-03 20:54:40 +00:00
offset = $(e.target).hasClass('OxThumb') ?
2010-01-07 20:21:07 +00:00
e.clientX - $thumb.offset().left - thumbWidth / 2 - 2 : 0;
function val(e) {
return getVal(e.clientX - left - offset);
}
setValue(val(e), 200);
$window.mousemove(function(e) {
setValue(val(e));
});
2010-09-03 20:54:40 +00:00
$window.one('mouseup', function() {
$window.unbind('mousemove');
2010-01-07 20:21:07 +00:00
});
}
function getPx(val) {
var pxPerVal = (trackWidth - thumbWidth - 2) /
2010-09-03 08:47:40 +00:00
(self.options.max - self.options.min);
2010-01-07 20:21:07 +00:00
return Math.ceil((val - self.options.min) * pxPerVal + 1);
}
function getVal(px) {
var px = trackWidth / values >= 16 ? px : px - 8,
valPerPx = (self.options.max - self.options.min) /
(trackWidth - thumbWidth);
2010-01-07 20:21:07 +00:00
return Ox.limit(self.options.min +
2010-09-03 08:47:40 +00:00
Math.floor(px * valPerPx / self.options.step) * self.options.step,
self.options.min, self.options.max);
2010-01-07 20:21:07 +00:00
}
function mousedownArrow() {
2010-09-03 20:54:40 +00:00
that.addClass('OxActive');
2010-01-07 20:21:07 +00:00
}
function setThumb(animate) {
2010-09-03 20:54:40 +00:00
var animate = typeof animate != 'undefined' ? animate : 0;
2010-01-07 20:21:07 +00:00
$thumb.animate({
2010-09-03 20:54:40 +00:00
marginLeft: (getPx(self.options.value) - 2) + 'px',
width: thumbWidth + 'px'
2010-01-07 20:21:07 +00:00
}, self.options.animate ? animate : 0, function() {
if (self.options.thumbValue) {
$thumb.options({
2010-09-03 08:47:40 +00:00
value: self.options.valueNames ?
self.options.valueNames[self.options.value] :
self.options.value
2010-01-07 20:21:07 +00:00
});
}
});
}
function setValue(val, animate) {
val = Ox.limit(val, self.options.min, self.options.max);
if (val != self.options.value) {
that.options({
value: val
});
setThumb(animate);
2010-09-03 20:54:40 +00:00
that.triggerEvent('change', { value: val });
2010-01-07 20:21:07 +00:00
}
}
function setWidth(width) {
trackWidth = width - self.options.arrows * 32;
thumbWidth = Math.max(trackWidth / values - 2, self.options.thumbSize - 2);
that.$element.css({
2010-09-03 20:54:40 +00:00
width: (width - 2) + 'px'
2010-01-07 20:21:07 +00:00
});
$track.css({
2010-09-03 20:54:40 +00:00
width: (trackWidth - 2) + 'px'
2010-01-07 20:21:07 +00:00
});
if (trackImages) {
$image.css({
2010-09-03 20:54:40 +00:00
width: (trackWidth - 2) + 'px'
2010-01-07 20:21:07 +00:00
});
}
$thumb.css({
2010-09-03 20:54:40 +00:00
width: (thumbWidth - 2) + 'px',
2010-01-07 20:21:07 +00:00
padding: 0
});
setThumb();
}
/*
shared functions
*/
2010-09-03 08:47:40 +00:00
self.onChange = function(key, value) {
2010-01-07 20:21:07 +00:00
}
return that;
};
2010-02-10 09:59:59 +00:00
/*
============================================================================
Lists
============================================================================
*/
2010-07-05 07:09:34 +00:00
Ox.IconList = function(options, self) {
var self = self || {},
that = new Ox.Element({}, self)
.defaults({
2011-01-03 12:01:38 +00:00
centerSelection: false,
2010-09-03 20:54:40 +00:00
id: '',
2010-07-05 07:09:34 +00:00
item: function() {},
keys: [],
2011-01-02 10:01:55 +00:00
max: -1,
min: 0,
2010-09-03 20:54:40 +00:00
orientation: 'both',
2010-07-05 07:09:34 +00:00
request: function() {},
2011-01-02 10:01:55 +00:00
selected: [],
2010-07-05 07:09:34 +00:00
size: 128,
sort: [],
})
.options(options || {});
$.extend(self, {
itemHeight: self.options.size * 1.5,
itemWidth: self.options.size
});
that.$element = new Ox.List({
2011-01-03 12:01:38 +00:00
centered: self.options.centered,
2010-07-05 07:09:34 +00:00
construct: constructItem,
2010-09-08 16:35:34 +00:00
id: self.options.id,
2010-07-05 07:09:34 +00:00
itemHeight: self.itemHeight,
itemWidth: self.itemWidth,
2010-09-08 16:35:34 +00:00
keys: self.options.keys,
2010-09-06 23:44:37 +00:00
orientation: self.options.orientation,
2010-09-05 14:24:22 +00:00
keys: self.options.keys,
2011-01-02 10:01:55 +00:00
max: self.options.max,
min: self.options.min,
2010-09-05 14:24:22 +00:00
request: self.options.request,
2011-01-02 10:01:55 +00:00
selected: self.options.selected,
2010-09-08 16:35:34 +00:00
size: self.options.size,
sort: self.options.sort,
type: 'icon',
2010-09-08 16:35:34 +00:00
unique: self.options.unique
2011-01-02 10:01:55 +00:00
}, $.extend({}, self)) // pass event handler
.addClass('OxIconList Ox' + Ox.toTitleCase(self.options.orientation))
2010-07-05 07:09:34 +00:00
.click(click)
.dblclick(dblclick)
.scroll(scroll);
2010-09-08 16:35:34 +00:00
updateKeys();
2010-07-05 07:09:34 +00:00
function click() {
}
function constructItem(data) {
2010-09-08 16:35:34 +00:00
var data = self.options.item(data, self.options.sort, self.options.size),
ratio = data.width / data.height;
2010-07-05 07:09:34 +00:00
return new Ox.IconItem($.extend(data, {
2010-09-08 16:35:34 +00:00
height: self.options.size / (ratio <= 1 ? 1 : ratio),
size: self.options.size,
width: self.options.size * (ratio >= 1 ? 1 : ratio)
2010-07-05 07:09:34 +00:00
}));
}
function dblclick() {
}
function scroll() {
}
2010-09-08 16:35:34 +00:00
function updateKeys() {
self.options.keys = Ox.unique($.merge(self.options.keys, [self.options.sort[0].key]));
that.$element.options({
keys: self.options.keys
});
}
self.onChange = function(key, value) {
if (key == 'request') {
that.$element.options(key, value);
}
}
2010-09-13 11:53:31 +00:00
that.closePreview = function() {
that.$element.closePreview();
};
2011-01-03 12:01:38 +00:00
that.scrollToSelection = function() {
that.$element.scrollToSelection();
};
2010-11-25 10:05:50 +00:00
that.size = function() {
that.$element.size();
}
2010-09-08 16:35:34 +00:00
that.sortList = function(key, operator) {
self.options.sort = [{
key: key,
operator: operator
}];
updateKeys();
that.$element.sortList(key, operator);
}
2010-09-03 20:54:40 +00:00
2010-07-05 07:09:34 +00:00
return that;
};
Ox.IconItem = function(options, self) {
var self = self || {},
2010-07-05 07:09:34 +00:00
that = new Ox.Element({}, self)
.defaults({
2010-09-08 16:35:34 +00:00
height: 128,
2010-09-03 20:54:40 +00:00
id: '',
info: '',
2010-07-05 07:09:34 +00:00
size: 128,
2010-09-03 20:54:40 +00:00
title: '',
2010-09-08 16:35:34 +00:00
width: 128,
2010-09-03 20:54:40 +00:00
url: ''
2010-07-05 07:09:34 +00:00
})
2010-09-06 23:44:37 +00:00
.options(options || {})
2010-07-05 07:09:34 +00:00
$.extend(self, {
2011-01-02 10:01:55 +00:00
fontSize: self.options.size == 64 ? 6 : 9,
height: self.options.size * 1.5,
2011-01-02 10:01:55 +00:00
lineLength: self.options.size == 64 ? 15 : 23,
2010-09-08 16:35:34 +00:00
lines: self.options.size == 64 ? 4 : 5,
2010-09-11 08:59:40 +00:00
url: oxui.path + '/png/ox.ui/transparent.png',
width: self.options.size
2010-07-05 07:09:34 +00:00
});
2010-09-08 16:35:34 +00:00
self.title = formatText(self.options.title, self.lines - 1, self.lineLength);
self.info = formatText(self.options.info, 5 - self.title.split('<br/>').length, self.lineLength);
2010-07-05 07:09:34 +00:00
that.css({
2010-09-03 20:54:40 +00:00
width: self.width + 'px',
height: self.height + 'px'
2010-07-05 07:09:34 +00:00
});
2010-09-03 20:54:40 +00:00
that.$icon = $('<div>')
.addClass('OxIcon')
2010-07-05 07:09:34 +00:00
.css({
2011-01-02 10:01:55 +00:00
top: self.options.size == 64 ? -64 : -124,
width: (self.options.size + 4) + 'px',
2010-09-11 08:59:40 +00:00
height: (self.options.size + 4) + 'px'
2010-07-05 07:09:34 +00:00
});
2010-09-03 20:54:40 +00:00
that.$iconImage = $('<img>')
.addClass('OxLoading OxTarget')
2010-07-05 07:09:34 +00:00
.attr({
2010-09-11 08:59:40 +00:00
src: self.url
2010-07-05 07:09:34 +00:00
})
.css({
2010-09-06 23:44:37 +00:00
width: self.options.width + 'px',
height: self.options.height + 'px'
2010-07-05 07:09:34 +00:00
})
.mousedown(mousedown)
2010-07-05 07:09:34 +00:00
.mouseenter(mouseenter)
.mouseleave(mouseleave)
2010-09-11 08:59:40 +00:00
.one('load', load);
2010-09-03 20:54:40 +00:00
that.$textBox = $('<div>')
.addClass('OxText')
2010-07-05 07:09:34 +00:00
.css({
2010-09-06 23:44:37 +00:00
top: (self.options.size / 2) + 'px',
width: (self.options.size + 4) + 'px',
2011-01-02 10:01:55 +00:00
height: (self.options.size == 64 ? 30 : 58) + 'px'
2010-07-05 07:09:34 +00:00
})
2010-09-03 20:54:40 +00:00
that.$text = $('<div>')
.addClass('OxTarget')
2010-09-08 16:35:34 +00:00
.css({
fontSize: self.fontSize + 'px'
})
2010-09-06 23:44:37 +00:00
.html(
2010-09-08 16:35:34 +00:00
self.title + '<br/><span class="OxInfo">' + self.info + '</span>'
2010-09-06 23:44:37 +00:00
)
2010-07-05 07:09:34 +00:00
.mouseenter(mouseenter)
.mouseleave(mouseleave);
2010-09-03 20:54:40 +00:00
that.$reflection = $('<div>')
.addClass('OxReflection')
2010-07-05 07:09:34 +00:00
.css({
2010-09-06 23:44:37 +00:00
top: self.options.size + 'px',
2010-09-11 08:59:40 +00:00
width: (self.options.size + 4) + 'px',
2010-09-03 20:54:40 +00:00
height: (self.options.size / 2) + 'px'
2010-07-05 07:09:34 +00:00
});
2010-09-03 20:54:40 +00:00
that.$reflectionImage = $('<img>')
.addClass('OxLoading')
2010-07-05 07:09:34 +00:00
.attr({
2010-09-11 08:59:40 +00:00
src: self.url
2010-07-05 07:09:34 +00:00
})
.css({
2010-09-03 20:54:40 +00:00
width: self.options.width + 'px',
2010-09-11 08:59:40 +00:00
height: self.options.height + 'px',
// firefox is 1px off when centering images with odd width and scaleY(-1)
paddingLeft: ($.browser.mozilla && self.options.width % 2 ? 1 : 0) + 'px'
});
2010-09-03 20:54:40 +00:00
that.$gradient = $('<div>')
2010-07-05 07:09:34 +00:00
.css({
2010-09-06 23:44:37 +00:00
//top: (-self.options.size / 2) + 'px',
width: self.options.width + 'px',
height: (self.options.size / 2) + 'px'
2010-07-05 07:09:34 +00:00
});
that.append(
that.$reflection.append(
that.$reflectionImage
).append(
2010-09-06 23:44:37 +00:00
that.$gradient
2010-07-05 07:09:34 +00:00
)
).append(
that.$textBox.append(
that.$text
)
).append(
that.$icon.append(
that.$iconImage
)
);
2010-09-08 16:35:34 +00:00
function formatText(text, maxLines, maxLength) {
var lines = Ox.wordwrap(text, maxLength, '<br/>', true, false).split('<br/>');
2010-09-06 23:44:37 +00:00
return $.map(lines, function(line, i) {
2010-09-08 16:35:34 +00:00
if (i < maxLines - 1) {
2010-09-06 23:44:37 +00:00
return line;
2010-09-08 16:35:34 +00:00
} else if (i == maxLines - 1) {
return lines.length == maxLines ? line : Ox.truncate($.map(lines, function(line, i) {
return i < maxLines - 1 ? null : line;
}).join(' '), maxLength, '...', 'center');
2010-09-06 23:44:37 +00:00
} else {
return null;
}
}).join('<br/>');
}
function load() {
2010-09-11 08:59:40 +00:00
that.$iconImage.attr({
src: self.options.url
})
.one('load', function() {
that.$iconImage.removeClass('OxLoading');
that.$reflectionImage.removeClass('OxLoading');
});
that.$reflectionImage.attr({
src: self.options.url
});
}
function mousedown(e) {
2010-09-14 13:50:51 +00:00
// fixme: preventDefault keeps image from being draggable in safari - but also keeps the list from getting focus
// e.preventDefault();
}
2010-07-05 07:09:34 +00:00
function mouseenter() {
2010-09-03 20:54:40 +00:00
that.addClass('OxHover');
2010-07-05 07:09:34 +00:00
}
function mouseleave() {
that.removeClass('OxHover');
2010-07-05 07:09:34 +00:00
}
return that;
};
2010-02-10 09:59:59 +00:00
Ox.List = function(options, self) {
var self = self || {},
2010-06-25 15:55:25 +00:00
that = new Ox.Container({}, self)
.defaults({
2011-01-03 12:01:38 +00:00
centered: false,
2010-06-28 11:19:04 +00:00
construct: function() {},
2010-06-25 15:55:25 +00:00
itemHeight: 16,
itemWidth: 16,
2010-06-28 09:16:36 +00:00
keys: [],
2011-01-02 10:01:55 +00:00
max: -1,
min: 0,
2010-09-03 20:54:40 +00:00
orientation: 'vertical',
pageLength: 100,
request: function() {}, // (data, callback), without data returns {items, size etc.}
2011-01-02 10:01:55 +00:00
selected: [],
2010-06-25 15:55:25 +00:00
sort: [],
2010-09-03 20:54:40 +00:00
type: 'text',
unique: ''
2010-06-25 15:55:25 +00:00
})
2010-06-28 09:16:36 +00:00
.options(options || {})
.scroll(scroll);
2010-06-25 15:55:25 +00:00
2010-12-22 14:16:15 +00:00
that.$content.mousedown(mousedown);
2010-06-25 15:55:25 +00:00
$.extend(self, {
$items: [],
$pages: [],
clickTimeout: 0,
ids: {},
itemMargin: self.options.type == 'text' ? 0 : 8, // 2 x 4 px margin ... fixme: the 2x should be computed later
2010-06-30 14:21:06 +00:00
keyboardEvents: {
key_end: scrollToFirst,
2010-07-20 20:04:13 +00:00
key_enter: open,
2010-06-30 14:21:06 +00:00
key_home: scrollToLast,
key_pagedown: scrollPageDown,
2010-07-20 20:04:13 +00:00
key_pageup: scrollPageUp,
key_section: preview, // fixme: firefox gets keyCode 0 when pressing space
2010-07-20 20:04:13 +00:00
key_space: preview
2010-06-30 14:21:06 +00:00
},
listMargin: self.options.type == 'text' ? 0 : 8, // 2 x 4 px padding
2010-06-25 15:55:25 +00:00
page: 0,
2010-07-20 20:04:13 +00:00
preview: false,
2010-06-28 09:16:36 +00:00
requests: [],
scrollTimeout: 0,
2010-06-25 15:55:25 +00:00
selected: []
});
2011-01-02 10:01:55 +00:00
if (self.options.max == -1) {
$.extend(self.keyboardEvents, {
key_alt_control_a: invertSelection,
key_control_a: selectAll
});
}
if (self.options.min == 0) {
$.extend(self.keyboardEvents, {
key_control_shift_a: selectNone
});
}
2010-09-06 23:44:37 +00:00
self.keyboardEvents['key_' + (self.options.orientation == 'vertical' ? 'up' : 'left')] = selectPrevious;
self.keyboardEvents['key_' + (self.options.orientation == 'vertical' ? 'down' : 'right')] = selectNext;
2011-01-02 10:01:55 +00:00
if (self.options.max == -1) {
self.keyboardEvents['key_' + (self.options.orientation == 'vertical' ? 'shift_up' : 'shift_left')] = addPreviousToSelection;
self.keyboardEvents['key_' + (self.options.orientation == 'vertical' ? 'shift_down' : 'shift_right')] = addNextToSelection;
}
2010-09-06 23:44:37 +00:00
if (self.options.orientation == 'both') {
$.extend(self.keyboardEvents, {
key_down: selectBelow,
2011-01-02 10:01:55 +00:00
key_up: selectAbove
});
2011-01-02 10:01:55 +00:00
if (self.options.max == -1) {
$.extend(self.keyboardEvents, {
key_shift_down: addBelowToSelection,
key_shift_up: addAboveToSelection
});
}
self.pageLengthByRowLength = [
2010-09-08 16:35:34 +00:00
0, 60, 60, 60, 60, 60, 60, 63, 64, 63, 60, 66, 60, 65, 70, 60, 64, 68, 72, 76, 60
];
2010-09-06 23:44:37 +00:00
}
2010-06-28 09:16:36 +00:00
updateQuery(self.options.selected);
2010-09-03 20:54:40 +00:00
Ox.print('s.o', self.options)
2010-12-26 20:16:35 +00:00
that.bindEvent(self.keyboardEvents);
2010-11-25 10:05:50 +00:00
$window.resize(that.size);
2010-06-25 15:55:25 +00:00
2010-09-06 23:44:37 +00:00
function addAboveToSelection() {
var pos = getAbove();
if (pos > -1) {
addToSelection(pos);
scrollToPosition(pos);
2010-09-06 23:44:37 +00:00
}
}
2010-06-25 15:55:25 +00:00
function addAllToSelection(pos) {
var arr,
len = self.$items.length;
if (!isSelected(pos)) {
2010-06-30 09:02:13 +00:00
if (self.selected.length == 0) {
2010-06-25 15:55:25 +00:00
addToSelection(pos);
} else {
2010-06-30 09:02:13 +00:00
if (Ox.min(self.selected) < pos) {
2010-06-25 15:55:25 +00:00
var arr = [pos];
for (var i = pos - 1; i >= 0; i--) {
if (isSelected(i)) {
$.each(arr, function(i, v) {
addToSelection(v);
});
break;
}
arr.push(i);
}
}
2010-06-30 09:02:13 +00:00
if (Ox.max(self.selected) > pos) {
2010-06-25 15:55:25 +00:00
var arr = [pos];
for (var i = pos + 1; i < len; i++) {
if (isSelected(i)) {
$.each(arr, function(i, v) {
addToSelection(v);
});
break;
}
arr.push(i);
}
}
}
}
}
2010-09-06 23:44:37 +00:00
function addBelowToSelection() {
var pos = getBelow();
if (pos > -1) {
addToSelection(pos);
scrollToPosition(pos);
2010-09-06 23:44:37 +00:00
}
}
2010-06-30 14:21:06 +00:00
function addNextToSelection() {
var pos = getNext();
if (pos > -1) {
addToSelection(pos);
scrollToPosition(pos);
2010-06-30 14:21:06 +00:00
}
}
function addPreviousToSelection() {
var pos = getPrevious();
if (pos > -1) {
addToSelection(pos);
scrollToPosition(pos);
2010-06-30 14:21:06 +00:00
}
}
2010-06-25 15:55:25 +00:00
function addToSelection(pos) {
if (!isSelected(pos)) {
2010-06-30 09:02:13 +00:00
self.selected.push(pos);
2010-06-25 15:55:25 +00:00
if (!Ox.isUndefined(self.$items[pos])) {
2010-09-03 20:54:40 +00:00
self.$items[pos].addClass('OxSelected');
2010-06-25 15:55:25 +00:00
}
2010-09-03 20:54:40 +00:00
Ox.print('addToSelection')
2010-07-17 08:46:27 +00:00
triggerSelectEvent();
2010-09-06 23:44:37 +00:00
} else {
// allow for 'cursor navigation' if orientation == 'both'
self.selected.splice(self.selected.indexOf(pos), 1);
self.selected.push(pos);
Ox.print('self.selected', self.selected)
2010-06-25 15:55:25 +00:00
}
}
2010-06-30 18:47:10 +00:00
function clear() {
$.each(self.requests, function(i, v) {
2010-09-03 20:54:40 +00:00
Ox.print('Ox.Request.cancel', v);
2010-06-30 18:47:10 +00:00
Ox.Request.cancel(v);
});
$.extend(self, {
$items: [],
$pages: [],
page: 0,
requests: []
});
}
2010-06-25 15:55:25 +00:00
function deselect(pos) {
if (isSelected(pos)) {
2010-06-30 09:02:13 +00:00
self.selected.splice(self.selected.indexOf(pos), 1);
2010-06-25 15:55:25 +00:00
if (!Ox.isUndefined(self.$items[pos])) {
2010-09-03 20:54:40 +00:00
self.$items[pos].removeClass('OxSelected');
2010-06-25 15:55:25 +00:00
}
2010-07-17 08:46:27 +00:00
triggerSelectEvent();
2010-06-25 15:55:25 +00:00
}
}
function emptyFirstPage() {
Ox.print('emptyFirstPage', self.$pages);
self.$pages[0] && self.$pages[0].find('.OxEmpty').remove();
}
function fillFirstPage() {
if (self.$pages[0]) {
var height = getHeight(),
lastItemHeight = height % self.options.itemHeight || self.options.itemHeight,
visibleItems = Math.ceil(height / self.options.itemHeight);
if (self.$items.length < visibleItems) {
$.each(Ox.range(self.$items.length, visibleItems), function(i, v) {
var $item = new Ox.ListItem({
construct: self.options.construct,
data: {},
id: '',
position: v
});
$item.addClass('OxEmpty').removeClass('OxTarget');
if (v == visibleItems - 1) {
$item.$element.css({
height: lastItemHeight + 'px',
overflowY: 'hidden'
});
}
$item.appendTo(self.$pages[0]);
});
}
}
}
function findItem(e) {
2010-12-22 14:16:15 +00:00
Ox.print('---- findItem', e.target)
var $element = $(e.target),
$item = null;
while (!$element.hasClass('OxTarget') && !$element.hasClass('OxPage') && !$element.is('body')) {
$element = $element.parent();
}
if ($element.hasClass('OxTarget')) {
while (!$element.hasClass('OxItem') && !$element.hasClass('OxPage') && !$element.is('body')) {
$element = $element.parent();
}
if ($element.hasClass('OxItem')) {
$item = $element;
}
}
return $item;
}
2010-09-06 23:44:37 +00:00
function getAbove() {
var pos = -1;
if (self.selected.length) {
pos = self.selected[self.selected.length - 1] - self.rowLength
2010-09-06 23:44:37 +00:00
if (pos < 0) {
pos = -1;
}
}
return pos;
}
function getBelow() {
var pos = -1;
if (self.selected.length) {
pos = self.selected[self.selected.length - 1] + self.rowLength;
2010-09-06 23:44:37 +00:00
if (pos >= self.$items.length) {
pos = -1;
}
}
return pos;
}
2010-06-30 14:21:06 +00:00
function getHeight() {
2010-06-30 18:47:10 +00:00
return that.height() - (that.$content.width() > that.width() ? oxui.scrollbarSize : 0);
2010-06-30 14:21:06 +00:00
}
2011-01-02 10:01:55 +00:00
function getListSize() {
return Math.ceil(self.listLength *
(self.options[self.options.orientation == 'horizontal' ?
'itemWidth' : 'itemHeight'] + self.itemMargin) / self.rowLength);
}
2010-06-25 15:55:25 +00:00
function getNext() {
var pos = -1;
2010-06-30 09:02:13 +00:00
if (self.selected.length) {
2010-09-06 23:44:37 +00:00
pos = (self.options.orientation == 'both' ?
self.selected[self.selected.length - 1] :
Ox.max(self.selected)) + 1;
2010-06-25 15:55:25 +00:00
if (pos == self.$items.length) {
pos = -1;
}
}
return pos;
}
function getPage() {
2010-09-11 08:59:40 +00:00
return Math.max(
Math.floor(self.options.orientation == 'horizontal' ?
(that.scrollLeft() - self.listMargin / 2) / self.pageWidth :
(that.scrollTop() - self.listMargin / 2) / self.pageHeight
), 0);
2010-06-25 15:55:25 +00:00
}
2010-11-25 10:05:50 +00:00
function getPageByPosition(pos) {
2010-12-22 14:16:15 +00:00
return parseInt(pos / self.options.pageLength);
2010-11-25 10:05:50 +00:00
}
function getPageHeight() {
return Math.ceil(self.pageLength * (self.options.itemHeight + self.itemMargin) / self.rowLength);
}
function getPosition() {
// if orientation is both, this returns the
// element position at the current scroll position
return parseInt(
that.scrollTop() / (self.options.itemHeight + self.itemMargin)
) * self.rowLength;
}
2011-01-02 10:01:55 +00:00
function getPositions(ids) {
ids = ids || getSelectedIds();
2010-06-30 18:47:10 +00:00
// fixme: optimize: send non-selected ids if more than half of the items are selected
2011-01-02 10:01:55 +00:00
if (ids.length /*&& ids.length < self.listLength*/) {
Ox.print('-------- request', {
ids: ids,
2010-12-22 14:16:15 +00:00
sort: self.options.sort
});
2010-06-30 18:47:10 +00:00
self.requests.push(self.options.request({
2011-01-02 10:01:55 +00:00
ids: ids,
2010-06-30 18:47:10 +00:00
sort: self.options.sort
}, getPositionsCallback));
2010-06-30 18:47:10 +00:00
} else {
getPositionsCallback();
}
}
function getPositionsCallback(result) {
2010-09-03 20:54:40 +00:00
Ox.print('getPositionsCallback', result)
2010-11-25 10:05:50 +00:00
var pos = 0;
2010-06-30 18:47:10 +00:00
if (result) {
$.extend(self, {
ids: {},
selected: []
});
$.each(result.data.positions, function(id, pos) {
2010-09-03 20:54:40 +00:00
Ox.print('id', id, 'pos', pos)
2010-06-30 18:47:10 +00:00
self.selected.push(pos);
});
2010-11-25 10:05:50 +00:00
pos = Ox.min(self.selected);
self.page = getPageByPosition(pos);
2010-06-30 18:47:10 +00:00
}
// that.scrollTop(0);
2010-11-25 10:05:50 +00:00
that.$content.empty();
2010-12-22 14:16:15 +00:00
Ox.print('self.selected', self.selected, 'self.page', self.page);
2010-11-25 10:05:50 +00:00
loadPages(self.page, function() {
2010-12-23 04:41:46 +00:00
scrollToPosition(pos, true);
2010-11-25 10:05:50 +00:00
});
2010-06-30 18:47:10 +00:00
}
2010-06-25 15:55:25 +00:00
function getPrevious() {
var pos = -1;
2010-06-30 09:02:13 +00:00
if (self.selected.length) {
2010-09-06 23:44:37 +00:00
pos = (self.options.orientation == 'both' ?
self.selected[self.selected.length - 1] :
Ox.min(self.selected)) - 1;
2010-06-25 15:55:25 +00:00
}
return pos;
}
2010-09-06 23:44:37 +00:00
function getRow(pos) {
return Math.floor(pos / self.rowLength);
2010-09-06 23:44:37 +00:00
}
function getRowLength() {
2010-09-17 16:37:11 +00:00
return self.options.orientation == 'both' ?
Math.floor((getWidth() - self.listMargin) /
(self.options.itemWidth + self.itemMargin)) : 1
2010-09-06 23:44:37 +00:00
}
2010-07-20 20:04:13 +00:00
function getSelectedIds() {
// fixme: is carring self.ids around the best way?
2011-01-03 23:38:43 +00:00
return $.map(self.selected, function(pos) {
return self.ids[pos];
2010-07-20 20:04:13 +00:00
});
}
2010-06-30 14:21:06 +00:00
function getWidth() {
2010-06-30 18:47:10 +00:00
return that.width() - (that.$content.height() > that.height() ? oxui.scrollbarSize : 0);
2010-06-30 14:21:06 +00:00
}
2010-06-25 15:55:25 +00:00
function invertSelection() {
2010-06-30 14:21:06 +00:00
$.each(Ox.range(self.listLength), function(i, v) {
toggleSelection(v);
2010-06-25 15:55:25 +00:00
});
}
function isSelected(pos) {
2010-06-28 09:16:36 +00:00
return self.selected.indexOf(pos) > -1;
2010-06-25 15:55:25 +00:00
}
function loadPage(page, callback) {
2010-06-30 18:47:10 +00:00
if (page < 0 || page >= self.pages) {
!Ox.isUndefined(callback) && callback();
2010-06-25 15:55:25 +00:00
return;
}
Ox.print('loadPage', page);
2010-09-03 20:54:40 +00:00
var keys = $.inArray('id', self.options.keys) > -1 ? self.options.keys :
$.merge(self.options.keys, ['id']),
offset = page * self.pageLength,
2010-06-25 15:55:25 +00:00
range = [offset, offset + (page < self.pages - 1 ?
self.pageLength : self.listLength % self.pageLength)];
2010-12-22 14:16:15 +00:00
Ox.print('is page', page, 'undefined?', Ox.isUndefined(self.$pages[page]))
if (Ox.isUndefined(self.$pages[page])) { // fixme: unload will have made this undefined already
2010-06-28 09:16:36 +00:00
self.requests.push(self.options.request({
keys: keys,
range: range,
sort: self.options.sort
}, function(result) {
2011-01-02 10:01:55 +00:00
self.$pages[page] = new Ox.ListPage();
if (self.options.orientation == 'horizontal') {
2011-01-02 10:01:55 +00:00
self.$pages[page].css({
left: (page * self.pageWidth + self.listMargin / 2) + 'px',
top: (self.listMargin / 2) + 'px',
width: (page < self.pages - 1 ? self.pageWidth :
self.listLength % self.pageLength *
(self.options.itemWidth + self.itemMargin)) + 'px'
});
} else {
self.$pages[page].css({
2011-01-02 10:01:55 +00:00
top: (page * self.pageHeight + self.listMargin / 2) + 'px',
width: self.pageWidth + 'px'
});
}
$.each(result.data.items, function(i, v) {
var pos = offset + i;
self.$items[pos] = new Ox.ListItem({
construct: self.options.construct,
data: v,
id: v[self.options.unique],
position: pos
});
self.ids[pos] = v[self.options.unique];
if (isSelected(pos)) {
2010-12-22 14:16:15 +00:00
Ox.print('pos', pos, 'isSelected')
self.$items[pos].addClass('OxSelected');
2010-06-25 15:55:25 +00:00
}
self.$items[pos].appendTo(self.$pages[page]);
});
if (self.options.type == 'text' && page == 0) {
fillFirstPage();
}
self.$pages[page].appendTo(that.$content);
!Ox.isUndefined(callback) && callback();
2010-06-28 09:16:36 +00:00
}));
} else {
Ox.print('loading a page from cache, this should probably not happen -----------')
2010-06-28 09:16:36 +00:00
self.$pages[page].appendTo(that.$content);
2010-06-25 15:55:25 +00:00
}
}
function loadPages(page, callback) {
var counter = 0,
fn = function() {
2010-12-22 20:28:27 +00:00
Ox.print('---- self.$pages', self.$pages)
2010-11-25 10:05:50 +00:00
++counter == 2 && !Ox.isUndefined(callback) && callback();
2010-06-25 15:55:25 +00:00
};
loadPage(page, function() {
loadPage(page - 1, fn);
loadPage(page + 1, fn);
});
}
function mousedown(e) {
Ox.print('click')
var $item = findItem(e),
pos,
deselectTimeout = false;
selectTimeout = false;
that.gainFocus();
if ($item) {
if (!self.clickTimeout) {
// click
pos = $item.data('position');
2011-01-02 10:01:55 +00:00
if (e.metaKey && self.options.max == -1) {
if (!isSelected(pos)) {
addToSelection(pos);
} else {
deselectTimeout = true;
}
2011-01-02 10:01:55 +00:00
} else if (e.shiftKey && self.options.max == -1) {
addAllToSelection(pos);
} else if (!isSelected(pos)) {
select(pos);
} else {
selectTimeout = true;
}
self.clickTimeout = setTimeout(function() {
self.clickTimeout = 0;
if (deselectTimeout) {
deselect(pos);
} else if (selectTimeout) {
select(pos);
}
}, 250);
} else {
// dblclick
clearTimeout(self.clickTimeout);
self.clickTimeout = 0;
open();
}
2011-01-02 10:01:55 +00:00
} else if (self.options.min == 0) {
selectNone();
}
}
2010-07-20 20:04:13 +00:00
function open() {
2010-09-03 20:54:40 +00:00
that.triggerEvent('open', {
2010-07-20 20:04:13 +00:00
ids: getSelectedIds()
});
}
function preview() {
self.preview = !self.preview;
if (self.preview) {
2010-09-03 20:54:40 +00:00
that.triggerEvent('openpreview', {
2010-07-20 20:04:13 +00:00
ids: getSelectedIds()
});
} else {
2010-09-03 20:54:40 +00:00
that.triggerEvent('closepreview');
2010-07-20 20:04:13 +00:00
}
}
2010-06-28 09:16:36 +00:00
function scroll() {
var page = self.page;
self.scrollTimeout && clearTimeout(self.scrollTimeout);
self.scrollTimeout = setTimeout(function() {
self.scrollTimeout = 0;
self.page = getPage();
if (self.page != page) {
Ox.print('page', page, '-->', self.page);
}
if (self.page == page - 1) {
unloadPage(self.page + 2);
loadPage(self.page - 1);
} else if (self.page == page + 1) {
unloadPage(self.page - 2);
loadPage(self.page + 1);
} else if (self.page == page - 2) {
unloadPage(self.page + 3);
unloadPage(self.page + 2);
loadPage(self.page);
loadPage(self.page - 1);
} else if (self.page == page + 2) {
unloadPage(self.page - 3);
unloadPage(self.page - 2);
loadPage(self.page);
loadPage(self.page + 1);
} else if (self.page != page) {
unloadPages(page);
loadPages(self.page);
}
}, 250);
2010-06-28 09:16:36 +00:00
}
2010-06-30 14:21:06 +00:00
function scrollPageDown() {
that.scrollBy(getHeight());
}
function scrollPageUp() {
that.scrollBy(-getHeight());
}
function scrollTo(value) {
2011-01-02 10:01:55 +00:00
that.animate(self.options.orientation == 'horizontal' ? {
scrollLeft: (self.listSize * value) + 'px'
} : {
scrollTop: (self.listSize * value) + 'px'
}, 0);
}
2011-01-02 10:01:55 +00:00
function scrollToFirst() {
that[self.options.orientation == 'horizontal' ? 'scrollLeft' : 'scrollTop'](0);
}
function scrollToLast() {
that[self.options.orientation == 'horizontal' ? 'scrollLeft' : 'scrollTop'](self.listSize);
}
function scrollToPosition(pos, leftOrTopAlign) {
2010-09-06 23:44:37 +00:00
var itemHeight = self.options.itemHeight + self.itemMargin,
itemWidth = self.options.itemWidth + self.itemMargin,
positions = [],
scroll,
size;
2010-09-03 20:54:40 +00:00
if (self.options.orientation == 'horizontal') {
2011-01-03 12:01:38 +00:00
if (self.options.centered) {
2011-01-02 10:01:55 +00:00
that.animate({
2011-01-03 12:01:38 +00:00
scrollLeft: (self.listMargin / 2 + (pos + 0.5) * itemWidth - that.width() / 2) + 'px'
2011-01-02 10:01:55 +00:00
}, 0);
2011-01-03 12:01:38 +00:00
} else {
positions[0] = pos * itemWidth + self.listMargin / 2;
positions[1] = positions[0] + itemWidth + self.itemMargin / 2;
scroll = that.scrollLeft();
size = getWidth();
if (positions[0] < scroll || leftOrTopAlign) {
that.animate({
scrollLeft: positions[0] + 'px'
}, 0);
} else if (positions[1] > scroll + size) {
that.animate({
scrollLeft: (positions[1] - size) + 'px'
}, 0);
}
2011-01-02 10:01:55 +00:00
}
2010-09-06 23:44:37 +00:00
} else {
positions[0] = (self.options.orientation == 'vertical' ? pos : getRow(pos)) * itemHeight;
positions[1] = positions[0] + itemHeight + (self.options.orientation == 'vertical' ? 0 : self.itemMargin);
2010-06-30 14:21:06 +00:00
scroll = that.scrollTop();
size = getHeight();
2011-01-02 10:01:55 +00:00
if (positions[0] < scroll || leftOrTopAlign) {
2010-06-30 14:21:06 +00:00
that.animate({
2010-09-03 20:54:40 +00:00
scrollTop: positions[0] + 'px'
2010-06-30 14:21:06 +00:00
}, 0);
} else if (positions[1] > scroll + size) {
that.animate({
2010-09-03 20:54:40 +00:00
scrollTop: (positions[1] - size) + 'px'
2010-06-30 14:21:06 +00:00
}, 0);
}
}
}
2010-06-25 15:55:25 +00:00
function select(pos) {
2010-06-30 09:02:13 +00:00
if (!isSelected(pos) || self.selected.length > 1) {
2010-06-25 15:55:25 +00:00
selectNone();
addToSelection(pos);
2011-01-03 12:01:38 +00:00
self.options.centered && scrollToPosition(pos);
2010-06-25 15:55:25 +00:00
}
}
2010-09-06 23:44:37 +00:00
function selectAbove() {
var pos = getAbove();
if (pos > -1) {
select(pos);
scrollToPosition(pos);
2010-09-06 23:44:37 +00:00
}
}
2010-06-25 15:55:25 +00:00
function selectAll() {
2010-06-30 14:21:06 +00:00
$.each(Ox.range(self.listLength), function(i, v) {
2010-09-03 20:54:40 +00:00
Ox.print('adding', v);
2010-06-30 14:21:06 +00:00
addToSelection(v);
2010-06-25 15:55:25 +00:00
});
}
2010-09-06 23:44:37 +00:00
function selectBelow() {
var pos = getBelow();
if (pos > -1) {
select(pos);
scrollToPosition(pos);
2010-09-06 23:44:37 +00:00
}
}
2010-06-25 15:55:25 +00:00
function selectNext() {
var pos = getNext();
if (pos > -1) {
select(pos);
scrollToPosition(pos);
2010-06-25 15:55:25 +00:00
}
}
function selectNone() {
$.each(self.$items, function(i, v) {
deselect(i);
});
}
function selectPrevious() {
var pos = getPrevious();
if (pos > -1) {
select(pos);
scrollToPosition(pos);
2010-06-25 15:55:25 +00:00
}
}
function selectQuery(str) {
$.each(self.$items, function(i, v) {
if (Ox.toLatin(v.title).toUpperCase().indexOf(str) == 0) {
select(i);
scrollToPosition(i);
2010-06-25 15:55:25 +00:00
return false;
}
});
}
function toggleSelection(pos) {
if (!isSelected(pos)) {
addToSelection(pos);
} else {
deselect(pos);
}
}
2010-07-17 08:46:27 +00:00
function triggerSelectEvent() {
2011-01-03 23:38:43 +00:00
var ids = self.options.selected = getSelectedIds();
2010-07-17 08:46:27 +00:00
setTimeout(function() {
2010-07-20 20:04:13 +00:00
var ids_ = getSelectedIds();
2010-09-08 16:35:34 +00:00
Ox.print('ids', ids, 'ids after 100 msec', ids_)
2010-07-17 08:46:27 +00:00
if (ids.length == ids_.length && (ids.length == 0 || ids[0] == ids_[0])) {
2010-09-03 20:54:40 +00:00
that.triggerEvent('select', {
2010-07-17 08:46:27 +00:00
ids: ids
});
2010-09-03 20:54:40 +00:00
self.preview && that.triggerEvent('openpreview', {
2010-07-20 20:04:13 +00:00
ids: ids
});
2010-07-17 08:46:27 +00:00
} else {
2010-09-03 20:54:40 +00:00
Ox.print('select event not triggered after timeout');
2010-07-17 08:46:27 +00:00
}
}, 100);
}
2010-06-25 15:55:25 +00:00
function unloadPage(page) {
2010-06-29 22:19:41 +00:00
if (page < 0 || page >= self.pages) {
return;
}
2010-09-03 20:54:40 +00:00
Ox.print('unloadPage', page)
Ox.print('self.$pages', self.$pages)
Ox.print('page not undefined', !Ox.isUndefined(self.$pages[page]))
if (!Ox.isUndefined(self.$pages[page])) {
self.$pages[page].remove();
delete self.$pages[page];
}
2010-06-25 15:55:25 +00:00
}
function unloadPages(page) {
unloadPage(page);
unloadPage(page - 1);
unloadPage(page + 1)
}
2010-02-10 09:59:59 +00:00
function updateQuery(ids) {
// ids are the selcected ids
// (in case list is loaded with selection)
2010-06-30 18:47:10 +00:00
clear();
self.requests.push(self.options.request({}, function(result) {
var keys = {};
that.triggerEvent('load', result.data);
self.rowLength = getRowLength();
self.pageLength = self.options.orientation == 'both' ?
self.pageLengthByRowLength[self.rowLength] :
self.options.pageLength;
$.extend(self, {
listLength: result.data.items,
pages: Math.ceil(result.data.items / self.pageLength),
pageWidth: self.options.orientation == 'vertical' ? 0 :
2011-01-02 10:01:55 +00:00
(self.options.itemWidth + self.itemMargin) *
(self.options.orientation == 'horizontal' ?
self.pageLength : self.rowLength),
pageHeight: self.options.orientation == 'horizontal' ? 0 :
2011-01-02 10:01:55 +00:00
Math.ceil(self.pageLength * (self.options.itemHeight +
self.itemMargin) / self.rowLength)
});
2011-01-02 10:01:55 +00:00
self.listSize = getListSize();
that.$content.css(
self.options.orientation == 'horizontal' ? 'width' : 'height',
self.listSize + 'px'
);
getPositions(ids);
2010-06-30 18:47:10 +00:00
}));
}
function updatePages(pos, scroll) {
// only used if orientation is both
clear();
self.pageLength = self.pageLengthByRowLength[self.rowLength]
$.extend(self, {
2011-01-02 10:01:55 +00:00
listSize: getListSize(),
pages: Math.ceil(self.listLength / self.pageLength),
pageWidth: (self.options.itemWidth + self.itemMargin) * self.rowLength,
pageHeight: getPageHeight()
});
that.$content.css({
2011-01-02 10:01:55 +00:00
height: self.listSize + 'px'
});
self.page = getPageByPosition(pos);
//that.scrollTop(0);
that.$content.empty();
loadPages(self.page, function() {
scrollTo(scroll);
});
}
2010-06-30 18:47:10 +00:00
function updateSort() {
2010-07-03 08:35:28 +00:00
if (self.listLength > 1) {
clear();
getPositions();
}
2010-06-28 09:16:36 +00:00
}
2010-06-30 18:47:10 +00:00
self.onChange = function(key, value) {
2010-09-03 20:54:40 +00:00
Ox.print('list onChange', key, value);
if (key == 'request') {
2010-06-30 18:47:10 +00:00
updateQuery();
}
};
that.clearCache = function() { // was used by TextList resizeColumn, now probably no longer necessary
2010-06-30 18:47:10 +00:00
self.$pages = [];
2010-09-04 14:28:40 +00:00
return that;
};
2010-09-13 11:53:31 +00:00
that.closePreview = function() {
self.preview = false;
};
2010-09-04 14:28:40 +00:00
that.reload = function() {
Ox.print('---------------- list reload, page', self.page)
var page = self.page;
2010-09-04 14:28:40 +00:00
clear();
self.page = page
2010-09-04 14:28:40 +00:00
that.$content.empty();
loadPages(self.page);
return that;
2010-06-30 18:47:10 +00:00
};
2011-01-03 12:01:38 +00:00
that.scrollToSelection = function() {
self.selected.length && scrollToPosition(self.selected[0]);
};
that.size = function() { // fixme: not a good function name
if (self.options.orientation == 'both') {
var rowLength = getRowLength(),
pageLength = self.pageLengthByRowLength[rowLength],
pos = getPosition(),
2011-01-02 10:01:55 +00:00
scroll = that.scrollTop() / self.listSize;
if (pageLength != self.pageLength) {
self.pageLength = pageLength;
self.rowLength = rowLength;
updatePages(pos, scroll);
} else if (rowLength != self.rowLength) {
self.rowLength = rowLength;
self.pageWidth = (self.options.itemWidth + self.itemMargin) * self.rowLength; // fixme: make function
2011-01-02 10:01:55 +00:00
self.listSize = getListSize();
self.pageHeight = getPageHeight();
$.each(self.$pages, function(i, $page) {
2010-12-22 20:28:27 +00:00
!Ox.isUndefined($page) && $page.css({
width: self.pageWidth + 'px',
top: (i * self.pageHeight + self.listMargin / 2) + 'px'
});
});
that.$content.css({
2011-01-02 10:01:55 +00:00
height: self.listSize + 'px'
});
Ox.print('scrolling to', scroll)
scrollTo(scroll);
}
} else if (self.options.type == 'text') {
2010-11-25 10:05:50 +00:00
Ox.print('that.size, type==text')
emptyFirstPage();
fillFirstPage();
}
}
2010-09-08 16:35:34 +00:00
that.sortList = function(key, operator) {
2010-06-28 09:16:36 +00:00
if (key != self.options.sort[0].key || operator != self.options.sort[0].operator) {
self.options.sort[0] = {
key: key,
operator: operator
}
2010-09-03 20:54:40 +00:00
that.triggerEvent('sort', self.options.sort[0]);
2010-06-30 18:47:10 +00:00
updateSort();
2010-06-28 09:16:36 +00:00
}
2010-09-04 14:28:40 +00:00
return that;
2010-06-28 09:16:36 +00:00
}
2010-02-10 09:59:59 +00:00
return that;
};
Ox.ListItem = function(options, self) {
2010-06-28 11:19:04 +00:00
var self = self || {},
that = new Ox.Element({}, self)
.defaults({
construct: function() {},
data: {},
2010-09-03 20:54:40 +00:00
id: '',
2010-06-30 09:02:13 +00:00
position: 0
2010-06-28 11:19:04 +00:00
})
.options(options || {});
2010-06-28 11:19:04 +00:00
$.each(self.options.data, function(k, v) {
2010-09-03 20:54:40 +00:00
self.options.data[k] = $.isArray(v) ? v.join(', ') : v;
2010-06-28 11:19:04 +00:00
});
2010-06-30 09:02:13 +00:00
that.$element = self.options.construct(self.options.data)
2010-09-03 20:54:40 +00:00
.addClass('OxItem')
2010-06-30 09:02:13 +00:00
.attr({
2010-06-30 18:47:10 +00:00
id: self.options.id
2010-06-30 09:02:13 +00:00
})
2010-09-03 20:54:40 +00:00
.data('position', self.options.position);
2010-06-28 11:19:04 +00:00
return that;
2010-02-10 09:59:59 +00:00
};
2010-06-25 15:55:25 +00:00
Ox.ListPage = function(options, self) {
var self = self || {},
that = new Ox.Element({}, self)
2010-09-03 20:54:40 +00:00
.addClass('OxPage');
2010-06-25 15:55:25 +00:00
return that;
};
Ox.TextList = function(options, self) {
var self = self || {},
that = new Ox.Element({}, self)
.defaults({
columns: [],
2010-09-04 14:28:40 +00:00
columnsMovable: false,
columnsRemovable: false,
columnsResizable: false,
2010-09-05 14:24:22 +00:00
columnWidth: [40, 800],
2010-09-03 20:54:40 +00:00
id: '',
2011-01-02 10:01:55 +00:00
max: -1,
min: 0,
selected: [],
2010-06-25 15:55:25 +00:00
request: function() {}, // {sort, range, keys, callback}
2010-06-28 09:16:36 +00:00
sort: []
2010-06-25 15:55:25 +00:00
})
.options(options || {})
2010-09-03 20:54:40 +00:00
.addClass('OxTextList');
2010-06-25 15:55:25 +00:00
2010-06-30 18:47:10 +00:00
$.each(self.options.columns, function(i, v) { // fixme: can this go into a generic ox.js function?
if (Ox.isUndefined(v.unique)) {
v.unique = false;
}
if (Ox.isUndefined(v.visible)) {
v.visible = false;
}
if (v.unique) {
self.unique = v.id;
}
});
2010-06-25 15:55:25 +00:00
$.extend(self, {
2010-07-03 11:31:25 +00:00
columnPositions: [],
2010-06-25 15:55:25 +00:00
itemHeight: 16,
page: 0,
pageLength: 100,
scrollLeft: 0,
2010-06-29 21:24:07 +00:00
selectedColumn: getColumnIndexById(self.options.sort[0].key),
2010-06-28 09:16:36 +00:00
visibleColumns: $.map(self.options.columns, function(v, i) {
return v.visible ? v : null;
})
2010-06-25 15:55:25 +00:00
});
$.extend(self, {
2010-09-05 21:36:51 +00:00
columnWidths: $.map(self.visibleColumns, function(v, i) {
return v.width;
}),
pageHeight: self.options.pageLength * self.itemHeight
2010-06-25 15:55:25 +00:00
});
// Head
that.$bar = new Ox.Bar({
2010-09-03 20:54:40 +00:00
orientation: 'horizontal',
2010-06-25 15:55:25 +00:00
size: 16
}).appendTo(that);
that.$head = new Ox.Container()
2010-09-03 20:54:40 +00:00
.addClass('OxHead')
2010-06-25 15:55:25 +00:00
.appendTo(that.$bar);
2010-09-03 20:54:40 +00:00
that.$head.$content.addClass('OxTitles');
2010-09-04 14:28:40 +00:00
constructHead();
if (self.options.columnsRemovable) {
that.$select = new Ox.Select({
id: self.options.id + 'SelectColumns',
items: $.map(self.options.columns, function(v, i) {
return {
checked: v.visible,
disabled: v.removable === false,
id: v.id,
title: v.title
}
}),
max: -1,
min: 1,
type: 'image'
2010-06-25 15:55:25 +00:00
})
2010-09-04 14:28:40 +00:00
.bindEvent('change', changeColumns)
.appendTo(that.$bar.$element);
2010-07-24 01:32:08 +00:00
}
2010-06-25 15:55:25 +00:00
// Body
2010-06-28 09:16:36 +00:00
that.$body = new Ox.List({
2010-06-28 11:19:04 +00:00
construct: constructItem,
2010-06-30 09:27:02 +00:00
id: self.options.id,
2010-06-28 09:16:36 +00:00
itemHeight: 16,
2010-06-30 09:02:13 +00:00
itemWidth: getItemWidth(),
2010-06-28 11:19:04 +00:00
keys: $.map(self.visibleColumns, function(v, i) {
return v.id;
}),
2011-01-02 10:01:55 +00:00
max: self.options.max,
min: self.options.min,
2010-09-03 20:54:40 +00:00
orientation: 'vertical',
2010-06-28 09:16:36 +00:00
request: self.options.request,
2011-01-02 10:01:55 +00:00
selected: self.options.selected,
2010-06-28 09:16:36 +00:00
sort: self.options.sort,
2010-09-03 20:54:40 +00:00
type: 'text',
2010-06-30 18:47:10 +00:00
unique: self.unique
2011-01-02 10:01:55 +00:00
}, $.extend({}, self)) // pass event handler
2010-09-03 20:54:40 +00:00
.addClass('OxBody')
2010-06-28 09:16:36 +00:00
.scroll(function() {
var scrollLeft = $(this).scrollLeft();
if (scrollLeft != self.scrollLeft) {
self.scrollLeft = scrollLeft;
that.$head.scrollLeft(scrollLeft);
}
})
2011-01-03 23:38:43 +00:00
.bindEvent({
select: function(event, data) {
self.options.selected = data.ids;
}
})
2010-06-28 09:16:36 +00:00
.appendTo(that);
that.$body.$content.css({
2010-09-03 20:54:40 +00:00
width: getItemWidth() + 'px'
2010-06-25 15:55:25 +00:00
});
2010-09-03 20:54:40 +00:00
Ox.print('s.vC', self.visibleColumns)
2010-07-06 04:39:11 +00:00
2010-06-28 09:16:36 +00:00
function addColumn(id) {
2010-09-04 14:28:40 +00:00
Ox.print('addColumn', id);
var column,
index = 0;
$.each(self.options.columns, function(i, v) {
if (v.visible) {
index++;
} else if (v.id == id) {
column = v;
return false;
}
});
column.visible = true;
self.visibleColumns.splice(index, 0, column);
self.columnWidths.splice(index, 0, column.width);
2010-09-04 14:28:40 +00:00
that.$head.$content.empty();
constructHead();
that.$body.options({
keys: $.map(self.visibleColumns, function(v, i) {
return v.id;
})
});
that.$body.reload();
}
function changeColumns(event, data) {
var add,
ids = [];
$.each(data.selected, function(i, column) {
var index = getColumnIndexById(column.id);
if (!self.options.columns[index].visible) {
addColumn(column.id);
add = true;
return false;
}
ids.push(column.id);
});
if (!add) {
$.each(self.visibleColumns, function(i, column) {
if (ids.indexOf(column.id) == -1) {
removeColumn(column.id);
return false;
2010-09-04 14:28:40 +00:00
}
});
}
2010-06-28 09:16:36 +00:00
}
2010-06-29 21:24:07 +00:00
function clickColumn(id) {
2010-09-03 20:54:40 +00:00
Ox.print('clickColumn', id);
2010-06-29 21:24:07 +00:00
var i = getColumnIndexById(id),
isSelected = self.options.sort[0].key == self.options.columns[i].id;
2010-09-08 16:35:34 +00:00
that.sortList(
2010-06-29 21:24:07 +00:00
self.options.columns[i].id, isSelected ?
2010-09-03 20:54:40 +00:00
(self.options.sort[0].operator === '' ? '-' : '') :
2010-06-29 21:24:07 +00:00
self.options.columns[i].operator
2010-06-29 16:28:22 +00:00
);
}
2010-09-04 14:28:40 +00:00
function constructHead() {
2010-09-05 22:26:30 +00:00
var offset = 0;
2010-09-04 14:28:40 +00:00
that.$titles = [];
2010-09-05 22:26:30 +00:00
self.columnOffsets = [];
2010-09-04 14:28:40 +00:00
$.each(self.visibleColumns, function(i, v) {
var $order, $resize, $left, $center, $right, timeout = 0;
2010-09-05 22:26:30 +00:00
offset += self.columnWidths[i];
self.columnOffsets[i] = offset - self.columnWidths[i] / 2;
2010-09-04 14:28:40 +00:00
that.$titles[i] = $('<div>')
.addClass('OxTitle OxColumn' + Ox.toTitleCase(v.id))
.css({
2010-09-05 21:36:51 +00:00
width: (self.columnWidths[i] - 9) + 'px',
2010-09-04 14:28:40 +00:00
textAlign: v.align
})
.html(v.title)
.mousedown(function(e) {
timeout = setTimeout(function() {
2010-09-06 23:44:37 +00:00
self.options.columnsMovable && dragColumn(v.id, e);
2010-09-04 14:28:40 +00:00
timeout = 0;
}, 250);
})
.mouseup(function() {
if (timeout) {
clearTimeout(timeout);
timeout = 0;
clickColumn(v.id);
}
})
.appendTo(that.$head.$content.$element);
$order = $('<div>')
.addClass('OxOrder')
.html(oxui.symbols['triangle_' + (
v.operator === '' ? 'up' : 'down'
)])
.click(function() {
$(this).prev().trigger('click')
})
.appendTo(that.$head.$content.$element);
$resize = $('<div>')
.addClass('OxResize')
.appendTo(that.$head.$content.$element);
if (self.options.columnsResizable) {
$resize.addClass('OxResizable')
.mousedown(function(e) {
var startWidth = self.columnWidths[i],
startX = e.clientX;
$window.mousemove(function(e) {
var x = e.clientX,
width = Ox.limit(
startWidth - startX + x,
self.options.columnWidth[0],
self.options.columnWidth[1]
);
resizeColumn(v.id, width);
});
$window.one('mouseup', function() {
$window.unbind('mousemove');
});
})
.dblclick(function() {
resizeColumn(v.id, v.width);
});
}
2010-09-04 14:28:40 +00:00
$left = $('<div>').addClass('OxLeft').appendTo($resize);
$center = $('<div>').addClass('OxCenter').appendTo($resize);
$right = $('<div>').addClass('OxRight').appendTo($resize);
});
that.$head.$content.css({
width: (Ox.sum(self.columnWidths) + 2) + 'px'
});
Ox.print('s.sC', self.selectedColumn)
2010-09-05 22:26:30 +00:00
Ox.print('s.cO', self.columnOffsets)
2010-09-04 14:28:40 +00:00
if (getColumnPositionById(self.options.columns[self.selectedColumn].id) > -1) { // fixme: save in var
toggleSelected(self.options.columns[self.selectedColumn].id);
that.$titles[getColumnPositionById(self.options.columns[self.selectedColumn].id)].css({
width: (self.options.columns[self.selectedColumn].width - 25) + 'px'
});
}
}
2010-06-30 09:02:13 +00:00
function constructItem(data) {
2010-09-03 20:54:40 +00:00
var $item = $('<div>')
.addClass('OxTarget')
2010-06-25 15:55:25 +00:00
.css({
2010-09-04 14:28:40 +00:00
width: getItemWidth() + 'px'
2010-06-30 09:02:13 +00:00
});
2010-06-28 11:19:04 +00:00
$.each(self.visibleColumns, function(i, v) {
2010-09-03 20:54:40 +00:00
var $cell = $('<div>')
2010-09-03 20:54:40 +00:00
.addClass('OxCell OxColumn' + Ox.toTitleCase(v.id))
2010-06-25 15:55:25 +00:00
.css({
2010-09-03 20:54:40 +00:00
width: (self.columnWidths[i] - 9) + 'px',
2010-06-25 15:55:25 +00:00
textAlign: v.align
})
.html(!$.isEmptyObject(data) && data[v.id] ? data[v.id] : '')
.appendTo($item);
2010-06-25 15:55:25 +00:00
});
return $item;
}
2010-07-03 11:31:25 +00:00
function dragColumn(id, e) {
var startX = e.clientX,
startPos = getColumnPositionById(id),
pos = startPos,
stopPos = startPos,
2010-09-05 22:26:30 +00:00
offsets = $.map(self.visibleColumns, function(v, i) {
return self.columnOffsets[i] - self.columnOffsets[startPos]
2010-07-03 11:31:25 +00:00
});
2010-09-03 20:54:40 +00:00
$('.OxColumn' + Ox.toTitleCase(id)).css({
opacity: 0.25
2010-07-05 07:09:34 +00:00
});
2010-09-03 20:54:40 +00:00
that.$titles[startPos].addClass('OxDrag').css({ // fixme: why does the class not work?
cursor: 'move'
2010-07-03 11:31:25 +00:00
});
2010-09-05 22:26:30 +00:00
Ox.print('offsets', offsets)
2010-07-03 11:31:25 +00:00
$window.mousemove(function(e) {
var d = e.clientX - startX;
2010-09-05 22:26:30 +00:00
$.each(offsets, function(i, v) {
2010-07-03 11:31:25 +00:00
if (d < 0 && d < v) {
stopPos = i;
return false;
} else if (d > 0 && d > v) {
stopPos = i;
}
});
if (stopPos != pos) {
pos = stopPos;
moveColumn(id, pos);
}
});
2010-09-03 20:54:40 +00:00
$window.one('mouseup', function() {
2010-07-03 11:31:25 +00:00
dropColumn(id, pos);
2010-09-03 20:54:40 +00:00
$window.unbind('mousemove');
2010-07-03 11:31:25 +00:00
});
}
function dropColumn(id, pos) {
2010-09-03 20:54:40 +00:00
Ox.print('dropColumn', id, pos)
2010-07-03 11:31:25 +00:00
var startPos = getColumnPositionById(id),
stopPos = pos,
$title = that.$titles.splice(startPos, 1)[0],
column = self.visibleColumns.splice(startPos, 1)[0],
width = self.columnWidths.splice(startPos, 1)[0];
self.visibleColumns.splice(stopPos, 0, column);
self.columnWidths.splice(stopPos, 0, width);
2010-09-05 22:26:30 +00:00
that.$head.$content.empty();
constructHead();
2010-09-03 20:54:40 +00:00
Ox.print('s.vC', self.visibleColumns)
$('.OxColumn' + Ox.toTitleCase(id)).css({
2010-07-05 07:09:34 +00:00
opacity: 1
});
2010-09-03 20:54:40 +00:00
that.$titles[stopPos].removeClass('OxDrag').css({
cursor: 'pointer'
2010-07-03 11:31:25 +00:00
});
that.$body.clearCache();
2010-07-03 11:31:25 +00:00
}
2010-06-29 21:24:07 +00:00
function getColumnIndexById(id) {
2010-06-25 15:55:25 +00:00
var pos = -1;
$.each(self.options.columns, function(i, v) {
if (v.id == id) {
pos = i;
return false;
}
});
return pos;
}
2010-06-29 21:24:07 +00:00
function getColumnPositionById(id) {
var pos = -1;
$.each(self.visibleColumns, function(i, v) {
if (v.id == id) {
pos = i;
return false;
}
});
return pos;
}
2010-06-30 09:02:13 +00:00
function getItemWidth() {
2010-06-30 18:47:10 +00:00
return Math.max(Ox.sum(self.columnWidths), that.$element.width() - oxui.scrollbarSize);
2010-09-04 14:28:40 +00:00
//return Ox.sum(self.columnWidths)
2010-06-30 09:02:13 +00:00
}
2010-07-03 11:31:25 +00:00
function moveColumn(id, pos) {
2010-09-05 22:26:30 +00:00
// fixme: column head should be one element, not three
2010-09-03 20:54:40 +00:00
Ox.print('moveColumn', id, pos)
2010-07-03 11:31:25 +00:00
var startPos = getColumnPositionById(id),
stopPos = pos,
2010-09-03 20:54:40 +00:00
startClassName = '.OxColumn' + Ox.toTitleCase(id),
stopClassName = '.OxColumn' + Ox.toTitleCase(self.visibleColumns[stopPos].id),
2010-09-05 22:26:30 +00:00
insert = startPos < stopPos ? 'insertAfter' : 'insertBefore'
2010-09-03 20:54:40 +00:00
$column = $('.OxTitle' + startClassName),
2010-07-03 11:31:25 +00:00
$order = $column.next(),
$resize = $order.next();
Ox.print(startClassName, insert, stopClassName)
2010-09-05 22:26:30 +00:00
$column.detach()[insert](insert == 'insertAfter' ? $('.OxTitle' + stopClassName).next().next() : $('.OxTitle' + stopClassName));
2010-07-03 11:31:25 +00:00
$order.detach().insertAfter($column);
$resize.detach().insertAfter($order);
2010-09-03 20:54:40 +00:00
$.each(that.$body.find('.OxItem'), function(i, v) {
2010-07-03 11:31:25 +00:00
var $v = $(v);
2010-09-05 22:26:30 +00:00
$v.children(startClassName).detach()[insert]($v.children(stopClassName));
2010-07-03 11:31:25 +00:00
});
column = self.visibleColumns.splice(startPos, 1)[0],
width = self.columnWidths.splice(startPos, 1)[0];
self.visibleColumns.splice(stopPos, 0, column);
self.columnWidths.splice(stopPos, 0, width);
2010-06-28 09:16:36 +00:00
}
function removeColumn(id) {
2010-09-04 14:28:40 +00:00
Ox.print('removeColumn', id);
var className = '.OxColumn' + Ox.toTitleCase(id),
index = getColumnIndexById(id),
itemWidth,
2010-09-04 14:28:40 +00:00
position = getColumnPositionById(id),
$column = $('.OxTitle' + className),
$order = $column.next(),
$resize = $order.next();
self.options.columns[index].visible = false;
self.visibleColumns.splice(position, 1);
self.columnWidths.splice(position, 1);
that.$head.$content.empty();
constructHead();
2010-09-04 14:28:40 +00:00
itemWidth = getItemWidth();
$.each(that.$body.find('.OxItem'), function(i, v) {
var $v = $(v);
$v.children(className).remove();
$v.css({
width: itemWidth + 'px'
});
});
that.$body.$content.css({
width: itemWidth + 'px'
});
that.$body.options({
keys: $.map(self.visibleColumns, function(v, i) {
return v.id;
})
});
//that.$body.clearCache();
2010-06-28 09:16:36 +00:00
}
2010-06-29 22:19:41 +00:00
function resize() {
}
2010-06-29 21:24:07 +00:00
function resizeColumn(id, width) {
var i = getColumnIndexById(id),
pos = getColumnPositionById(id);
2010-06-25 15:55:25 +00:00
self.columnWidths[pos] = width;
that.$head.$content.css({
2010-09-03 20:54:40 +00:00
width: (Ox.sum(self.columnWidths) + 2) + 'px'
2010-06-25 15:55:25 +00:00
});
that.$titles[pos].css({
2010-09-03 20:54:40 +00:00
width: (width - 9 - (i == self.selectedColumn ? 16 : 0)) + 'px'
2010-06-25 15:55:25 +00:00
});
2010-09-03 20:54:40 +00:00
$('.OxCell.OxColumn' + Ox.toTitleCase(self.options.columns[i].id)).css({
width: (width - 9) + 'px'
2010-06-25 15:55:25 +00:00
});
setWidth();
//that.$body.clearCache();
2010-06-25 15:55:25 +00:00
}
function setWidth() {
var width = getItemWidth();
that.$body.$content.find('.OxItem').css({ // fixme: can we avoid this lookup?
width: width + 'px'
});
that.$body.$content.css({
width: width + 'px' // fixme: check if scrollbar visible, and listen to resize/toggle event
});
}
2010-06-29 21:24:07 +00:00
function toggleSelected(id) {
var pos = getColumnPositionById(id);
2010-07-07 12:36:12 +00:00
if (pos > -1) {
updateOrder(id);
2010-09-03 20:54:40 +00:00
pos > 0 && that.$titles[pos].prev().children().eq(2).toggleClass('OxSelected');
that.$titles[pos].toggleClass('OxSelected');
that.$titles[pos].next().toggleClass('OxSelected');
that.$titles[pos].next().next().children().eq(0).toggleClass('OxSelected');
2010-07-07 12:36:12 +00:00
that.$titles[pos].css({
width: (
2010-09-03 20:54:40 +00:00
that.$titles[pos].width() + (that.$titles[pos].hasClass('OxSelected') ? -16 : 16)
) + 'px'
2010-07-07 12:36:12 +00:00
});
}
2010-06-25 15:55:25 +00:00
}
2010-06-29 21:24:07 +00:00
function updateOrder(id) {
var pos = getColumnPositionById(id);
Ox.print(id, pos)
2010-06-29 17:39:21 +00:00
that.$titles[pos].next().html(oxui.symbols[
2010-09-03 20:54:40 +00:00
'triangle_' + (self.options.sort[0].operator === '' ? 'up' : 'down')
2010-06-29 17:39:21 +00:00
]);
}
2010-06-30 18:47:10 +00:00
self.onChange = function(key, value) {
2010-09-03 20:54:40 +00:00
if (key == 'request') {
2011-01-03 23:38:43 +00:00
//alert('request set!!')
that.$body.options(key, value);
} else if (key == 'selected') {
2010-06-30 18:47:10 +00:00
that.$body.options(key, value);
}
};
2010-07-20 20:04:13 +00:00
that.closePreview = function() {
2010-09-13 11:53:31 +00:00
that.$body.closePreview();
2010-07-20 20:04:13 +00:00
};
2010-11-25 10:05:50 +00:00
that.size = function() {
setWidth();
2010-11-25 10:05:50 +00:00
that.$body.size();
}
that.resizeColumn = function(id, width) {
resizeColumn(id, width);
return that;
}
2010-09-08 16:35:34 +00:00
that.sortList = function(key, operator) {
2010-06-29 16:28:22 +00:00
var isSelected = key == self.options.sort[0].key;
self.options.sort = [
{
key: key,
operator: operator
}
];
if (isSelected) {
2010-06-29 21:24:07 +00:00
updateOrder(self.options.columns[self.selectedColumn].id);
2010-06-29 16:28:22 +00:00
} else {
2010-06-29 21:24:07 +00:00
toggleSelected(self.options.columns[self.selectedColumn].id);
self.selectedColumn = getColumnIndexById(key);
toggleSelected(self.options.columns[self.selectedColumn].id);
2010-06-25 15:55:25 +00:00
}
2010-09-08 16:35:34 +00:00
that.$body.sortList(self.options.sort[0].key, self.options.sort[0].operator);
return that;
2010-06-25 15:55:25 +00:00
};
return that;
};
2010-07-24 01:32:08 +00:00
/*
============================================================================
Maps
============================================================================
*/
Ox.Map = function(options, self) {
var self = self || {}
2010-09-03 20:54:40 +00:00
that = new Ox.Element('div', self)
2010-07-24 01:32:08 +00:00
.defaults({
2010-11-28 15:06:47 +00:00
clickable: false,
2010-07-24 01:32:08 +00:00
places: [],
2010-09-03 20:54:40 +00:00
type: 'satellite'
2010-07-24 01:32:08 +00:00
})
2010-11-28 15:06:47 +00:00
.options(options || {})
2010-12-26 20:16:35 +00:00
.bindEvent({
2010-11-28 15:06:47 +00:00
key_up: function() {
pan(0, -1);
},
key_down: function() {
pan(0, 1);
},
key_left: function() {
pan(-1, 0);
},
key_right: function() {
pan(1, 0);
},
key_0: reset,
key_minus: function() {
zoom(-1);
},
key_equal: function() {
zoom(1);
},
key_enter: focusOnPlace,
key_shift_enter: zoomToPlace,
key_escape: deselectPlace
});
2010-07-24 01:32:08 +00:00
2010-12-22 17:58:39 +00:00
if (Ox.isUndefined(window.google)) {
window.google = function() {};
$.getScript('http://maps.google.com/maps/api/js?callback=google&sensor=false', function() {
var interval = setInterval(function() {
if (!Ox.isUndefined(google.maps)) {
clearInterval(interval);
initMap();
}
}, 100);
});
} else {
initMap();
}
function initMap() {
$.extend(self, {
geocoder: new google.maps.Geocoder(),
selected: -1
});
if (Ox.isObject(self.options.places[0])) {
$.each(self.options.places, function(i, place) {
place.bounds = getBounds(),
place.center = place.bounds.getCenter();
$.extend(place, {
marker: new Marker(place),
polygon: new Polygon(place)
});
self.bounds = i == 0 ? getBounds() : self.bounds.union(place.bounds);
self.options.places[i] = place;
function getBounds() {
return new google.maps.LatLngBounds(
new google.maps.LatLng(place.points.southwest[0], place.points.southwest[1]),
new google.maps.LatLng(place.points.northeast[0], place.points.northeast[1])
);
}
});
loadMap();
} else {
self.counter = 0;
$.each(self.options.places, function(i, place) {
getLocationByName(place, function(place) {
Ox.print(self.counter, i, place);
self.options.places[i] = place;
if (self.counter++ == self.options.places.length - 1) {
loadMap();
}
});
});
}
}
2010-12-06 17:42:05 +00:00
function loadMap() {
self.center = self.bounds ? self.bounds.getCenter() : new google.maps.LatLng(0, 0);
self.zoom = 1;
$.extend(self, {
map: new google.maps.Map(that.$element[0], {
center: self.center,
disableDefaultUI: true,
mapTypeId: google.maps.MapTypeId[self.options.type.toUpperCase()],
zoom: self.zoom
})
});
if (self.bounds) {
self.map.fitBounds(self.bounds);
// self.center = self.map.getCenter();
self.zoom = self.map.getZoom();
2010-11-28 15:06:47 +00:00
}
2010-12-06 17:42:05 +00:00
google.maps.event.addListener(self.map, 'click', click);
google.maps.event.addListener(self.map, 'zoom_changed', zoomChanged);
$.each(self.options.places, function(i, place) {
place.marker.add();
});
resize();
that.gainFocus();
2010-12-22 17:58:39 +00:00
that.triggerEvent('loaded');
2010-12-06 17:42:05 +00:00
}
2010-07-24 19:27:39 +00:00
function canContain(outerBounds, innerBounds) {
var outerSpan = outerBounds.toSpan(),
innerSpan = innerBounds.toSpan();
return outerSpan.lat() > innerSpan.lat() &&
outerSpan.lng() > innerSpan.lng();
}
function click(event) {
2010-09-03 20:54:40 +00:00
Ox.print('event', event);
2010-11-28 15:06:47 +00:00
that.gainFocus();
if (self.options.clickable) {
getLocationByLatLng(event.latLng, self.map.getBounds(), function(location) {
self.marker && self.marker.remove();
self.polygon && self.polygon.remove();
if (location) {
self.marker = location.marker.add();
self.polygon && self.polygon.remove();
self.polygon = location.polygon.add();
that.triggerEvent('select', location);
}
});
}
2010-07-24 19:27:39 +00:00
}
2010-11-28 15:06:47 +00:00
function deselectPlace() {
}
function focusOnPlace() {
if (self.selected > -1) {
self.map.panTo(self.options.places[self.selected].center);
}
}
2010-07-24 19:27:39 +00:00
function getLocationByLatLng(latlng, bounds, callback) {
2010-09-03 20:54:40 +00:00
Ox.print('ll b', latlng, bounds)
2010-07-24 19:27:39 +00:00
var callback = arguments.length == 3 ? callback : bounds,
bounds = arguments.length == 3 ? bounds : null;
2010-07-24 01:32:08 +00:00
self.geocoder.geocode({
2010-07-24 19:27:39 +00:00
latLng: latlng
2010-07-24 01:32:08 +00:00
}, function(results, status) {
2010-09-03 20:54:40 +00:00
Ox.print('results', results)
2010-07-24 19:27:39 +00:00
var length = results.length;
2010-07-24 01:32:08 +00:00
if (status == google.maps.GeocoderStatus.OK) {
if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
2010-07-24 19:27:39 +00:00
if (bounds) {
$.each(results.reverse(), function(i, result) {
if (
i == length - 1 ||
canContain(bounds, result.geometry.bounds || result.geometry.viewport)
) {
callback(new Location(results[i]));
return false;
}
});
} else {
callback(new Location(results[0]));
}
2010-07-24 01:32:08 +00:00
} else {
callback(null);
}
} else {
2010-09-03 20:54:40 +00:00
Ox.print('geocode failed:', status);
2010-07-24 01:32:08 +00:00
callback(null);
}
});
}
2010-07-24 19:27:39 +00:00
function getLocationByName(name, callback) {
self.geocoder.geocode({
address: name
}, function(results, status) {
2010-07-24 01:32:08 +00:00
if (status == google.maps.GeocoderStatus.OK) {
if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
2010-07-24 19:27:39 +00:00
callback(new Location(results[0]))
} else {
callback(null);
2010-07-24 01:32:08 +00:00
}
2010-07-24 19:27:39 +00:00
} else {
2010-09-03 20:54:40 +00:00
Ox.print('geocode failed:', status);
2010-07-24 19:27:39 +00:00
callback(null);
2010-07-24 01:32:08 +00:00
}
});
}
2010-11-28 15:06:47 +00:00
function getPositionByName(name) {
var position = -1;
$.each(self.options.places, function(i, place) {
if (place.name == name) {
position = i;
return false;
}
});
return position;
}
2010-07-24 01:32:08 +00:00
2010-11-28 15:06:47 +00:00
function pan(x, y) {
self.map.panBy(x * 256, y * 256);
};
2010-07-24 01:32:08 +00:00
2010-07-24 19:27:39 +00:00
function resize() {
2010-09-03 20:54:40 +00:00
google.maps.event.trigger(self.map, 'resize');
2010-07-24 19:27:39 +00:00
}
2010-11-28 15:06:47 +00:00
function reset() {
Ox.print(self.map.getZoom(), self.zoom);
self.map.getZoom() == self.zoom ?
self.map.panTo(self.center) :
self.map.fitBounds(self.bounds);
}
function zoomChanged() {
2010-09-03 20:54:40 +00:00
that.triggerEvent('zoom', {
2010-09-03 08:47:40 +00:00
value: self.map.getZoom()
});
2010-07-24 01:32:08 +00:00
}
2010-11-28 15:06:47 +00:00
function zoom(z) {
self.map.setZoom(self.map.getZoom() + z);
}
function zoomToPlace() {
if (self.selected > -1) {
self.map.fitBounds(self.options.places[self.selected].bounds);
}
}
function Location(geodata) {
Ox.print('geodata', geodata);
var bounds = geodata.geometry.bounds || geodata.geometry.viewport,
center = bounds.getCenter(),
location = {
bounds: bounds,
center: center,
geoname: geodata.formatted_address,
points: {
'center': [center.lat(), center.lng()],
'southwest': [bounds.getSouthWest().lat(), bounds.getSouthWest().lng()],
'northeast': [bounds.getNorthEast().lat(), bounds.getNorthEast().lng()]
},
name: geodata.formatted_address.split(', ')[0],
size: 0,
type: geodata.address_components[0].types.join(', ')
};
2010-07-24 01:32:08 +00:00
return $.extend(location, {
marker: new Marker(location),
polygon: new Polygon(location)
});
}
2010-11-28 15:06:47 +00:00
function Marker(place) {
Ox.print(place.center)
2010-07-24 01:32:08 +00:00
var listeners = {},
marker = new google.maps.Marker({
2010-09-03 20:54:40 +00:00
icon: icon('red'),
2010-11-28 15:06:47 +00:00
position: place.center,
title: place.name
2010-07-24 19:27:39 +00:00
}),
selected = false;
2010-07-24 01:32:08 +00:00
function click() {
2010-11-28 15:06:47 +00:00
Ox.print('click', self.selected, selected)
2010-07-24 19:27:39 +00:00
selected = !selected;
2010-11-28 15:06:47 +00:00
selected && self.selected > -1 && self.options.places[self.selected].marker.deselect();
self.selected = selected ? getPositionByName(place.name) : -1;
2010-07-24 01:32:08 +00:00
marker.setOptions({
2010-09-03 20:54:40 +00:00
icon: icon(selected ? 'blue' : 'red')
2010-07-24 01:32:08 +00:00
});
2010-11-28 15:06:47 +00:00
place.polygon[selected ? 'add' : 'remove']();
that.triggerEvent(selected ? 'select' : 'deselect', place);
2010-07-24 19:27:39 +00:00
}
2010-11-28 15:06:47 +00:00
function deselect() {
}
2010-07-24 19:27:39 +00:00
function dblclick() {
2010-11-28 15:06:47 +00:00
Ox.print('dblclick', place.bounds)
self.map.fitBounds(place.bounds);
2010-07-24 01:32:08 +00:00
}
2010-11-28 15:06:47 +00:00
function select() {
}
2010-07-24 01:32:08 +00:00
function icon(color) {
return oxui.path + 'png/ox.ui/marker' + Ox.toTitleCase(color) + '.png'
2010-07-24 01:32:08 +00:00
}
return {
add: function() {
2010-11-28 15:06:47 +00:00
Ox.print('add Marker')
2010-07-24 01:32:08 +00:00
marker.setMap(self.map);
2010-07-24 19:27:39 +00:00
listeners = {
2010-09-03 20:54:40 +00:00
click: google.maps.event.addListener(marker, 'click', click),
dblclick: google.maps.event.addListener(marker, 'dblclick', dblclick),
2010-07-24 19:27:39 +00:00
};
return this;
},
deselect: function() {
2010-11-28 15:06:47 +00:00
self.selected = -1;
selected = false;
marker.setOptions({
icon: icon('red')
});
place.polygon.remove();
2010-07-24 01:32:08 +00:00
},
remove: function() {
marker.setMap(null);
2010-07-24 19:27:39 +00:00
$.each(listeners, function(i, listener) {
google.maps.event.removeListener(listener);
});
return this;
},
select: function() {
2010-11-28 15:06:47 +00:00
if (self.selected > -1) {
self.options.places[self.selected].marker.deselect();
}
self.selected = getPositionByName(place.name);
selected = true;
marker.setOptions({
icon: icon('blue')
});
place.polygon.add();
2010-07-24 01:32:08 +00:00
}
};
}
function Polygon(location) {
var listeners = {},
2010-11-28 15:06:47 +00:00
paths = [
new google.maps.LatLng(location.points.southwest[0], location.points.southwest[1]),
new google.maps.LatLng(location.points.northeast[0], location.points.southwest[1]),
new google.maps.LatLng(location.points.northeast[0], location.points.northeast[1]),
new google.maps.LatLng(location.points.southwest[0], location.points.northeast[1]),
new google.maps.LatLng(location.points.southwest[0], location.points.southwest[1])
2010-07-24 01:32:08 +00:00
],
polygon = new google.maps.Polygon({
2010-11-28 15:06:47 +00:00
paths: paths
2010-07-24 01:32:08 +00:00
}),
selected = false;
2010-07-24 19:27:39 +00:00
setOptions();
function click() {
selected = !selected;
setOptions();
}
2010-07-24 01:32:08 +00:00
function setOptions() {
2010-09-03 20:54:40 +00:00
var color = selected ? '#8080FF' : '#FFFFFF';
2010-07-24 01:32:08 +00:00
polygon.setOptions({
clickable: true,
fillColor: color,
fillOpacity: selected ? 0.1 : 0,
strokeColor: color,
strokeOpacity: 1,
strokeWeight: 2
});
}
return {
add: function() {
polygon.setMap(self.map);
2010-09-03 20:54:40 +00:00
listeners.click = google.maps.event.addListener(polygon, 'click', click);
2010-07-24 19:27:39 +00:00
return this;
2010-07-24 01:32:08 +00:00
},
deselect: function() {
selected = false;
setOptions();
},
remove: function() {
polygon.setMap(null);
google.maps.event.removeListener(listeners.click);
2010-07-24 19:27:39 +00:00
return this;
2010-07-24 01:32:08 +00:00
},
select: function() {
selected = true;
setOptions();
}
};
}
2010-12-06 17:42:05 +00:00
function Rectangle(area) { // fixme: not used
2010-07-24 01:32:08 +00:00
var latlng = {
sw: new google.maps.LatLng(area[0][0], area[0][1]),
ne: new google.maps.LatLng(area[1][0], area[1][1])
},
bounds = new google.maps.LatLngBounds(latlng.sw, latlng.ne),
lat = {},
lng = {};
latlng.mc = bounds.getCenter();
$.each(latlng, function(k, v) {
lat[k] = v.lat();
lng[k] = v.lng();
});
$.extend(latlng, {
sc: new google.maps.LatLng(lat.sw, lng.mc),
2010-07-24 19:27:39 +00:00
se: new google.maps.LatLng(lat.sw, lng.ne),
mw: new google.maps.LatLng(lat.mc, lng.sw),
2010-11-28 15:06:47 +00:00
me: new google.maps.LatLng(lat.mc, lng.ne),
2010-07-24 19:27:39 +00:00
nw: new google.maps.LatLng(lat.ne, lng.sw),
nc: new google.maps.LatLng(lat.ne, lng.mc),
2010-07-24 01:32:08 +00:00
});
return {
area: area,
bounds: bounds,
canContain: function(rectangle) {
var outerSpan = this.bounds.toSpan(),
innerSpan = rectangle.bounds.toSpan();
return outerSpan.lat() > innerSpan.lat() &&
outerSpan.lng() > innerSpan.lng();
},
center: latlng.mc,
contains: function(rectangle) {
return this.bounds.contains(rectangle.bounds.getSouthWest()) &&
this.bounds.contains(rectangle.bounds.getNorthEast());
},
latlng: latlng
};
}
self.onChange = function(key, value) {
2010-09-03 20:54:40 +00:00
if (key == 'type') {
2010-07-24 01:32:08 +00:00
}
};
2010-09-03 08:47:40 +00:00
that.find = function(name, callback) {
2010-12-06 17:42:05 +00:00
getLocationByName(name, function(place) {
if (place) {
2010-11-28 15:06:47 +00:00
//self.marker = location.marker.add();
2010-09-03 08:47:40 +00:00
self.polygon && self.polygon.remove();
2010-12-06 17:42:05 +00:00
self.polygon = place.polygon.add();
self.bounds = place.bounds;
2010-09-03 08:47:40 +00:00
self.map.fitBounds(self.bounds);
}
2010-12-06 17:42:05 +00:00
callback(place);
2010-09-03 08:47:40 +00:00
});
};
2010-12-22 18:19:47 +00:00
that.triggerResize = function() {
Ox.print('---- triggerResize');
var center = self.map.getCenter();
google.maps.event.trigger(self.map, 'resize');
self.map.setCenter(center);
}
2010-09-03 08:47:40 +00:00
that.zoom = function(value) {
self.map.setZoom(value);
};
2010-07-24 01:32:08 +00:00
return that;
};
2010-12-06 17:42:45 +00:00
/**
options
2010-07-24 01:32:08 +00:00
height image height (px)
2010-09-03 20:54:40 +00:00
places array of either names (''), points ([0, 0]),
2010-07-24 01:32:08 +00:00
or objects ({name, point, highlight})
2010-09-03 20:54:40 +00:00
type map type ('hybrid', 'roadmap', 'satellite', 'terrain')
2010-07-24 01:32:08 +00:00
width image width (px)
2010-12-06 17:42:45 +00:00
*/
Ox.MapImage = function(options, self) {
2010-07-24 01:32:08 +00:00
var self = self || {},
2010-09-03 20:54:40 +00:00
that = new Ox.Element('img', self)
2010-07-24 01:32:08 +00:00
.defaults({
height: 360,
2010-09-03 20:54:40 +00:00
markerColorHighlight: 'yellow',
markerColorNormal: 'blue',
2010-07-24 01:32:08 +00:00
places: [],
2010-09-03 20:54:40 +00:00
type: 'satellite',
2010-07-24 01:32:08 +00:00
width: 640
})
.options(options || {})
$.extend(self, {
markers: {
highlight: [],
normal: []
},
2010-09-03 20:54:40 +00:00
src: 'http://maps.google.com/maps/api/staticmap?sensor=false' +
'&size=' + self.options.width + 'x' + self.options.height +
'&maptype=' + self.options.type
2010-07-24 01:32:08 +00:00
});
if (self.options.places.length) {
$.each(self.options.places, function(i, place) {
if (Ox.isString(place)) {
self.markers.normal.push(place);
} else if (Ox.isArray(place)) {
2010-09-03 20:54:40 +00:00
self.markers.normal.push(place.join(','));
2010-07-24 01:32:08 +00:00
} else {
2010-09-03 20:54:40 +00:00
self.markers[place.highlight ? 'highlight' : 'normal']
.push('point' in place ? place.point.join(',') : place.name)
2010-07-24 01:32:08 +00:00
}
});
$.each(self.markers, function(k, markers) {
if (markers.length) {
2010-09-03 20:54:40 +00:00
self.src += '&markers=icon:' + 'http://dev.pan.do:8000' + oxui.path + 'png/ox.ui/marker' +
Ox.toTitleCase(self.options['markerColor' + Ox.toTitleCase(k)]) + '.png|' +
markers.join('|')
2010-07-24 01:32:08 +00:00
}
});
} else {
2010-09-03 20:54:40 +00:00
self.src += '&center=0,0&zoom=2'
2010-07-24 01:32:08 +00:00
}
that.attr({
src: self.src
});
self.onChange = function(key, value) {
};
return that;
};
2010-01-07 20:21:07 +00:00
/*
============================================================================
2010-02-02 16:03:11 +00:00
Menus
============================================================================
*/
2010-12-06 17:42:45 +00:00
/**
*/
2010-02-02 16:03:11 +00:00
Ox.MainMenu = function(options, self) {
2010-02-07 15:01:22 +00:00
var self = self || {},
that = new Ox.Bar({}, self)
2010-02-08 09:35:24 +00:00
.defaults({
2010-02-19 10:24:02 +00:00
extras: [],
2010-02-08 09:35:24 +00:00
menus: [],
2010-09-03 20:54:40 +00:00
size: 'medium'
2010-02-08 09:35:24 +00:00
})
.options(options || {})
2010-09-03 20:54:40 +00:00
.addClass('OxMainMenu Ox' + Ox.toTitleCase(self.options.size)) // fixme: bar should accept small/medium/large ... like toolbar
2010-02-08 09:35:24 +00:00
.click(click)
.mousemove(mousemove);
2010-02-07 15:01:22 +00:00
2010-02-08 09:35:24 +00:00
self.focused = false;
self.selected = -1;
that.menus = [];
2010-02-09 05:43:36 +00:00
that.titles = [];
2010-09-03 20:54:40 +00:00
that.layer = $('<div>').addClass('OxLayer');
2010-02-08 09:35:24 +00:00
2010-02-19 10:57:19 +00:00
$.each(self.options.menus, function(position, menu) {
2010-09-03 20:54:40 +00:00
that.titles[position] = $('<div>')
.addClass('OxTitle')
2010-02-08 09:35:24 +00:00
.html(menu.title)
2010-09-03 20:54:40 +00:00
.data('position', position)
2010-02-08 09:35:24 +00:00
.appendTo(that.$element);
that.menus[position] = new Ox.Menu($.extend(menu, {
2010-09-03 08:47:40 +00:00
element: that.titles[position],
mainmenu: that,
size: self.options.size
}))
2010-09-03 20:54:40 +00:00
.bindEvent({
hide: onHideMenu
});
2010-02-08 09:35:24 +00:00
});
2010-02-19 10:57:19 +00:00
if (self.options.extras.length) {
2010-09-03 20:54:40 +00:00
that.extras = $('<div>')
.addClass('OxExtras')
2010-02-19 10:57:19 +00:00
.appendTo(that.$element);
$.each(self.options.extras, function(position, extra) {
2010-02-20 08:29:03 +00:00
extra.css({
2010-09-03 20:54:40 +00:00
float: 'left' // fixme: need class!
2010-02-20 08:29:03 +00:00
}).appendTo(that.extras);
2010-02-19 10:57:19 +00:00
});
}
2010-02-08 09:35:24 +00:00
function click(event) {
var $target = $(event.target),
2010-09-03 20:54:40 +00:00
position = typeof $target.data('position') != 'undefined' ?
$target.data('position') : -1;
2010-02-09 05:43:36 +00:00
clickTitle(position);
}
function clickTitle(position) {
var selected = self.selected;
if (self.selected > -1) {
that.menus[self.selected].hideMenu();
}
if (position > -1) {
if (position != selected) {
self.focused = true;
self.selected = position;
2010-09-03 20:54:40 +00:00
that.titles[self.selected].addClass('OxSelected');
2010-02-09 05:43:36 +00:00
that.menus[self.selected].showMenu();
}
2010-02-08 09:35:24 +00:00
}
}
function mousemove(event) {
var $target = $(event.target),
2010-02-09 05:43:36 +00:00
focused,
2010-09-03 20:54:40 +00:00
position = typeof $target.data('position') != 'undefined' ?
$target.data('position') : -1;
2010-02-09 05:43:36 +00:00
if (self.focused && position != self.selected) {
if (position > -1) {
clickTitle(position);
2010-02-09 05:43:36 +00:00
} else {
focused = self.focused;
that.menus[self.selected].hideMenu();
self.focused = focused;
2010-02-08 10:17:00 +00:00
}
2010-02-08 09:35:24 +00:00
}
}
2010-02-09 05:43:36 +00:00
function onHideMenu() {
2010-02-09 12:20:23 +00:00
if (self.selected > -1) {
2010-09-03 20:54:40 +00:00
that.titles[self.selected].removeClass('OxSelected');
2010-02-09 12:20:23 +00:00
self.selected = -1;
}
2010-02-08 09:35:24 +00:00
self.focused = false;
}
self.onChange = function(key, value) {
};
2010-02-19 10:24:02 +00:00
that.addMenuAfter = function(id) {
2010-02-08 10:17:00 +00:00
};
2010-02-19 10:24:02 +00:00
that.addMenuBefore = function(id) {
2010-02-08 10:17:00 +00:00
};
2010-07-07 12:36:12 +00:00
that.checkItem = function(id) {
2010-09-03 20:54:40 +00:00
var ids = id.split('_'),
itemId = ids.pop(),
menuId = ids.join('_');
that.getMenu(menuId).checkItem(itemId);
2010-07-07 12:36:12 +00:00
};
2010-02-10 16:37:26 +00:00
that.disableItem = function(id) {
2010-07-13 22:38:53 +00:00
that.getItem(id).options({
disabled: true
});
2010-02-10 16:37:26 +00:00
};
that.enableItem = function(id) {
2010-07-13 22:38:53 +00:00
that.getItem(id).options({
disabled: false
});
2010-02-10 16:37:26 +00:00
};
2010-02-19 16:13:22 +00:00
that.getItem = function(id) {
2010-09-03 20:54:40 +00:00
var ids = id.split('_'),
2010-07-07 12:36:12 +00:00
item;
if (ids.length == 1) {
$.each(that.menus, function(i, menu) {
item = menu.getItem(id);
return !item;
});
} else {
2010-09-03 20:54:40 +00:00
item = that.getMenu(ids.shift()).getItem(ids.join('_'));
2010-07-07 12:36:12 +00:00
}
2010-09-03 20:54:40 +00:00
Ox.print('getItem', id, item);
2010-02-19 16:13:22 +00:00
return item;
};
2010-09-03 20:54:40 +00:00
that.getMenu = function(id) {
var ids = id.split('_'),
menu;
if (ids.length == 1) {
$.each(that.menus, function(i, v) {
if (v.options('id') == id) {
menu = v;
return false;
}
});
} else {
menu = that.getMenu(ids.shift()).getSubmenu(ids.join('_'));
}
Ox.print('getMenu', id, menu);
return menu;
};
2010-02-08 10:17:00 +00:00
that.removeMenu = function() {
};
2010-02-08 09:35:24 +00:00
that.selectNextMenu = function() {
if (self.selected < self.options.menus.length - 1) {
clickTitle(self.selected + 1);
}
};
that.selectPreviousMenu = function() {
if (self.selected) {
clickTitle(self.selected - 1);
}
};
2010-07-07 12:36:12 +00:00
that.uncheckItem = function(id) {
2010-07-13 22:38:53 +00:00
that.getItem(id).options({
checked: false
});
2010-07-07 12:36:12 +00:00
};
2010-02-08 09:35:24 +00:00
return that;
};
2010-02-02 16:03:11 +00:00
2010-12-06 17:42:45 +00:00
/**
options
2010-09-03 08:47:40 +00:00
element the element the menu is attached to
id the menu id
items array of menu items
mainmenu the main menu this menu is part of, if any
offset offset of the menu, in px
parent the supermenu, if any
selected the position of the selected item
2010-09-03 20:54:40 +00:00
side open to 'bottom' or 'right'
size 'large', 'medium' or 'small'
2010-02-10 15:49:57 +00:00
events:
2010-09-03 08:47:40 +00:00
change_groupId {id, value} checked item of a group has changed
click_itemId item not belonging to a group was clicked
click_menuId {id, value} item not belonging to a group was clicked
deselect_menuId {id, value} item was deselected not needed, not implemented
hide_menuId menu was hidden
select_menuId {id, value} item was selected
2010-12-06 17:42:45 +00:00
*/
Ox.Menu = function(options, self) {
2010-02-02 16:03:11 +00:00
var self = self || {},
that = new Ox.Element({}, self)
.defaults({
2010-02-04 08:02:23 +00:00
element: null,
2010-09-03 20:54:40 +00:00
id: '',
2010-02-02 16:03:11 +00:00
items: [],
2010-02-08 09:35:24 +00:00
mainmenu: null,
2010-02-02 16:03:11 +00:00
offset: {
left: 0,
top: 0
},
2010-02-05 09:13:03 +00:00
parent: null,
2010-02-04 09:50:45 +00:00
selected: -1,
2010-09-03 20:54:40 +00:00
side: 'bottom',
size: 'medium',
2010-02-02 16:03:11 +00:00
})
2010-09-03 08:47:40 +00:00
.options(options || {})
2010-02-03 12:12:21 +00:00
.addClass(
2010-09-03 20:54:40 +00:00
'OxMenu Ox' + Ox.toTitleCase(self.options.side) +
' Ox' + Ox.toTitleCase(self.options.size)
2010-02-07 15:01:22 +00:00
)
.click(click)
.mouseenter(mouseenter)
.mouseleave(mouseleave)
2010-09-03 08:47:40 +00:00
.mousemove(mousemove)
2010-12-26 20:16:35 +00:00
.bindEvent({
2010-09-03 08:47:40 +00:00
key_up: selectPreviousItem,
key_down: selectNextItem,
key_left: selectSupermenu,
key_right: selectSubmenu,
key_escape: hideMenu,
key_enter: clickSelectedItem
}),
2010-09-03 20:54:40 +00:00
itemHeight = self.options.size == 'small' ? 12 : (self.options.size == 'medium' ? 16 : 20),
2010-02-18 14:24:17 +00:00
// menuHeight,
2010-02-07 15:01:22 +00:00
scrollSpeed = 1,
2010-02-05 14:59:24 +00:00
$item; // fixme: used?
2010-09-03 08:47:40 +00:00
// fixme: attach all private vars to self
2010-02-02 16:03:11 +00:00
// construct
2010-02-04 08:02:23 +00:00
that.items = [];
that.submenus = {};
2010-02-02 16:03:11 +00:00
that.$scrollbars = [];
2010-09-03 20:54:40 +00:00
that.$top = $('<div>')
.addClass('OxTop')
2010-02-02 16:03:11 +00:00
.appendTo(that.$element);
2010-09-03 20:54:40 +00:00
that.$scrollbars.up = constructScrollbar('up')
2010-02-02 16:03:11 +00:00
.appendTo(that.$element);
2010-09-03 20:54:40 +00:00
that.$container = $('<div>')
.addClass('OxContainer')
2010-02-02 16:03:11 +00:00
.appendTo(that.$element);
2010-09-03 20:54:40 +00:00
that.$content = $('<table>')
.addClass('OxContent')
2010-02-02 16:03:11 +00:00
.appendTo(that.$container);
2010-02-18 07:27:32 +00:00
constructItems(self.options.items);
2010-09-03 20:54:40 +00:00
that.$scrollbars.down = constructScrollbar('down')
2010-02-02 16:03:11 +00:00
.appendTo(that.$element);
2010-09-03 20:54:40 +00:00
that.$bottom = $('<div>')
.addClass('OxBottom')
2010-02-02 16:03:11 +00:00
.appendTo(that.$element);
2010-09-03 20:54:40 +00:00
that.$layer = $('<div>')
2010-12-26 20:16:35 +00:00
.addClass(self.options.mainmenu ? 'OxMainMenuLayer' : 'OxMenuLayer')
2010-09-03 08:47:40 +00:00
.click(click);
2010-02-02 16:03:11 +00:00
2010-02-05 09:13:03 +00:00
function click(event) {
2010-02-07 15:01:22 +00:00
var item,
2010-02-07 15:12:14 +00:00
position,
2010-09-03 08:47:40 +00:00
$target = $(event.target),
$parent = $target.parent();
// necessary for highlight
2010-09-03 20:54:40 +00:00
if ($parent.is('.OxCell')) {
2010-09-03 08:47:40 +00:00
$target = $parent;
$parent = $target.parent();
}
2010-09-03 20:54:40 +00:00
if ($target.is('.OxCell')) {
position = $parent.data('position');
2010-02-07 15:12:14 +00:00
item = that.items[position];
2010-09-03 20:54:40 +00:00
if (!item.options('disabled')) {
2010-02-07 15:12:14 +00:00
clickItem(position);
2010-02-09 12:20:23 +00:00
} else {
that.hideMenu();
2010-02-07 15:01:22 +00:00
}
2010-02-09 12:20:23 +00:00
} else {
that.hideMenu();
2010-02-05 09:13:03 +00:00
}
}
2010-02-07 15:12:14 +00:00
function clickItem(position) {
2010-09-03 08:47:40 +00:00
var item = that.items[position],
toggled;
2010-09-04 14:28:40 +00:00
that.hideMenu();
2010-09-03 20:54:40 +00:00
if (!item.options('items').length) {
if (that.options('parent')) {
that.options('parent').hideMenu().triggerEvent('click');
2010-02-08 09:35:24 +00:00
}
2010-09-03 20:54:40 +00:00
if (item.options('checked') !== null) {
if (item.options('group')) {
Ox.print('has group', item.options('group'))
toggled = self.optionGroups[item.options('group')].toggle(position);
Ox.print('toggled', toggled)
2010-09-03 08:47:40 +00:00
if (toggled.length) {
$.each(toggled, function(i, pos) {
that.items[pos].toggleChecked();
});
2010-09-03 20:54:40 +00:00
Ox.print('--triggering change event--');
(self.options.mainmenu || that).triggerEvent('change', {
id: item.options('group'),
checked: $.map(self.optionGroups[item.options('group')].checked(), function(v, i) {
2010-09-03 08:47:40 +00:00
return {
2010-09-03 20:54:40 +00:00
id: that.items[v].options('id'),
title: Ox.stripTags(that.items[v].options('title')[0])
};
2010-09-03 08:47:40 +00:00
})
});
}
} else {
item.toggleChecked();
2010-09-03 20:54:40 +00:00
(self.options.mainmenu || that).triggerEvent('change', {
checked: item.options('checked'),
id: item.options('id'),
title: Ox.stripTags(item.options('title')[0])
2010-09-03 08:47:40 +00:00
});
}
2010-02-09 12:20:23 +00:00
} else {
2010-09-03 20:54:40 +00:00
(self.options.mainmenu || that).triggerEvent('click', {
id: item.options('id'),
title: Ox.stripTags(item.options('title')[0])
2010-02-18 14:24:17 +00:00
});
2010-02-07 15:12:14 +00:00
}
2010-09-03 20:54:40 +00:00
if (item.options('title').length == 2) {
2010-02-07 15:12:14 +00:00
item.toggleTitle();
}
}
}
function clickSelectedItem() {
2010-02-09 05:43:36 +00:00
// called on key.enter
2010-02-04 09:50:45 +00:00
if (self.options.selected > -1) {
2010-02-07 15:12:14 +00:00
clickItem(self.options.selected);
2010-02-05 05:20:13 +00:00
} else {
that.hideMenu();
2010-02-04 09:50:45 +00:00
}
}
2010-02-18 07:27:32 +00:00
function constructItems(items) {
2010-09-03 08:47:40 +00:00
2010-02-18 07:27:32 +00:00
that.$content.empty();
2010-02-18 14:24:17 +00:00
scrollMenuUp();
2010-09-03 08:47:40 +00:00
self.optionGroups = {};
$.each(items, function(i, item) {
if (item.group) {
items[i] = $.map(item.items, function(v, i) {
return $.extend(v, {
group: item.group
});
});
self.optionGroups[item.group] = new Ox.OptionGroup(
items[i],
2010-09-03 20:54:40 +00:00
'min' in item ? item.min : 1,
'max' in item ? item.max : 1
2010-09-03 08:47:40 +00:00
);
}
});
items = Ox.flatten(items);
that.items = [];
2010-02-18 07:27:32 +00:00
$.each(items, function(i, item) {
var position;
if (item.id) {
that.items.push(new Ox.MenuItem($.extend(item, {
menu: that,
position: position = that.items.length
2010-09-03 20:54:40 +00:00
})).data('position', position).appendTo(that.$content)); // fixme: jquery bug when passing {position: position}? does not return the object?;
2010-02-18 07:27:32 +00:00
if (item.items) {
that.submenus[item.id] = new Ox.Menu({
element: that.items[position],
2010-09-03 20:54:40 +00:00
id: Ox.toCamelCase(self.options.id + '/' + item.id),
2010-02-18 07:27:32 +00:00
items: item.items,
mainmenu: self.options.mainmenu,
offset: {
left: 0,
top: -4
},
parent: that,
2010-09-03 20:54:40 +00:00
side: 'right',
2010-02-18 07:27:32 +00:00
size: self.options.size,
});
}
} else {
that.$content.append(constructSpace());
that.$content.append(constructLine());
that.$content.append(constructSpace());
}
});
2010-09-03 08:47:40 +00:00
2010-09-03 20:54:40 +00:00
if (!that.is(':hidden')) {
2010-02-18 14:24:17 +00:00
that.hideMenu();
that.showMenu();
}
2010-09-03 08:47:40 +00:00
2010-02-18 07:27:32 +00:00
}
2010-02-02 16:03:11 +00:00
function constructLine() {
2010-09-03 20:54:40 +00:00
return $('<tr>').append(
$('<td>', {
'class': 'OxLine',
2010-02-04 08:02:23 +00:00
colspan: 5
2010-02-03 12:12:21 +00:00
})
);
2010-02-02 16:03:11 +00:00
}
function constructScrollbar(direction) {
2010-02-05 14:59:24 +00:00
var interval,
2010-09-03 20:54:40 +00:00
speed = direction == 'up' ? -1 : 1;
return $('<div/>', {
'class': 'OxScrollbar Ox' + Ox.toTitleCase(direction),
html: oxui.symbols['triangle_' + direction],
2010-02-03 12:12:21 +00:00
click: function() { // fixme: do we need to listen to click event?
return false;
},
mousedown: function() {
scrollSpeed = 2;
return false;
},
mouseenter: function() {
2010-09-03 20:54:40 +00:00
var $otherScrollbar = that.$scrollbars[direction == 'up' ? 'down' : 'up'];
$(this).addClass('OxSelected');
if ($otherScrollbar.is(':hidden')) {
2010-02-03 12:12:21 +00:00
$otherScrollbar.show();
that.$container.height(that.$container.height() - itemHeight);
2010-09-03 20:54:40 +00:00
if (direction == 'down') {
2010-02-03 12:12:21 +00:00
that.$content.css({
2010-09-03 20:54:40 +00:00
top: -itemHeight + 'px'
2010-02-03 12:12:21 +00:00
});
}
}
2010-02-05 14:59:24 +00:00
scrollMenu(speed);
2010-02-03 12:12:21 +00:00
interval = setInterval(function() {
2010-02-05 14:59:24 +00:00
scrollMenu(speed);
2010-02-03 12:12:21 +00:00
}, 100);
},
mouseleave: function() {
2010-09-03 20:54:40 +00:00
$(this).removeClass('OxSelected');
2010-02-03 12:12:21 +00:00
clearInterval(interval);
},
mouseup: function() {
scrollSpeed = 1;
return false;
}
});
2010-02-02 16:03:11 +00:00
}
function constructSpace() {
2010-09-03 20:54:40 +00:00
return $('<tr>').append(
$('<td>', {
'class': 'OxSpace',
2010-02-04 08:02:23 +00:00
colspan: 5
2010-02-03 12:12:21 +00:00
})
);
2010-02-02 16:03:11 +00:00
}
function getElement(id) {
2010-02-05 09:13:03 +00:00
// fixme: needed?
2010-09-03 20:54:40 +00:00
return $('#' + Ox.toCamelCase(options.id + '/' + id));
}
function getItemPositionById(id) {
var position;
$.each(that.items, function(i, v) {
if (v.options('id') == id) {
position = i;
return false;
}
});
return position;
2010-02-02 16:03:11 +00:00
}
function hideMenu() {
// called on key_escape
that.hideMenu();
}
2010-02-05 05:20:13 +00:00
function isFirstEnabledItem() {
var ret = true;
$.each(that.items, function(i, item) {
2010-09-03 20:54:40 +00:00
if (i < self.options.selected && !item.options('disabled')) {
2010-02-05 05:20:13 +00:00
return ret = false;
}
});
return ret;
}
function isLastEnabledItem() {
var ret = true;
$.each(that.items, function(i, item) {
2010-09-03 20:54:40 +00:00
if (i > self.options.selected && !item.options('disabled')) {
2010-02-05 05:20:13 +00:00
return ret = false;
}
});
return ret;
}
2010-02-07 15:01:22 +00:00
function mouseenter() {
that.gainFocus();
}
function mouseleave() {
2010-09-03 20:54:40 +00:00
if (self.options.selected > -1 && !that.items[self.options.selected].options('items').length) {
2010-02-07 15:01:22 +00:00
selectItem(-1);
}
}
function mousemove(event) {
var item,
position,
$target = $(event.target);
2010-09-03 08:47:40 +00:00
$parent = $target.parent();
2010-09-03 20:54:40 +00:00
if ($parent.is('.OxCell')) {
2010-09-03 08:47:40 +00:00
$target = $parent;
$parent = $target.parent();
}
2010-09-03 20:54:40 +00:00
if ($target.is('.OxCell')) {
position = $parent.data('position');
2010-02-07 15:01:22 +00:00
item = that.items[position];
2010-09-03 20:54:40 +00:00
if (!item.options('disabled') && position != self.options.selected) {
2010-02-07 15:01:22 +00:00
selectItem(position);
}
} else {
mouseleave();
}
}
2010-02-03 12:12:21 +00:00
function scrollMenu(speed) {
var containerHeight = that.$container.height(),
contentHeight = that.$content.height(),
2010-09-03 20:54:40 +00:00
top = parseInt(that.$content.css('top')) || 0,
2010-02-03 12:12:21 +00:00
min = containerHeight - contentHeight + itemHeight,
max = 0;
top += speed * scrollSpeed * -itemHeight;
if (top <= min) {
top = min;
2010-09-03 20:54:40 +00:00
that.$scrollbars.down.hide().trigger('mouseleave');
2010-02-05 14:59:24 +00:00
that.$container.height(containerHeight + itemHeight);
2010-09-03 20:54:40 +00:00
that.items[that.items.length - 1].trigger('mouseover');
2010-02-03 12:12:21 +00:00
} else if (top >= max - itemHeight) {
top = max;
2010-09-03 20:54:40 +00:00
that.$scrollbars.up.hide().trigger('mouseleave');
2010-02-05 14:59:24 +00:00
that.$container.height(containerHeight + itemHeight);
2010-09-03 20:54:40 +00:00
that.items[0].trigger('mouseover');
2010-02-03 12:12:21 +00:00
}
that.$content.css({
2010-09-03 20:54:40 +00:00
top: top + 'px'
2010-02-03 12:12:21 +00:00
});
2010-02-02 16:03:11 +00:00
}
2010-02-05 17:37:15 +00:00
function scrollMenuUp() {
2010-09-03 20:54:40 +00:00
if (that.$scrollbars.up.is(':visible')) {
2010-02-05 17:37:15 +00:00
that.$content.css({
2010-09-03 20:54:40 +00:00
top: '0px'
2010-02-05 17:37:15 +00:00
});
that.$scrollbars.up.hide();
2010-09-03 20:54:40 +00:00
if (that.$scrollbars.down.is(':hidden')) {
2010-02-05 17:37:15 +00:00
that.$scrollbars.down.show();
} else {
that.$container.height(that.$container.height() + itemHeight);
}
}
}
2010-02-07 15:01:22 +00:00
function selectItem(position) {
var item;
if (self.options.selected > -1) {
2010-09-03 20:54:40 +00:00
//Ox.print('s.o.s', self.options.selected, that.items)
2010-02-18 14:24:17 +00:00
item = that.items[self.options.selected]
2010-09-03 20:54:40 +00:00
item.removeClass('OxSelected');
/* disabled
that.triggerEvent('deselect', {
id: item.options('id'),
title: Ox.stripTags(item.options('title')[0])
2010-09-03 08:47:40 +00:00
});
2010-09-03 20:54:40 +00:00
*/
2010-02-07 15:01:22 +00:00
}
if (position > -1) {
item = that.items[position];
$.each(that.submenus, function(id, submenu) {
2010-09-03 20:54:40 +00:00
if (!submenu.is(':hidden')) {
2010-02-07 15:01:22 +00:00
submenu.hideMenu();
return false;
}
});
2010-09-03 20:54:40 +00:00
item.options('items').length && that.submenus[item.options('id')].showMenu(); // fixme: do we want to switch to this style?
item.addClass('OxSelected');
/* disabled
that.triggerEvent('select', {
id: item.options('id'),
title: Ox.stripTags(item.options('title')[0])
2010-09-03 08:47:40 +00:00
});
2010-09-03 20:54:40 +00:00
*/
2010-02-07 15:01:22 +00:00
}
2010-02-18 09:11:47 +00:00
self.options.selected = position;
2010-02-07 15:01:22 +00:00
}
2010-02-02 16:03:11 +00:00
function selectNextItem() {
2010-02-05 15:26:23 +00:00
var offset,
selected = self.options.selected;
2010-09-03 20:54:40 +00:00
Ox.print('sNI', selected)
2010-02-05 05:20:13 +00:00
if (!isLastEnabledItem()) {
2010-02-05 17:37:15 +00:00
if (selected == -1) {
scrollMenuUp();
} else {
2010-09-03 20:54:40 +00:00
that.items[selected].removeClass('OxSelected');
2010-02-04 09:50:45 +00:00
}
2010-02-05 05:20:13 +00:00
do {
selected++;
2010-09-03 20:54:40 +00:00
} while (that.items[selected].options('disabled'))
2010-02-07 15:01:22 +00:00
selectItem(selected);
2010-02-05 15:26:23 +00:00
offset = that.items[selected].offset().top + itemHeight -
that.$container.offset().top - that.$container.height();
if (offset > 0) {
2010-09-03 20:54:40 +00:00
if (that.$scrollbars.up.is(':hidden')) {
2010-02-05 15:26:23 +00:00
that.$scrollbars.up.show();
that.$container.height(that.$container.height() - itemHeight);
offset += itemHeight;
}
if (selected == that.items.length - 1) {
that.$scrollbars.down.hide();
that.$container.height(that.$container.height() + itemHeight);
} else {
that.$content.css({
2010-09-03 20:54:40 +00:00
top: ((parseInt(that.$content.css('top')) || 0) - offset) + 'px'
2010-02-05 15:26:23 +00:00
});
}
}
}
2010-02-02 16:03:11 +00:00
}
function selectPreviousItem() {
2010-02-05 15:26:23 +00:00
var offset,
selected = self.options.selected;
2010-09-03 20:54:40 +00:00
Ox.print('sPI', selected)
2010-02-05 17:37:15 +00:00
if (selected > - 1) {
if (!isFirstEnabledItem()) {
2010-09-03 20:54:40 +00:00
that.items[selected].removeClass('OxSelected');
2010-02-05 17:37:15 +00:00
do {
selected--;
2010-09-03 20:54:40 +00:00
} while (that.items[selected].options('disabled'))
2010-02-07 15:01:22 +00:00
selectItem(selected);
2010-02-05 15:26:23 +00:00
}
2010-02-05 17:37:15 +00:00
offset = that.items[selected].offset().top - that.$container.offset().top;
if (offset < 0) {
2010-09-03 20:54:40 +00:00
if (that.$scrollbars.down.is(':hidden')) {
2010-02-05 17:37:15 +00:00
that.$scrollbars.down.show();
that.$container.height(that.$container.height() - itemHeight);
}
if (selected == 0) {
that.$scrollbars.up.hide();
that.$container.height(that.$container.height() + itemHeight);
}
that.$content.css({
2010-09-03 20:54:40 +00:00
top: ((parseInt(that.$content.css('top')) || 0) - offset) + 'px'
2010-02-05 17:37:15 +00:00
});
2010-02-05 15:26:23 +00:00
}
}
2010-02-02 16:03:11 +00:00
}
2010-02-05 09:13:03 +00:00
function selectSubmenu() {
2010-09-03 20:54:40 +00:00
Ox.print('selectSubmenu', self.options.selected)
2010-02-05 17:37:15 +00:00
if (self.options.selected > -1) {
2010-09-03 20:54:40 +00:00
var submenu = that.submenus[that.items[self.options.selected].options('id')];
Ox.print('submenu', submenu, that.submenus);
2010-02-05 17:37:15 +00:00
if (submenu && submenu.hasEnabledItems()) {
submenu.gainFocus();
submenu.selectFirstItem();
2010-02-08 09:35:24 +00:00
} else if (self.options.mainmenu) {
self.options.mainmenu.selectNextMenu();
}
} else if (self.options.mainmenu) {
self.options.mainmenu.selectNextMenu();
2010-02-05 09:13:03 +00:00
}
}
function selectSupermenu() {
2010-09-03 20:54:40 +00:00
Ox.print('selectSupermenu', self.options.selected)
2010-02-05 09:13:03 +00:00
if (self.options.parent) {
2010-09-03 20:54:40 +00:00
self.options.selected > -1 && that.items[self.options.selected].trigger('mouseleave');
scrollMenuUp();
2010-02-05 09:13:03 +00:00
self.options.parent.gainFocus();
2010-02-08 09:35:24 +00:00
} else if (self.options.mainmenu) {
self.options.mainmenu.selectPreviousMenu();
2010-02-05 09:13:03 +00:00
}
}
self.onChange = function(key, value) {
2010-09-03 20:54:40 +00:00
if (key == 'items') {
2010-02-18 07:27:32 +00:00
constructItems(value);
2010-09-03 20:54:40 +00:00
} else if (key == 'selected') {
that.$content.find('.OxSelected').removeClass('OxSelected');
2010-02-18 09:11:47 +00:00
selectItem(value);
2010-02-18 07:27:32 +00:00
}
2010-02-05 09:13:03 +00:00
}
2010-02-20 03:42:03 +00:00
that.addItem = function(item, position) {
};
2010-02-19 16:13:22 +00:00
that.addItemAfter = function(item, id) {
2010-02-08 10:17:00 +00:00
};
2010-02-19 16:13:22 +00:00
that.addItemBefore = function(item, id) {
2010-02-08 10:17:00 +00:00
};
2010-07-07 12:36:12 +00:00
that.checkItem = function(id) {
2010-09-03 20:54:40 +00:00
var item = that.getItem(id);
if (item.options('group')) {
var position = getItemPositionById(id),
toggled = self.optionGroups[item.options('group')].toggle(position);
if (toggled.length) {
$.each(toggled, function(i, pos) {
that.items[pos].toggleChecked();
});
}
} else {
item.options({
checked: true
});
}
2010-07-07 12:36:12 +00:00
};
2010-02-10 16:37:26 +00:00
that.getItem = function(id) {
2010-09-08 16:35:34 +00:00
//Ox.print('id', id)
2010-09-03 20:54:40 +00:00
var ids = id.split('_'),
2010-07-07 12:36:12 +00:00
item;
if (ids.length == 1) {
$.each(that.items, function(i, v) {
2010-09-03 20:54:40 +00:00
if (v.options('id') == id) {
2010-07-07 12:36:12 +00:00
item = v;
return false;
}
});
if (!item) {
$.each(that.submenus, function(k, submenu) {
item = submenu.getItem(id);
return !item;
});
2010-02-10 20:02:58 +00:00
}
2010-07-07 12:36:12 +00:00
} else {
2010-09-03 20:54:40 +00:00
item = that.submenus[ids.shift()].getItem(ids.join('_'));
2010-07-07 12:36:12 +00:00
}
2010-02-19 16:13:22 +00:00
return item;
2010-02-10 16:37:26 +00:00
};
2010-09-03 20:54:40 +00:00
that.getSubmenu = function(id) {
var ids = id.split('_'),
submenu;
if (ids.length == 1) {
submenu = that.submenus[id];
} else {
submenu = that.submenus[ids.shift()].getSubmenu(ids.join('_'));
}
Ox.print('getSubmenu', id, submenu);
return submenu;
}
2010-02-05 09:13:03 +00:00
that.hasEnabledItems = function() {
var ret = false;
$.each(that.items, function(i, item) {
2010-09-03 20:54:40 +00:00
if (!item.options('disabled')) {
2010-02-05 09:13:03 +00:00
return ret = true;
}
});
return ret;
};
2010-02-02 16:03:11 +00:00
that.hideMenu = function() {
2010-09-03 20:54:40 +00:00
if (that.is(':hidden')) {
return;
}
2010-02-04 08:02:23 +00:00
$.each(that.submenus, function(i, submenu) {
2010-09-03 20:54:40 +00:00
if (submenu.is(':visible')) {
2010-02-04 08:02:23 +00:00
submenu.hideMenu();
2010-02-03 12:12:21 +00:00
return false;
}
});
2010-02-08 09:39:15 +00:00
selectItem(-1);
2010-02-05 17:37:15 +00:00
scrollMenuUp();
2010-09-03 20:54:40 +00:00
that.$scrollbars.up.is(':visible') && that.$scrollbars.up.hide();
that.$scrollbars.down.is(':visible') && that.$scrollbars.down.hide();
2010-02-05 15:42:52 +00:00
if (self.options.parent) {
2010-09-03 20:54:40 +00:00
//self.options.element.removeClass('OxSelected');
self.options.parent.options({
selected: -1
});
2010-02-05 15:42:52 +00:00
}
2010-02-09 05:43:36 +00:00
that.hide()
.loseFocus()
2010-09-03 20:54:40 +00:00
.triggerEvent('hide');
2010-02-09 05:43:36 +00:00
that.$layer.hide();
2010-02-08 09:35:24 +00:00
return that;
2010-02-05 09:13:03 +00:00
};
2010-02-08 10:17:00 +00:00
that.removeItem = function() {
};
2010-02-05 09:13:03 +00:00
that.selectFirstItem = function() {
selectNextItem();
2010-02-02 16:03:11 +00:00
};
that.showMenu = function() {
2010-09-03 20:54:40 +00:00
if (!that.is(':hidden')) {
return;
}
2010-02-09 05:43:36 +00:00
if (!self.options.parent && !that.$layer.parent().length) {
that.$layer.appendTo($body);
}
2010-09-03 08:47:40 +00:00
that.parent().length == 0 && that.appendTo($body);
2010-02-18 14:42:53 +00:00
that.css({
2010-09-03 20:54:40 +00:00
left: '-1000px',
top: '-1000px',
2010-02-18 14:42:53 +00:00
}).show();
2010-02-04 08:02:23 +00:00
var offset = self.options.element.offset(),
width = self.options.element.outerWidth(),
height = self.options.element.outerHeight(),
2010-09-03 20:54:40 +00:00
left = Ox.limit(
offset.left + self.options.offset.left + (self.options.side == 'bottom' ? 0 : width),
0, $window.width() - that.width()
),
top = offset.top + self.options.offset.top + (self.options.side == 'bottom' ? height : 0),
2010-02-18 14:42:53 +00:00
menuHeight = that.$content.outerHeight(); // fixme: why is outerHeight 0 when hidden?
2010-06-29 13:10:13 +00:00
menuMaxHeight = Math.floor($window.height() - top - 16);
2010-02-05 17:37:15 +00:00
if (self.options.parent) {
2010-02-18 14:24:17 +00:00
if (menuHeight > menuMaxHeight) {
2010-09-03 08:47:40 +00:00
top = Ox.limit(top - menuHeight + menuMaxHeight, self.options.parent.offset().top, top);
2010-02-18 14:24:17 +00:00
menuMaxHeight = Math.floor($window.height() - top - 16);
2010-02-05 17:37:15 +00:00
}
}
2010-02-18 14:42:53 +00:00
that.css({
2010-09-03 20:54:40 +00:00
left: left + 'px',
top: top + 'px'
2010-02-18 14:42:53 +00:00
});
2010-02-18 14:24:17 +00:00
if (menuHeight > menuMaxHeight) {
that.$container.height(menuMaxHeight - itemHeight - 8); // margin
2010-02-03 12:12:21 +00:00
that.$scrollbars.down.show();
2010-02-18 14:24:17 +00:00
} else {
that.$container.height(menuHeight);
2010-02-03 12:12:21 +00:00
}
2010-09-03 20:54:40 +00:00
if (!self.options.parent) {
2010-09-03 08:47:40 +00:00
that.gainFocus();
}
that.$layer.show();
2010-02-08 09:35:24 +00:00
return that;
2010-09-03 20:54:40 +00:00
//that.triggerEvent('show');
2010-02-02 16:03:11 +00:00
};
2010-02-03 12:12:21 +00:00
that.toggleMenu = function() {
2010-09-03 20:54:40 +00:00
that.is(':hidden') ? that.showMenu() : that.hideMenu();
2010-02-02 16:03:11 +00:00
};
return that;
2010-02-03 12:12:21 +00:00
};
2010-02-02 16:03:11 +00:00
Ox.MenuItem = function(options, self) {
var self = self || {},
2010-09-03 20:54:40 +00:00
that = new Ox.Element('tr', self)
2010-02-02 16:03:11 +00:00
.defaults({
2010-09-03 08:47:40 +00:00
bind: [], // fixme: what's this?
2010-02-04 08:02:23 +00:00
checked: null,
2010-02-02 16:03:11 +00:00
disabled: false,
2010-09-03 20:54:40 +00:00
group: '',
icon: '',
id: '',
2010-02-05 05:20:13 +00:00
items: [],
2010-09-03 20:54:40 +00:00
keyboard: '',
2010-02-03 12:12:21 +00:00
menu: null, // fixme: is passing the menu to 100s of menu items really memory-neutral?
2010-02-04 09:50:45 +00:00
position: 0,
2010-02-03 12:12:21 +00:00
title: [],
})
.options($.extend(options, {
keyboard: parseKeyboard(options.keyboard || self.defaults.keyboard),
title: Ox.makeArray(options.title || self.defaults.title)
}))
2010-09-03 20:54:40 +00:00
.addClass('OxItem' + (self.options.disabled ? ' OxDisabled' : ''))
2010-02-03 12:12:21 +00:00
.attr({
2010-09-03 20:54:40 +00:00
id: Ox.toCamelCase(self.options.menu.options('id') + '/' + self.options.id)
2010-02-03 12:12:21 +00:00
})
2010-09-03 20:54:40 +00:00
.data('group', self.options.group); // fixme: why?
2010-02-03 12:12:21 +00:00
2010-09-03 08:47:40 +00:00
if (self.options.group && self.options.checked === null) {
self.options.checked = false;
}
2010-02-03 12:12:21 +00:00
// construct
that.append(
2010-09-03 20:54:40 +00:00
that.$status = $('<td>', {
'class': 'OxCell OxStatus',
html: self.options.checked ? oxui.symbols.check : ''
2010-02-03 12:12:21 +00:00
})
)
.append(
2010-09-03 20:54:40 +00:00
that.$icon = $('<td>', {
'class': 'OxCell OxIcon'
2010-02-02 16:03:11 +00:00
})
2010-02-03 12:12:21 +00:00
.append(self.options.icon ?
2010-09-03 20:54:40 +00:00
$('<img>', {
2010-02-04 08:02:23 +00:00
src: self.options.icon
2010-02-03 12:12:21 +00:00
}) : null
)
)
.append(
2010-09-03 20:54:40 +00:00
that.$title = $('<td>', {
'class': 'OxCell OxTitle',
2010-02-03 12:12:21 +00:00
html: self.options.title[0]
})
)
.append(
2010-09-03 20:54:40 +00:00
$('<td>', {
'class': 'OxCell OxModifiers',
2010-02-03 12:12:21 +00:00
html: $.map(self.options.keyboard.modifiers, function(modifier) {
2010-02-08 09:45:48 +00:00
return oxui.symbols[modifier];
2010-09-03 20:54:40 +00:00
}).join('')
2010-02-03 12:12:21 +00:00
})
)
.append(
2010-09-03 20:54:40 +00:00
$('<td>', {
'class': 'OxCell Ox' + (self.options.items.length ? 'Submenu' : 'Key'),
2010-02-05 05:20:13 +00:00
html: self.options.items.length ? oxui.symbols.triangle_right :
2010-02-08 09:45:48 +00:00
oxui.symbols[self.options.keyboard.key] ||
self.options.keyboard.key.toUpperCase()
2010-02-03 12:12:21 +00:00
})
);
2010-02-03 12:12:21 +00:00
function parseKeyboard(str) {
2010-09-03 20:54:40 +00:00
var modifiers = str.split(' '),
2010-02-03 12:12:21 +00:00
key = modifiers.pop();
return {
modifiers: modifiers,
key: key
2010-07-05 17:01:42 +00:00
};
2010-02-03 12:12:21 +00:00
}
self.onChange = function(key, value) {
2010-09-03 20:54:40 +00:00
if (key == 'checked') {
that.$status.html(value ? oxui.symbols.check : '')
} else if (key == 'disabled') {
that.toggleClass('OxDisabled'); // fixme: this will only work if onChange is only invoked on actual change
} else if (key == 'title') {
2010-07-24 01:32:08 +00:00
self.options.title = Ox.makeArray(value);
that.$title.html(self.options.title[0]);
2010-02-03 12:12:21 +00:00
}
}
2010-02-10 16:37:26 +00:00
that.toggle = function() {
// toggle id and title
};
2010-02-03 12:12:21 +00:00
that.toggleChecked = function() {
2010-09-03 08:47:40 +00:00
that.options({
checked: !self.options.checked
});
2010-02-03 12:12:21 +00:00
};
that.toggleDisabled = function() {
};
that.toggleTitle = function() {
2010-09-03 20:54:40 +00:00
Ox.print('s.o.t', self.options.title)
2010-02-03 12:12:21 +00:00
that.options({
2010-07-24 01:32:08 +00:00
title: self.options.title.reverse()
2010-02-10 16:37:26 +00:00
});
2010-02-03 12:12:21 +00:00
};
2010-02-02 16:03:11 +00:00
return that;
2010-02-03 12:12:21 +00:00
};
2010-02-02 16:03:11 +00:00
/*
============================================================================
2010-01-07 20:21:07 +00:00
Panels
============================================================================
*/
2010-12-06 17:42:45 +00:00
/**
2010-01-07 20:21:07 +00:00
*/
Ox.CollapsePanel = function(options, self) {
var self = self || {},
that = new Ox.Panel({}, self)
.defaults({
collapsed: false,
2010-12-23 17:05:46 +00:00
menu: [],
2010-11-28 15:06:47 +00:00
size: 16,
2010-09-03 20:54:40 +00:00
title: ''
2010-01-07 20:21:07 +00:00
})
.options(options)
2010-09-03 20:54:40 +00:00
.addClass('OxCollapsePanel'),
title = self.options.collapsed ?
[{id: 'expand', title: 'expand'}, {id: 'collapse', title: 'collapse'}] :
[{id: 'collapse', title: 'collapse'}, {id: 'expand', title: 'expand'}],
2010-01-07 20:21:07 +00:00
$titlebar = new Ox.Bar({
2010-09-03 20:54:40 +00:00
orientation: 'horizontal',
2010-01-07 20:21:07 +00:00
size: self.options.size,
})
.dblclick(dblclickTitlebar)
.appendTo(that),
$switch = new Ox.Button({
2010-09-03 20:54:40 +00:00
id: self.options.id + 'Switch',
style: 'symbol',
title: title,
type: 'image',
2010-01-07 20:21:07 +00:00
})
.click(toggleCollapsed)
.appendTo($titlebar),
$title = new Ox.Element()
2010-09-03 20:54:40 +00:00
.addClass('OxTitle')
2010-01-07 20:21:07 +00:00
.html(self.options.title/*.toUpperCase()*/)
.appendTo($titlebar);
2010-12-23 17:05:46 +00:00
if (self.options.menu.length) {
that.$menu = new Ox.Select({
id: self.options.id + 'Menu',
items: self.options.menu,
max: 0,
min: 0,
type: 'image'
})
.bindEvent('click', clickMenu)
.appendTo($titlebar)
}
2010-01-07 20:21:07 +00:00
that.$content = new Ox.Element()
2010-09-03 20:54:40 +00:00
.addClass('OxContent')
2010-01-07 20:21:07 +00:00
.appendTo(that);
// fixme: doesn't work, content still empty
// need to hide it if collapsed
if (self.options.collapsed) {
that.$content.css({
2010-09-03 20:54:40 +00:00
marginTop: -that.$content.height() + 'px'
2010-01-07 20:21:07 +00:00
});
2010-12-23 17:05:46 +00:00
}
function clickMenu() {
2010-01-07 20:21:07 +00:00
}
function dblclickTitlebar(e) {
2010-09-03 20:54:40 +00:00
if (!$(e.target).hasClass('OxButton')) {
$switch.trigger('click');
2010-01-07 20:21:07 +00:00
}
}
function toggleCollapsed() {
2010-09-03 20:54:40 +00:00
var marginTop;
self.options.collapsed = !self.options.collapsed;
marginTop = self.options.collapsed ? -that.$content.height() : 0;
2010-01-07 20:21:07 +00:00
that.$content.animate({
2010-09-03 20:54:40 +00:00
marginTop: marginTop + 'px'
2010-01-07 20:21:07 +00:00
}, 200);
}
2010-09-03 20:54:40 +00:00
self.onChange = function(key, value) {
if (key == 'collapsed') {
} else if (key == 'title') {
2010-01-07 20:21:07 +00:00
$title.html(self.options.title);
}
};
return that;
};
2010-12-06 17:42:45 +00:00
/**
2010-01-07 20:21:07 +00:00
*/
Ox.Panel = function(options, self) {
var self = self || {},
that = new Ox.Element({}, self)
2010-09-03 20:54:40 +00:00
.addClass('OxPanel');
2010-01-07 20:21:07 +00:00
return that;
};
2010-12-06 17:42:45 +00:00
/**
options:
elements: [{ array of one, two or three elements
collapsible: false, collapsible or not (only for outer elements)
collapsed: false, collapsed or not (only for collapsible elements)
element: {}, OxElement (if any element is resizable or
collapsible, all OxElements must have an id)
resizable: false, resizable or not (only for outer elements)
resize: [], array of sizes (only for resizable elements,
first value is min, last value is max,
other values are 'snappy' points in between)
size: 0 size in px (one element must have no size)
}],
orientation: '' 'horizontal' or 'vertical'
events:
resize
toggle
2010-01-07 20:21:07 +00:00
*/
Ox.SplitPanel = function(options, self) {
var self = self || {},
that = new Ox.Element({}, self) // fixme: Container
2010-01-07 20:21:07 +00:00
.defaults({
elements: [],
2010-09-03 20:54:40 +00:00
orientation: 'horizontal'
2010-01-07 20:21:07 +00:00
})
.options(options || {})
2010-09-03 20:54:40 +00:00
.addClass('OxSplitPanel');
2010-07-17 08:46:27 +00:00
$.extend(self, {
dimensions: oxui.getDimensions(self.options.orientation),
edges: oxui.getEdges(self.options.orientation),
2010-09-17 16:37:11 +00:00
length: self.options.elements.length,
resizebarElements: [],
2010-09-17 16:37:11 +00:00
$resizebars: []
2010-07-17 08:46:27 +00:00
});
2010-07-06 18:28:58 +00:00
// create elements
2010-07-06 18:28:58 +00:00
that.$elements = [];
self.options.elements.forEach(function(v, i) {
2010-07-17 08:46:27 +00:00
self.options.elements[i] = $.extend({
collapsible: false,
collapsed: false,
resizable: false,
resize: [],
2010-09-03 20:54:40 +00:00
size: 'auto'
2010-07-17 08:46:27 +00:00
}, v);
2010-07-06 18:28:58 +00:00
that.$elements[i] = v.element
.css(self.edges[2], (parseInt(v.element.css(self.edges[2])) || 0) + 'px')
.css(self.edges[3], (parseInt(v.element.css(self.edges[3])) || 0) + 'px');
2010-07-06 18:28:58 +00:00
});
// create resizebars
self.options.elements.forEach(function(v, i) {
2010-01-07 20:21:07 +00:00
//that.append(element)
2010-09-03 20:54:40 +00:00
//Ox.print('V: ', v, that.$elements[i])
var index = i == 0 ? 0 : 1;
2010-09-03 08:47:40 +00:00
that.$elements[i].appendTo(that.$element); // fixme: that.$content
2010-07-06 18:28:58 +00:00
if (v.collapsible || v.resizable) {
2010-09-03 20:54:40 +00:00
Ox.print('v.size', v.size)
self.resizebarElements[index] = i < 2 ? [0, 1] : [1, 2];
self.$resizebars[index] = new Ox.Resizebar({
collapsible: v.collapsible,
edge: self.edges[index],
elements: [
that.$elements[self.resizebarElements[index][0]],
that.$elements[self.resizebarElements[index][1]]
],
id: v.element.options('id'),
orientation: self.options.orientation == 'horizontal' ? 'vertical' : 'horizontal',
parent: that, // fixme: that.$content
resizable: v.resizable,
resize: v.resize,
size: v.size
});
self.$resizebars[index][i == 0 ? 'insertAfter' : 'insertBefore'](that.$elements[i]);
2010-07-06 18:28:58 +00:00
}
2010-01-07 20:21:07 +00:00
});
2010-07-06 18:28:58 +00:00
self.options.elements.forEach(function(v, i) {
v.collapsed && that.css(
self.edges[i == 0 ? 0 : 1], -self.options.elements[i].size + 'px'
);
});
setSizes(true);
2010-09-17 16:37:11 +00:00
2010-07-07 07:18:38 +00:00
function getPositionById(id) {
var position = -1;
$.each(self.options.elements, function(i, element) {
2010-09-03 20:54:40 +00:00
if (element.element.options('id') == id) {
2010-07-17 08:46:27 +00:00
position = i;
2010-07-07 07:18:38 +00:00
return false;
}
});
2010-09-03 20:54:40 +00:00
Ox.print('getPositionById', id, position);
2010-07-07 07:18:38 +00:00
return position;
}
2010-07-06 18:28:58 +00:00
function getSize(element) {
return element.size + (element.collapsible || element.resizable);
//return (element.size + (element.collapsible || element.resizable)) * !element.collapsed;
}
function getVisibleSize(element) {
return getSize(element) * !element.collapsed;
2010-07-06 18:28:58 +00:00
}
function setSizes(init) {
self.options.elements.forEach(function(v, i) {
var edges = [
(init && parseInt(that.$elements[i].css(self.edges[0]))) || 0,
(init && parseInt(that.$elements[i].css(self.edges[1]))) || 0
];
2010-09-03 20:54:40 +00:00
v.size != 'auto' && that.$elements[i].css(self.dimensions[0], v.size + 'px');
2010-07-07 07:18:38 +00:00
if (i == 0) {
that.$elements[i].css(
self.edges[0], edges[0] + 'px'
);
2010-11-28 15:06:47 +00:00
that.$elements[i].css(
2010-09-03 20:54:40 +00:00
self.edges[1], (getSize(self.options.elements[1]) + (length == 3 ? getSize(self.options.elements[2]) : 0)) + 'px'
2010-07-07 07:18:38 +00:00
);
} else if (i == 1) {
2010-11-28 15:06:47 +00:00
that.$elements[i].css(
self.edges[0], self.options.elements[0].size == 'auto' ? 'auto' :
edges[0] + getSize(self.options.elements[0]) + 'px'
2010-07-07 07:18:38 +00:00
);
2010-09-03 20:54:40 +00:00
(self.options.elements[0].size != 'auto' || v.size != 'auto') && that.$elements[i].css(
self.edges[1], (self.length == 3 ? getSize(self.options.elements[2]) : 0) + 'px'
2010-07-07 07:18:38 +00:00
);
} else {
that.$elements[i].css(
self.edges[0], v.size == 'auto' ? 'auto' :
(getVisibleSize(self.options.elements[0]) + getVisibleSize(self.options.elements[1])) + 'px'
2010-11-28 15:06:47 +00:00
);
that.$elements[i].css(
self.edges[1], edges[1] + 'px'
2010-07-07 07:18:38 +00:00
);
}
2010-09-17 16:37:11 +00:00
if (v.collapsible || v.resizable) {
self.$resizebars[i == 0 ? 0 : 1].css(self.edges[i == 0 ? 0 : 1], v.size);
}
2010-07-07 07:18:38 +00:00
});
}
2010-07-17 08:46:27 +00:00
that.isCollapsed = function(id) {
2010-11-28 15:06:47 +00:00
var pos = Ox.isNumber(id) ? id : getPositionById(id);
return self.options.elements[pos].collapsed;
2010-07-17 08:46:27 +00:00
};
2011-01-03 12:01:38 +00:00
that.replace = function(id, element) { // fixme: should be replaceElement
2010-09-03 20:54:40 +00:00
// one can pass pos instead of id
var pos = Ox.isNumber(id) ? id : getPositionById(id);
Ox.print('replace', pos, element);
2010-09-03 20:54:40 +00:00
Ox.print('element', self.options.elements[pos].element, element)
self.options.elements[pos].element.replaceWith(element.$element.$element || element.$element);
self.options.elements[pos].element = element;
that.$elements[pos] = element
.css(self.edges[2], (parseInt(element.css(self.edges[2])) || 0) + 'px')
.css(self.edges[3], (parseInt(element.css(self.edges[3])) || 0) + 'px');
2010-09-03 20:54:40 +00:00
setSizes();
self.$resizebars.forEach(function($resizebar, i) {
$resizebar.options({
elements: [
that.$elements[self.resizebarElements[i][0]],
that.$elements[self.resizebarElements[i][1]]
]
});
});
2010-09-03 20:54:40 +00:00
Ox.print(self.options.elements[pos])
2010-12-29 06:50:40 +00:00
return that;
2010-09-03 20:54:40 +00:00
};
that.replaceElements = function(elements) {
elements.forEach(function(element, i) {
if (Ox.isNumber(element.size)) {
that.size(i, element.size);
if (element.collapsible || element.resizable) {
self.$resizebars[i == 0 ? 0 : 1].options({
collapsible: element.collapsible,
resizable: element.resizable,
size: element.size
});
}
}
that.replace(i, element.element);
});
self.options.elements = elements;
self.$resizebars.forEach(function($resizebar, i) {
$resizebar.options({
elements: [
that.$elements[self.resizebarElements[i][0]],
that.$elements[self.resizebarElements[i][1]]
]
});
});
2010-12-29 06:50:40 +00:00
return that;
}
2010-11-25 10:05:50 +00:00
that.size = function(id, size) {
2010-07-07 07:18:38 +00:00
// one can pass pos instead of id
var pos = Ox.isNumber(id) ? id : getPositionById(id),
element = self.options.elements[pos];
2010-11-28 15:06:47 +00:00
if (arguments.length == 1) {
return element.element[self.dimensions[0]]() * !that.isCollapsed(pos);
2010-11-28 15:06:47 +00:00
} else {
element.size = size;
2010-11-28 15:06:47 +00:00
setSizes();
return that;
}
2010-07-07 07:18:38 +00:00
};
2010-07-17 08:46:27 +00:00
that.toggle = function(id) {
// one can pass pos instead of id
2010-09-03 20:54:40 +00:00
Ox.print('toggle', id);
var pos = Ox.isNumber(id) ? id : getPositionById(id),
2010-11-28 15:06:47 +00:00
element = self.options.elements[pos],
value = parseInt(that.css(self.edges[pos == 0 ? 0 : 1])) +
element.element[self.dimensions[0]]() *
(element.collapsed ? 1 : -1),
2010-07-17 08:46:27 +00:00
animate = {};
2010-11-28 15:06:47 +00:00
animate[self.edges[pos == 0 ? 0 : 1]] = value;
Ox.print('animate', animate)
2010-11-28 15:06:47 +00:00
that.animate(animate, 200, function() {
2011-01-03 14:00:28 +00:00
element.collapsed = !element.collapsed;
element.element.triggerEvent('toggle', {
'collapsed': element.collapsed
});
element = self.options.elements[pos == 0 ? 1 : pos - 1];
element.element.triggerEvent(
2010-11-28 15:06:47 +00:00
'resize',
2011-01-03 14:00:28 +00:00
element.element[self.dimensions[0]]()
2010-11-28 15:06:47 +00:00
);
2010-07-17 08:46:27 +00:00
});
};
that.updateSize = function(pos, size) {
// this is called from resizebar
var pos = pos == 0 ? 0 : self.options.elements.length - 1; // fixme: silly that 0 or 1 is passed, and not pos
self.options.elements[pos].size = size;
}
2010-01-07 20:21:07 +00:00
return that;
2010-07-06 18:28:58 +00:00
2010-01-07 20:21:07 +00:00
};
2010-02-10 16:37:26 +00:00
Ox.TabPanel = function(options, self) {
};
2010-02-20 08:29:03 +00:00
/*
============================================================================
Requests
============================================================================
*/
2010-01-27 12:30:00 +00:00
2010-12-06 17:42:45 +00:00
/**
2010-02-20 08:29:03 +00:00
*/
Ox.LoadingIcon = function(options, self) {
var self = self || {},
2010-09-03 20:54:40 +00:00
that = new Ox.Element('img', self)
2010-02-20 08:29:03 +00:00
.defaults({
2010-09-03 20:54:40 +00:00
size: 'medium'
2010-02-20 08:29:03 +00:00
})
.options(options || {})
.attr({
2010-09-03 20:54:40 +00:00
src: oxui.path + '/png/ox.ui.' + Ox.theme() + '/loading.png' // fixme: oxui.themePath needed?
2010-02-20 08:29:03 +00:00
})
.addClass(
2010-09-03 20:54:40 +00:00
'OxLoadingIcon Ox' + Ox.toTitleCase(self.options.size)
2010-02-20 08:29:03 +00:00
);
self.deg = 0;
2010-07-01 23:51:08 +00:00
self.interval = 0;
self.isRunning = false;
function clear() {
clearInterval(self.interval);
self.deg = 0;
self.interval = 0;
update();
}
2010-02-20 08:29:03 +00:00
function update() {
that.css({
2010-09-03 20:54:40 +00:00
MozTransform: 'rotate(' + self.deg + 'deg)',
WebkitTransform: 'rotate(' + self.deg + 'deg)'
2010-02-20 08:29:03 +00:00
});
}
that.start = function() {
2010-07-01 23:51:08 +00:00
self.isRunning = true;
clear();
that.animate({
opacity: 1
}, 250);
self.interval = setInterval(function() {
self.deg = (self.deg + 30) % 360;
update();
}, 83);
2010-09-03 08:47:40 +00:00
return that;
2010-02-20 08:29:03 +00:00
};
that.stop = function() {
2010-07-01 23:51:08 +00:00
that.animate({
opacity: 0
}, 250, function() {
self.isRunning && clear();
2010-07-01 23:51:08 +00:00
self.isRunning = false;
});
2010-09-03 08:47:40 +00:00
return that;
2010-02-20 08:29:03 +00:00
}
return that;
}
2010-12-06 17:42:45 +00:00
/**
2010-02-20 08:29:03 +00:00
Ox.Progressbar
*/
2010-01-27 12:30:00 +00:00
2010-09-17 22:10:07 +00:00
/*
============================================================================
Video
============================================================================
*/
2010-12-06 17:42:45 +00:00
/**
*/
2010-09-17 22:10:07 +00:00
Ox.LargeTimeline = function(options, self) {
var self = self || {},
that = new Ox.Element('div', self)
.defaults({
cuts: [],
duration: 0,
find: '',
matches: [],
points: [0, 0],
position: 0,
style: 'default',
subtitles: [],
videoId: '',
width: 0
})
.options(options || {})
.addClass('OxTimelineLarge')
.mousedown(mousedown)
.mouseleave(mouseleave)
.mousemove(mousemove);
$.extend(self, {
$cuts: [],
$markerPoint: [],
$subtitles: [],
$tiles: {},
$tooltip: new Ox.Tooltip(),
center: parseInt(self.options.width / 2),
element: that.$element[0],
fps: 25,
height: 64,
tileWidth: 1500
});
self.tiles = self.options.duration * self.fps / self.tileWidth;
self.$timeline = $('<div>')
.css({
left: self.center + 'px'
})
.appendTo(that.$element)
$.each(self.options.subtitles, function(i, v) {
self.$subtitles[i] = $('<div>')
.addClass('OxSubtitle' + (self.options.matches.indexOf(i) > -1 ? ' OxHighlight' : ''))
.css({
left: (v['in'] * self.fps) + 'px',
width: (((v['out'] - v['in']) * self.fps) - 4) + 'px'
})
.html(Ox.highlight(v.text, self.options.find))
.appendTo(self.$timeline)
});
$.each(self.options.cuts, function(i, v) {
self.$cuts[i] = $('<img>')
.addClass('OxCut')
.attr({
src: '/static/oxjs/build/png/ox.ui/videoMarkerCut.png'
})
.css({
left: (v * self.fps) + 'px'
})
.appendTo(self.$timeline)
});
self.$markerPosition = $('<img>')
.addClass('OxMarkerPosition')
.attr({
src: '/static/oxjs/build/png/ox.ui/videoMarkerPlay.png'
})
.appendTo(that.$element);
setMarker();
$.each(['In', 'Out'], function(i, v) {
self.$markerPoint[i] = $('<img>')
.addClass('OxMarkerPoint' + v)
.attr({
src: '/static/oxjs/build/png/ox.ui/videoMarker' + v + '.png'
})
.appendTo(self.$timeline);
setMarkerPoint(i);
});
setWidth();
setPosition();
function mousedown(e) {
var mousemove = false,
x = e.clientX;
$window.mousemove(function(e) {
mousemove = true;
self.options.position = Ox.limit(
self.options.position + (x - e.clientX) / self.fps,
0, self.options.duration
);
x = e.clientX;
setPosition();
that.triggerEvent('change', {
position: self.options.position
});
});
$window.one('mouseup', function() {
$window.unbind('mousemove');
if (!mousemove) {
self.options.position = Ox.limit(
self.options.position + (e.clientX - that.$element.offset().left - self.center) / self.fps,
0, self.options.duration
);
setPosition();
}
that.triggerEvent('change', {
position: self.options.position
});
});
e.preventDefault();
}
function mouseleave(e) {
2010-09-18 13:35:22 +00:00
self.clientX = 0;
self.clientY = 0;
2010-09-17 22:10:07 +00:00
self.$tooltip.hide();
}
function mousemove(e) {
self.clientX = e.clientX;
self.clientY = e.clientY;
updateTooltip();
2010-09-17 22:10:07 +00:00
}
function setMarkerPoint(i) {
self.$markerPoint[i].css({
left: (self.options.points[i] * self.fps) + 'px'
});
}
function setMarker() {
self.$markerPosition.css({
left: (self.center - 4) + 'px',
});
}
function setPosition() {
self.tile = parseInt(self.options.position * self.fps / self.tileWidth);
self.$timeline.css({
marginLeft: (-self.options.position * self.fps) + 'px'
});
$.each(Ox.range(Math.max(self.tile - 1, 0), Math.min(self.tile + 2, self.tiles)), function(i, v) {
if (!self.$tiles[v]) {
self.$tiles[v] = $('<img>')
.attr({
src: '/' + self.options.videoId + '/timelines/' +
(self.options.style == 'default' ? 'timeline' : self.options.style) + '.64.' + v + '.png'
})
.css({
left: (v * self.tileWidth) + 'px'
})
.appendTo(self.$timeline);
}
});
2010-09-18 13:35:22 +00:00
if (self.clientX && self.clientY) {
updateTooltip();
}
2010-09-17 22:10:07 +00:00
}
function setWidth() {
self.center = parseInt(self.options.width / 2);
that.css({
width: self.options.width + 'px'
});
self.$timeline.css({
left: self.center + 'px'
});
setMarker();
}
function updateTooltip() {
var position = self.options.position + (self.clientX - that.offset().left - self.center) / self.fps;
if (position >= 0 && position <= self.options.duration) {
self.$tooltip
.options({
title: Ox.formatDuration(position, 3)
})
.show(self.clientX, self.clientY);
} else {
self.$tooltip.hide();
}
}
2010-09-17 22:10:07 +00:00
self.onChange = function(key, value) {
if (key == 'points') {
setMarkerPoint(0);
setMarkerPoint(1);
} else if (key == 'position') {
setPosition();
} else if (key == 'width') {
setWidth();
}
};
return that;
};
2010-12-06 17:42:45 +00:00
/**
*/
2010-09-17 22:10:07 +00:00
Ox.SmallTimeline = function(options, self) {
var self = self || {},
that = new Ox.Element('div', self)
.defaults({
cuts: [],
duration: 0,
find: '',
matches: [],
points: [0, 0],
position: 0,
subtitles: [],
videoId: '',
width: 0
})
.options(options || {})
.addClass('OxTimelineSmall')
.mousedown(mousedown)
.mouseleave(mouseleave)
.mousemove(mousemove);
$.extend(self, {
$images: [],
$lines: [],
$markerPoint: [],
$subtitles: [],
hasSubtitles: self.options.subtitles.length,
height: 16,
lines: Math.ceil(self.options.duration / self.options.width),
margin: 8
});
2010-11-25 10:05:50 +00:00
that.css({
2010-11-28 15:06:47 +00:00
width: (self.options.width + self.margin) + 'px',
2010-11-25 10:05:50 +00:00
height: ((self.height + self.margin) * self.lines + 4) + 'px'
});
2010-09-17 22:10:07 +00:00
getTimelineImageURL(function(url) {
self.timelineImageURL = url;
$.each(Ox.range(0, self.lines), function(i) {
addLine(i);
});
self.$markerPosition = $('<img>')
.addClass('OxMarkerPosition')
.attr({
src: '/static/oxjs/build/png/ox.ui/videoMarkerPlay.png'
})
.css({
position: 'absolute',
width: '9px',
height: '5px',
zIndex: 10
})
.appendTo(that.$element);
setPosition();
$.each(['in', 'out'], function(i, v) {
var titleCase = Ox.toTitleCase(v);
self.$markerPoint[i] = $('<img>')
.addClass('OxMarkerPoint' + titleCase)
.attr({
src: '/static/oxjs/build/png/ox.ui/videoMarker' + titleCase + '.png'
})
.appendTo(that.$element);
setMarkerPoint(i);
});
});
function addLine(i) {
self.$lines[i] = new Ox.Element('div')
.css({
top: i * (self.height + self.margin) + 'px',
width: self.options.width + 'px'
})
.appendTo(that);
self.$images[i] = $('<img>')
.addClass('OxTimelineSmallImage')
.attr({
src: self.timelineImageURL
})
.css({
marginLeft: (-i * self.options.width) + 'px'
})
.appendTo(self.$lines[i].$element)
if (self.hasSubtitles) {
self.subtitlesImageURL = getSubtitlesImageURL();
self.$subtitles[i] = $('<img>')
.attr({
src: self.subtitlesImageURL
})
.css({
marginLeft: (-i * self.options.width) + 'px'
})
.appendTo(self.$lines[i].$element);
}
}
function getPosition(e) {
//FIXME: this might still be broken in opera according to http://acko.net/blog/mouse-handling-and-absolute-positions-in-javascript
2010-09-17 22:16:42 +00:00
return (e.offsetX ? e.offsetX : e.clientX - $(e.target).offset().left);
2010-09-17 22:10:07 +00:00
}
function getSubtitle(position) {
var subtitle = null;
$.each(self.options.subtitles, function(i, v) {
if (v['in'] <= position && v['out'] >= position) {
subtitle = v;
return false;
}
});
return subtitle;
}
function getSubtitlesImageURL() {
var height = 18,
width = Math.ceil(self.options.duration),
$canvas = $('<canvas>')
.attr({
height: height,
width: width
}),
canvas = $canvas[0],
context = canvas.getContext('2d'),
imageData = context.createImageData(width, height),
data = imageData.data;
$.each(self.options.subtitles, function(i, v) {
var color = self.options.matches.indexOf(i) > -1 ? [255, 255, 0] : [255, 255, 255]
2010-09-17 22:10:07 +00:00
$.each(Ox.range(
Math.round(v['in']),
Math.round(v['out']) + 1
), function(i, x) {
$.each(Ox.range(0, 18), function(i, y) {
var index = x * 4 + y * 4 * width;
data[index] = color[0];
data[index + 1] = color[1];
data[index + 2] = color[2];
data[index + 3] = (y == 0 || y == 17) ? 255 : 128
});
});
});
context.putImageData(imageData, 0, 0);
return canvas.toDataURL();
}
function getTimelineImageURL(callback) {
var height = 16,
images = Math.ceil(self.options.duration / 3600),
loaded = 0,
width = Math.ceil(self.options.duration),
$canvas = $('<canvas>')
.attr({
height: height,
width: width
}),
canvas = $canvas[0],
context = canvas.getContext('2d');
Ox.range(images).forEach(function(i) {
var $img = $('<img>')
.attr({
src: '/' + self.options.videoId + '/timelines/timeline.16.' + i + '.png'
})
.load(function() {
context.drawImage($img[0], i * 3600, 0);
Ox.print('loaded, images', loaded, images, $img[0])
if (++loaded == images) {
Ox.print('callback', canvas.toDataURL().length)
callback(canvas.toDataURL());
}
});
});
}
function mousedown(e) {
if ($(e.target).is('img')) {
self.options.position = getPosition(e);
setPosition();
that.triggerEvent('change', {
position: self.options.position
});
}
$window.mousemove(function(e) {
if ($(e.target).is('img')) {
self.options.position = getPosition(e);
setPosition();
that.triggerEvent('change', {
position: self.options.position
});
}
});
$window.one('mouseup', function() {
$window.unbind('mousemove');
})
e.preventDefault();
}
function mouseleave(e) {
self.$tooltip.hide();
}
function mousemove(e) {
var $target = $(e.target),
position,
subtitle;
if ($target.is('img')) {
//FIXME: this might still be broken in opera according to http://acko.net/blog/mouse-handling-and-absolute-positions-in-javascript
position = getPosition(e),
subtitle = getSubtitle(position);
Ox.print('position', position, e)
self.$tooltip = new Ox.Tooltip({
title: subtitle ?
'<span class=\'OxBright\'>' +
Ox.highlight(subtitle.text, self.options.find).replace(/\n/g, '<br/>') + '</span><br/>' +
Ox.formatDuration(subtitle['in'], 3) + ' - ' + Ox.formatDuration(subtitle['out'], 3) :
Ox.formatDuration(position, 3)
})
.css({
textAlign: 'center'
})
.show(e.clientX, e.clientY);
}
}
function setMarker() {
self.$markerPosition
.css({
left: (self.options.position % self.options.width) + 'px',
top: (parseInt(self.options.position / self.options.width) * (self.height + self.margin) + 2) + 'px',
});
}
function setMarkerPoint(i) {
var position = self.options.points[i];
self.$markerPoint[i]
.css({
left: (position % self.options.width) + 'px',
top: (parseInt(position / self.options.width) * (self.height + self.margin) + 16) + 'px',
});
}
function setPosition() {
self.options.position = Ox.limit(self.options.position, 0, self.options.duration);
setMarker();
}
function setWidth() {
self.lines = Math.ceil(self.options.duration / self.options.width);
2010-11-28 15:06:47 +00:00
that.css({
width: (self.options.width + self.margin) + 'px',
height: ((self.height + self.margin) * self.lines + 4) + 'px'
});
2010-09-17 22:10:07 +00:00
$.each(Ox.range(self.lines), function(i) {
if (self.$lines[i]) {
self.$lines[i].css({
width: self.options.width + 'px'
});
self.$images[i].css({
marginLeft: (-i * self.options.width) + 'px'
});
if (self.hasSubtitles) {
self.$subtitles[i].css({
marginLeft: (-i * self.options.width) + 'px'
});
}
} else {
addLine(i);
}
});
while (self.$lines.length > self.lines) {
self.$lines[self.$lines.length - 1].remove();
self.$lines.pop();
}
setMarker();
setMarkerPoint(0);
setMarkerPoint(1);
}
self.onChange = function(key, value) {
Ox.print('onChange:', key, value)
2010-09-17 22:10:07 +00:00
if (key == 'points') {
Ox.print('key', key, 'value', value)
2010-09-17 22:10:07 +00:00
setMarkerPoint(0);
setMarkerPoint(1);
} else if (key == 'position') {
setPosition();
} else if (key == 'width') {
setWidth();
}
};
return that;
};
2010-12-06 17:42:45 +00:00
/**
*/
2010-09-17 22:10:07 +00:00
Ox.VideoEditor = function(options, self) {
var self = self || {},
that = new Ox.Element('div', self)
.defaults({
cuts: [],
duration: 0,
find: '',
frameURL: function() {},
2010-11-25 10:05:50 +00:00
height: 0,
2010-09-17 22:10:07 +00:00
largeTimeline: true,
matches: [],
points: [0, 0],
position: 0,
posterFrame: 0,
subtitles: [],
videoHeight: 0,
videoId: '',
videoWidth: 0,
videoSize: 'small',
videoURL: '',
width: 0
})
.options(options || {})
2011-01-03 12:01:38 +00:00
.addClass('OxEditor')
2010-12-31 11:01:35 +00:00
/*.css({
2010-11-28 15:06:47 +00:00
//height: self.options.height + 'px',
overflowY: 'scroll',
//width: self.options.width + 'px'
2010-12-31 11:01:35 +00:00
});*/
2010-09-17 22:10:07 +00:00
2011-01-03 12:01:38 +00:00
that.click(that.gainFocus)
2010-09-17 22:10:07 +00:00
$.extend(self, {
$player: [],
$timeline: [],
controlsHeight: 16,
margin: 8,
videoRatio: self.options.videoWidth / self.options.videoHeight
});
self.sizes = getSizes();
$.each(['play', 'in', 'out'], function(i, type) {
self.$player[i] = new Ox.VideoPlayer({
duration: self.options.duration,
find: self.options.find,
height: self.sizes.player[i].height,
id: 'player' + Ox.toTitleCase(type),
points: self.options.points,
position: type == 'play' ? self.options.position : self.options.points[type == 'in' ? 0 : 1],
posterFrame: self.options.posterFrame,
subtitles: self.options.subtitles,
type: type,
url: type == 'play' ? self.options.videoURL : self.options.frameURL,
2010-09-17 22:10:07 +00:00
width: self.sizes.player[i].width
})
.css({
left: self.sizes.player[i].left + 'px',
top: self.sizes.player[i].top + 'px'
})
.appendTo(that);
if (type == 'in' || type == 'out') {
self.$player[i].bindEvent({
change: function() {
goToPoint(type);
},
set: function() {
setPoint(type);
}
})
}
});
self.$player[0].bindEvent({
change: changePlayer,
togglesize: togglePlayerSize
});
2010-09-17 22:10:07 +00:00
self.$timeline[0] = new Ox.LargeTimeline({
cuts: self.options.cuts,
duration: self.options.duration,
find: self.options.find,
id: 'timelineLarge',
matches: self.options.matches,
points: self.options.points,
position: self.options.position,
subtitles: self.options.subtitles,
videoId: self.options.videoId,
width: self.sizes.timeline[0].width
})
.css({
left: self.sizes.timeline[0].left + 'px',
top: self.sizes.timeline[0].top + 'px'
})
.bindEvent('change', changeTimelineLarge)
.bindEvent('changeEnd', changeTimelineLarge)
.appendTo(that);
self.$timeline[1] = new Ox.SmallTimeline({
cuts: self.options.cuts,
duration: self.options.duration,
find: self.options.find,
id: 'timelineSmall',
matches: self.options.matches,
points: self.options.points,
position: self.options.position,
subtitles: self.options.subtitles,
videoId: self.options.videoId,
width: self.sizes.timeline[1].width
})
.css({
left: self.sizes.timeline[1].left + 'px',
top: self.sizes.timeline[1].top + 'px'
})
.bindEvent('change', changeTimelineSmall)
.appendTo(that);
2010-12-26 20:16:35 +00:00
that.bindEvent({
2010-09-17 22:10:07 +00:00
key_alt_left: function() {
movePositionTo('cut', -1);
2010-09-17 22:10:07 +00:00
},
key_alt_right: function() {
movePositionTo('cut', 1);
2010-09-17 22:10:07 +00:00
},
key_alt_shift_left: function() {
},
key_alt_shift_right: function() {
},
key_closebracket: function() {
goToPoint('out');
},
key_comma: function() {
movePositionTo('subtitle', -1);
},
key_dot: function() {
movePositionTo('subtitle', 1);
},
key_down: function() {
movePositionBy(self.options.width - 2 * self.margin);
},
key_i: function() {
setPoint('in');
},
key_left: function() {
movePositionBy(-1);
},
key_m: toggleMute,
key_o: function() {
setPoint('out');
},
key_openbracket: function() {
goToPoint('in');
},
key_p: playInToOut,
key_right: function() {
movePositionBy(1);
},
key_shift_comma: function() {
movePositionTo('match', -1)
},
key_shift_dot: function() {
movePositionTo('match', 1)
},
key_shift_down: function() {
movePositionBy(self.options.duration);
},
key_shift_left: function() {
movePositionBy(-0.04);
//movePositionBy(-60);
2010-09-17 22:10:07 +00:00
},
key_shift_right: function() {
movePositionBy(0.04);
//movePositionBy(60);
2010-09-17 22:10:07 +00:00
},
key_shift_up: function() {
movePositionBy(-self.options.duration);
},
key_space: togglePlay,
key_up: function() {
movePositionBy(-(self.options.width - 2 * self.margin));
}
});
2011-01-03 12:01:38 +00:00
// that.gainFocus();
2010-09-17 22:10:07 +00:00
function changePlayer(event, data) {
self.options.position = data.position;
self.$timeline[0].options({
position: data.position
});
self.$timeline[1].options({
position: data.position
});
}
function changeTimelineLarge(event, data) {
self.options.position = data.position;
self.$player[0].options({
position: data.position
});
self.$timeline[1].options({
position: data.position
});
}
function changeTimelineSmall(event, data) {
self.options.position = data.position;
self.$player[0].options({
position: data.position
});
self.$timeline[0].options({
position: data.position
});
}
function getNextPosition(type, direction) {
var found = false,
position = 0,
positions;
if (type == 'cut') {
positions = self.options.cuts;
} else if (type == 'match') {
positions = $.map(self.options.matches, function(v, i) {
return self.options.subtitles[v]['in'];
});
} else if (type == 'subtitle') {
positions = $.map(self.options.subtitles, function(v, i) {
return v['in'];
});
}
direction == -1 && positions.reverse();
$.each(positions, function(i, v) {
if (direction == 1 ? v > self.options.position : v < self.options.position) {
position = v;
found = true;
return false;
}
});
direction == -1 && positions.reverse();
if (!found) {
position = positions[direction == 1 ? 0 : positions.length - 1];
}
return position;
}
2010-12-31 11:01:35 +00:00
function getSizes(scrollbarIsVisible) {
Ox.print('getSizes', scrollbarIsVisible)
2010-11-25 10:05:50 +00:00
var scrollbarWidth = oxui.scrollbarSize,
2010-12-31 11:01:35 +00:00
contentWidth = self.options.width - (scrollbarIsVisible ? scrollbarWidth : 0),
lines,
2010-11-25 10:05:50 +00:00
size = {
2010-09-17 22:10:07 +00:00
player: [],
timeline: []
},
width, widths;
if (self.options.videoSize == 'small') {
width = 0;
2010-11-25 10:05:50 +00:00
widths = Ox.divideInt(contentWidth - 4 * self.margin, 3);
[1, 0, 2].forEach(function(v, i) {
size.player[v] = {
2010-09-17 22:10:07 +00:00
left: (i + 0.5) * self.margin + width,
top: self.margin / 2,
width: widths[i],
height: Math.round(widths[1] / self.videoRatio)
}
width += widths[i];
});
} else {
size.player[0] = {
left: self.margin / 2,
top: self.margin / 2,
2010-11-25 10:05:50 +00:00
width: Math.round((contentWidth - 3 * self.margin + (self.controlsHeight + self.margin) / 2 * self.videoRatio) * 2/3),
2010-09-17 22:10:07 +00:00
}
size.player[0].height = Math.round(size.player[0].width / self.videoRatio);
size.player[1] = {
left: size.player[0].left + size.player[0].width + self.margin,
top: size.player[0].top,
2010-11-25 10:05:50 +00:00
width: contentWidth - 3 * self.margin - size.player[0].width
2010-09-17 22:10:07 +00:00
}
size.player[1].height = Math.ceil(size.player[1].width / self.videoRatio)
size.player[2] = {
left: size.player[1].left,
top: size.player[0].top + size.player[1].height + self.controlsHeight + self.margin,
width: size.player[1].width,
height: size.player[0].height - size.player[1].height - self.controlsHeight - self.margin
}
}
size.timeline[0] = {
left: self.margin / 2,
top: size.player[0].height + self.controlsHeight + 1.5 * self.margin,
2010-11-25 10:05:50 +00:00
width: contentWidth - 2 * self.margin,
2010-09-17 22:10:07 +00:00
height: 64
}
size.timeline[1] = {
left: size.timeline[0].left,
top: size.timeline[0].top + size.timeline[0].height + self.margin,
width: size.timeline[0].width
}
2010-12-31 11:01:35 +00:00
lines = Math.ceil(self.options.duration / size.timeline[1].width);
Ox.print('lines', lines, getHeight(), self.options.height, (scrollbarIsVisible && getHeight() <= self.options.height) ? 'scroll' : 'auto')
that.css({
overflowY: (scrollbarIsVisible && getHeight() <= self.options.height) ? 'scroll' : 'auto'
});
return (!scrollbarIsVisible && getHeight() > self.options.height) ? getSizes(true) : size;
function getHeight() {
return size.player[0].height + self.controlsHeight +
size.timeline[0].height + lines * 16 +
(lines + 3) * self.margin;
}
2010-09-17 22:10:07 +00:00
}
function goToPoint(point) {
self.options.position = self.options.points[point == 'in' ? 0 : 1];
setPosition();
that.triggerEvent('change', {
position: self.options.position
});
}
function movePositionBy(sec) {
self.options.position = Ox.limit(self.options.position + sec, 0, self.options.duration);
setPosition();
that.triggerEvent('change', {
position: self.options.position
});
}
function movePositionTo(type, direction) {
self.options.position = getNextPosition(type, direction);
setPosition();
that.triggerEvent('change', {
position: self.options.position
});
}
function playInToOut() {
self.$player[0].playInToOut();
}
function resizeEditor(event, data) {
var width = data - 2 * margin + 100;
resizeVideoPlayers(width);
$timelineLarge.options({
width: width
});
$timelineSmall.options({
width: width
});
}
function resizePlayers() {
$.each(self.$player, function(i, v) {
v.options({
width: size[i].width,
height: size[i].height
})
.css({
left: size[i].left + 'px',
top: size[i].top + 'px',
});
});
}
function setPoint(point) {
self.options.points[point == 'in' ? 0 : 1] = self.options.position;
self.$player[point == 'in' ? 1 : 2].options({
position: self.options.position
});
if (self.options.points[1] < self.options.points[0]) {
self.options.points[point == 'in' ? 1 : 0] = self.options.position;
self.$player[point == 'in' ? 2 : 1].options({
position: self.options.position
});
}
$.each(self.$player, function(i, v) {
v.options({
points: self.options.points
});
});
$.each(self.$timeline, function(i, v) {
Ox.print('points:', self.options.points)
2010-09-17 22:10:07 +00:00
v.options({
points: self.options.points
});
});
}
function setPosition() {
self.$player[0].options({
position: self.options.position
});
$.each(self.$timeline, function(i, v) {
v.options({
position: self.options.position
});
});
}
function setSizes() {
self.sizes = getSizes();
$.each(self.$player, function(i, v) {
v.options({
height: self.sizes.player[i].height,
width: self.sizes.player[i].width
})
.css({
left: self.sizes.player[i].left + 'px',
top: self.sizes.player[i].top + 'px'
});
});
$.each(self.$timeline, function(i, v) {
v.options({
width: self.sizes.timeline[i].width
})
.css({
left: self.sizes.timeline[i].left + 'px',
top: self.sizes.timeline[i].top + 'px'
});
});
}
2010-09-17 22:10:07 +00:00
function toggleMute() {
self.$player[0].toggleMute();
}
function togglePlay() {
self.$player[0].togglePlay();
}
function togglePlayerSize() {
self.options.videoSize = self.options.videoSize == 'large' ? 'small' : 'large';
setSizes();
}
2010-09-17 22:10:07 +00:00
self.onChange = function(key, value) {
2010-11-28 15:06:47 +00:00
if (key == 'width' || key == 'height') {
2010-12-29 06:50:40 +00:00
Ox.print('XXXX setSizes', key, value, self.options.width, self.options.height)
setSizes();
2010-09-17 22:10:07 +00:00
}
};
return that;
};
2010-12-06 17:42:45 +00:00
/**
*/
2010-09-17 22:10:07 +00:00
Ox.VideoPlayer = function(options, self) {
var self = self || {},
that = new Ox.Element('div', self)
.defaults({
find: '',
height: 0,
points: [0, 0],
position: 0,
posterFrame: 0,
subtitles: [],
type: 'play',
url: '',
width: 0
})
.options(options || {})
.addClass('OxVideoPlayer')
.css({
height: (self.options.height + 16) + 'px',
width: self.options.width + 'px'
});
self.controlsHeight = 16;
if (self.options.type == 'play') {
self.$video = $('<video>')
.attr({
preload: 'auto',
src: self.options.url
})
.css({
height: self.options.height + 'px',
width: self.options.width + 'px'
})
.appendTo(that.$element);
self.video = self.$video[0];
} else {
self.$video = $('<img>')
.css({
height: self.options.height + 'px',
width: self.options.width + 'px'
})
.appendTo(that.$element)
}
2010-09-17 22:10:07 +00:00
self.$subtitle = $('<div>')
.addClass('OxSubtitle')
.appendTo(that.$element);
setSubtitleSize();
self.$markerFrame = $('<div>')
.addClass('OxMarkerFrame')
.append(
$('<div>')
.addClass('OxFrame')
.css({
width: Math.floor((self.options.width - self.options.height) / 2) + 'px',
height: self.options.height + 'px'
})
)
.append(
$('<div>')
.addClass('OxPoster')
.css({
width: (self.options.height - 2) + 'px',
height: (self.options.height - 2) + 'px'
})
)
.append(
$('<div>')
.addClass('OxFrame')
.css({
width: Math.ceil((self.options.width - self.options.height) / 2) + 'px',
height: self.options.height + 'px'
})
)
.hide()
.appendTo(that.$element);
self.$markerPoint = {}
$.each(['in', 'out'], function(i, point) {
self.$markerPoint[point] = {};
$.each(['top', 'bottom'], function(i, edge) {
var titleCase = Ox.toTitleCase(point) + Ox.toTitleCase(edge);
self.$markerPoint[point][edge] = $('<img>')
.addClass('OxMarkerPoint OxMarker' + titleCase)
.attr({
src: '/static/oxjs/build/png/ox.ui/videoMarker' + titleCase + '.png' // fixme: remove static path
})
.hide()
.appendTo(that.$element);
if (self.options.points[point == 'in' ? 0 : 1] == self.options.position) {
self.$markerPoint[point][edge].show();
}
});
});
self.$controls = new Ox.Bar({
size: self.controlsHeight
})
.css({
marginTop: '-2px'
})
.appendTo(that);
if (self.options.type == 'play') {
// fixme: $buttonPlay etc.
self.$playButton = new Ox.Button({
id: self.options.id + 'Play',
title: [
{id: 'play', title: 'play'},
{id: 'pause', title: 'pause'}
],
tooltip: ['Play', 'Pause'],
type: 'image'
})
.bindEvent('click', togglePlay)
.appendTo(self.$controls);
self.$playInToOutButton = new Ox.Button({
id: self.options.id + 'PlayInToOut',
title: 'PlayInToOut',
tooltip: 'Play In to Out',
type: 'image'
})
.bindEvent('click', function() {
that.playInToOut();
})
.appendTo(self.$controls);
self.$muteButton = new Ox.Button({
id: self.options.id + 'Mute',
title: [
{id: 'mute', title: 'mute'},
{id: 'unmute', title: 'unmute'}
],
tooltip: ['Mute', 'Unmute'],
type: 'image'
})
.bindEvent('click', toggleMute)
.appendTo(self.$controls);
self.$sizeButton = new Ox.Button({
id: self.options.id + 'Size',
title: [
{id: 'large', title: 'add'},
{id: 'small', title: 'remove'}
],
tooltip: ['Larger', 'Smaller'],
type: 'image'
})
.bindEvent('click', toggleSize)
.appendTo(self.$controls);
2010-09-17 22:10:07 +00:00
} else {
self.$goToPointButton = new Ox.Button({
id: self.options.id + 'GoTo' + Ox.toTitleCase(self.options.type),
title: 'GoTo' + Ox.toTitleCase(self.options.type),
tooltip: 'Go to ' + Ox.toTitleCase(self.options.type) + ' Point',
type: 'image'
})
.bindEvent('click', goToPoint)
.appendTo(self.$controls);
self.$setPointButton = new Ox.Button({
id: self.options.id + 'Set' + Ox.toTitleCase(self.options.type),
title: 'Set' + Ox.toTitleCase(self.options.type),
tooltip: 'Set ' + Ox.toTitleCase(self.options.type) + ' Point',
type: 'image'
})
.bindEvent('click', setPoint)
.appendTo(self.$controls);
}
self.$positionInput = new Ox.TimeInput({
milliseconds: true,
seconds: true,
value: Ox.formatDuration(self.options.position, 3)
})
.css({
float: 'right',
})
.appendTo(self.$controls)
self.$positionInput.css({
width: '98px'
});
// fixme: children doesnt work w/o $element
self.$positionInput.$element.children('.OxLabel').each(function(i, element) {
$(this).css({
width: '22px',
marginLeft: (i == 0 ? 8 : 0) + 'px',
background: 'rgb(32, 32, 32)'
});
});
self.$positionInput.$element.children('div.OxInput').each(function(i) {
var marginLeft = [-82, -58, -34, -10];
$(this).css({
marginLeft: marginLeft[i] + 'px'
}).addClass('foo');
});
if (self.options.type == 'play') {
self.$loadingIcon = new Ox.LoadingIcon()
.appendTo(that)
.start();
self.loadingInterval = setInterval(function() {
if (self.video.readyState) {
clearInterval(self.loadingInterval);
self.$loadingIcon.stop();
setPosition();
}
}, 50);
} else {
setPosition();
}
2010-09-17 22:10:07 +00:00
function getSubtitle() {
var subtitle = '';
$.each(self.options.subtitles, function(i, v) {
if (v['in'] <= self.options.position && v['out'] > self.options.position) {
subtitle = v.text;
return false;
}
});
return subtitle;
}
function goToPoint() {
that.triggerEvent('change', {
position: self.options.points[self.options.type == 'in' ? 0 : 1]
});
}
function playing() {
self.options.position = Math.round(self.video.currentTime * 25) / 25;
if (self.video.ended) {
self.$playButton.trigger('click');
}
if (self.playingInToOut && self.options.position >= self.options.points[1]) {
self.$playButton.trigger('click');
self.options.position = self.options.points[1];
}
setMarkers();
setSubtitle();
self.$positionInput.options({
value: Ox.formatDuration(self.options.position, 3)
});
that.triggerEvent('change', {
position: self.options.position
});
}
function setHeight() {
that.css({
height: (self.options.height + 16) + 'px'
});
self.$video.css({
height: self.options.height + 'px'
});
setSubtitleSize();
}
function setMarkers() {
self.options.position == self.options.posterFrame ? self.$markerFrame.show() : self.$markerFrame.hide();
$.each(self.$markerPoint, function(point, markers) {
$.each(markers, function(edge, marker) {
self.options.position == self.options.points[point == 'in' ? 0 : 1] ?
marker.show() : marker.hide();
});
})
}
function setPoint() {
var data = {};
self.options.points[self.options.type == 'in' ? 0 : 1] = self.options.position;
setMarkers();
data[self.options.type] = self.options.position;
that.triggerEvent('set', data);
}
function setPosition() {
if (self.options.type == 'play') {
self.video.currentTime = self.options.position;
} else {
self.$loadingIcon && self.$loadingIcon.stop();
if (self.$video.attr('src') != self.options.url(self.options.position)) {
self.$loadingIcon = new Ox.LoadingIcon()
.appendTo(that)
.start();
self.$video.attr({
src: self.options.url(self.options.position)
})
.load(self.$loadingIcon.stop);
}
}
2010-09-17 22:10:07 +00:00
setMarkers();
setSubtitle();
self.$positionInput.options({
value: Ox.formatDuration(self.options.position, 3)
});
}
function setSubtitle() {
var subtitle = getSubtitle();
if (subtitle != self.subtitle) {
self.subtitle = subtitle;
self.$subtitle.html(Ox.highlight(self.subtitle, self.options.find).replace(/\n/g, '<br/>'));
}
}
function setSubtitleSize() {
self.$subtitle.css({
bottom: parseInt(self.controlsHeight + self.options.height / 16) + 'px',
width: self.options.width + 'px',
fontSize: parseInt(self.options.height / 20) + 'px',
WebkitTextStroke: (self.options.height / 1000) + 'px rgb(0, 0, 0)'
});
}
function setWidth() {
that.css({
width: self.options.width + 'px'
});
self.$video.css({
width: self.options.width + 'px'
});
setSubtitleSize();
}
function toggleMute() {
self.video.muted = !self.video.muted;
}
function togglePlay() {
self.video.paused ? that.play() : that.pause();
}
function toggleSize(event, data) {
that.triggerEvent('togglesize', {
size: data.id
});
}
2010-09-17 22:10:07 +00:00
self.onChange = function(key, value) {
if (key == 'height') {
setHeight();
} else if (key == 'points') {
setMarkers();
} else if (key == 'position') {
setPosition();
} else if (key == 'posterFrame') {
setMarkers();
} else if (key == 'width') {
setWidth();
}
}
that.mute = function() {
self.video.muted = true;
return that;
};
that.pause = function() {
self.video.pause();
clearInterval(self.playInterval);
self.playingInToOut = false;
return that;
};
that.play = function() {
self.video.play();
self.playInterval = setInterval(playing, 40);
return that;
};
that.playInToOut = function() {
self.options.position = self.options.points[0];
setPosition();
Ox.print('sop', self.options.position, self.options.points);
self.playingInToOut = true;
self.video.paused && self.$playButton.trigger('click');
return that;
};
that.toggleMute = function() {
self.$muteButton.trigger('click');
return that;
}
that.togglePlay = function() {
self.$playButton.trigger('click');
return that;
}
that.unmute = function() {
self.video.muted = false;
return that;
};
return that;
};
2010-07-24 01:32:08 +00:00
/*
============================================================================
Miscellaneous
============================================================================
*/
2010-12-06 17:42:45 +00:00
/**
2010-07-24 01:32:08 +00:00
*/
Ox.Tooltip = function(options, self) {
var self = self || {},
2010-09-03 20:54:40 +00:00
that = new Ox.Element('div', self)
2010-07-24 01:32:08 +00:00
.defaults({
2010-09-03 20:54:40 +00:00
title: ''
2010-07-24 01:32:08 +00:00
})
.options(options || {})
2010-09-03 20:54:40 +00:00
.addClass('OxTooltip')
2010-09-03 08:47:40 +00:00
.html(self.options.title);
2010-07-24 01:32:08 +00:00
self.onChange = function(key, value) {
2010-09-03 20:54:40 +00:00
if (key == 'title') {
2010-07-24 01:32:08 +00:00
that.html(value);
}
};
that.hide = function() {
that.animate({
opacity: 0
2010-09-03 08:47:40 +00:00
}, 0, function() {
2010-07-24 01:32:08 +00:00
that.remove();
});
return that;
};
2010-09-03 08:47:40 +00:00
that.show = function(x, y) {
2010-07-24 01:32:08 +00:00
var left, top, width, height;
2010-09-03 20:54:40 +00:00
$('.OxTooltip').remove(); // fixme: don't use dom
2010-07-24 01:32:08 +00:00
that.appendTo($body);
width = that.width();
height = that.height();
2010-09-03 08:47:40 +00:00
left = Ox.limit(x - width / 2, 0, $document.width() - width);
top = y > $document.height() - height - 16 ? y - 32 : y + 16;
2010-07-24 01:32:08 +00:00
that.css({
2010-09-03 20:54:40 +00:00
left: left + 'px',
top: top + 'px'
2010-07-24 01:32:08 +00:00
})
.animate({
opacity: 1
2010-09-03 08:47:40 +00:00
}, 0);
2010-07-24 01:32:08 +00:00
return that;
};
return that;
};
2010-01-27 12:30:00 +00:00
})();