Merge branch '2016.11-develop' into 2017.11
[tine20] / tine20 / Timetracker / js / TimeaccountGridPanel.js
1 /*
2  * Tine 2.0
3  * 
4  * @package     Timetracker
5  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
6  * @author      Philipp Schüle <p.schuele@metaways.de>
7  * @copyright   Copyright (c) 2007-2016 Metaways Infosystems GmbH (http://www.metaways.de)
8  *
9  */
10  
11 Ext.namespace('Tine.Timetracker');
12
13 /**
14  * Timeaccount grid panel
15  * 
16  * @namespace   Tine.Timetracker
17  * @class       Tine.Timetracker.TimeaccountGridPanel
18  * @extends     Tine.widgets.grid.GridPanel
19  * 
20  * <p>Timeaccount Grid Panel</p>
21  * <p><pre>
22  * TODO         copy action needs to copy the acl too
23  * </pre></p>
24  * 
25  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
26  * @author      Philipp Schuele <p.schuele@metaways.de>
27  * @copyright   Copyright (c) 2007-2009 Metaways Infosystems GmbH (http://www.metaways.de)
28  * 
29  * @param       {Object} config
30  * @constructor
31  * Create a new Tine.Timetracker.TimeaccountGridPanel
32  */
33 Tine.Timetracker.TimeaccountGridPanel = Ext.extend(Tine.widgets.grid.GridPanel, {
34     defaultSortInfo: {field: 'creation_time', direction: 'DESC'},
35     gridConfig: {
36         autoExpandColumn: 'title'
37     },
38     copyEditAction: true,
39     defaultFilters: [{
40         field: 'query',
41         operator: 'contains',
42         value: ''
43     }, {
44         field: 'is_open',
45         operator: 'equals',
46         value: true
47     }],
48     action_bookmark: null,
49
50     initComponent: function() {
51         Tine.Timetracker.TimeaccountGridPanel.superclass.initComponent.call(this);
52
53         this.action_addInNewWindow.setDisabled(! Tine.Tinebase.common.hasRight('manage', 'Timetracker', 'timeaccounts'));
54         this.action_editInNewWindow.requiredGrant = 'editGrant';
55     },
56
57     /**
58      * @private
59      */
60     initActions: function() {
61         Tine.Timetracker.TimesheetGridPanel.superclass.initActions.call(this);
62
63         this.action_bookmark = new Ext.Action({
64             text: i18n._('Bookmark Timeaccount'),
65             iconCls: 'action_add',
66             requiredGrant: 'readGrant',
67             scope: this,
68             disabled: true,
69             allowMultiple: false,
70             visible: false,
71             handler: this.addBookmark
72         });
73
74         // register actions in updater
75         this.actionUpdater.addActions([
76             this.action_bookmark
77         ]);
78     },
79
80     addBookmark: function () {
81         var grid = this,
82             app = this.app,
83             selectionModel = grid.selectionModel;
84
85         Ext.each(selectionModel.getSelections(), function (record) {
86             Ext.Ajax.request({
87                 url: 'index.php',
88                 scope: this,
89                 params: {
90                     method: 'Timetracker.addTimeAccountFavorite',
91                     timeaccountId: record.get('id')
92                 },
93                 success : function(_result, _request) {
94                     if (_result.responseText) {
95                         Tine.Timetracker.registry.set('timeaccountFavorites', JSON.parse(_result.responseText).timeaccountFavorites);
96
97                         var rootNode = Ext.getCmp('TimeaccountFavoritesPanel').getRootNode();
98
99                         if (!rootNode.hasChildNodes()) {
100                             rootNode.removeAll();
101                         }
102
103                         rootNode.removeAll();
104                         rootNode.appendChild(Tine.Timetracker.registry.get('timeaccountFavorites'));
105
106                         rootNode.expand();
107                     }
108                 },
109                 failure: function(result) {
110                     Tine.Tinebase.ExceptionHandler.handleRequestException(result);
111                 }
112             });
113         });
114     },
115
116     /**
117      * add custom items to context menu
118      *
119      * @return {Array}
120      */
121     getContextMenuItems: function() {
122         var items = [
123             '-', {
124                 text: Tine.Tinebase.appMgr.get('Timetracker').i18n._('Close Timeaccount'),
125                 iconCls: 'action_close',
126                 scope: this,
127                 disabled: !Tine.Tinebase.common.hasRight('manage', 'Timetracker', 'timeaccounts'),
128                 itemId: 'closeAccount',
129                 handler: this.onCloseTimeaccount.createDelegate(this)
130             }
131         ];
132
133         if (this.app.featureEnabled('featureTimeaccountBookmark')) {
134             items.push(this.action_bookmark);
135         }
136
137         return items;
138     },
139
140     /**
141      * Closes selected timeaccount
142      */
143     onCloseTimeaccount: function () {
144         var grid = this,
145             recordProxy = this.recordProxy,
146             selectionModel = grid.selectionModel;
147
148         Ext.each(selectionModel.getSelections(), function (record) {
149             recordProxy.loadRecord(record, {
150                 success: function (record) {
151                     record.set('is_open', false);
152                     recordProxy.saveRecord(record, {
153                         success: function () {
154                             grid.store.reload();
155                             grid.store.remove(record);
156                         }
157                     });
158                 }
159             });
160         });
161     }
162 });