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-03 10:25:15 +00:00
|
|
|
width: 256
|
|
|
|
})
|
|
|
|
.options(options || {})
|
|
|
|
.addClass('OxArrayEditable')
|
|
|
|
.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,
|
|
|
|
key_down: selectLast,
|
|
|
|
key_left: selectPrevious,
|
|
|
|
key_right: selectNext,
|
|
|
|
key_up: selectFirst
|
2012-01-03 10:25:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
self.$items = [];
|
2012-01-03 19:41:50 +00:00
|
|
|
self.selected = getSelectedPosition();
|
|
|
|
|
|
|
|
renderItems();
|
|
|
|
|
|
|
|
function anyclick(e) {
|
|
|
|
var $target = $(e.target),
|
|
|
|
$parent = $target.parent();
|
|
|
|
$target.is(':not(input)') && that.gainFocus();
|
|
|
|
if ($parent.is('.OxEditableElement')) {
|
|
|
|
selectItem($parent.data('position'));
|
|
|
|
} else {
|
|
|
|
selectNone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03 19:41:50 +00:00
|
|
|
var $parent = $(e.target).parent();
|
|
|
|
if ($parent.is('.OxEditableElement')) {
|
|
|
|
that.editItem(self.options.selected);
|
2012-01-03 10:25:15 +00:00
|
|
|
} else {
|
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() {
|
|
|
|
return Ox.getPositionById(self.options.items, self.options.selected);
|
|
|
|
}
|
|
|
|
|
2012-01-03 10:25:15 +00:00
|
|
|
function renderItems() {
|
|
|
|
that.empty();
|
|
|
|
self.options.items.forEach(function(item, i) {
|
2012-01-03 19:41:50 +00:00
|
|
|
i && $('<span>')
|
|
|
|
.html(', ')
|
2012-01-03 10:25:15 +00:00
|
|
|
.appendTo(that);
|
|
|
|
self.$items[i] = Ox.Editable({
|
|
|
|
editable: item.editable,
|
|
|
|
format: function(value) {
|
|
|
|
return value || ' '
|
|
|
|
},
|
|
|
|
tooltip: 'Click to select' + (
|
|
|
|
item.editable ? ', doubleclick to edit' : ''
|
|
|
|
),
|
|
|
|
value: item.value
|
|
|
|
})
|
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({
|
|
|
|
cancel: function(data) {
|
|
|
|
|
|
|
|
},
|
|
|
|
submit: function(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);
|
|
|
|
}
|
|
|
|
|
|
|
|
function selectItem(position) {
|
|
|
|
self.selected = position;
|
|
|
|
self.options.selected = getSelectedId();
|
|
|
|
that.selectItem(self.options.selected);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-03 19:41:50 +00:00
|
|
|
deleteItem();
|
2012-01-03 10:25:15 +00:00
|
|
|
} else {
|
2012-01-03 19:41:50 +00:00
|
|
|
item.value != value && that.triggerEvent('submit', {
|
|
|
|
id: item.id,
|
|
|
|
value: value
|
|
|
|
});
|
|
|
|
item.value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.setOption = function(key, value) {
|
|
|
|
if (key == 'items') {
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
that.editItem = function(position) {
|
2012-01-03 19:41:50 +00:00
|
|
|
selectItem(position);
|
|
|
|
editItem();
|
|
|
|
};
|
|
|
|
|
|
|
|
that.selectItem = function(id) {
|
|
|
|
self.options.selected = id;
|
|
|
|
self.selected = getSelectedPosition();
|
|
|
|
that.find('.OxSelected').removeClass('OxSelected');
|
|
|
|
self.selected > -1 && self.$items[self.selected].addClass('OxSelected');
|
|
|
|
that.triggerEvent('select', {id: self.options.selected});
|
2012-01-03 10:25:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
that.removeItem = function(position) {
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
return that;
|
|
|
|
|
|
|
|
};
|