1 /**
  2  * @fileoverview A utility class which provides common functions for 
  3  * all controls that use CodeMirror.
  4  */
  5 
  6 goog.provide('xrx.codemirror');
  7 
  8 
  9 goog.require('xrx.model');
 10 goog.require('xrx.view');
 11 
 12 
 13 
 14 /**
 15  * @constructor
 16  */
 17 xrx.codemirror = function(element) {
 18 
 19 
 20 
 21   /**
 22    * Call constructor of class xrx.view
 23    * @param {!xrx.codemirror}
 24    * @param {Element}
 25    */
 26   goog.base(this, element);
 27 
 28 
 29 
 30   this.codemirror_;
 31 
 32 
 33 
 34   this.internalUpdate = false;
 35 
 36 
 37 
 38   this.setOptions();
 39 };
 40 goog.inherits(xrx.codemirror, xrx.view);
 41 
 42 
 43 
 44 xrx.codemirror.prototype.setOptions = function() {
 45 
 46   for(var opt in this.options) {
 47     this.codemirror_.setOption(opt, this.options[opt]);
 48   }
 49 };
 50 
 51 
 52 /**
 53  * 
 54  */
 55 xrx.codemirror.prototype.createDom = function() {
 56   var self = this;
 57 
 58   var cm = this.codemirror_ = window.CodeMirror.fromTextArea(this.element_);
 59 
 60   cm.on('beforeChange', function(cm, change) { self.eventBeforeChange(cm, change); });
 61   cm.on('blur', function(cm, change) { self.eventBlur(); });
 62   cm.on('cursorActivity', function() { self.eventCursorActivity(); });
 63   cm.on('focus', function() { self.eventFocus(); });
 64 };
 65 
 66 
 67 
 68 /**
 69  * 
 70  */
 71 xrx.codemirror.prototype.eventBeforeChange = function(cm, change) {
 72 
 73   if (this.internalUpdate === false) {
 74     var doc = this.codemirror_.getDoc().copy();
 75     doc.replaceRange(change.text.join('\n'), change.from, change.to);
 76 
 77     xrx.controller.update(this, doc.getValue());
 78   }
 79 };
 80 
 81 
 82 
 83 /**
 84  * 
 85  */
 86 xrx.codemirror.prototype.eventFocus = function()  {
 87   var arr = new Array(1);
 88   arr[0] = this.getNode();
 89   xrx.model.cursor.setNodes(arr);
 90 };
 91 
 92 
 93 
 94 
 95 xrx.codemirror.prototype.setFocus = function() {
 96   this.codemirror_.focus();
 97 };
 98 
 99 
100 
101 xrx.codemirror.prototype.getValue = function() {
102   return this.codemirror_.getValue();
103 };
104 
105 
106 
107 xrx.codemirror.prototype.setValue = function(xml, internal) {
108   this.internalUpdate = internal || false;
109   this.codemirror_.setValue(xml);
110   this.codemirror_.refresh();
111   this.internalUpdate = false;
112 };
113 
114 
115 
116 /**
117  * 
118  */
119 xrx.codemirror.prototype.refresh = function() {
120   var xml = this.getNode().xml();
121 
122   this.setValue(xml, true);
123 };
124