46:14d6c0a48023
Anton Shestakov <engored@ya.ru>, Sun, 05 Apr 2015 03:44:40 +0800
Catch and work around exceptions when localStorage is not available

next change 61:ecb8269aa17f
previous change 0:a02e94c5b96b

static/js/framework/views/inline.js

Permissions: -rw-r--r--

Other formats: Feeds:
var InlineEditorView = Backbone.View.extend({
initialize: function(options) {
this.render(options);
},
renderInput: function(options) {
var view = this;
this.$input = $('<input>').attr('type', 'text').addClass('input-xlarge').val(options.target.text());
this.$input.keyup(function(e) {
switch (e.keyCode) {
case 13:
view.$saveButton.click();
break;
case 27:
view.$cancelButton.click();
break;
}
});
this.$el.append(this.$input, this.$saveButton, this.$cancelButton);
options.target.after(this.$el);
this.$input.focus();
return this;
},
renderTextarea: function(options) {
var view = this;
this.$input = $('<textarea>').addClass('input-xlarge').attr('rows', 3).text(options.target.text());
this.$input.keyup(function(e) {
switch (e.keyCode) {
case 13:
if (e.ctrlKey) {
view.$saveButton.click();
}
break;
case 27:
view.$cancelButton.click();
break;
}
});
this.$el.append(this.$input, this.$saveButton, this.$cancelButton);
options.target.after(this.$el);
this.$input.focus();
return this;
},
render: function(options) {
var view = this;
this.$saveButton = $('<button>').addClass('btn btn-small btn-success').html('<i class="icon-white icon-ok"></i>');
this.$cancelButton = $('<button>').addClass('btn btn-small').html('<i class="icon-remove"></i>');
this.$el = $('<div>').addClass('inline-editor input-append');
this.$cancelButton.click(function() {
view.$el.remove();
options.target.show();
});
this.$saveButton.click(function() {
view.trigger('save', view.$input.val());
});
switch (options.type) {
case 'input':
return this.renderInput(options);
case 'textarea':
return this.renderTextarea(options);
}
}
});
var CollectionViewWithInlineEditor = CollectionView.extend({
bindInlineEditable: function(model, selector) {
model.$item.delegate(selector, 'click', function(e) {
e.preventDefault();
var $this = $(this);
var attribute = $this.attr('data-model-attribute');
var inlineEditor = new InlineEditorView({
target: $this,
type: $this.attr('data-input-type') || 'input'
});
$this.hide();
inlineEditor.on('save', function(value) {
var data = {};
data[attribute] = value;
model.adhoc(data).then(function() {
inlineEditor.remove();
$this.show();
});
});
});
}
});