forked from 0x2620/oxjs
fix issues with type textarea editables
This commit is contained in:
parent
8384fcc913
commit
0423800b05
4 changed files with 200 additions and 141 deletions
|
|
@ -19,11 +19,13 @@ Ox.Editable = function(options, self) {
|
|||
tooltip: options.tooltip
|
||||
}, self)
|
||||
.defaults({
|
||||
blurred: false,
|
||||
clickLink: null,
|
||||
editable: true,
|
||||
editing: false,
|
||||
format: null,
|
||||
height: 0,
|
||||
maxHeight: void 0,
|
||||
placeholder: '',
|
||||
submitOnBlur: true,
|
||||
tooltip: '',
|
||||
|
|
@ -62,13 +64,22 @@ Ox.Editable = function(options, self) {
|
|||
.appendTo(that);
|
||||
|
||||
if (self.options.editing) {
|
||||
// edit will toggle self.options.editing
|
||||
self.options.editing = false;
|
||||
edit();
|
||||
// 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);
|
||||
}
|
||||
|
||||
function blur() {
|
||||
that.triggerEvent('blur');
|
||||
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);
|
||||
}
|
||||
|
||||
function cancel() {
|
||||
|
|
@ -80,55 +91,29 @@ Ox.Editable = function(options, self) {
|
|||
that.triggerEvent('cancel', {value: self.options.value});
|
||||
}
|
||||
|
||||
function change(event) {
|
||||
var height, width;
|
||||
self.options.value = event.value;
|
||||
self.$value.html(formatValue());
|
||||
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);
|
||||
Ox.Log('Form', self.options.width, self.$test.html(), self.$test.width(), self.$test.height(), 'wxh', width, height)
|
||||
if (self.options.type == 'input') {
|
||||
self.$input.options({
|
||||
width: width
|
||||
});
|
||||
self.$input.find('input').css({width: width + 'px'});
|
||||
} else {
|
||||
self.$input.options({
|
||||
height: height,
|
||||
width: width
|
||||
});
|
||||
self.$input.find('textarea').css({
|
||||
height: height + 'px',
|
||||
width: width + 'px'
|
||||
});
|
||||
}
|
||||
/*
|
||||
that.triggerEvent('change', {
|
||||
value: event.value
|
||||
});
|
||||
*/
|
||||
function change(data) {
|
||||
setTimeout(function() {
|
||||
var height, width;
|
||||
self.options.value = data.value;
|
||||
self.$value.html(formatValue());
|
||||
self.$test.html(formatTestValue());
|
||||
setSizes();
|
||||
}, 25);
|
||||
}
|
||||
|
||||
function edit() {
|
||||
Ox.print('E EDIT! editable editing', self.options.editable, self.options.editing)
|
||||
var height, width;
|
||||
if (self.options.editable && !self.options.editing) {
|
||||
self.options.editing = true;
|
||||
self.originalValue = self.options.value;
|
||||
self.minWidth = 8;
|
||||
self.maxWidth = that.parent().width();
|
||||
self.minHeight = 14;
|
||||
self.maxHeight = that.parent().height();
|
||||
height = self.options.height || self.$value.height();
|
||||
width = self.options.width || self.$value.width();
|
||||
self.$value.hide();
|
||||
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()
|
||||
.css({background: 'rgb(192, 192, 192)'})
|
||||
.appendTo(that.$element);
|
||||
self.$input = Ox.Input({
|
||||
changeOnKeypress: true,
|
||||
|
|
@ -149,29 +134,23 @@ Ox.Editable = function(options, self) {
|
|||
});
|
||||
self.$input.find('input').css(self.css);
|
||||
}
|
||||
self.$input.options({
|
||||
width: width,
|
||||
height: height
|
||||
})
|
||||
.show();
|
||||
if (self.options.type == 'input') {
|
||||
self.$input.find('input').css({
|
||||
height: height + 'px',
|
||||
width: width + 'px'
|
||||
});
|
||||
} else {
|
||||
self.$input.find('textarea').css({
|
||||
height: height + 'px',
|
||||
width: width + 'px'
|
||||
});
|
||||
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');
|
||||
}
|
||||
// fixme: why can't this be chained?
|
||||
setTimeout(function() {
|
||||
self.$input.focusInput(self.options.type == 'input');
|
||||
}, 0);
|
||||
that.$tooltip && that.$tooltip.options({title: ''});
|
||||
that.triggerEvent('edit');
|
||||
}
|
||||
self.options.blurred = false;
|
||||
}
|
||||
|
||||
function formatInputValue() {
|
||||
|
|
@ -184,7 +163,7 @@ Ox.Editable = function(options, self) {
|
|||
|
||||
function formatTestValue() {
|
||||
return self.options.type == 'input'
|
||||
? self.options.value.replace(/ /g, ' ')
|
||||
? Ox.encodeHTML(self.options.value) //.replace(/ /g, ' ')
|
||||
: Ox.encodeHTML(self.options.value || ' ')
|
||||
.replace(/\n$/, '\n ')
|
||||
.replace(/\n/g, '<br/>')
|
||||
|
|
@ -195,18 +174,46 @@ Ox.Editable = function(options, self) {
|
|||
}
|
||||
|
||||
function formatValue() {
|
||||
Ox.print('HUH?', self.options.value, self.options.format);
|
||||
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)
|
||||
} else if (self.options.type == 'input') {
|
||||
Ox.print('HELLO??')
|
||||
value = Ox.encodeHTML(self.options.value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function parseValue() {
|
||||
return self.options.type == 'input'
|
||||
? Ox.encodeHTML(self.$input.value())
|
||||
: Ox.parseHTML(self.$input.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);
|
||||
Ox.print('stH', self.$test.height(), self.options.value)
|
||||
self.$test.hide();
|
||||
self.$input.options({
|
||||
width: width,
|
||||
height: height
|
||||
})
|
||||
//.show();
|
||||
self.$input.find(self.options.type).css({
|
||||
height: height + 'px',
|
||||
width: width + 'px'
|
||||
});
|
||||
}
|
||||
|
||||
function submit() {
|
||||
self.options.editing = false;
|
||||
self.options.value = Ox.parseHTML(self.$input.value());
|
||||
//self.options.value = parseValue();
|
||||
self.$input.value(formatInputValue()).hide();
|
||||
self.$test.html(formatTestValue());
|
||||
self.$value.html(formatValue()).show();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue