2012-01-03 10:25:15 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.ArrayEditable <f> Array Editable Object
|
|
|
|
@*/
|
|
|
|
|
|
|
|
Ox.ArrayEditable = function(options, self) {
|
|
|
|
|
|
|
|
self = self || {};
|
|
|
|
var that = Ox.Element(options.editable === false ? {} : {
|
2012-01-03 19:41:50 +00:00
|
|
|
tooltip: 'Doubleclick to add ' + (options.itemName || 'item')
|
|
|
|
}, self)
|
2012-01-03 10:25:15 +00:00
|
|
|
.defaults({
|
|
|
|
editable: true,
|
|
|
|
itemName: 'item',
|
|
|
|
items: [],
|
|
|
|
position: -1,
|
2012-01-03 19:41:50 +00:00
|
|
|
selected: '',
|
2012-01-11 11:11:13 +00:00
|
|
|
sort: [],
|
2012-01-09 20:25:38 +00:00
|
|
|
submitOnBlur: true,
|
2012-01-04 17:27:32 +00:00
|
|
|
type: 'input',
|
2012-01-03 10:25:15 +00:00
|
|
|
width: 256
|
|
|
|
})
|
|
|
|
.options(options || {})
|
2012-01-10 14:49:28 +00:00
|
|
|
.addClass('OxArrayEditable OxArrayEditable' + Ox.toTitleCase(self.options.type))
|
2012-01-03 10:25:15 +00:00
|
|
|
.css({width: self.options.width - 8 + 'px'}) // 2 x 4 px padding
|
|
|
|
.bindEvent({
|
2012-01-03 19:41:50 +00:00
|
|
|
anyclick: anyclick,
|
|
|
|
doubleclick: doubleclick,
|
|
|
|
key_delete: deleteItem,
|
|
|
|
key_enter: editItem,
|
|
|
|
key_escape: selectNone,
|
2012-01-10 14:49:28 +00:00
|
|
|
key_down: self.options.type == 'input' ? selectLast : selectNext,
|
|
|
|
key_left: self.options.type == 'input' ? selectPrevious : selectFirst,
|
|
|
|
key_right: self.options.type == 'input' ? selectNext : selectLast,
|
|
|
|
key_up: self.options.type == 'input' ? selectFirst : selectPrevious
|
2012-01-03 10:25:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
self.$items = [];
|
2012-01-03 19:41:50 +00:00
|
|
|
|
|
|
|
renderItems();
|
|
|
|
|
2012-01-11 11:54:58 +00:00
|
|
|
self.selected = getSelectedPosition();
|
|
|
|
|
2012-01-03 19:41:50 +00:00
|
|
|
function anyclick(e) {
|
|
|
|
var $target = $(e.target),
|
2012-01-04 17:27:32 +00:00
|
|
|
$parent = $target.parent(),
|
|
|
|
position = $parent.data('position');
|
2012-01-09 20:25:38 +00:00
|
|
|
//ignore clicks while editing
|
|
|
|
if (!$target.is('.OxInput')) {
|
|
|
|
if (self.selected > -1) {
|
|
|
|
//end editing but keep selected if clicked next to a keyword
|
|
|
|
if (self.editing || self.$items[self.selected].options('editing')) {
|
|
|
|
self.editing = false;
|
|
|
|
self.$items[self.selected].options({editing: false});
|
|
|
|
//deselect if not editing and not going to select antoher one
|
|
|
|
} else if (!$parent.is('.OxEditableElement')) {
|
|
|
|
selectNone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//select if clicked on other editable element
|
|
|
|
if ($parent.is('.OxEditableElement')) {
|
|
|
|
selectItem(
|
|
|
|
e.metaKey && position == self.selected
|
|
|
|
? '' : $parent.data('position')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
that.gainFocus();
|
2012-01-03 19:41:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteItem() {
|
|
|
|
self.options.items.splice(self.selected, 1);
|
|
|
|
renderItems();
|
|
|
|
that.triggerEvent('delete', {
|
|
|
|
id: self.options.selected
|
|
|
|
});
|
|
|
|
}
|
2012-01-03 10:25:15 +00:00
|
|
|
|
|
|
|
function doubleclick(e) {
|
2012-01-04 17:27:32 +00:00
|
|
|
var $target = $(e.target),
|
|
|
|
$parent = $target.parent();
|
2012-01-03 19:41:50 +00:00
|
|
|
if ($parent.is('.OxEditableElement')) {
|
|
|
|
that.editItem(self.options.selected);
|
2012-01-04 17:27:32 +00:00
|
|
|
} else if(!$target.is('.OxInput')) {
|
2012-01-03 19:41:50 +00:00
|
|
|
that.triggerEvent('add');
|
2012-01-03 10:25:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-03 19:41:50 +00:00
|
|
|
function editItem() {
|
|
|
|
if (self.selected > -1) {
|
|
|
|
Ox.forEach(self.$items, function($item) {
|
|
|
|
if ($item.data('position') == self.selected) {
|
|
|
|
$item.triggerEvent('doubleclick');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSelectedId() {
|
|
|
|
return self.selected > -1 ? self.options.items[self.selected].id : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSelectedPosition() {
|
2012-01-04 08:11:05 +00:00
|
|
|
return Ox.getIndexById(self.options.items, self.options.selected);
|
2012-01-03 19:41:50 +00:00
|
|
|
}
|
|
|
|
|
2012-01-03 10:25:15 +00:00
|
|
|
function renderItems() {
|
|
|
|
that.empty();
|
2012-01-11 11:54:58 +00:00
|
|
|
sortItems();
|
|
|
|
self.options.items.forEach(function(item, i) {
|
2012-01-04 17:27:32 +00:00
|
|
|
i && self.options.type == 'input'
|
|
|
|
&& $('<span>')
|
|
|
|
.html(', ')
|
|
|
|
.appendTo(that);
|
2012-01-03 10:25:15 +00:00
|
|
|
self.$items[i] = Ox.Editable({
|
|
|
|
editable: item.editable,
|
|
|
|
format: function(value) {
|
|
|
|
return value || ' '
|
|
|
|
},
|
2012-01-09 20:25:38 +00:00
|
|
|
submitOnBlur: self.options.submitOnBlur,
|
2012-01-03 10:25:15 +00:00
|
|
|
tooltip: 'Click to select' + (
|
|
|
|
item.editable ? ', doubleclick to edit' : ''
|
|
|
|
),
|
2012-01-04 17:27:32 +00:00
|
|
|
type: self.options.type,
|
|
|
|
value: item.value,
|
2012-01-10 14:49:28 +00:00
|
|
|
width: self.options.type == 'input' ? 0 : self.options.width - 8
|
2012-01-03 10:25:15 +00:00
|
|
|
})
|
2012-01-03 19:41:50 +00:00
|
|
|
.addClass(item.id == self.options.selected ? 'OxSelected' : '')
|
2012-01-03 10:25:15 +00:00
|
|
|
.data({position: i})
|
|
|
|
.bindEvent({
|
2012-01-09 20:25:38 +00:00
|
|
|
blur: function(data) {
|
|
|
|
that.triggerEvent('blur', data);
|
|
|
|
},
|
2012-01-03 10:25:15 +00:00
|
|
|
cancel: function(data) {
|
|
|
|
|
2012-01-04 17:27:32 +00:00
|
|
|
},
|
|
|
|
edit: function(data) {
|
2012-01-09 20:25:38 +00:00
|
|
|
self.editing = true;
|
2012-01-04 17:27:32 +00:00
|
|
|
that.triggerEvent('edit', data);
|
2012-01-03 10:25:15 +00:00
|
|
|
},
|
|
|
|
submit: function(data) {
|
2012-01-09 20:25:38 +00:00
|
|
|
Ox.Log("AE", "SUBMIT", data);
|
2012-01-03 19:41:50 +00:00
|
|
|
submit(i, data.value);
|
2012-01-03 10:25:15 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.appendTo(that);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-01-03 19:41:50 +00:00
|
|
|
function selectFirst() {
|
|
|
|
self.selected > -1 && selectItem(0);
|
|
|
|
}
|
|
|
|
|
2012-01-09 20:25:38 +00:00
|
|
|
function selectItem(idOrPosition) {
|
|
|
|
if (Ox.isString(idOrPosition)) {
|
|
|
|
self.options.selected = idOrPosition;
|
|
|
|
self.selected = getSelectedPosition();
|
|
|
|
} else {
|
|
|
|
self.selected = idOrPosition;
|
|
|
|
self.options.selected = getSelectedId();
|
|
|
|
}
|
|
|
|
that.find('.OxSelected').removeClass('OxSelected');
|
|
|
|
self.selected > -1 && self.$items[self.selected].addClass('OxSelected');
|
2012-01-10 14:49:28 +00:00
|
|
|
triggerSelectEvent();
|
2012-01-03 19:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function selectLast() {
|
|
|
|
self.selected > -1 && selectItem(self.options.items.length - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function selectNext() {
|
|
|
|
self.selected > -1
|
|
|
|
&& self.selected < self.options.items.length - 1
|
|
|
|
&& selectItem(self.selected + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function selectNone() {
|
|
|
|
selectItem(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function selectPrevious() {
|
|
|
|
self.selected > 0 && selectItem(self.selected - 1);
|
|
|
|
}
|
|
|
|
|
2012-01-11 11:54:58 +00:00
|
|
|
function sortItems() {
|
|
|
|
if (!Ox.isEmpty(self.options.sort)) {
|
|
|
|
self.options.items = Ox.sortBy(self.options.items, self.options.sort);
|
|
|
|
self.selected = getSelectedPosition();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-03 10:25:15 +00:00
|
|
|
function submit(position, value) {
|
2012-01-03 19:41:50 +00:00
|
|
|
var item = self.options.items[position];
|
2012-01-03 10:25:15 +00:00
|
|
|
if (value === '') {
|
2012-01-09 20:25:38 +00:00
|
|
|
|
2012-01-03 19:41:50 +00:00
|
|
|
deleteItem();
|
2012-01-09 20:25:38 +00:00
|
|
|
} /*else if (item.value === value) {
|
|
|
|
that.triggerEvent('blur');
|
|
|
|
} else*/ {
|
|
|
|
that.triggerEvent('submit', {
|
2012-01-03 19:41:50 +00:00
|
|
|
id: item.id,
|
|
|
|
value: value
|
|
|
|
});
|
|
|
|
item.value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 14:49:28 +00:00
|
|
|
function triggerSelectEvent() {
|
|
|
|
if (!self.triggered) {
|
2012-01-11 09:32:33 +00:00
|
|
|
that.triggerEvent('select', Ox.extend({
|
|
|
|
id: self.options.selected
|
|
|
|
}, self.options.selected ? {
|
|
|
|
top: self.$items[self.selected].offset().top
|
|
|
|
} : {}));
|
2012-01-10 14:49:28 +00:00
|
|
|
self.triggered = true;
|
|
|
|
setTimeout(function() {
|
|
|
|
self.triggered = false;
|
|
|
|
}, 250);
|
|
|
|
}
|
2012-01-11 09:32:33 +00:00
|
|
|
}
|
2012-01-10 14:49:28 +00:00
|
|
|
|
2012-01-03 19:41:50 +00:00
|
|
|
self.setOption = function(key, value) {
|
|
|
|
if (key == 'items') {
|
|
|
|
renderItems();
|
2012-01-04 17:27:32 +00:00
|
|
|
} else if (key == 'selected') {
|
2012-01-09 20:25:38 +00:00
|
|
|
selectItem(value);
|
2012-01-11 11:11:13 +00:00
|
|
|
} else if (key == 'sort') {
|
|
|
|
renderItems();
|
2012-01-03 10:25:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-03 19:41:50 +00:00
|
|
|
that.addItem = function(position, item) {
|
|
|
|
self.options.items.splice(position, 0, item);
|
|
|
|
renderItems();
|
|
|
|
//that.triggerEvent('add');
|
|
|
|
/*
|
2012-01-03 10:25:15 +00:00
|
|
|
self.values = Ox.filter(values, function(value) {
|
|
|
|
return value;
|
|
|
|
});
|
|
|
|
self.values.push('');
|
|
|
|
renderItems();
|
|
|
|
Ox.last(self.$items).triggerEvent('doubleclick');
|
2012-01-03 19:41:50 +00:00
|
|
|
*/
|
2012-01-03 10:25:15 +00:00
|
|
|
};
|
|
|
|
|
2012-01-09 20:25:38 +00:00
|
|
|
that.blurItem = function() {
|
|
|
|
Ox.Log('AE', 'bI', self.selected);
|
|
|
|
self.selected > -1 && self.$items[self.selected].options({editing: false});
|
2012-01-03 19:41:50 +00:00
|
|
|
};
|
|
|
|
|
2012-01-09 20:25:38 +00:00
|
|
|
that.editItem = function(id) {
|
|
|
|
Ox.Log('AE', 'editItem', id);
|
2012-01-03 19:41:50 +00:00
|
|
|
self.options.selected = id;
|
|
|
|
self.selected = getSelectedPosition();
|
2012-01-09 20:25:38 +00:00
|
|
|
editItem();
|
2012-01-03 10:25:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
that.removeItem = function(position) {
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
return that;
|
|
|
|
|
|
|
|
};
|