oxjs/source/Ox.UI/js/Form/Ox.Editable.js

244 lines
7.9 KiB
JavaScript
Raw Normal View History

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({
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({
blurred: false,
clickLink: null,
2011-10-24 15:58:51 +00:00
editable: true,
2011-08-12 21:01:19 +00:00
editing: false,
format: null,
height: 0,
maxHeight: void 0,
2011-10-25 14:40:02 +00:00
placeholder: '',
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({
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) {
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();
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) {
// 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
}
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
}
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) {
self.$input = Ox.Input({
changeOnKeypress: true,
element: self.options.type == 'input' ? '<span>' : '<div>',
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,
cancel: cancel,
change: change,
submit: submit
})
.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);
}
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
}
}
self.options.blurred = false;
2011-08-12 21:01:19 +00:00
}
2011-10-27 08:47:31 +00:00
function formatInputValue() {
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 ? '&nbsp;'
: self.options.type == 'input'
? value.replace(/ /g, '&nbsp;')
: value.replace(/\n$/, '\n ')
.replace(/ /g, ' &nbsp;')
.replace(/(^ | $)/, '&nbsp;')
.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
}
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)
);
}
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
});
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();
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);
}
};
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;
};