editor: Setting descriptions for transactions performed in actions
[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(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                     boundries.node1.document.createList(listParams);
45                 }, {
46                     metadata: {
47                         description: action.getState().description
48                     }
49                 });
50             } else {
51                 throw new Error('Invalid boundries');
52             }
53         },
54         remove: function(params) {
55             /* globals Node */
56             var current = params.fragment.node,
57                 action = this;
58
59             var toSearch = current.nodeType === Node.ELEMENT_NODE ? [current] : [];
60             toSearch = toSearch.concat(current.parents());
61             toSearch.some(function(node) {
62                 if(node.is('list')) {
63                     node.document.transaction(function() {
64                         node.object.extractListItems();
65                     }, {
66                         metadata: {
67                             description: action.getState().description
68                         }
69                     });
70                     
71                     return true; // break
72                 }
73             }.bind(this));
74         },
75         changeType: function(params) {
76             var node = params.fragment.node,
77                 action = this;
78             node.document.transaction(function() {
79                 node.getParent('list').setClass(type === 'Bullet' ? 'list' : 'list.enum');
80             }, {
81                 metadata: {
82                     description: action.getState().description
83                 }
84             });
85         }
86     };
87
88     var isToggled = function(params) {
89         if(params.fragment && params.fragment.node && params.fragment.node.isInside('list')) {
90             var list = params.fragment.node.getParent('list');
91             return list.getClass() === (type === 'Bullet' ? 'list' : 'list.enum');
92         }
93         return false;
94     };
95
96
97     return {
98         name: 'toggle' + type + 'List',
99         context: ['fragment'],
100         params: {
101             fragment: {type: 'context', name: 'fragment'}
102         },
103         stateDefaults: {
104             label: type === 'Bullet' ? gettext('bull. list') : gettext('num. list')
105         },
106         getState: function(params) {
107             if(!params.fragment || !params.fragment.isValid()) {
108                 return false;
109             }
110
111             if(params.fragment instanceof params.fragment.CaretFragment && params.fragment.node.isInside('list')) {
112                 var list = params.fragment.node.getParent('list');
113                 if((list.getClass() === 'list' && type === 'Enum') || (list.getClass() === 'list.enum' && type === 'Bullet')) {
114                     return {
115                         allowed: true,
116                         description: interpolate(gettext('Change list type to %s'), [type]),
117                         execute: execute.changeType
118                     };
119                 }
120                 return {
121                     allowed: true,
122                     toggled: isToggled(params),
123                     description: gettext('Remove list'),
124                     execute: execute.remove
125                 };
126
127             }
128             var boundries = getBoundriesForAList(params.fragment);
129             if(boundries) {
130                 return {
131                     allowed: true,
132                     description: interpolate(gettext('Make %s fragment(s) into list'), [countItems(getBoundriesForAList(params.fragment))]),
133                     execute: execute.add
134                 };
135             }
136         }
137     };
138 };
139
140
141 return {
142     actions: [toggleListAction('Bullet'), toggleListAction('Enum')]
143 };
144
145 });