Gazelle/static/functions/textareapreview.class.js

87 lines
2.2 KiB
JavaScript
Raw Normal View History

2012-10-27 08:00:09 +00:00
var TextareaPreview;
2013-06-27 08:01:06 +00:00
$(document).ready(function () {
2013-01-23 08:00:38 +00:00
'use strict';
2012-10-27 08:00:09 +00:00
TextareaPreview = function (id, textarea_id) {
2013-01-23 08:00:38 +00:00
if (!isNaN(+id)) {
var textarea = document.getElementById(textarea_id || 'quickpost_' + id);
2012-10-27 08:00:09 +00:00
if (textarea) {
this.id = id;
this.init(textarea);
}
}
};
2013-02-22 08:00:24 +00:00
2012-10-27 08:00:09 +00:00
TextareaPreview.factory = function (arrays) {
var i = 0, j = arrays.length, t;
for (i; i < j; i++) {
t = arrays[i];
t = new TextareaPreview(t[0], t[1]);
}
2013-01-23 08:00:38 +00:00
};
2012-10-27 08:00:09 +00:00
TextareaPreview.prototype = {
constructor: TextareaPreview,
last : false,
2013-01-23 08:00:38 +00:00
text : [['Edit', 'Edit text'], ['Preview', 'Preview text']],
2012-10-27 08:00:09 +00:00
init : function (textarea) {
this.elements(textarea);
2013-01-23 08:00:38 +00:00
2012-10-27 08:00:09 +00:00
this.el.preview
2013-01-23 08:00:38 +00:00
.on('dblclick.preview', $.proxy(this.toggle, this))
2012-10-27 08:00:09 +00:00
.addClass('text_preview')
2013-11-04 08:01:08 +00:00
.addClass('tooltip')
.attr('title', 'Double-click to edit');
2013-02-22 08:00:24 +00:00
2012-10-27 08:00:09 +00:00
this.buttons.preview
.on('click.preview', $.proxy(this.get, this))
.toggleClass('hidden');
},
elements : function (textarea) {
this.el = {
textarea : $(textarea),
2013-01-23 08:00:38 +00:00
wrap : $('#textarea_wrap_' + this.id),
preview : $('#preview_' + this.id),
pwrap : $('#preview_wrap_' + this.id)
2012-10-27 08:00:09 +00:00
};
this.buttons = {
2013-01-23 08:00:38 +00:00
preview : $('.button_preview_' + this.id)
2012-10-27 08:00:09 +00:00
};
},
toggle : function () {
2013-01-23 08:00:38 +00:00
var t = this.text[+(this.buttons.preview.val() === 'Edit')];
2012-10-27 08:00:09 +00:00
this.el.wrap.toggleClass('hidden');
this.el.pwrap.toggleClass('hidden');
2013-01-23 08:00:38 +00:00
this.buttons.preview.val(t[0]);
this.buttons.preview.attr('title', t[1]);
2012-10-27 08:00:09 +00:00
},
get : function () {
2013-01-23 08:00:38 +00:00
if (this.buttons.preview.val() === 'Edit') {
return this.toggle();
}
if (this.buttons.preview.val() === 'Preview'
&& this.el.textarea.val().length > 0) {
2012-10-27 08:00:09 +00:00
this.toggle();
if (this.last !== this.el.textarea.val()) {
2013-11-04 08:01:08 +00:00
this.el.preview.text('Loading...');
2012-10-27 08:00:09 +00:00
this.last = this.el.textarea.val();
this.post();
}
2013-01-23 08:00:38 +00:00
return;
2012-10-27 08:00:09 +00:00
}
},
post : function () {
$.post('ajax.php?action=preview',
2013-01-23 08:00:38 +00:00
{ body : this.el.textarea.val() },
$.proxy(this.html, this),
'html'
);
// .fail(function (jqXHR, textStatus) {
// alert('Request failed: ' + textStatus);
// });
2012-10-27 08:00:09 +00:00
},
html : function (data) {
this.el.preview.html(data);
}
};
2013-11-04 08:01:08 +00:00
});