Project

General

Profile

Download (10.9 KB) Statistics
| Branch: | Revision:

git_sitools_idoc / flarecast / workspace / client-admin / js / menu / seeAlso.js @ master

1

    
2
/***************************************
3
* Copyright 2010-2014 CNES - CENTRE NATIONAL d'ETUDES SPATIALES
4
* 
5
* This file is part of SITools2.
6
* 
7
* SITools2 is free software: you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation, either version 3 of the License, or
10
* (at your option) any later version.
11
* 
12
* SITools2 is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
* GNU General Public License for more details.
16
* 
17
* You should have received a copy of the GNU General Public License
18
* along with SITools2.  If not, see <http://www.gnu.org/licenses/>.
19
***************************************/
20
/*global Ext, sitools, ID, i18n, document, showResponse, alertFailure, LOCALE, ImageChooser, 
21
 showHelp, ann, mainPanel, helpUrl:true, loadUrl, SHOW_HELP*/
22
Ext.namespace('sitools.admin.menu');
23

    
24
/**
25
 * Create A dynamic Dataview in function of the sitools.admin.menu.dataView Dataview
26
 * 
27
 * @cfg {Ext.XTemplate} the template to display in the dataView
28
 * @cfg {Ext.data.JsonStore} the store where nodes are saved
29
 * @class sitools.admin.menu.seeAlso
30
 * @extends Ext.DataView
31
 */
