2011-11-05 16:46:53 +00:00
|
|
|
'use strict';
|
2011-08-12 21:01:19 +00:00
|
|
|
/*@
|
|
|
|
Ox.Editable <f> Editable element
|
|
|
|
() -> <f> Input Element
|
|
|
|
(options) -> <f> Input Element
|
|
|
|
(options, self) -> <f> Input Element
|
|
|
|
options <o> Options object
|
|
|
|
editing <b|false> If true, loads in editing state
|
|
|
|
format <f|null> Format function
|
|
|
|
(value) -> <s> Formatted value
|
|
|
|
value <s> Input value
|
|
|
|
self <o> Shared private variable
|
|
|
|
@*/
|
|
|
|
Ox.Editable = function(options, self) {
|
|
|
|
|
|
|
|
self = self || {};
|
2011-10-24 15:58:51 +00:00
|
|
|
var that = Ox.Element({
|
2012-01-03 19:41:50 +00:00
|
|
|
element: options.type == 'textarea' ? '<div>' : '<span>',
|
2011-10-24 15:58:51 +00:00
|
|
|
tooltip: options.tooltip
|
|
|
|
}, self)
|
2011-08-12 21:01:19 +00:00
|
|
|
.defaults({
|
2012-01-16 07:09:16 +00:00
|
|
|
blurred: false,
|
2011-10-27 18:50:23 +00:00
|
|
|
clickLink: null,
|
2011-10-24 15:58:51 +00:00
|
|
|
editable: true,
|
2011-08-12 21:01:19 +00:00
|
|
|
editing: false,
|
|
|
|
format: null,
|
2011-10-27 18:50:23 +00:00
|
|
|
height: 0,
|
2012-01-16 07:09:16 +00:00
|
|
|
maxHeight: void 0,
|
2011-10-25 14:40:02 +00:00
|
|
|
placeholder: '',
|
2012-01-03 19:41:50 +00:00
|
|
|
submitOnBlur: true,
|
2011-10-24 15:58:51 +00:00
|
|
|
tooltip: '',
|
2011-08-12 21:01:19 +00:00
|
|
|
value: '',
|
2011-10-27 13:13:28 +00:00
|
|
|
width: 0,
|
2011-08-12 21:01:19 +00:00
|
|
|
type: 'input'
|
|
|
|
})
|
|
|
|
.options(options || {})
|
2011-10-24 15:58:51 +00:00
|
|
|
.addClass('OxEditableElement')
|
|
|
|
.bind({
|
2011-10-27 18:50:23 +00:00
|
|
|
click: function() {
|
|
|
|
return false;
|
2011-10-24 15:58:51 +00:00
|
|
|
}
|
|
|
|
})
|
2011-08-12 21:01:19 +00:00
|
|
|
.bindEvent({
|
2011-10-24 15:58:51 +00:00
|
|
|
doubleclick: edit,
|
|
|
|
singleclick: function(e) {
|
2011-10-27 18:50:23 +00:00
|
|
|
if ($(e.target).is('a')) {
|
|
|
|
if (self.options.clickLink) {
|
|
|
|
self.options.clickLink(e);
|
|
|
|
} else {
|
|
|
|
document.location.href = $(e.target).attr('href');
|
|
|
|
}
|
|
|
|
}
|
2011-10-24 15:58:51 +00:00
|
|
|
}
|
2011-08-12 21:01:19 +00:00
|
|
|
});
|
|
|
|
|
2011-08-15 16:41:54 +00:00
|
|
|
self.options.value = self.options.value.toString();
|
|
|
|
|
2011-10-30 08:32:57 +00:00
|
|
|
self.css = {};
|
2011-08-12 21:01:19 +00:00
|
|
|
self.$value = Ox.Element(self.options.type == 'input' ? '<span>' : '<div>')
|
|
|
|
.addClass('OxValue')
|
2011-10-27 08:47:31 +00:00
|
|
|
.html(formatValue())
|
2011-08-12 21:01:19 +00:00
|
|
|
.appendTo(that);
|
|
|
|
|
|
|
|
if (self.options.editing) {
|
2012-01-16 07:09:16 +00:00
|
|
|
// need timeout so that when determining height
|
|
|
|
// the element is actually in the DOM
|
|
|
|
setTimeout(function() {
|
|
|
|
// edit will toggle self.options.editing
|
|
|
|
self.options.editing = false;
|
|
|
|
edit();
|
|
|
|
}, 0);
|
2011-08-12 21:01:19 +00:00
|
|
|
}
|
|
|
|
|
2012-01-16 07:09:16 +00:00
|
|
|
function blur(data) {
|
|
|
|
self.options.value = parseValue();
|
|
|
|
if (self.options.value !== self.originalValue) {
|
|
|
|
self.originalValue = self.options.value;
|
|
|
|
that.triggerEvent('change', {value: self.options.value});
|
|
|
|
}
|
|
|
|
that.triggerEvent('blur', data);
|
2012-01-09 20:25:38 +00:00
|
|
|
}
|
|
|
|
|
2011-08-12 21:01:19 +00:00
|
|
|
function cancel() {
|
2012-01-12 19:04:32 +00:00
|
|
|
self.options.editing = false;
|
2012-01-16 11:22:34 +00:00
|
|
|
that.removeClass('OxEditing');
|
2011-10-27 13:13:28 +00:00
|
|
|
self.options.value = self.originalValue;
|
2011-12-21 15:33:52 +00:00
|
|
|
self.$input.value(formatInputValue()).hide();
|
2011-10-27 13:13:28 +00:00
|
|
|
self.$test.html(formatTestValue());
|
|
|
|
self.$value.html(formatValue()).show();
|
2012-01-03 10:25:15 +00:00
|
|
|
that.triggerEvent('cancel', {value: self.options.value});
|
2011-08-12 21:01:19 +00:00
|
|
|
}
|
|
|
|
|
2012-01-16 07:09:16 +00:00
|
|
|
function change(data) {
|
2012-01-16 11:22:34 +00:00
|
|
|
self.options.value = parseValue(data.value);
|
|
|
|
self.$value.html(formatValue());
|
|
|
|
self.$test.html(formatTestValue());
|
|
|
|
setSizes();
|
2011-08-12 21:01:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function edit() {
|
|
|
|
var height, width;
|
2011-10-24 15:58:51 +00:00
|
|
|
if (self.options.editable && !self.options.editing) {
|
2011-11-06 12:26:12 +00:00
|
|
|
self.options.editing = true;
|
2012-01-16 11:22:34 +00:00
|
|
|
that.addClass('OxEditing');
|
2011-10-27 13:13:28 +00:00
|
|
|
self.originalValue = self.options.value;
|
2011-08-12 21:01:19 +00:00
|
|
|
self.$value.hide();
|
2012-01-16 11:22:34 +00:00
|
|
|
if (!self.$input) {
|
2011-10-30 08:32:57 +00:00
|
|
|
self.$input = Ox.Input({
|
|
|
|
changeOnKeypress: true,
|
2012-01-03 19:41:50 +00:00
|
|
|
element: self.options.type == 'input' ? '<span>' : '<div>',
|
2011-10-30 08:32:57 +00:00
|
|
|
style: 'square',
|
|
|
|
type: self.options.type,
|
|
|
|
value: formatInputValue(),
|
|
|
|
})
|
|
|
|
.css(self.css)
|
|
|
|
.bindEvent({
|
2012-01-16 11:22:34 +00:00
|
|
|
blur: self.options.submitOnBlur ? submit : blur,
|
2011-10-30 08:32:57 +00:00
|
|
|
cancel: cancel,
|
2011-11-06 11:50:13 +00:00
|
|
|
change: change,
|
|
|
|
submit: submit
|
2011-10-30 08:32:57 +00:00
|
|
|
})
|
|
|
|
.appendTo(that.$element);
|
|
|
|
self.$input.find('input').css(self.css);
|
2012-01-16 11:22:34 +00:00
|
|
|
self.$test = self.$value.$element.clone()
|
|
|
|
.css(Ox.extend({display: 'inline-block'}, self.css))
|
|
|
|
.html(formatTestValue())
|
|
|
|
.css({background: 'rgb(192, 192, 192)'})
|
|
|
|
.appendTo(that.$element);
|
2011-10-30 08:32:57 +00:00
|
|
|
}
|
2012-01-16 07:09:16 +00:00
|
|
|
self.minWidth = 8;
|
|
|
|
self.maxWidth = that.parent().width();
|
|
|
|
self.minHeight = 13;
|
|
|
|
self.maxHeight = self.options.type == 'input'
|
|
|
|
? self.minHeight
|
|
|
|
: self.options.maxHeight || that.parent().height();
|
|
|
|
setSizes();
|
|
|
|
self.$input.show();
|
|
|
|
if (!self.options.blurred) {
|
|
|
|
setTimeout(function() {
|
|
|
|
self.$input.focusInput(self.options.type == 'input');
|
|
|
|
}, 0);
|
|
|
|
that.$tooltip && that.$tooltip.options({title: ''});
|
|
|
|
that.triggerEvent('edit');
|
2011-08-12 21:01:19 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-16 07:09:16 +00:00
|
|
|
self.options.blurred = false;
|
2011-08-12 21:01:19 +00:00
|
|
|
}
|
|
|
|
|
2011-10-27 08:47:31 +00:00
|
|
|
function formatInputValue() {
|
2011-10-27 18:50:23 +00:00
|
|
|
return Ox.decodeHTML(
|
|
|
|
self.options.type == 'input'
|
|
|
|
? self.options.value
|
|
|
|
: self.options.value.replace(/<br\/?><br\/?>/g, '\n\n')
|
|
|
|
);
|
2011-08-15 16:41:54 +00:00
|
|
|
}
|
|
|
|
|
2011-10-27 08:47:31 +00:00
|
|
|
function formatTestValue() {
|
2012-01-16 11:22:34 +00:00
|
|
|
var value = Ox.encodeHTML(self.$input.options('value'));
|
|
|
|
return !value ? ' '
|
|
|
|
: self.options.type == 'input'
|
|
|
|
? value.replace(/ /g, ' ')
|
|
|
|
: value.replace(/\n$/, '\n ')
|
|
|
|
.replace(/ /g, ' ')
|
|
|
|
.replace(/(^ | $)/, ' ')
|
|
|
|
.replace(/\n/g, '<br/>')
|
2011-10-27 08:47:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function formatValue() {
|
2011-11-03 16:53:29 +00:00
|
|
|
var value = self.options.value;
|
|
|
|
if (self.options.value === '' && self.options.placeholder) {
|
|
|
|
value = self.options.placeholder;
|
|
|
|
} else if (self.options.format) {
|
|
|
|
value = self.options.format(self.options.value)
|
|
|
|
}
|
|
|
|
return value;
|
2011-10-27 08:47:31 +00:00
|
|
|
}
|
|
|
|
|
2012-01-16 07:09:16 +00:00
|
|
|
function parseValue() {
|
2012-01-16 11:22:34 +00:00
|
|
|
var value = Ox.clean(
|
|
|
|
self.$input.value().replace(/\n\n+/g, '\0')
|
|
|
|
).replace(/\0/g, '\n\n').trim();
|
|
|
|
return (self.options.type == 'input'
|
|
|
|
? Ox.encodeHTML(value)
|
|
|
|
: Ox.parseHTML(value)
|
|
|
|
);
|
2012-01-16 07:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setSizes() {
|
|
|
|
var height, width;
|
|
|
|
self.$test.show();
|
|
|
|
height = self.options.height || Ox.limit(self.$test.height(), self.minHeight, self.maxHeight);
|
|
|
|
width = self.options.width || Ox.limit(self.$test.width(), self.minWidth, self.maxWidth);
|
|
|
|
self.$test.hide();
|
|
|
|
self.$input.options({
|
|
|
|
width: width,
|
|
|
|
height: height
|
2012-01-16 07:31:48 +00:00
|
|
|
});
|
2012-01-16 07:09:16 +00:00
|
|
|
self.$input.find(self.options.type).css({
|
|
|
|
height: height + 'px',
|
|
|
|
width: width + 'px'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-10-27 08:47:31 +00:00
|
|
|
function submit() {
|
2011-11-06 12:26:12 +00:00
|
|
|
self.options.editing = false;
|
2012-01-16 11:22:34 +00:00
|
|
|
that.removeClass('OxEditing');
|
2011-12-21 15:33:52 +00:00
|
|
|
self.$input.value(formatInputValue()).hide();
|
2011-10-27 13:13:28 +00:00
|
|
|
self.$test.html(formatTestValue());
|
|
|
|
self.$value.html(formatValue()).show();
|
2012-01-02 06:24:54 +00:00
|
|
|
that.$tooltip && that.$tooltip.options({title: self.options.tooltip});
|
2012-01-16 11:22:34 +00:00
|
|
|
that.triggerEvent('submit', {value: self.options.value});
|
2011-08-12 21:01:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-06 12:26:12 +00:00
|
|
|
self.setOption = function(key, value) {
|
2012-01-09 20:25:38 +00:00
|
|
|
if (key == 'editing') {
|
|
|
|
if (value) {
|
|
|
|
// edit will toggle self.options.editing
|
|
|
|
self.options.editing = false;
|
|
|
|
edit();
|
|
|
|
} else {
|
|
|
|
submit();
|
|
|
|
}
|
|
|
|
} else if (key == 'height' || key == 'width') {
|
2011-11-06 12:26:12 +00:00
|
|
|
var css = {};
|
|
|
|
css[key] = value + 'px';
|
|
|
|
self.$test && self.$test.css(css);
|
|
|
|
self.$input && self.$input.css(css);
|
|
|
|
self.$input && self.$input.find(self.options.type).css(css);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-10-30 08:32:57 +00:00
|
|
|
that.css = function(css) {
|
|
|
|
self.css = css;
|
|
|
|
that.$element.css(css);
|
|
|
|
self.$value.css(css);
|
|
|
|
self.$test && self.$test.css(css);
|
|
|
|
self.$input && self.$input.css(css);
|
|
|
|
return that;
|
2011-08-12 21:01:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return that;
|
|
|
|
|
2012-01-04 17:27:32 +00:00
|
|
|
};
|