2011-11-05 16:46:53 +00:00
|
|
|
'use strict';
|
2012-05-21 10:38:18 +00:00
|
|
|
|
2011-08-12 21:01:19 +00:00
|
|
|
/*@
|
2012-05-31 10:32:54 +00:00
|
|
|
Ox.Editable <f> Editable element
|
2011-08-12 21:01:19 +00:00
|
|
|
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
|
2012-07-04 11:29:18 +00:00
|
|
|
([options[, self]]) -> <o:Ox.Element> Input Element
|
|
|
|
blur <!> blur
|
|
|
|
cancel <!> cancel
|
|
|
|
edit <!> edit
|
|
|
|
open <!> open
|
|
|
|
submit <!> submit
|
2011-08-12 21:01:19 +00:00
|
|
|
@*/
|
|
|
|
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,
|
2013-11-10 21:59:36 +00:00
|
|
|
globalAttributes: [],
|
2011-10-27 18:50:23 +00:00
|
|
|
height: 0,
|
2012-01-31 10:57:09 +00:00
|
|
|
highlight: null,
|
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,
|
2013-02-15 12:31:49 +00:00
|
|
|
tags: null,
|
2011-10-24 15:58:51 +00:00
|
|
|
tooltip: '',
|
2013-02-25 16:40:14 +00:00
|
|
|
type: 'input',
|
2014-12-16 20:38:41 +00:00
|
|
|
unformat: null,
|
2011-08-12 21:01:19 +00:00
|
|
|
value: '',
|
2013-02-25 16:40:14 +00:00
|
|
|
width: 0
|
2011-08-12 21:01:19 +00:00
|
|
|
})
|
|
|
|
.options(options || {})
|
2012-05-28 19:35:41 +00:00
|
|
|
.update({
|
|
|
|
editing: function() {
|
|
|
|
if (self.options.editing) {
|
|
|
|
// edit will toggle self.options.editing
|
|
|
|
self.options.editing = false;
|
|
|
|
edit();
|
|
|
|
} else {
|
|
|
|
submit();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
height: function() {
|
|
|
|
setCSS({height: self.options.height + 'px'});
|
|
|
|
},
|
|
|
|
width: function() {
|
|
|
|
setCSS({width: self.options.width + 'px'});
|
|
|
|
},
|
|
|
|
highlight: function() {
|
|
|
|
self.$value.html(formatValue());
|
|
|
|
},
|
2013-03-08 11:12:03 +00:00
|
|
|
placeholder: function() {
|
|
|
|
self.$value.html(formatValue());
|
|
|
|
},
|
2012-05-28 19:35:41 +00:00
|
|
|
value: function() {
|
|
|
|
self.$value.html(formatValue());
|
|
|
|
}
|
|
|
|
})
|
2014-09-26 10:24:34 +00:00
|
|
|
.addClass(
|
|
|
|
'OxEditableElement OxKeyboardFocus'
|
|
|
|
+ (self.options.editable ? ' OxEditable' : '')
|
|
|
|
)
|
2012-05-28 14:06:22 +00:00
|
|
|
.on({
|
2013-02-01 14:07:20 +00:00
|
|
|
click: function(e) {
|
2012-01-27 14:29:11 +00:00
|
|
|
var $target = $(e.target);
|
2013-02-01 14:22:17 +00:00
|
|
|
if (!e.shiftKey && ($target.is('a') || ($target = $target.parents('a')).length)) {
|
2013-02-01 14:07:20 +00:00
|
|
|
e.preventDefault();
|
2011-10-27 18:50:23 +00:00
|
|
|
if (self.options.clickLink) {
|
2012-01-17 15:43:46 +00:00
|
|
|
e.target = $target[0];
|
2011-10-27 18:50:23 +00:00
|
|
|
self.options.clickLink(e);
|
|
|
|
} else {
|
2012-01-17 15:43:46 +00:00
|
|
|
document.location.href = $target.attr('href');
|
2011-10-27 18:50:23 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-01 14:07:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.bindEvent({
|
|
|
|
doubleclick: edit,
|
|
|
|
singleclick: function(e) {
|
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>')
|
2014-02-04 07:58:52 +00:00
|
|
|
.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();
|
2013-02-27 07:32:51 +00:00
|
|
|
});
|
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;
|
2012-01-16 11:22:34 +00:00
|
|
|
if (!self.$input) {
|
2011-10-30 08:32:57 +00:00
|
|
|
self.$input = Ox.Input({
|
2014-12-16 13:02:18 +00:00
|
|
|
autocomplete: self.options.autocomplete,
|
|
|
|
autocompleteReplace: self.options.autocompleteReplace,
|
|
|
|
autocompleteReplaceCorrect: self.options.autocompleteReplaceCorrect,
|
|
|
|
autocompleteSelect: self.options.autocompleteSelect,
|
|
|
|
autocompleteSelectHighlight: self.options.autocompleteSelectHighlight,
|
|
|
|
autocompleteSelectMaxWidth: self.options.autocompleteSelectMaxWidth,
|
2014-12-17 15:39:58 +00:00
|
|
|
autocompleteSelectOffset: self.options.autocompleteSelectOffset,
|
2014-12-16 13:02:18 +00:00
|
|
|
autocompleteSelectSubmit: self.options.autocompleteSelectSubmit,
|
2014-12-17 15:39:58 +00:00
|
|
|
autocompleteSelectUpdate: self.options.autocompleteSelectUpdate,
|
2011-10-30 08:32:57 +00:00
|
|
|
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,
|
2012-05-26 15:48:19 +00:00
|
|
|
value: formatInputValue()
|
2011-10-30 08:32:57 +00:00
|
|
|
})
|
|
|
|
.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,
|
2012-02-16 16:35:59 +00:00
|
|
|
insert: function(data) {
|
|
|
|
that.triggerEvent('insert', data);
|
|
|
|
},
|
2011-11-06 11:50:13 +00:00
|
|
|
submit: submit
|
2011-10-30 08:32:57 +00:00
|
|
|
})
|
2012-06-26 16:21:39 +00:00
|
|
|
.appendTo(that);
|
2011-10-30 08:32:57 +00:00
|
|
|
self.$input.find('input').css(self.css);
|
2014-09-24 18:14:57 +00:00
|
|
|
self.$test = self.$value.clone()
|
2012-01-16 11:22:34 +00:00
|
|
|
.css(Ox.extend({display: 'inline-block'}, self.css))
|
|
|
|
.html(formatTestValue())
|
|
|
|
.css({background: 'rgb(192, 192, 192)'})
|
2012-06-26 16:21:39 +00:00
|
|
|
.appendTo(that);
|
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();
|
2012-01-19 16:37:39 +00:00
|
|
|
self.$value.hide();
|
2012-01-16 07:09:16 +00:00
|
|
|
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-02-19 11:06:03 +00:00
|
|
|
} else if (!self.options.editable) {
|
|
|
|
that.triggerEvent('open');
|
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() {
|
2014-02-04 07:58:52 +00:00
|
|
|
return self.options.type == 'input'
|
2014-12-16 20:38:41 +00:00
|
|
|
? (self.options.unformat || Ox.identity)(self.options.value)
|
2014-02-04 07:58:52 +00:00
|
|
|
: 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() {
|
2014-12-16 20:38:41 +00:00
|
|
|
var value = Ox.encodeHTMLEntities(
|
|
|
|
(self.options.unformat || Ox.identity)(self.$input.options('value'))
|
|
|
|
);
|
2012-01-16 11:22:34 +00:00
|
|
|
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;
|
2013-02-27 02:58:52 +00:00
|
|
|
that.removeClass('OxPlaceholder');
|
2011-11-03 16:53:29 +00:00
|
|
|
if (self.options.value === '' && self.options.placeholder) {
|
|
|
|
value = self.options.placeholder;
|
2013-02-27 02:58:52 +00:00
|
|
|
that.addClass('OxPlaceholder');
|
2011-11-03 16:53:29 +00:00
|
|
|
} else if (self.options.format) {
|
2013-02-22 03:34:51 +00:00
|
|
|
value = self.options.format(self.options.value);
|
2011-11-03 16:53:29 +00:00
|
|
|
}
|
2012-01-31 10:57:09 +00:00
|
|
|
if (self.options.highlight) {
|
2012-06-03 08:41:18 +00:00
|
|
|
value = Ox.highlight(
|
|
|
|
value, self.options.highlight, 'OxHighlight', true
|
|
|
|
);
|
2012-01-31 10:57:09 +00:00
|
|
|
}
|
2014-02-04 07:58:52 +00:00
|
|
|
// timeout needed since formatValue is used when assinging self.$value
|
|
|
|
setTimeout(function() {
|
|
|
|
self.$value[
|
|
|
|
self.options.value === '' ? 'removeClass' : 'addClass'
|
|
|
|
]('OxSelectable');
|
|
|
|
})
|
2011-11-03 16:53:29 +00:00
|
|
|
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();
|
2013-02-27 07:32:51 +00:00
|
|
|
return (
|
|
|
|
self.options.type == 'input'
|
2012-05-27 10:40:02 +00:00
|
|
|
? Ox.encodeHTMLEntities(value)
|
2013-11-10 21:59:36 +00:00
|
|
|
: Ox.sanitizeHTML(value, self.options.tags, self.options.globalAttributes)
|
2012-01-16 11:22:34 +00:00
|
|
|
);
|
2012-01-16 07:09:16 +00:00
|
|
|
}
|
|
|
|
|
2012-05-28 19:35:41 +00:00
|
|
|
function setCSS(css) {
|
|
|
|
self.$test && self.$test.css(css);
|
|
|
|
self.$input && self.$input.css(css);
|
|
|
|
self.$input && self.$input.find(self.options.type).css(css);
|
|
|
|
}
|
|
|
|
|
2012-01-16 07:09:16 +00:00
|
|
|
function setSizes() {
|
|
|
|
var height, width;
|
2012-01-19 16:37:39 +00:00
|
|
|
self.$test.css({display: 'inline-block'});
|
2012-01-16 07:09:16 +00:00
|
|
|
height = self.options.height || Ox.limit(self.$test.height(), self.minHeight, self.maxHeight);
|
2012-01-21 14:58:32 +00:00
|
|
|
width = self.$test.width();
|
2014-09-26 12:18:11 +00:00
|
|
|
// +Ox.UI.SCROLLBAR_SIZE to prevent scrollbar from showing up
|
2012-01-21 14:58:32 +00:00
|
|
|
if (self.options.type == 'textarea') {
|
2014-09-26 12:18:11 +00:00
|
|
|
width += Ox.UI.SCROLLBAR_SIZE;
|
2012-01-21 14:58:32 +00:00
|
|
|
}
|
|
|
|
width = self.options.width || Ox.limit(width, self.minWidth, self.maxWidth);
|
2012-01-19 16:37:39 +00:00
|
|
|
self.$test.css({display: 'none'});
|
2013-02-18 09:31:50 +00:00
|
|
|
/*
|
|
|
|
that.css({
|
|
|
|
width: width + 'px',
|
|
|
|
height: height + 'px'
|
|
|
|
});
|
|
|
|
*/
|
2012-01-16 07:09:16 +00:00
|
|
|
self.$input.options({
|
2012-01-19 16:37:39 +00:00
|
|
|
width: width,
|
|
|
|
height: height
|
|
|
|
});
|
2012-01-16 07:09:16 +00:00
|
|
|
self.$input.find(self.options.type).css({
|
2013-02-18 09:31:50 +00:00
|
|
|
width: width + 'px',
|
|
|
|
height: height + 'px'
|
2012-01-16 07:09:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-06-17 22:38:26 +00:00
|
|
|
/*@
|
|
|
|
css <f> 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
|
|
|
};
|