/*
 *  Copyright 2015 Anyware Services
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * @private
 * The model for the {@link Ametys.plugins.web.site.SitesTool} tree tool. 
 */
Ext.define('Ametys.web.sitemap.SitemapTree.Page', { 
    extend: 'Ext.data.TreeModel', 
    fields: [ 
        { name: 'id', type: 'string' }, 
        { name: 'name', type: 'string' }, 
        { name: 'title', type: 'string' },
        { name: 'longTitle', type: 'string' },
        { 
        	name: 'text', 
        	type: 'string',
        	calculate: function (data)
      	   	{
        		return data.title;
      	   	}
        },
        { name: 'type', type: 'string' },
        { name: 'template', type: 'string' },
        { name: 'path', type: 'string' },
        { name: 'moveable', type: 'boolean' },
        { name: 'modifiable', type: 'boolean' },
        { name: 'locked', type: 'boolean' },
        { name: 'hasChild', type: 'boolean' },
        { name: 'iconGlyph', type: 'string' },
        { name: 'iconDecorator', type: 'string' },
        {
     	   name: 'iconCls',
     	   depends: ['iconGlyph', 'iconDecorator'],
     	   calculate: function (data)
     	   {
     		   if (!Ext.isEmpty(data.iconGlyph))
     	       {
     			   return 'a-tree-glyph ' + data.iconGlyph + (!Ext.isEmpty(data.iconDecorator) ? ' ' + data.iconDecorator : '');
     	       }
     		   return data.iconCls;
     	   }
        },
        { name: 'iconSmall', type: 'string' },
        {
        	name: 'icon',
        	type: 'string',
        	depends: ['iconSmall', 'iconGlyph'],
        	calculate: function (data) {
        		if (Ext.isEmpty(data.iconGlyph) && Ext.isEmpty(data.iconCls))
      	       	{
        			return Ametys.CONTEXT_PATH + data.iconSmall
      	       	}
        		return null;
        	}
        },
        { name: 'isAllowed', type: 'boolean', defaultValue: true },
        { name: 'decorators'},
        {
        	name: 'cls',
        	depends: ['isAllowed'],
        	calculate: function (data) {
        		if (!data.isAllowed)
      	       	{
        			return 'disabled';
      	       	}
        		return null;
        	}
        }
    ]
}); 

Ext.define('Ametys.web.sitemap.SitemapTree.CheckablePage', { 
    extend: 'Ext.data.TreeModel', 
    fields: [ 
        { name: 'id', type: 'string' }, 
        { name: 'name', type: 'string' }, 
        { name: 'title', type: 'string' },
        { name: 'longTitle', type: 'string' },
        { 
        	name: 'text', 
        	type: 'string',
        	depends: ['title'],
        	calculate: function (data)
      	   	{
        		return data.title;
      	   	}
        },
        { name: 'type', type: 'string' },
        { name: 'template', type: 'string' },
        { name: 'path', type: 'string' },
        { name: 'moveable', type: 'boolean' },
        { name: 'hasChild', type: 'boolean' },
        { name: 'iconGlyph', type: 'string' },
        { name: 'iconDecorator', type: 'string' },
        { name: 'locked', type: 'boolean' },
        {
           name: 'iconCls',
           depends: ['iconGlyph', 'iconDecorator'],
           calculate: function (data)
           {
               if (!Ext.isEmpty(data.iconGlyph))
               {
                   return 'a-tree-glyph ' + data.iconGlyph + (!Ext.isEmpty(data.iconDecorator) ? ' ' + data.iconDecorator : '');
               }
               return data.iconCls;
           }
        },
        { name: 'iconSmall', type: 'string' },
        {
            name: 'icon',
            type: 'string',
            depends: ['iconSmall', 'iconGlyph'],
            calculate: function (data) {
                if (Ext.isEmpty(data.iconGlyph) && Ext.isEmpty(data.iconCls))
                {
                    return Ametys.CONTEXT_PATH + data.iconSmall
                }
                return null;
            }
        },
        { name: 'checked', type: 'boolean', defaultValue: false },
        { name: 'isAllowed', type: 'boolean', defaultValue: true },
        {
        	name: 'cls',
        	depends: ['isAllowed'],
        	calculate: function (data) {
        		if (!data.isAllowed)
      	       	{
        			return 'disabled';
      	       	}
        		return null;
        	}
        }
    ]
});