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

225 lines
7.3 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: '<div>',
tooltip: options.tooltip
}, self)
2011-08-12 21:01:19 +00:00
.defaults({
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,
2011-10-25 14:40:02 +00:00
placeholder: '',
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')
//.css({background: 'red'})
2011-10-27 08:47:31 +00:00
.html(formatValue())
2011-08-12 21:01:19 +00:00
//[self.options.editing ? 'hide' : 'show']()
.appendTo(that);
if (self.options.editing) {
self.options.editing = false;
edit();
}
function cancel() {
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(event) {
var height, width;
2011-10-27 08:47:31 +00:00
self.options.value = event.value;
self.$value.html(formatValue());
2011-10-27 08:47:31 +00:00
self.$test.html(formatTestValue());
//height = self.options.height || Ox.limit(self.$test.height() + 2, self.minHeight, self.maxHeight);
height = self.options.height || Math.max(self.$test.height() + 2, self.minHeight);
width = self.options.width || Ox.limit(self.$test.width() + 2, self.minWidth, self.maxWidth);
2011-11-04 15:54:28 +00:00
Ox.Log('Form', self.options.width, self.$test.width(), 'wxh', width, height)
2011-08-12 21:01:19 +00:00
if (self.options.type == 'input') {
self.$input.options({
2011-08-15 16:41:54 +00:00
width: width
2011-08-12 21:01:19 +00:00
});
2011-08-15 16:41:54 +00:00
self.$input.find('input').css({width: width + 'px'});
2011-08-12 21:01:19 +00:00
} else {
self.$input.options({
height: height,
2011-11-06 12:26:12 +00:00
width: width
2011-08-12 21:01:19 +00:00
});
self.$input.find('textarea').css({
height: height + 'px',
2011-11-06 12:26:12 +00:00
width: width + 'px'
2011-08-12 21:01:19 +00:00
});
}
/*
that.triggerEvent('change', {
value: event.value
});
*/
}
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;
2011-10-27 13:13:28 +00:00
self.originalValue = self.options.value;
self.minWidth = 8;
2011-10-24 15:58:51 +00:00
self.maxWidth = that.parent().width();
self.minHeight = 14;
self.maxHeight = that.parent().height();
2011-10-25 14:40:02 +00:00
height = self.options.height || self.$value.height();
width = self.options.width || self.$value.width();
2011-08-12 21:01:19 +00:00
self.$value.hide();
2011-11-04 15:54:28 +00:00
Ox.Log('Form', 'H:::', self.options.height, height)
if (!self.$test) {
self.$test = self.$value.$element.clone()
.css(Ox.extend({display: 'inline-block'}, self.css))
.html(formatTestValue())
.hide()
.appendTo(that.$element);
self.$input = Ox.Input({
changeOnKeypress: true,
style: 'square',
type: self.options.type,
value: formatInputValue(),
})
.css(self.css)
.bindEvent({
blur: submit,
cancel: cancel,
change: change,
submit: submit
})
.appendTo(that.$element);
self.$input.find('input').css(self.css);
}
2011-08-12 21:01:19 +00:00
self.$input.options({
width: width,
height: height
})
.show();
2011-08-12 21:01:19 +00:00
if (self.options.type == 'input') {
2011-10-24 15:58:51 +00:00
self.$input.find('input').css({
height: height + 'px',
width: width + 'px'
});
2011-08-12 21:01:19 +00:00
} else {
self.$input.find('textarea').css({
height: height + 'px',
2011-10-25 14:40:02 +00:00
width: width + 'px'
2011-08-12 21:01:19 +00:00
});
}
// fixme: why can't this be chained?
setTimeout(function() {
2012-01-03 10:25:15 +00:00
self.$input.focusInput(true);
2011-08-12 21:01:19 +00:00
}, 0);
that.$tooltip && that.$tooltip.options({title: ''});
2011-10-27 13:13:28 +00:00
that.triggerEvent('edit', {editing: true});
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() {
return self.options.type == 'input'
? self.options.value.replace(/ /g, '&nbsp;')
2011-10-27 13:13:28 +00:00
: Ox.parseHTML(self.options.value || '&nbsp;')
2011-11-03 15:42:41 +00:00
.replace(/<br\/?>$/, '<br/>&nbsp;');
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 submit() {
2011-11-06 12:26:12 +00:00
self.options.editing = false;
2011-10-27 08:47:31 +00:00
self.options.value = Ox.parseHTML(self.$input.value());
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});
2011-10-24 21:31:53 +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) {
if (key == 'height' || key == 'width') {
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;
};