2013-02-20 13:15:50 -05:00
|
|
|
/*global ace:true */
|
|
|
|
|
2013-02-22 15:41:12 -05:00
|
|
|
/**
|
|
|
|
A view that wraps the ACE editor (http://ace.ajax.org/)
|
2013-02-21 14:09:28 -05:00
|
|
|
|
2013-03-01 12:45:25 -05:00
|
|
|
@class AceEditorView
|
|
|
|
@extends Discourse.View
|
2013-02-22 15:41:12 -05:00
|
|
|
@namespace Discourse
|
|
|
|
@module Discourse
|
2013-03-01 12:45:25 -05:00
|
|
|
**/
|
2013-02-22 15:41:12 -05:00
|
|
|
Discourse.AceEditorView = Discourse.View.extend({
|
|
|
|
mode: 'css',
|
|
|
|
classNames: ['ace-wrapper'],
|
2013-02-21 14:09:28 -05:00
|
|
|
|
2013-02-22 15:41:12 -05:00
|
|
|
contentChanged: (function() {
|
|
|
|
if (this.editor && !this.skipContentChangeEvent) {
|
|
|
|
return this.editor.getSession().setValue(this.get('content'));
|
|
|
|
}
|
|
|
|
}).observes('content'),
|
2013-02-21 14:09:28 -05:00
|
|
|
|
2013-02-22 15:41:12 -05:00
|
|
|
render: function(buffer) {
|
|
|
|
buffer.push("<div class='ace'>");
|
|
|
|
if (this.get('content')) {
|
|
|
|
buffer.push(Handlebars.Utils.escapeExpression(this.get('content')));
|
|
|
|
}
|
|
|
|
return buffer.push("</div>");
|
|
|
|
},
|
2013-02-21 14:09:28 -05:00
|
|
|
|
2013-02-22 15:41:12 -05:00
|
|
|
willDestroyElement: function() {
|
|
|
|
if (this.editor) {
|
|
|
|
this.editor.destroy();
|
|
|
|
this.editor = null;
|
|
|
|
}
|
|
|
|
},
|
2013-02-21 14:09:28 -05:00
|
|
|
|
2013-02-22 15:41:12 -05:00
|
|
|
didInsertElement: function() {
|
2013-04-04 12:59:44 -04:00
|
|
|
|
2013-05-29 14:08:00 -04:00
|
|
|
var aceEditorView = this;
|
|
|
|
|
|
|
|
var initAce = function() {
|
|
|
|
aceEditorView.editor = ace.edit(aceEditorView.$('.ace')[0]);
|
|
|
|
aceEditorView.editor.setTheme("ace/theme/chrome");
|
|
|
|
aceEditorView.editor.setShowPrintMargin(false);
|
|
|
|
aceEditorView.editor.getSession().setMode("ace/mode/" + (aceEditorView.get('mode')));
|
|
|
|
aceEditorView.editor.on("change", function(e) {
|
|
|
|
aceEditorView.skipContentChangeEvent = true;
|
|
|
|
aceEditorView.set('content', aceEditorView.editor.getSession().getValue());
|
|
|
|
aceEditorView.skipContentChangeEvent = false;
|
2013-02-22 15:41:12 -05:00
|
|
|
});
|
|
|
|
};
|
2013-05-29 14:08:00 -04:00
|
|
|
|
2013-02-22 15:41:12 -05:00
|
|
|
if (window.ace) {
|
2013-05-29 14:08:00 -04:00
|
|
|
initAce();
|
2013-02-22 15:41:12 -05:00
|
|
|
} else {
|
2013-06-10 16:48:50 -04:00
|
|
|
$LAB.script('/javascripts/ace/ace.js').wait(initAce);
|
2013-02-20 13:15:50 -05:00
|
|
|
}
|
2013-02-22 15:41:12 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-05-29 15:00:35 -04:00
|
|
|
|
2013-06-10 16:48:50 -04:00
|
|
|
Discourse.View.registerHelper('aceEditor', Discourse.AceEditorView);
|