2014-03-02 18:30:35 +04:00
|
|
|
/* global CodeMirror*/
|
|
|
|
|
|
|
|
var onChangeHandler = function (cm) {
|
|
|
|
cm.component.set('value', cm.getDoc().getValue());
|
|
|
|
};
|
|
|
|
|
|
|
|
var onScrollHandler = function (cm) {
|
|
|
|
var scrollInfo = cm.getScrollInfo(),
|
|
|
|
percentage = scrollInfo.top / scrollInfo.height,
|
|
|
|
component = cm.component;
|
|
|
|
|
|
|
|
// throttle scroll updates
|
|
|
|
component.throttle = Ember.run.throttle(component, function () {
|
|
|
|
this.set('scrollPosition', percentage);
|
|
|
|
}, 50);
|
|
|
|
};
|
|
|
|
|
2014-03-04 00:18:10 +04:00
|
|
|
var Codemirror = Ember.TextArea.extend({
|
2014-03-02 18:30:35 +04:00
|
|
|
initCodemirror: function () {
|
|
|
|
// create codemirror
|
|
|
|
this.codemirror = CodeMirror.fromTextArea(this.get('element'), {
|
|
|
|
lineWrapping: true
|
|
|
|
});
|
|
|
|
this.codemirror.component = this; // save reference to this
|
|
|
|
|
|
|
|
// propagate changes to value property
|
2014-03-04 00:18:10 +04:00
|
|
|
this.codemirror.on('change', onChangeHandler);
|
2014-03-02 18:30:35 +04:00
|
|
|
|
|
|
|
// on scroll update scrollPosition property
|
2014-03-04 00:18:10 +04:00
|
|
|
this.codemirror.on('scroll', onScrollHandler);
|
|
|
|
}.on('didInsertElement'),
|
2014-03-02 18:30:35 +04:00
|
|
|
|
|
|
|
removeThrottle: function () {
|
|
|
|
Ember.run.cancel(this.throttle);
|
2014-03-04 00:18:10 +04:00
|
|
|
}.on('willDestroyElement'),
|
2014-03-02 18:30:35 +04:00
|
|
|
|
|
|
|
removeCodemirrorHandlers: function () {
|
|
|
|
// not sure if this is needed.
|
2014-03-04 00:18:10 +04:00
|
|
|
this.codemirror.off('change', onChangeHandler);
|
|
|
|
this.codemirror.off('scroll', onScrollHandler);
|
|
|
|
}.on('willDestroyElement')
|
|
|
|
});
|
2014-03-02 18:30:35 +04:00
|
|
|
|
2014-05-28 23:26:05 +04:00
|
|
|
export default Codemirror;
|