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                     boundries.node1.document.createList(listParams);
 
  47                         description: action.getState().description
 
  52                 throw new Error('Invalid boundries');
 
  55         remove: function(callback, params) {
 
  57             var current = params.fragment.node,
 
  60             var toSearch = current.nodeType === Node.ELEMENT_NODE ? [current] : [];
 
  61             toSearch = toSearch.concat(current.parents());
 
  62             toSearch.some(function(node) {
 
  64                     node.document.transaction(function() {
 
  65                         node.object.extractListItems();
 
  68                             description: action.getState().description
 
  77         changeType: function(callback, params) {
 
  78             var node = params.fragment.node,
 
  80             node.document.transaction(function() {
 
  81                 node.getParent('list').setClass(type === 'Bullet' ? 'list' : 'list.enum');
 
  84                     description: action.getState().description
 
  91     var isToggled = function(params) {
 
  92         if(params.fragment && params.fragment.node && params.fragment.node.isInside('list')) {
 
  93             var list = params.fragment.node.getParent('list');
 
  94             return list.getClass() === (type === 'Bullet' ? 'list' : 'list.enum');
 
 101         name: 'toggle' + type + 'List',
 
 102         context: ['fragment'],
 
 104             fragment: {type: 'context', name: 'fragment'}
 
 107             label: type === 'Bullet' ? gettext('bull. list') : gettext('num. list')
 
 109         getState: function(params) {
 
 110             if(!params.fragment || !params.fragment.isValid()) {
 
 114             if(params.fragment instanceof params.fragment.CaretFragment && params.fragment.node.isInside('list')) {
 
 115                 var list = params.fragment.node.getParent('list');
 
 116                 if((list.getClass() === 'list' && type === 'Enum') || (list.getClass() === 'list.enum' && type === 'Bullet')) {
 
 119                         description: interpolate(gettext('Change list type to %s'), [type]),
 
 120                         execute: execute.changeType
 
 125                     toggled: isToggled(params),
 
 126                     description: gettext('Remove list'),
 
 127                     execute: execute.remove
 
 131             var boundries = getBoundriesForAList(params.fragment);
 
 135                     description: interpolate(gettext('Make %s fragment(s) into list'), [countItems(getBoundriesForAList(params.fragment))]),
 
 145     actions: [toggleListAction('Bullet'), toggleListAction('Enum')]