placeholder.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var Range = require("./range").Range;
  33. var EventEmitter = require("./lib/event_emitter").EventEmitter;
  34. var oop = require("./lib/oop");
  35. /**
  36. * @class PlaceHolder
  37. *
  38. *
  39. **/
  40. /**
  41. * - session (Document): The document to associate with the anchor
  42. * - length (Number): The starting row position
  43. * - pos (Number): The starting column position
  44. * - others (String):
  45. * - mainClass (String):
  46. * - othersClass (String):
  47. *
  48. *
  49. * @constructor
  50. **/
  51. var PlaceHolder = function(session, length, pos, others, mainClass, othersClass) {
  52. var _self = this;
  53. this.length = length;
  54. this.session = session;
  55. this.doc = session.getDocument();
  56. this.mainClass = mainClass;
  57. this.othersClass = othersClass;
  58. this.$onUpdate = this.onUpdate.bind(this);
  59. this.doc.on("change", this.$onUpdate);
  60. this.$others = others;
  61. this.$onCursorChange = function() {
  62. setTimeout(function() {
  63. _self.onCursorChange();
  64. });
  65. };
  66. this.$pos = pos;
  67. // Used for reset
  68. var undoStack = session.getUndoManager().$undoStack || session.getUndoManager().$undostack || {length: -1};
  69. this.$undoStackDepth = undoStack.length;
  70. this.setup();
  71. session.selection.on("changeCursor", this.$onCursorChange);
  72. };
  73. (function() {
  74. oop.implement(this, EventEmitter);
  75. /**
  76. * PlaceHolder.setup()
  77. *
  78. * TODO
  79. *
  80. **/
  81. this.setup = function() {
  82. var _self = this;
  83. var doc = this.doc;
  84. var session = this.session;
  85. var pos = this.$pos;
  86. this.pos = doc.createAnchor(pos.row, pos.column);
  87. this.markerId = session.addMarker(new Range(pos.row, pos.column, pos.row, pos.column + this.length), this.mainClass, null, false);
  88. this.pos.on("change", function(event) {
  89. session.removeMarker(_self.markerId);
  90. _self.markerId = session.addMarker(new Range(event.value.row, event.value.column, event.value.row, event.value.column+_self.length), _self.mainClass, null, false);
  91. });
  92. this.others = [];
  93. this.$others.forEach(function(other) {
  94. var anchor = doc.createAnchor(other.row, other.column);
  95. _self.others.push(anchor);
  96. });
  97. session.setUndoSelect(false);
  98. };
  99. /**
  100. * PlaceHolder.showOtherMarkers()
  101. *
  102. * TODO
  103. *
  104. **/
  105. this.showOtherMarkers = function() {
  106. if(this.othersActive) return;
  107. var session = this.session;
  108. var _self = this;
  109. this.othersActive = true;
  110. this.others.forEach(function(anchor) {
  111. anchor.markerId = session.addMarker(new Range(anchor.row, anchor.column, anchor.row, anchor.column+_self.length), _self.othersClass, null, false);
  112. anchor.on("change", function(event) {
  113. session.removeMarker(anchor.markerId);
  114. anchor.markerId = session.addMarker(new Range(event.value.row, event.value.column, event.value.row, event.value.column+_self.length), _self.othersClass, null, false);
  115. });
  116. });
  117. };
  118. /**
  119. * PlaceHolder.hideOtherMarkers()
  120. *
  121. * Hides all over markers in the [[EditSession `EditSession`]] that are not the currently selected one.
  122. *
  123. **/
  124. this.hideOtherMarkers = function() {
  125. if(!this.othersActive) return;
  126. this.othersActive = false;
  127. for (var i = 0; i < this.others.length; i++) {
  128. this.session.removeMarker(this.others[i].markerId);
  129. }
  130. };
  131. /**
  132. * PlaceHolder@onUpdate(e)
  133. *
  134. * Emitted when the place holder updates.
  135. *
  136. **/
  137. this.onUpdate = function(event) {
  138. var delta = event.data;
  139. var range = delta.range;
  140. if(range.start.row !== range.end.row) return;
  141. if(range.start.row !== this.pos.row) return;
  142. if (this.$updating) return;
  143. this.$updating = true;
  144. var lengthDiff = delta.action === "insertText" ? range.end.column - range.start.column : range.start.column - range.end.column;
  145. if(range.start.column >= this.pos.column && range.start.column <= this.pos.column + this.length + 1) {
  146. var distanceFromStart = range.start.column - this.pos.column;
  147. this.length += lengthDiff;
  148. if(!this.session.$fromUndo) {
  149. if(delta.action === "insertText") {
  150. for (var i = this.others.length - 1; i >= 0; i--) {
  151. var otherPos = this.others[i];
  152. var newPos = {row: otherPos.row, column: otherPos.column + distanceFromStart};
  153. if(otherPos.row === range.start.row && range.start.column < otherPos.column)
  154. newPos.column += lengthDiff;
  155. this.doc.insert(newPos, delta.text);
  156. }
  157. } else if(delta.action === "removeText") {
  158. for (var i = this.others.length - 1; i >= 0; i--) {
  159. var otherPos = this.others[i];
  160. var newPos = {row: otherPos.row, column: otherPos.column + distanceFromStart};
  161. if(otherPos.row === range.start.row && range.start.column < otherPos.column)
  162. newPos.column += lengthDiff;
  163. this.doc.remove(new Range(newPos.row, newPos.column, newPos.row, newPos.column - lengthDiff));
  164. }
  165. }
  166. // Special case: insert in beginning
  167. if(range.start.column === this.pos.column && delta.action === "insertText") {
  168. setTimeout(function() {
  169. this.pos.setPosition(this.pos.row, this.pos.column - lengthDiff);
  170. for (var i = 0; i < this.others.length; i++) {
  171. var other = this.others[i];
  172. var newPos = {row: other.row, column: other.column - lengthDiff};
  173. if(other.row === range.start.row && range.start.column < other.column)
  174. newPos.column += lengthDiff;
  175. other.setPosition(newPos.row, newPos.column);
  176. }
  177. }.bind(this), 0);
  178. }
  179. else if(range.start.column === this.pos.column && delta.action === "removeText") {
  180. setTimeout(function() {
  181. for (var i = 0; i < this.others.length; i++) {
  182. var other = this.others[i];
  183. if(other.row === range.start.row && range.start.column < other.column) {
  184. other.setPosition(other.row, other.column - lengthDiff);
  185. }
  186. }
  187. }.bind(this), 0);
  188. }
  189. }
  190. this.pos._emit("change", {value: this.pos});
  191. for (var i = 0; i < this.others.length; i++) {
  192. this.others[i]._emit("change", {value: this.others[i]});
  193. }
  194. }
  195. this.$updating = false;
  196. };
  197. /**
  198. * PlaceHolder@onCursorChange(e)
  199. *
  200. * Emitted when the cursor changes.
  201. *
  202. **/
  203. this.onCursorChange = function(event) {
  204. if (this.$updating) return;
  205. var pos = this.session.selection.getCursor();
  206. if(pos.row === this.pos.row && pos.column >= this.pos.column && pos.column <= this.pos.column + this.length) {
  207. this.showOtherMarkers();
  208. this._emit("cursorEnter", event);
  209. } else {
  210. this.hideOtherMarkers();
  211. this._emit("cursorLeave", event);
  212. }
  213. };
  214. /**
  215. * PlaceHolder.detach()
  216. *
  217. * TODO
  218. *
  219. **/
  220. this.detach = function() {
  221. this.session.removeMarker(this.markerId);
  222. this.hideOtherMarkers();
  223. this.doc.removeEventListener("change", this.$onUpdate);
  224. this.session.selection.removeEventListener("changeCursor", this.$onCursorChange);
  225. this.pos.detach();
  226. for (var i = 0; i < this.others.length; i++) {
  227. this.others[i].detach();
  228. }
  229. this.session.setUndoSelect(true);
  230. };
  231. /**
  232. * PlaceHolder.cancel()
  233. *
  234. * TODO
  235. *
  236. **/
  237. this.cancel = function() {
  238. if(this.$undoStackDepth === -1)
  239. throw Error("Canceling placeholders only supported with undo manager attached to session.");
  240. var undoManager = this.session.getUndoManager();
  241. var undosRequired = (undoManager.$undoStack || undoManager.$undostack).length - this.$undoStackDepth;
  242. for (var i = 0; i < undosRequired; i++) {
  243. undoManager.undo(true);
  244. }
  245. };
  246. }).call(PlaceHolder.prototype);
  247. exports.PlaceHolder = PlaceHolder;
  248. });