662dc53ba99e5530e4bba75de2f4bd67dcb79f58
[fnpeditor.git] / src / editor / plugins / core / lists.js
1 define(function() {
2     
3 'use strict';
4 /* globals gettext, interpolate */
5
6
7 var getBoundriesForAList = function(fragment) {
8     var node;
9
10     if(fragment instanceof fragment.RangeFragment && fragment.hasSiblingBoundries()) {
11         return fragment.boundriesSiblingParents();
12     }
13     if(fragment instanceof fragment.NodeFragment) {
14         node = fragment.node.getNearestElementNode();
15         return {
16             node1: node,
17             node2: node
18         };
19     }
20 };
21
22 var countItems = function(boundries) {
23     var ptr = boundries.node1,
24         c = 1;
25     while(ptr && !ptr.sameNode(boundries.node2)) {
26         c++;
27         ptr = ptr.next();
28     }
29     return c;
30 };
31
32 var toggleListAction = function(type) {
33     
34     var execute = {
35         add: function(callback, params) {
36             var boundries = getBoundriesForAList(params.fragment),
37                 listParams = {klass: type === 'Bullet' ? 'list' : 'list.enum'},
38                 action = this;
39
40             if(boundries && boundries.node1) {
41                 listParams.node1 = boundries.node1;
42                 listParams.node2 = boundries.node2;
43                 boundries.node1.document.transaction(function() {
44                     var list = boundries.node1.document.createList(listParams),
45                         item1 = list.object.getItem(0),
46                         text = item1 ? item1.contents()[0] : undefined, //
47                         doc = boundries.node1.document;
48
49                    return doc.createFragment(doc.CaretFragment, {node: text, offset:0});
50                 }, {
51                     metadata: {
52                         description: action.getState().description,
53                         fragment: params.fragment
54                     },
55                     success: callback
56                 });
57             } else {
58                 throw new Error('Invalid boundries');
59             }
60         },
61         remove: function(callback, params) {
62             /* globals Node */
63             var current = params.fragment.node,
64                 action = this;
65
66             var toSearch = current.nodeType === Node.ELEMENT_NODE ? [current] : [];
67             toSearch = toSearch.concat(current.parents());
68             toSearch.some(function(node) {
69                 if(node.is('list')) {
70                     node.document.transaction(function() {
71                         var firstItem = node.object.extractListItems(),
72                             toret;
73                         if(params.fragment.isValid()) {
74                             toret = params.fragment;
75                         } else {
76                             toret = node.document.createFragment(node.document.NodeFragment, {node: firstItem});
77                         }
78                         return toret;
79                     }, {
80                         metadata: {
81                             description: action.getState().description,
82                             fragment: params.fragment
83                         },
84                         success: callback
85                     });
86                     
87                     return true; // break
88                 }
89             }.bind(this));
90         },
91         changeType: function(callback, params) {
92             var node = params.fragment.node,
93                 action = this;
94             node.document.transaction(function() {
95                 var list = node.getParent('list');
96                 list.setClass(type === 'Bullet' ? 'list' : 'list.enum');
97                 if(params.fragment.isValid()) {
98                     return params.fragment;
99                 } else {
100                     return node.document.createFragment(node.document.NodeFragment, {node: list.contents()[0]});
101                 }
102             }, {
103                 metadata: {
104                     description: action.getState().description,
105                     fragment: params.fragment
106                 },
107                 success: callback
108             });
109         }
110     };
111
112     var isToggled = function(params) {
113         if(params.fragment && params.fragment.node && params.fragment.node.isInside('list')) {
114             var list = params.fragment.node.getParent('list');
115             return list.getClass() === (type === 'Bullet' ? 'list' : 'list.enum');
116         }
117         return false;
118     };
119
120     var label = type === 'Bullet' ? gettext('bull. list') : gettext('num. list');
121
122     return {
123         name: 'toggle' + type + 'List',
124         context: ['fragment'],
125         params: {
126             fragment: {type: 'context', name: 'fragment'}
127         },
128         stateDefaults: {
129             label: label
130         },
131         getState: function(params) {
132             if(!params.fragment || !params.fragment.isValid()) {
133                 return false;
134             }
135
136             if(params.fragment instanceof params.fragment.CaretFragment && params.fragment.node.isInside('list')) {
137                 var list = params.fragment.node.getParent('list');
138                 if((list.getClass() === 'list' && type === 'Enum') || (list.getClass() === 'list.enum' && type === 'Bullet')) {
139                     return {
140                         allowed: true,
141                         description: interpolate(gettext('Change list type to %s'), [label]),
142                         execute: execute.changeType
143                     };
144                 }
145                 return {
146                     allowed: true,
147                     toggled: isToggled(params),
148                     description: gettext('Remove list'),
149                     execute: execute.remove
150                 };
151
152             }
153             var boundries = getBoundriesForAList(params.fragment);
154             if(boundries) {
155                 return {
156                     allowed: true,
157                     description: interpolate(gettext('Make %s fragment(s) into list'), [countItems(getBoundriesForAList(params.fragment))]),
158                     execute: execute.add
159                 };
160             }
161         }
162     };
163 };
164
165
166 return {
167     actions: [toggleListAction('Bullet'), toggleListAction('Enum')]
168 };
169
170 });