4 /* globals gettext, interpolate */
7 var getBoundriesForAList = function(fragment) {
10 if(fragment instanceof fragment.RangeFragment && fragment.hasSiblingBoundries()) {
11 return fragment.boundriesSiblingParents();
13 if(fragment instanceof fragment.NodeFragment) {
14 node = fragment.node.getNearestElementNode();
22 var countItems = function(boundries) {
23 var ptr = boundries.node1,
25 while(ptr && !ptr.sameNode(boundries.node2)) {
32 var toggleListAction = function(type) {
35 add: function(callback, params) {
36 var boundries = getBoundriesForAList(params.fragment),
37 listParams = {klass: type === 'Bullet' ? 'list' : 'list.enum'},
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;
49 return doc.createFragment(doc.CaretFragment, {node: text, offset:0});
53 description: action.getState().description,
54 fragment: params.fragment
59 throw new Error('Invalid boundries');
62 remove: function(callback, params) {
64 var current = params.fragment.node,
67 var toSearch = current.nodeType === Node.ELEMENT_NODE ? [current] : [];
68 toSearch = toSearch.concat(current.parents());
69 toSearch.some(function(node) {
71 node.document.transaction(function() {
72 var firstItem = node.object.extractListItems(),
74 if(params.fragment.isValid()) {
75 toret = params.fragment;
77 toret = node.document.createFragment(node.document.NodeFragment, {node: firstItem});
82 description: action.getState().description,
83 fragment: params.fragment
92 changeType: function(callback, params) {
93 var node = params.fragment.node,
95 node.document.transaction(function() {
96 var list = node.getParent('list');
97 list.setClass(type === 'Bullet' ? 'list' : 'list.enum');
98 if(params.fragment.isValid()) {
99 return params.fragment;
101 return node.document.createFragment(node.document.NodeFragment, {node: list.contents()[0]});
105 description: action.getState().description,
106 fragment: params.fragment
113 var isToggled = function(params) {
114 if(params.fragment && params.fragment.node && params.fragment.node.isInside('list')) {
115 var list = params.fragment.node.getParent('list');
116 return list.getClass() === (type === 'Bullet' ? 'list' : 'list.enum');
121 var label = type === 'Bullet' ? gettext('bull. list') : gettext('num. list');
124 name: 'toggle' + type + 'List',
125 context: ['fragment'],
127 fragment: {type: 'context', name: 'fragment'}
132 getState: function(params) {
133 if(!params.fragment || !params.fragment.isValid()) {
137 if(params.fragment instanceof params.fragment.CaretFragment && params.fragment.node.isInside('list')) {
138 var list = params.fragment.node.getParent('list');
139 if((list.getClass() === 'list' && type === 'Enum') || (list.getClass() === 'list.enum' && type === 'Bullet')) {
142 description: interpolate(gettext('Change list type to %s'), [label]),
143 execute: execute.changeType
148 toggled: isToggled(params),
149 description: gettext('Remove list'),
150 execute: execute.remove
154 var boundries = getBoundriesForAList(params.fragment);
158 description: interpolate(gettext('Make %s fragment(s) into list'), [countItems(getBoundriesForAList(params.fragment))]),
168 actions: [toggleListAction('Bullet'), toggleListAction('Enum')]