1 | /*
|
---|
2 | * file: ChangeContainerAction.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.ChangeContainerAction");
|
---|
22 |
|
---|
23 | (function() {
|
---|
24 |
|
---|
25 | var changeBlockMap = $createLookupMap("p,pre,h1,h2,h3,h4,h5,h6,address");
|
---|
26 |
|
---|
27 | _registerAction("ChangeContainer", {
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * An undoable action. Changes all block level elements in a given range - creates new containers where needed.
|
---|
31 | *
|
---|
32 | * @author Brook Novak
|
---|
33 | *
|
---|
34 | * @param {String} containerTag (Optional) The container nodename to encapsulate the range.
|
---|
35 | *
|
---|
36 | * @param {Node} startNode (Optional) The starting dom node of the range to align.
|
---|
37 | * If not provided then the current selection will be used.
|
---|
38 | * If provieded must also provide endNode
|
---|
39 | *
|
---|
40 | * @param {Node} endNode (Optional) The ending dom node of the range to align. Can be the same as start node
|
---|
41 | * If not provided then the current selection will be used.
|
---|
42 | *
|
---|
43 | */
|
---|
44 | exec : function(containerTag, startNode, endNode) {
|
---|
45 |
|
---|
46 | if (!containerTag)
|
---|
47 | containerTag = "p";
|
---|
48 | containerTag = containerTag.toLowerCase();
|
---|
49 |
|
---|
50 | debug.assert(changeBlockMap[containerTag], "Cannot change container type to \"" + changeBlockMap + "\"");
|
---|
51 |
|
---|
52 | // Auto-set range if not provided.
|
---|
53 | if (!startNode) {
|
---|
54 |
|
---|
55 | if (!this.selBefore)
|
---|
56 | return; // Nothing to select
|
---|
57 |
|
---|
58 | if (this.selBefore.endNode) {
|
---|
59 | startNode = this.selBeforeOrdered.startNode;
|
---|
60 | endNode = this.selBeforeOrdered.endNode;
|
---|
61 | } else
|
---|
62 | startNode = endNode = this.selBefore.startNode;
|
---|
63 |
|
---|
64 | }
|
---|
65 |
|
---|
66 | debug.assert(endNode, "Supplied start node but not the end node");
|
---|
67 |
|
---|
68 |
|
---|
69 | var containers = _getNormalizedContainerRange(startNode, endNode);
|
---|
70 |
|
---|
71 | for (var i in containers) {
|
---|
72 |
|
---|
73 | var container = containers[i];
|
---|
74 |
|
---|
75 | // Can this block level container be changed to a certain type?
|
---|
76 | if (_nodeName(container) != containerTag && changeBlockMap[_nodeName(container)]) {
|
---|
77 |
|
---|
78 | var migrants = [],
|
---|
79 | migrant,
|
---|
80 | parent = container.parentNode,
|
---|
81 | index = _indexInParent(container);
|
---|
82 |
|
---|
83 | // Remove container contents
|
---|
84 | while(migrant = container.firstChild) {
|
---|
85 | migrants.push(migrant);
|
---|
86 | _execOp(_Operation.REMOVE_NODE, migrant);
|
---|
87 | }
|
---|
88 |
|
---|
89 | // Remove container
|
---|
90 | _execOp(_Operation.REMOVE_NODE, container);
|
---|
91 |
|
---|
92 | // Insert new container
|
---|
93 | var newContainer = $createElement(containerTag);
|
---|
94 | _execOp(_Operation.INSERT_NODE, newContainer, parent, index);
|
---|
95 |
|
---|
96 | // Migrate original container elements
|
---|
97 | for (var j in migrants) {
|
---|
98 | _execOp(_Operation.INSERT_NODE, migrants[j], newContainer);
|
---|
99 | }
|
---|
100 |
|
---|
101 | }
|
---|
102 |
|
---|
103 | }
|
---|
104 |
|
---|
105 | this.selAfter = this.selBefore;
|
---|
106 |
|
---|
107 | }
|
---|
108 |
|
---|
109 | });
|
---|
110 |
|
---|
111 | })();
|
---|