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 = params.node.getPath();
24 NodeFragment.prototype = Object.create(Fragment.prototype);
25 $.extend(NodeFragment.prototype, {
27 return this.document.containsNode(this.node);
29 restoreFromPaths: function() {
30 this.node = this.document.getNodeByPath(this.nodePath);
35 var CaretFragment = function(document, params) {
36 NodeFragment.call(this, document, params);
37 this.offset = params.offset;
40 CaretFragment.prototype = Object.create(NodeFragment.prototype);
41 $.extend(CaretFragment.prototype, {
44 return NodeFragment.prototype.isValid.call(this) &&
45 this.node.nodeType === Node.TEXT_NODE &&
46 _.isNumber(this.offset);
52 var RangeFragment = function(document, params) {
53 Fragment.call(this, document);
55 if(params.node1.sameNode(params.node2)) {
56 this.startNode = this.endNode = params.node1;
58 /*jshint bitwise: false*/
60 var node1First = params.node1.nativeNode.compareDocumentPosition(params.node2.nativeNode) & Node.DOCUMENT_POSITION_FOLLOWING;
61 (node1First ? ['start', 'end'] : ['end','start']).forEach(function(prefix, idx) {
62 this[prefix + 'Node'] = params['node'+(idx+1)];
65 this.startNodePath = this.startNode.getPath();
66 this.endNodePath = this.endNode.getPath();
68 RangeFragment.prototype = Object.create(Fragment.prototype);
69 $.extend(RangeFragment.prototype, {
71 return this.document.containsNode(this.startNode) && this.document.containsNode(this.endNode);
73 restoreFromPaths: function() {
74 this.startNode = this.document.getNodeByPath(this.startNodePath);
75 this.endNode = this.document.getNodeByPath(this.endNodePath);
77 hasSiblingBoundries: function() {
78 return this.isValid() && this.startNode.isSiblingOf(this.endNode);
80 boundriesSiblingParents: function() {
81 return this.startNode.document.getSiblingParents({
82 node1: this.startNode,
86 getCommonParent: function() {
87 var siblingParents = this.boundriesSiblingParents();
89 return siblingParents.node1.parent();
94 var TextRangeFragment = function(document, params) {
97 RangeFragment.call(this, document, params);
99 if(this.startNode.sameNode(this.endNode)) {
100 this.startOffset = Math.min(params.offset1, params.offset2);
101 this.endOffset = Math.max(params.offset1, params.offset2);
103 orderChanged = !params.node1.sameNode(this.startNode);
104 this.startOffset = orderChanged ? params.offset2 : params.offset1;
105 this.endOffset = orderChanged ? params.offset1 : params.offset2;
108 TextRangeFragment.prototype = Object.create(RangeFragment.prototype);
109 $.extend(TextRangeFragment.prototype, {
110 isValid: function() {
111 return RangeFragment.prototype.isValid.call(this) &&
112 _.isNumber(this.startOffset) &&
113 _.isNumber(this.endOffset);
117 var FragmentTypes = {
119 NodeFragment: NodeFragment,
120 CaretFragment: CaretFragment,
121 RangeFragment: RangeFragment,
122 TextRangeFragment: TextRangeFragment
124 _.values(FragmentTypes).forEach(function(Type) {
125 $.extend(Type.prototype, FragmentTypes);
128 return FragmentTypes;