1+ /**
2+ * Copyright (C) 2010-2014 KO GmbH <copyright@kogmbh.com>
3+ *
4+ * @licstart
5+ * This file is part of WebODF.
6+ *
7+ * WebODF is free software: you can redistribute it and/or modify it
8+ * under the terms of the GNU Affero General Public License (GNU AGPL)
9+ * as published by the Free Software Foundation, either version 3 of
10+ * the License, or (at your option) any later version.
11+ *
12+ * WebODF is distributed in the hope that it will be useful, but
13+ * WITHOUT ANY WARRANTY; without even the implied warranty of
14+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+ * GNU Affero General Public License for more details.
16+ *
17+ * You should have received a copy of the GNU Affero General Public License
18+ * along with WebODF. If not, see <http://www.gnu.org/licenses/>.
19+ * @licend
20+ *
21+ * @source : http://www.webodf.org/
22+ * @source : https://github.com/kogmbh/WebODF/
23+ */
24+
25+ /*global ops, runtime, odf, core, Range*/
26+
27+ /**
28+ *
29+ * @constructor
30+ * @implements ops.Operation
31+ */
32+ ops . OpSplitList = function OpSplitList ( ) {
33+ "use strict" ;
34+
35+ var memberid ,
36+ timestamp ,
37+ /**@type {!number }*/
38+ sourceStartPosition ,
39+ /**@type {!number }*/
40+ splitPosition ,
41+ odfUtils = odf . OdfUtils ,
42+ /**@const */
43+ xmlns = odf . Namespaces . xmlns ;
44+
45+ /**
46+ * @param {!ops.OpSplitList.InitSpec } data
47+ */
48+ this . init = function ( data ) {
49+ memberid = data . memberid ;
50+ timestamp = data . timestamp ;
51+ sourceStartPosition = data . sourceStartPosition ;
52+ splitPosition = data . splitPosition ;
53+ } ;
54+
55+ this . isEdit = true ;
56+ this . group = undefined ;
57+
58+ /**
59+ * @return {!ops.OpSplitList.Spec }
60+ */
61+ this . spec = function ( ) {
62+ return {
63+ optype : "SplitList" ,
64+ memberid : memberid ,
65+ timestamp : timestamp ,
66+ sourceStartPosition : sourceStartPosition ,
67+ splitPosition : splitPosition
68+ } ;
69+ } ;
70+
71+ /**
72+ * @param {!ops.Document } document
73+ */
74+ this . execute = function ( document ) {
75+ var odtDocument = /**@type {ops.OdtDocument }*/ ( document ) ,
76+ ownerDocument = odtDocument . getDOMDocument ( ) ,
77+ rootNode = odtDocument . getRootNode ( ) ,
78+ collapseRules = new odf . CollapsingRules ( rootNode ) ,
79+ sourceDomPosition = odtDocument . convertCursorStepToDomPoint ( sourceStartPosition ) ,
80+ splitDomPosition = odtDocument . convertCursorStepToDomPoint ( splitPosition ) ,
81+ sourceParagraph = /**@type {!Element }*/ ( odfUtils . getParagraphElement ( sourceDomPosition . node , sourceDomPosition . offset ) ) ,
82+ splitParagraph = /**@type {!Element }*/ ( odfUtils . getParagraphElement ( splitDomPosition . node , splitDomPosition . offset ) ) ,
83+ sourceList = odfUtils . getTopLevelListElement ( sourceParagraph , rootNode ) ,
84+ destinationList ,
85+ splitPositionParentList ,
86+ range = ownerDocument . createRange ( ) ,
87+ fragment ;
88+
89+ if ( ! sourceList || ! splitParagraph ) {
90+ return false ;
91+ }
92+
93+ destinationList = sourceList . cloneNode ( false ) ;
94+ destinationList . removeAttributeNS ( xmlns , "id" ) ;
95+
96+ // create a range starting at before the list item element we split at and
97+ // ending at just after the last list item in the list
98+ range . setStartBefore ( splitParagraph . parentNode ) ;
99+ range . setEndAfter ( sourceList . lastElementChild ) ;
100+
101+ // extract the range to get all list items from the split position to the end of the list
102+ // then collapse any empty nodes at the parent text:list element of the paragraph at the split position
103+ splitPositionParentList = /**@type {!Node }*/ ( splitParagraph . parentNode . parentNode ) ;
104+ fragment = range . extractContents ( ) ;
105+
106+ // don't collapse nodes if its the top level list
107+ if ( splitPositionParentList !== sourceList ) {
108+ collapseRules . mergeChildrenIntoParent ( splitPositionParentList ) ;
109+ }
110+
111+ destinationList . appendChild ( fragment ) ;
112+ sourceList . parentNode . insertBefore ( destinationList , sourceList . nextElementSibling ) ;
113+ return true ;
114+ } ;
115+ } ;
116+
117+ /**@typedef {{
118+ optype: !string,
119+ memberid: !string,
120+ timestamp: !number,
121+ sourceStartPosition: !number,
122+ splitPosition: !number
123+ }}*/
124+ ops . OpSplitList . Spec ;
125+
126+ /**@typedef {{
127+ memberid: !string,
128+ timestamp:(number|undefined),
129+ sourceStartPosition: !number,
130+ splitPosition: !number
131+ }}*/
132+ ops . OpSplitList . InitSpec ;
0 commit comments