more styling
[roojs1] / Roo / lib / Selection.js
1 /**
2  * @class Roo.lib.Selection
3  * @constructor
4  * This is a toolkit, normally used to copy features into a Dom Selection element
5  * Roo.lib.Selection.wrap(x);
6  *
7  *
8  *
9  */
10 Roo.lib.Selection = function() { };
11
12 /**
13  * Wrap a Dom Range object, to give it new features...
14  * @static
15  * @param {Range} the range to wrap
16  */
17 Roo.lib.Selection.wrap = function(r, doc) {
18     Roo.apply(r, Roo.lib.Selection.prototype);
19     r.ownerDocument = doc; // usefull so we dont have to keep referening to it.
20     return r;
21 };
22 /**
23  * find a parent node eg. LI / OL
24  * @param {string|Array} node name or array of nodenames
25  * @return {DomElement|false}
26  */
27 Roo.apply(Roo.lib.Selection.prototype,
28 {
29     /**
30      * the owner document
31      */
32     ownerDocument : false,
33     
34     getRangeAt : function(n)
35     {
36         return Roo.lib.Range.wrap(Selection.prototype.getRangeAt.call(this,n));
37     },
38     
39     /**
40      * insert node at selection 
41      * @param {DomElement|string} node
42      * @param {string} cursor (after|in|none) where to place the cursor after inserting.
43      */
44     insertNode: function(node, cursor)
45     {
46         if (typeof(node) == 'string') {
47             node = this.ownerDocument.createElement(node);
48             if (cursor == 'in') {
49                 node.innerHTML = ' ';
50             }
51         }
52         
53         var range = this.getRangeAt(0);
54         
55         if (this.type != 'Caret') {
56             range.deleteContents();
57         }
58         var sn = node.childNodes[0]; // select the contents.
59
60         
61         
62         range.insertNode(node);
63         if (cursor == 'after') {
64             node.insertAdjacentHTML('afterend', ' ');
65             sn = node.nextSibling;
66         }
67         
68         if (cursor == 'none') {
69             return;
70         }
71         
72         this.cursorText(sn);
73     },
74     
75     cursorText : function(n)
76     {
77        
78         //var range = this.getRangeAt(0);
79         range = Roo.lib.Range.wrap(new Range());
80         //range.selectNode(n);
81         
82         var ix = Array.from(n.parentNode.childNodes).indexOf(n);
83         range.setStart(n.parentNode,ix);
84         range.setEnd(n.parentNode,ix+1);
85         //range.collapse(false);
86          
87         this.removeAllRanges();
88         this.addRange(range);
89         
90         Roo.log([n, range, this,this.baseOffset,this.extentOffset, this.type]);
91     },
92     cursorAfter : function(n)
93     {
94         if (!n.nextSibling || n.nextSibling.nodeValue != ' ') {
95             n.insertAdjacentHTML('afterend', ' ');
96         }
97         this.cursorText (n.nextSibling);
98     }
99         
100     
101 });