2015-04-04 00:50:08 +03:00
|
|
|
/* global CodeMirror */
|
|
|
|
import Ember from 'ember';
|
|
|
|
|
2015-08-19 14:55:40 +03:00
|
|
|
export default Ember.Component.extend({
|
2015-04-04 00:50:08 +03:00
|
|
|
|
|
|
|
// DOM stuff
|
|
|
|
classNameBindings: ['isFocused:focused'],
|
|
|
|
isFocused: false,
|
|
|
|
|
|
|
|
value: '', // make sure a value exists
|
2015-11-20 19:40:41 +03:00
|
|
|
_editor: null, // reference to CodeMirror editor
|
2015-04-04 00:50:08 +03:00
|
|
|
|
|
|
|
// options for the editor
|
|
|
|
lineNumbers: true,
|
|
|
|
indentUnit: 4,
|
|
|
|
mode: 'htmlmixed',
|
|
|
|
theme: 'xq-light',
|
|
|
|
|
|
|
|
didInsertElement: function () {
|
|
|
|
var options = this.getProperties('lineNumbers', 'indentUnit', 'mode', 'theme'),
|
2015-10-15 15:03:26 +03:00
|
|
|
editor = new CodeMirror(this.get('element'), options);
|
|
|
|
|
2015-04-04 00:50:08 +03:00
|
|
|
editor.getDoc().setValue(this.get('value'));
|
|
|
|
|
|
|
|
// events
|
2015-10-15 15:03:26 +03:00
|
|
|
editor.on('focus', Ember.run.bind(this, 'set', 'isFocused', true));
|
|
|
|
editor.on('blur', Ember.run.bind(this, 'set', 'isFocused', false));
|
|
|
|
editor.on('change', () => {
|
|
|
|
Ember.run(this, function () {
|
|
|
|
this.set('value', editor.getDoc().getValue());
|
|
|
|
});
|
2015-04-04 00:50:08 +03:00
|
|
|
});
|
|
|
|
|
2015-11-20 19:40:41 +03:00
|
|
|
this._editor = editor;
|
2015-04-04 00:50:08 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement: function () {
|
2015-11-20 19:40:41 +03:00
|
|
|
var editor = this._editor.getWrapperElement();
|
2015-04-04 00:50:08 +03:00
|
|
|
editor.parentNode.removeChild(editor);
|
2015-11-20 19:40:41 +03:00
|
|
|
this._editor = null;
|
2015-04-04 00:50:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|