5a1b875ee15ce7c5dbbccbc075289c034d9a8e40
[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                     },
54                     success: callback
55                 });
56             } else {
57                 throw new Error('Invalid boundries');
58             }
59         },
60         remove: function(callback, params) {
61             /* globals Node */
62             var current = params.fragment.node,
63                 action = this;
64
65             var toSearch = current.nodeType === Node.ELEMENT_NODE ? [current] : [];
66             toSearch = toSearch.concat(current.parents());
67             toSearch.some(function(node) {
68                 if(node.is('list')) {
69                     node.document.transaction(function() {
70                         var firstItem = node.object.extractListItems(),
71                             toret;
72                         if(params.fragment.isValid()) {
73                             toret = params.fragment;
74                         } else {
75                             toret = node.document.createFragment(node.document.NodeFragment, {node: firstItem});
76                         }
77                         return toret;
78                     }, {
79                         metadata: {
80                             description: action.getState().description
81                         },
82                         success: callback
83                     });
84                     
85                     return true; // break
86                 }
87             }.bind(this));
88         },
89         changeType: function(callback, params) {
90             var node = params.fragment.node,
91                 action = this;
92             node.document.transaction(function() {
93                 var list = node.getParent('list');
94                 list.setClass(type === 'Bullet' ? 'list' : 'list.enum');
95                 if(params.fragment.isValid()) {
96                     return params.fragment;
97                 } else {
98                     return node.document.createFragment(node.document.NodeFragment, {node: list.contents()[0]});
99                 }
100             }, {
101                 metadata: {
102                     description: action.getState().description
103                 },
104                 success: callback
105             });
106         }
107     };
108
109     var isToggled = function(params) {
110         if(params.fragment && params.fragment.node && params.fragment.node.isInside('list')) {
111             var list = params.fragment.node.getParent('list');
112             return list.getClass() === (type === 'Bullet' ? 'list' : 'list.enum');
113         }
114         return false;
115     };
116
117     var label = type === 'Bullet' ? gettext('bull. list') : gettext('num. list');
118
119     return {
120         name: 'toggle' + type + 'List',
121         context: ['fragment'],
122         params: {
123             fragment: {type: 'context', name: 'fragment'}
124         },
125         stateDefaults: {
126             label: label
127         },
128         getState: function(params) {
129             if(!params.fragment || !params.fragment.isValid()) {
130                 return false;
131             }
132
133             if(params.fragment instanceof params.fragment.CaretFragment && params.fragment.node.isInside('list')) {
134                 var list = params.fragment.node.getParent('list');
135                 if((list.getClass() === 'list' && type === 'Enum') || (list.getClass() === 'list.enum' && type === 'Bullet')) {
136                     return {
137                         allowed: true,
138                         description: interpolate(gettext('Change list type to %s'), [label]),
139                         execute: execute.changeType
140                     };
141                 }
142                 return {
143                     allowed: true,
144                     toggled: isToggled(params),
145                     description: gettext('Remove list'),
146                     execute: execute.remove
147                 };
148
149             }
150             var boundries = getBoundriesForAList(params.fragment);
151             if(boundries) {
152                 return {
153                     allowed: true,
154                     description: interpolate(gettext('Make %s fragment(s) into list'), [countItems(getBoundriesForAList(params.fragment))]),
155                     execute: execute.add
156                 };
157             }
158         }
159     };
160 };
161
162
163 return {
164     actions: [toggleListAction('Bullet'), toggleListAction('Enum')]
165 };
166
167 });