1 | /*
|
---|
2 | * file: PromoteItemAction.js
|
---|
3 | *
|
---|
4 | * @BEGINLICENSE
|
---|
5 | * Copyright 2010 Brook Novak (email : [email protected])
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | * You should have received a copy of the GNU General Public License
|
---|
15 | * along with this program; if not, write to the Free Software
|
---|
16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
17 | * @ENDLICENSE
|
---|
18 | */
|
---|
19 |
|
---|
20 | // @DEPENDS: UndoMan
|
---|
21 | bootstrap.provides("actions.PromoteItemAction");
|
---|
22 |
|
---|
23 | _registerAction("PromoteItem", {
|
---|
24 |
|
---|
25 | exec : function(startNode, endNode) {
|
---|
26 |
|
---|
27 | var t = this;
|
---|
28 |
|
---|
29 | // Auto-set range if not provided.
|
---|
30 | if (!startNode) {
|
---|
31 |
|
---|
32 | if (!t.selBefore)
|
---|
33 | return; // Nothing to promote
|
---|
34 |
|
---|
35 | if (t.selBefore.endNode) {
|
---|
36 | startNode = t.selBeforeOrdered.startNode;
|
---|
37 | endNode = t.selBeforeOrdered.endNode;
|
---|
38 | } else
|
---|
39 | startNode = endNode = t.selBefore.startNode;
|
---|
40 |
|
---|
41 | }
|
---|
42 |
|
---|
43 | debug.assert(endNode, "Supplied start node but not the end node");
|
---|
44 |
|
---|
45 | var ca = _findAncestor(_getCommonAncestor(startNode, endNode, 1), docBody, function(testNode) {
|
---|
46 | var nn = _nodeName(testNode);
|
---|
47 | return nn == "li" || nn == "ol" || nn == "ul";
|
---|
48 | }, 1);
|
---|
49 |
|
---|
50 | // Found a list item / list container in the given range?
|
---|
51 | if (ca) {
|
---|
52 |
|
---|
53 | // Get all list items in the given range
|
---|
54 | var listItems = [];
|
---|
55 | if (_nodeName(ca) == "li")
|
---|
56 | listItems.push(ca);
|
---|
57 | else {
|
---|
58 | _visitAllNodes(ca, startNode, true, function(domNode){
|
---|
59 | if (_nodeName(domNode) == "li") listItems.push(domNode);
|
---|
60 | return domNode != endNode;
|
---|
61 | });
|
---|
62 | // The traversal above will skip the start node's li
|
---|
63 | var li = _findAncestor(startNode, ca, function(testNode) {return _nodeName(testNode) == "li";}, 1);
|
---|
64 | if (li)
|
---|
65 | listItems.push(li);
|
---|
66 | }
|
---|
67 |
|
---|
68 | // Promote LI: Top => down
|
---|
69 | for (var i in listItems) {
|
---|
70 | promoteLI(listItems[i]);
|
---|
71 | }
|
---|
72 |
|
---|
73 | }
|
---|
74 |
|
---|
75 | t.selAfter = t.selBefore;
|
---|
76 |
|
---|
77 | function promoteLI(li) {
|
---|
78 |
|
---|
79 | // Can this list item be promoted? It must have a list item at the same level preceeding it
|
---|
80 | var prevLICon = li.previousSibling, nextLICon = li.nextSibling;
|
---|
81 | while (prevLICon && prevLICon.nodeType == Node.TEXT_NODE) {
|
---|
82 | prevLICon = prevLICon.previousSibling;
|
---|
83 | }
|
---|
84 | while (nextLICon && nextLICon.nodeType == Node.TEXT_NODE) {
|
---|
85 | nextLICon = nextLICon.nextSibling;
|
---|
86 | }
|
---|
87 |
|
---|
88 | var subList;
|
---|
89 |
|
---|
90 | if (prevLICon && (_nodeName(prevLICon) == "ol" || _nodeName(prevLICon) == "ul")) // Add to end of exisiting sb-list within previous LI?
|
---|
91 | subList = prevLICon;
|
---|
92 | else {
|
---|
93 | // Create a new LI->[OL/UL] and insert it before the LI to promote
|
---|
94 | subList = li.parentNode.cloneNode(false);
|
---|
95 | _execOp(_Operation.INSERT_NODE, subList, li.parentNode, _indexInParent(li));
|
---|
96 | }
|
---|
97 |
|
---|
98 | // Move the LI into the sublist
|
---|
99 | _execOp(_Operation.REMOVE_NODE, li);
|
---|
100 | _execOp(_Operation.INSERT_NODE, li, subList);
|
---|
101 |
|
---|
102 | // Check if need to merge follow list items
|
---|
103 | if (nextLICon && _nodeName(nextLICon) == _nodeName(subList)) {
|
---|
104 |
|
---|
105 | // If so then merge the list containers
|
---|
106 |
|
---|
107 | // Merge the lists
|
---|
108 | _execOp(_Operation.REMOVE_NODE, nextLICon);
|
---|
109 | while(nextLICon.firstChild) {
|
---|
110 | var migrant = nextLICon.firstChild;
|
---|
111 | _execOp(_Operation.REMOVE_NODE, migrant);
|
---|
112 | _execOp(_Operation.INSERT_NODE, migrant, subList);
|
---|
113 | }
|
---|
114 |
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | }
|
---|
120 | });
|
---|
121 |
|
---|