32
Ext.define('sitools.admin.menu.seeAlso', { extend : 'Ext.view.View',
33
    
34
    id: 'links',
35
    
36
    itemSelector: 'div.links',
37
    overClass   : 'links-hover',
38
    singleSelect: true,
39
    multiSelect : false,
40
    autoScroll  : false,
41
    
42
    initComponent : function () {
43
        
44
        var tpl = new Ext.XTemplate(
45
           '<div class="title-seeAlso">See Also...</div>',
46
           '<tpl for=".">',
47
               '<tpl if="this.isNotEmpty(links)">',
48
                   '<tpl for="links">',
49
                            '<div id="{nodeName}" name="{text}" class="links">',
50
                                '<img class="icon-seeAlso" width="16" height="16" src="{iconMenu}" />',
51
                                '<span id="{nodeName}">{label}</span>',
52
                            '</div>',
53
                    '</tpl>',
54
                '</tpl>',
55
            '</tpl>',
56
            {
57
                compiled : true,
58
                isNotEmpty : function (children) {
59
                    return !Ext.isEmpty(children);
60
                }, 
61
                isEmpty : function (children) {
62
                    return Ext.isEmpty(children);
63
                }
64
            }
65
        );
66
        this.tpl = tpl;
67
        this.height = Ext.getBody().getSize().height - 220;
68
        
69
        this.listeners = {
70
            click : function (t, ind, node, e) {
71
                var nodeName = node.id;
72
                var title = node.attributes[1].textContent;
73
                var rec = seeAlsoMenu.getStore().getAt(0);
74
                var path = this.getPath(rec, title);
75
                this.openSubCategory(t, title, e, nodeName, path);
76
            }
77
        };
78
        
79
        this.store = new Ext.data.JsonStore({
80
            root : '',
81
            restful : true,
82
            remoteSort : false,
83
            fields : [
84
                {name: 'nodeName'},
85
                {name : 'id'},
86
                {name : 'parentNode'},
87
                {name : 'children'}, 
88
                {name : 'iconMenu'},
89
                {name : 'category'},
90
                {name : 'links'},
91
                {name : 'label'},
92
                {name : 'path'}
93
            ]
94
        });
95
        
96
        this.allButtons = [];
97
        
98
        sitools.admin.menu.seeAlso.superclass.initComponent.call(this);
99
    },
100
    
101
    /**
102
     * Get the path from the split of a string
103
     * 
104
     * @param rec the record
105
     * @param title the title of the item to get the path
106
     * @returns pathTab a tab of strings
107
     */
108
    getPath : function (rec, title) {
109
        var reg = new RegExp("/");
110
        
111
        for (var i = 0; i < rec.data.links.length; i++){
112
            if (rec.data.links[i].text == title){
113
                var pathTab = rec.data.links[i].path.split(reg);
114
                return pathTab;
115
            }
116
        }
117
    },
118

    
119
    /**
120
     * Display children of a node
121
     * 
122
     * @param t the parent element
123
     * @param title the title
124
     * @param e the event
125
     * @param nodeName the nodeId
126
     * @param path
127
     */
128
    openSubCategory : function (t, title, e, nodeName, path) {
129
        dataViewMenu.allButtons = [];
130

    
131
        var isAddable = this.populateStore(t, title);
132
        var toolbar = mainPanel.toolbars[0];
133
        var textRoot = toolbar.items.items[0].text;
134
        // On ajoute le noeud sur lequel on vient de cliquer en tant que bouton de la toolbar
135
        toolbar.removeAll();
136
        toolbar.addText(textRoot);
137
        toolbar.addSeparator();
138
        this.addRootButton();
139
        
140
        for (var i = 0; i < path.length; i++){
141
            var button = {
142
                    xtype : 'button', 
143
                    text : path[i],
144
                    id : path[i] + 'Id',
145
                    scope : t,
146
                    path : path,
147
                    handler : function (btn, e) {
148
                        this.populateStore(this, btn.getText());
149
                        
150
                        dataViewMenu.deleteItemsFromToolbar(mainPanel.toolbars[0], btn.getText(), path);
151
                        dataViewWin.show();
152
                        mainPanel.getTopToolbar().show();
153
                        var dataViewEl = dataViewWin.getEl();
154
                        dataViewEl.fadeIn({
155
                            opacity: 0.95, //can be any value between 0 and 1 (e.g. .5)
156
                            easing: 'easeOut',
157
                            duration: 0.5
158
                        });
159
                    }
160
            };
161
            this.addButton (button, toolbar);
162
        }
163
        mainPanel.doLayout();
164
        
165
        if (!isAddable) {
166
            this.treeAction(title, nodeName);
167
        }
168
    },
169
    
170
    /**
171
     * Add a button to the a toolbar
172
     * 
173
     * @param btn the button to add
174
     * @param toolbar which receive the button
175
     */
176
    addButton : function (btn, toolbar) {
177
        toolbar.add(btn);
178
        toolbar.addSeparator();
179
        dataViewMenu.allButtons.push(btn);
180
    },
181
    
182
    /**
183
     * Add the Root button to the top toolbar
184
     */
185
    addRootButton : function () {
186
        var button = {
187
                xtype : 'button', 
188
                text : 'root',
189
                id : 'rootId',
190
                scope : this,
191
                handler : function (btn, e) {
192
                    this.populateStore(this, btn.getText());
193
                    dataViewMenu.deleteItemsFromToolbar(mainPanel.toolbars[0], btn.getText());
194
                    dataViewWin.show();
195
                    mainPanel.getTopToolbar().show();
196
                    var dataViewEl = dataViewWin.getEl();
197
                    dataViewEl.fadeIn({
198
                        opacity: 0.95, //can be any value between 0 and 1 (e.g. .5)
199
                        easing: 'easeOut',
200
                        duration: 0.5
201
                    });
202
                }
203
            };
204
            
205
            this.addButton (button, mainPanel.toolbars[0]);
206
            mainPanel.doLayout();
207
    },
208
    
209
    /**
210
     * Populate the store with children of a node
211
     * 
212
     * @param t
213
     * @param title the title of the item to populate
214
     * @returns {Boolean}
215
     */
216
    populateStore : function (t, title) {
217
        var children = t.getChildrenByParentNode(dataViewMenu.allData, title);
218
        if (!Ext.isEmpty(children)) {
219
            t.store.removeAll();
220
            
221
            dataViewMenu.store.removeAll();
222
            dataViewMenu.store.loadData(children);
223
            return true;
224
        }
225
        else {
226
            return false;
227
        }
228
    },
229
    
230
    /**
231
     * Get All the children of a parent node
232
     * 
233
     * @param data all the node
234
     * @param parentNode the name of the parentNode
235
     * @returns
236
     */
237
    getChildrenByParentNode : function (data, parentNode) {
238
        if (Ext.isEmpty(data)) {
239
            return null;
240
        }
241
        
242
        if (parentNode == 'root') {
243
            return data;
244
        }
245
        else {
246
            for (var i = 0; i < data.length; i++) {
247
                if (data[i].text == parentNode) {
248
                    return data[i].children;
249
                    
250
                }
251
                else {
252
                    var tmp = this.getChildrenByParentNode(data[i].children, parentNode);
253
                    if (!Ext.isEmpty(tmp)) {
254
                        return tmp;
255
                    }
256
                }
257
            }
258
        }
259
    },
260
    
261
    /**
262
     * Create a component from his component Registry name
263
     * 
264
     * @param title
265
     * @param nodeName
266
     */
267
    treeAction : function (title, nodeName) {
268
        // Getting nodeName
269
        var nodeId = title + 'id';
270
        
271
        if (!Ext.isDefined(nodeName)) {
272
            Ext.Msg.alert('warning', 'Undefined');
273
            return;
274
        }
275
        var selectedNode = this.getSelectedRecords()[0];
276
        
277
        if (!Ext.ComponentMgr.isRegistered('s-' + nodeName)) {
278
            Ext.Msg.alert('warning', 'label.component' + ' \'' + 's-' + nodeName + '\' ' + 'msg.undefined');
279
            return;
280
        }
281
        
282
        var pan_config = new sitools.admin.menu.dataView();
283
        
284
        
285
        // Loading component 's-nodeName'
286
       mainPanel.removeAll();
287
       mainPanel.add({
288
            width: "100%", 
289
            items : [ {
290
                xtype : 's-box',
291
                label : i18n.get('label.' + nodeName),
292
                items : [ {
293
                    xtype : 's-' + nodeName, 
294
                    sitoolsType : "mainAdminPanel"
295
                } ],
296
                idItem : nodeId //selectedNode.get('id')
297
            } ], 
298
            listeners : {
299
                resize : function (panel, width, height) {
300
                    var size = panel.items.items[0].body.getSize();
301
                    var sBoxTitle = panel.items.items[0].items.items[0].getEl();
302
                    size = {
303
                        height : size.height - (sBoxTitle.getHeight() + sBoxTitle.getMargin("t") + sBoxTitle.getMargin("b")), 
304
                        width : size.width - 8
305
                    };
306
                    var mainAdminPanel = panel.find("sitoolsType", "mainAdminPanel");
307
                    mainAdminPanel[0].setSize(size);
308
                }
309
            }
310
        });
311
        var helpPanel = new Ext.ux.ManagedIFrame.Panel({
312
            id : ID.PANEL.HELP,
313
            width : "100%", 
314
            flex : 1,
315
            // autoScroll:true,
316
            defaultSrc : loadUrl.get('APP_URL') + "/client-admin/res/help/" + LOCALE + "/" + nodeName + ".html",
317
            defaults : {
318
                padding : 10
319
            }
320
        });
321
        mainPanel.add(
322
            helpPanel
323
        );
324
        mainPanel.doLayout();
325
        helpPanel.setVisible(SHOW_HELP);
326
        
327
        helpUrl = loadUrl.get('APP_URL') + "/client-admin/res/help/" + LOCALE + "/" + nodeName + ".html";
328
        mainPanel.getTopToolbar().show();
329
        dataViewWin.hide();
330
       
331
    }
332
});