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({
|
2012-01-17 15:43:46 +00:00
|
|
|
clickLink: null,
|
2012-01-03 10:25:15 +00:00
|
|
|
editable: true,
|
|
|
|
itemName: 'item',
|
|
|
|
items: [],
|
2012-01-16 07:09:16 +00:00
|
|
|
maxHeight: void 0,
|
|
|
|
placeholder: '',
|
2012-01-03 10:25:15 +00:00
|
|
|
position: -1,
|
2012-01-03 19:41:50 +00:00
|
|
|
selected: '',
|
2012-01-16 07:09:16 +00:00
|
|
|
separator: ',',
|
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-16 07:09:16 +00:00
|
|
|
.css({width: self.options.width - (self.options.type == 'input' ? 8 : 0) + 'px'}) // 2 x 4 px padding
|
2012-01-03 10:25:15 +00:00
|
|
|
.bindEvent({
|
2012-01-03 19:41:50 +00:00
|
|
|
anyclick: anyclick,
|
|
|
|
doubleclick: doubleclick,
|
|
|
|
key_delete: deleteItem,
|
2012-01-12 10:39:05 +00:00
|
|
|
key_enter: function() {
|
2012-01-16 11:22:34 +00:00
|
|
|
// make sure the newline does
|
|
|
|
// not end up in the textarea
|
|
|
|
setTimeout(function() {
|
|
|
|
that.editItem();
|
|
|
|
}, 0);
|
2012-01-12 10:39:05 +00:00
|
|
|
},
|
2012-01-03 19:41:50 +00:00
|
|
|
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-16 07:09:16 +00:00
|
|
|
self.editing = false;
|
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) {
|
2012-01-16 07:09:16 +00:00
|
|
|
Ox.print('SELF EDITING', self.editing)
|
2012-01-03 19:41:50 +00:00
|
|
|
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
|
|
|
if (!$target.is('.OxInput')) {
|
2012-01-16 07:09:16 +00:00
|
|
|
Ox.print('BLURRED EDITING', self.blurred, self.editing)
|
2012-01-09 20:25:38 +00:00
|
|
|
if ($parent.is('.OxEditableElement')) {
|
2012-01-16 17:51:53 +00:00
|
|
|
if (!$parent.is('.OxSelected')) {
|
|
|
|
// select another item
|
|
|
|
selectItem(
|
|
|
|
e.metaKey && position == self.selected
|
|
|
|
? '' : $parent.data('position')
|
|
|
|
);
|
|
|
|
}
|
2012-01-16 07:09:16 +00:00
|
|
|
} else if (!self.blurred) {
|
|
|
|
// if there wasn't an active input element
|
|
|
|
if (self.editing) {
|
|
|
|
// blur if still in editing mode
|
|
|
|
that.blurItem();
|
|
|
|
} else {
|
|
|
|
// othewise deselect selected
|
|
|
|
selectNone();
|
|
|
|
}
|
2012-01-09 20:25:38 +00:00
|
|
|
}
|
|
|
|
that.gainFocus();
|
2012-01-03 19:41:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteItem() {
|
2012-01-12 19:04:32 +00:00
|
|
|
if (self.options.editable) {
|
|
|
|
self.options.items.splice(self.selected, 1);
|
|
|
|
renderItems();
|
|
|
|
that.triggerEvent('delete', {
|
|
|
|
id: self.options.selected
|
|
|
|
});
|
2012-01-16 07:09:16 +00:00
|
|
|
self.selected = -1;
|
|
|
|
self.options.selected = '';
|
2012-01-12 19:04:32 +00:00
|
|
|
}
|
2012-01-03 19:41:50 +00:00
|
|
|
}
|
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')) {
|
2012-01-12 10:39:05 +00:00
|
|
|
that.editItem();
|
2012-01-12 19:04: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 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-16 07:09:16 +00:00
|
|
|
function renderItems(blur) {
|
|
|
|
if (self.editing) {
|
|
|
|
self.options.items[getSelectedPosition()].value = that.find(self.options.type + ':visible').val();
|
|
|
|
}
|
2012-01-03 10:25:15 +00:00
|
|
|
that.empty();
|
2012-01-16 07:09:16 +00:00
|
|
|
if (self.options.items.length == 0) {
|
|
|
|
Ox.Editable({
|
2012-01-17 15:43:46 +00:00
|
|
|
clickLink: self.options.clickLink,
|
2012-01-16 07:09:16 +00:00
|
|
|
editable: false,
|
|
|
|
type: 'text',
|
|
|
|
value: self.options.placeholder
|
|
|
|
})
|
|
|
|
.addClass('OxPlaceholder')
|
|
|
|
.appendTo(that);
|
|
|
|
} else {
|
|
|
|
sortItems();
|
|
|
|
self.options.items.forEach(function(item, i) {
|
|
|
|
if (i && self.options.type == 'input') {
|
|
|
|
$('<span>')
|
|
|
|
.addClass('OxSeparator')
|
|
|
|
.html(self.options.separator + ' ')
|
|
|
|
.appendTo(that);
|
|
|
|
}
|
|
|
|
self.$items[i] = Ox.Editable({
|
|
|
|
blurred: self.editing && i == self.selected ? blur : false,
|
2012-01-17 15:43:46 +00:00
|
|
|
clickLink: self.options.clickLink,
|
2012-01-16 07:09:16 +00:00
|
|
|
editable: self.options.editable && item.editable,
|
|
|
|
editing: self.editing && i == self.selected,
|
|
|
|
/*
|
|
|
|
format: function(value) {
|
|
|
|
return value || ' '
|
|
|
|
},
|
|
|
|
*/
|
|
|
|
maxHeight: self.options.maxHeight,
|
|
|
|
submitOnBlur: self.options.submitOnBlur,
|
|
|
|
tooltip: 'Click to select' + (
|
|
|
|
item.editable ? ', doubleclick to edit' : ''
|
|
|
|
),
|
|
|
|
type: self.options.type,
|
|
|
|
value: item.value,
|
|
|
|
width: self.options.type == 'input' ? 0 : self.options.width - 9
|
|
|
|
})
|
|
|
|
.addClass(item.id == self.options.selected ? 'OxSelected' : '')
|
|
|
|
//.css(self.options.type == 'textarea' ? {padding: '4px'} : {})
|
2012-01-17 11:50:31 +00:00
|
|
|
.data({id: item.id, position: i})
|
2012-01-16 07:09:16 +00:00
|
|
|
.bindEvent({
|
|
|
|
blur: function(data) {
|
|
|
|
// fixme: remove data
|
|
|
|
that.gainFocus();
|
|
|
|
that.triggerEvent('blur', {
|
|
|
|
id: item.id,
|
|
|
|
value: data.value
|
|
|
|
});
|
|
|
|
self.blurred = true;
|
|
|
|
setTimeout(function() {
|
|
|
|
self.blurred = false;
|
|
|
|
}, 250);
|
|
|
|
},
|
|
|
|
cancel: function(data) {
|
|
|
|
self.editing = false;
|
|
|
|
that.gainFocus();
|
|
|
|
that.triggerEvent('blur', data);
|
|
|
|
},
|
|
|
|
change: function(data) {
|
|
|
|
that.triggerEvent('change', {
|
|
|
|
id: item.id,
|
|
|
|
value: data.value
|
|
|
|
});
|
|
|
|
},
|
|
|
|
edit: function(data) {
|
|
|
|
self.editing = true;
|
|
|
|
that.triggerEvent('edit', data);
|
|
|
|
},
|
|
|
|
submit: function(data) {
|
|
|
|
self.editing = false;
|
|
|
|
that.gainFocus();
|
|
|
|
submitItem(i, data.value);
|
|
|
|
}
|
|
|
|
})
|
2012-01-04 17:27:32 +00:00
|
|
|
.appendTo(that);
|
2012-01-16 07:09:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
//self.editing && that.editItem(blur);
|
2012-01-03 10:25:15 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2012-01-16 17:51:53 +00:00
|
|
|
if (Ox.isString(idOrPosition)) {
|
|
|
|
self.options.selected = idOrPosition;
|
|
|
|
self.selected = getSelectedPosition();
|
|
|
|
} else {
|
|
|
|
self.selected = idOrPosition;
|
|
|
|
self.options.selected = getSelectedId();
|
|
|
|
}
|
|
|
|
if (/*self.options.selected == '' && */self.editing) {
|
|
|
|
self.editing = false;
|
|
|
|
that.blurItem();
|
2012-01-12 19:04:32 +00:00
|
|
|
}
|
2012-01-16 17:51:53 +00:00
|
|
|
Ox.print('SELECT ITEM', self.options.selected, self.selected);
|
|
|
|
that.find('.OxSelected').removeClass('OxSelected');
|
|
|
|
self.selected > -1 && self.$items[self.selected].addClass('OxSelected');
|
|
|
|
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-12 10:39:05 +00:00
|
|
|
function submitItem(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-12 10:39:05 +00:00
|
|
|
} else {
|
2012-01-16 07:09:16 +00:00
|
|
|
that.triggerEvent(item.value === value ? 'blur' : '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-17 09:25:58 +00:00
|
|
|
that.triggerEvent('select', {
|
2012-01-11 09:32:33 +00:00
|
|
|
id: self.options.selected
|
2012-01-17 09:25:58 +00:00
|
|
|
});
|
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') {
|
2012-01-16 14:02:30 +00:00
|
|
|
if (self.options.selected && getSelectedPosition() == -1) {
|
|
|
|
selectNone();
|
|
|
|
}
|
2012-01-16 07:09:16 +00:00
|
|
|
renderItems(true);
|
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-12 19:04:32 +00:00
|
|
|
} else if (key == 'width') {
|
|
|
|
that.css({width: value - 8 + 'px'}); // 2 x 4 px padding
|
|
|
|
self.options.type == 'textarea' && self.$items.forEach(function($item) {
|
|
|
|
$item.options({width: value})
|
|
|
|
});
|
2012-01-03 10:25:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-03 19:41:50 +00:00
|
|
|
that.addItem = function(position, item) {
|
2012-01-12 19:04:32 +00:00
|
|
|
if (self.options.editable) {
|
|
|
|
self.options.items.splice(position, 0, item);
|
|
|
|
renderItems();
|
|
|
|
}
|
2012-01-16 07:09:16 +00:00
|
|
|
return that;
|
2012-01-03 19:41:50 +00:00
|
|
|
//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() {
|
2012-01-13 16:25:47 +00:00
|
|
|
/*
|
2012-01-12 19:04:32 +00:00
|
|
|
if (self.options.selected) {
|
|
|
|
self.$items[self.selected].options({editing: false});
|
|
|
|
} else {
|
2012-01-13 16:25:47 +00:00
|
|
|
*/
|
2012-01-16 07:09:16 +00:00
|
|
|
self.editing = false;
|
|
|
|
self.$items.forEach(function($item) {
|
|
|
|
$item.options({editing: false});
|
|
|
|
});
|
2012-01-13 16:25:47 +00:00
|
|
|
//}
|
2012-01-16 07:09:16 +00:00
|
|
|
return that;
|
2012-01-03 19:41:50 +00:00
|
|
|
};
|
|
|
|
|
2012-01-12 10:39:05 +00:00
|
|
|
that.editItem = function() {
|
2012-01-16 07:09:16 +00:00
|
|
|
Ox.print('AE EDIT ITEM')
|
2012-01-12 19:04:32 +00:00
|
|
|
if (self.options.editable && self.options.selected) {
|
2012-01-16 07:09:16 +00:00
|
|
|
self.editing = true;
|
|
|
|
self.$items[self.selected].options({editing: true});
|
2012-01-12 10:39:05 +00:00
|
|
|
}
|
2012-01-16 07:09:16 +00:00
|
|
|
return that;
|
2012-01-12 10:39:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
that.reloadItems = function() {
|
|
|
|
renderItems();
|
2012-01-16 07:09:16 +00:00
|
|
|
return that;
|
2012-01-03 10:25:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
that.removeItem = function(position) {
|
2012-01-12 19:04:32 +00:00
|
|
|
if (self.options.editable) {
|
|
|
|
|
|
|
|
}
|
2012-01-16 07:09:16 +00:00
|
|
|
return that;
|
2012-01-03 10:25:15 +00:00
|
|
|
};
|
|
|
|
|
2012-01-16 07:09:16 +00:00
|
|
|
/*
|
|
|
|
that.submitItem = function() {
|
|
|
|
if (self.editing) {
|
|
|
|
self.editing = false;
|
|
|
|
self.$items[self.selected].options({editing: false});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2012-01-03 10:25:15 +00:00
|
|
|
return that;
|
|
|
|
|
|
|
|
};
|