1 define(function(require) {
5 var $ = require('libs/jquery'),
6 _ = require('libs/underscore');
9 var Fragment = function(document) {
10 this.document = document;
12 $.extend(Fragment.prototype, {
19 var NodeFragment = function(document, params) {
20 Fragment.call(this, document);
21 this.node = params.node;
22 this.nodePath = this.isValid() ? params.node.getPath() : null;
24 NodeFragment.prototype = Object.create(Fragment.prototype);
25 $.extend(NodeFragment.prototype, {
27 return this.document.containsNode(this.node);
29 restoreFromPaths: function() {
31 this.node = this.document.getNodeByPath(this.nodePath);
37 var CaretFragment = function(document, params) {
38 this.offset = params.offset;
39 NodeFragment.call(this, document, params);
42 CaretFragment.prototype = Object.create(NodeFragment.prototype);
43 $.extend(CaretFragment.prototype, {
46 return NodeFragment.prototype.isValid.call(this) &&
47 this.node.nodeType === Node.TEXT_NODE &&
48 _.isNumber(this.offset);
54 var RangeFragment = function(document, params) {
55 Fragment.call(this, document);
57 if(params.node1.sameNode(params.node2)) {
58 this.startNode = this.endNode = params.node1;
60 /*jshint bitwise: false*/
62 var node1First = params.node1.nativeNode.compareDocumentPosition(params.node2.nativeNode) & Node.DOCUMENT_POSITION_FOLLOWING;
63 (node1First ? ['start', 'end'] : ['end','start']).forEach(function(prefix, idx) {
64 this[prefix + 'Node'] = params['node'+(idx+1)];
67 this.startNodePath = this.startNode.getPath();
68 this.endNodePath = this.endNode.getPath();
70 RangeFragment.prototype = Object.create(Fragment.prototype);
71 $.extend(RangeFragment.prototype, {
73 return this.document.containsNode(this.startNode) && this.document.containsNode(this.endNode);
75 restoreFromPaths: function() {
76 this.startNode = this.document.getNodeByPath(this.startNodePath);
77 this.endNode = this.document.getNodeByPath(this.endNodePath);
79 hasSiblingBoundries: function() {
80 return this.isValid() && this.startNode.isSiblingOf(this.endNode);
82 boundriesSiblingParents: function() {
83 return this.startNode.document.getSiblingParents({
84 node1: this.startNode,
88 getCommonParent: function() {
89 var siblingParents = this.boundriesSiblingParents();
91 return siblingParents.node1.parent();
96 var TextRangeFragment = function(document, params) {
99 RangeFragment.call(this, document, params);
101 if(this.startNode.sameNode(this.endNode)) {
102 this.startOffset = Math.min(params.offset1, params.offset2);
103 this.endOffset = Math.max(params.offset1, params.offset2);
105 orderChanged = !params.node1.sameNode(this.startNode);
106 this.startOffset = orderChanged ? params.offset2 : params.offset1;
107 this.endOffset = orderChanged ? params.offset1 : params.offset2;
110 TextRangeFragment.prototype = Object.create(RangeFragment.prototype);
111 $.extend(TextRangeFragment.prototype, {
112 isValid: function() {
113 return RangeFragment.prototype.isValid.call(this) &&
114 _.isNumber(this.startOffset) &&
115 _.isNumber(this.endOffset);
119 var FragmentTypes = {
121 NodeFragment: NodeFragment,
122 CaretFragment: CaretFragment,
123 RangeFragment: RangeFragment,
124 TextRangeFragment: TextRangeFragment
126 _.values(FragmentTypes).forEach(function(Type) {
127 $.extend(Type.prototype, FragmentTypes);
130 return FragmentTypes;