Projekt

Allgemein

Profil

GX-Bug #43158 » emails_table.js

/admin/javascript/engine/controllers/emails/emails_table.js - Till Tepelmann, 24.09.2015 19:09

 
1
/* --------------------------------------------------------------
2
 emails_table.js 2015-07-21 gm
3
 Gambio GmbH
4
 http://www.gambio.de
5
 Copyright (c) 2015 Gambio GmbH
6
 Released under the GNU General Public License (Version 2)
7
 [http://www.gnu.org/licenses/gpl-2.0.html]
8
 --------------------------------------------------------------
9
 */
10

    
11
/**
12
 * ## Emails Table Controller
13
 *
14
 * This controller will handle the main table operations of the admin/emails page.
15
 *
16
 * @module Controllers/emails_table
17
 * @version 0.1
18
 */
19
window.gambio.controllers.register('emails_table', 0.1, [ 'emails' ], /** @lends module:Controllers/emails_table */ function (data) {
20

    
21
    'use strict';
22

    
23
    // ------------------------------------------------------------------------
24
    // VARIABLE DEFINITION
25
    // ------------------------------------------------------------------------
26

    
27
    var
28
        /**
29
         * Module Reference
30
         *
31
         * @type {object}
32
         */
33
        $this = $(this),
34

    
35
        /**
36
         * Toolbar Selector 
37
         * 
38
         * @type {object}
39
         */
40
        $toolbar = $('#emails-toolbar'),
41

    
42
        /**
43
         * Modal Selector
44
         * 
45
         * @type {object}
46
         */
47
        $modal = $('#emails-modal'),
48
        
49
        /**
50
         * Default Module Options
51
         *
52
         * @type {object}
53
         */
54
        defaults = {
55
            emailsTableActions: function() {
56
                return '<button class="send-email btn btn-icon remove-margin" title="' + Lang.translate('send', 'buttons') + '">'
57
                    + '<img src="' + Config.shopUrl() + '/admin/images/export_schemes/icon_export.png" />'
58
                    + '</button>' 
59
                    + '<button class="forward-email btn btn-icon remove-margin" title="' + Lang.translate('forward', 'buttons') + '">'
60
                    + '<img src="' + Config.shopUrl() + '/admin/images/export_schemes/icon_copy.png" />'
61
                    + '</button>'
62
                    + '<button class="delete-email btn btn-icon remove-margin" title="' + Lang.translate('delete', 'buttons') + '">'
63
                    + '<img src="' + Config.shopUrl() + '/admin/images/export_schemes/icon_delete.png" />'
64
                    + '</button>'
65
                    + '<button class="preview-email btn btn-icon remove-margin" title="' + Lang.translate('preview', 'buttons') + '">'
66
                    + '<img src="' + Config.shopUrl() + '/admin/images/export_schemes/icon_preview.png" />'
67
                    + '</button>';
68
            },
69

    
70
            convertPendingToString: function(data, type, row, meta) {
71
                return (data === true) ? Lang.translate('email_pending', 'emails') : Lang.translate('email_sent', 'emails');
72
            }
73
        },
74

    
75
        /**
76
         * Final Module Options
77
         *
78
         * @type {object}
79
         */
80
        options = $.extend(true, {}, defaults, data),
81

    
82
        /**
83
         * Meta Object
84
         *
85
         * @type {object}
86
         */
87
        meta = {};
88

    
89

    
90
    // ------------------------------------------------------------------------
91
    // EVENT HANDLERS
92
    // ------------------------------------------------------------------------
93

    
94
    /**
95
     * Toggle row selection for main page table.
96
     *
97
     * @param {object} event Contains event information.
98
     */
99
    var _onSelectAllRows = function(event) {
100
        var isChecked = $(this).prop('checked');
101
        $this.find('input[type=checkbox]').prop('checked', isChecked);
102
    };
103

    
104
    /**
105
     * Will send the email to its contacts (even if its status is "sent").
106
     *
107
     * @param {object} event Contains event information.
108
     */
109
    var _onSendEmail = function (event) {
110
        var $row = $(this).parents('tr');
111

    
112
        Modal.message({
113
            title: Lang.translate('send', 'buttons'),
114
            content: Lang.translate('prompt_send_email', 'emails'),
115
            buttons: [
116
                {
117
                    text: Lang.translate('yes', 'lightbox_buttons'),
118
                    click: function() {
119
                        var email = $row.data();
120
                        window.gambio.libs.emails.sendCollection([ email ])
121
                            .done(function(response) {
122
                                $this.DataTable().ajax.reload();
123
                                window.gambio.libs.emails.getAttachmentsSize($('#attachments-size'));
124
                            })
125
                            .fail(function(response) {
126
                                Modal.message({
127
                                    title: Lang.translate('error', 'messages'),
128
                                    content: response.message
129
                                }) 
130
                            });
131
                        $(this).dialog('close');
132
                    }
133
                },
134
                {
135
                    text: Lang.translate('no', 'lightbox_buttons'),
136
                    click: function() {
137
                        $(this).dialog('close');
138
                    }
139
                }
140
            ]
141
        });
142
    };
143

    
144
    /**
145
     * Display modal with email information but without contacts.
146
     *
147
     * The user will be able to set new contacts and send the email (kind of "duplicate" method).
148
     *
149
     * @param {object} event Contains event information.
150
     */
151
    var _onForwardEmail = function(event) {
152
        var email = $(this).parents('tr').data();
153

    
154
        window.gambio.libs.emails.resetModal($modal);
155
        window.gambio.libs.emails.loadEmailOnModal(email, $modal);
156

    
157
        // Clear contact fields but let the rest of the data untouched.
158
        $modal.find('#email-id').val('');
159
        $modal.find('#sender-email, #sender-name').val('');
160
        $modal.find('#reply-to-email, #reply-to-name').val('');
161
        $modal.find('#recipient-email, #recipient-name').val('');
162
        $modal.find('#contacts-table').DataTable().clear().draw();
163

    
164
        $modal.dialog({
165
            title: Lang.translate('forward', 'buttons'),
166
            width: 1000,
167
            height: 750,
168
            modal: true,
169
            position: 'top+15',
170
            dialogClass: 'gx-container',
171
            closeOnEscape: false,
172
            buttons: window.gambio.libs.emails.getDefaultModalButtons($modal, $this),
173
            open: window.gambio.libs.emails.colorizeButtonsForEditMode
174
        });
175
    };
176

    
177
    /**
178
     * Delete selected row email.
179
     *
180
     * @param {object} event Contains event information.
181
     */
182
    var _onDeleteEmail = function (event) {
183
        var $row = $(this).parents('tr'),
184
            email = $row.data();
185

    
186
        Modal.message({
187
            title: Lang.translate('delete', 'buttons'),
188
            content: Lang.translate('prompt_delete_email', 'emails'),
189
            buttons: [
190
                {
191
                    text: Lang.translate('yes', 'lightbox_buttons'),
192
                    click: function() {
193
                        window.gambio.libs.emails.deleteCollection([ email ])
194
                            .done(function(response) {
195
                                $this.DataTable().ajax.reload();
196
                                window.gambio.libs.emails.getAttachmentsSize($('#attachments-size'));
197
                            })
198
                            .fail(function(response) {
199
                                Modal.message({
200
                                    title: Lang.translate('error', 'messages'),
201
                                    content: response.message
202
                                })
203
                            });
204
                        $(this).dialog('close');
205
                    }
206
                },
207
                {
208
                    text: Lang.translate('no', 'lightbox_buttons'),
209
                    click: function() {
210
                        $(this).dialog('close');
211
                    }
212
                }
213
            ]
214
        });
215
    };
216

    
217
    /**
218
     * Display modal with email information
219
     *
220
     * The user can select an action to perform upon the previewed email (Send, Forward,
221
     * Delete, Close).
222
     *
223
     * @param  {object} event Contains event information.
224
     */
225
    var _onPreviewEmail = function (event) {
226
        var email = $(this).parents('tr').data();
227

    
228
        window.gambio.libs.emails.resetModal($modal);
229
        window.gambio.libs.emails.loadEmailOnModal(email, $modal);
230

    
231
        $modal.dialog({
232
            title: Lang.translate('preview', 'buttons'),
233
            width: 1000,
234
            height: 750,
235
            modal: true,
236
            position: 'top+15',
237
            dialogClass: 'gx-container',
238
            closeOnEscape: false,
239
            buttons: window.gambio.libs.emails.getPreviewModalButtons($modal, $this),
240
            open: window.gambio.libs.emails.colorizeButtonsForPreviewMode
241
        });
242
    }; 
243

    
244

    
245
    // ------------------------------------------------------------------------
246
    // INITIALIZATION
247
    // ------------------------------------------------------------------------
248
    
249
    /**
250
     * Initialize method of the module, called by the engine. 
251
     * 
252
     * The emails table operates with server processing because it is much faster and efficient than preparing 
253
     * and sending all the records in every AJAX request. Check the Emails/DataTable controller method for 
254
     * requested data and the following link for more info about server processing in DataTables.
255
     * 
256
     * {@link http://www.datatables.net/manual/server-side}
257
     */
258
    meta.init = function () {        
259
        // Create a DataTable instance for the email records.
260
        DataTable.create($this, {
261
            processing: true,
262
            serverSide: true,
263
            dom: 'rtip', 
264
            autoWidth: false,
265
            language: (Config.languageCode() === 'de') ? DataTable.getGermanTranslation() : null,
266
            ajax:  {
267
                url: Config.shopUrl() + '/admin/admin.php?do=Emails/DataTable',
268
                type: 'POST'
269
            },
270
            order: [[2, 'desc']],
271
            pageLength: 50,
272
            columns: [
273
                { data: null, orderable: false, defaultContent: '<input type="checkbox" />', width: '2%', 
274
                        className: 'dt-head-center dt-body-center' },
275
                { data: 'row_count', orderable: false, width: '3%', className: 'dt-head-center dt-body-center' },
276
                { data: 'creation_date', width: '12%'},
277
                { data: 'sent_date', width: '12%' },
278
                { data: 'sender', width: '12%' },
279
                { data: 'recipient', width: '12%' },
280
                { data: 'subject', width: '24%' },
281
                { data: 'is_pending', width: '8%', render: options.convertPendingToString },
282
                { data: null, orderable: false, defaultContent: '', render: options.emailsTableActions, width: '15%' }
283
            ]
284
        });
285
        
286
        // Add table error handler.
287
        DataTable.error($this, function(event, settings, techNote, message) {
288
            Modal.message({
289
                title: 'DataTables ' + Lang.translate('error', 'messages'),
290
                content: message
291
            });
292
        });
293
        
294
        // Add ajax error handler.
295
        DataTable.ajaxComplete($this, function(event, settings, json) {
296
            if (json.exception === true) {
297
                Dbg.error('DataTables Processing Error', $this.get(0),  json);
298
                Modal.message({
299
                    title: 'AJAX ' + Lang.translate('error', 'messages'),
300
                    content: json.message
301
                });
302
            }
303
        });
304
        
305
        // Bind event handlers of the emails table. 
306
        $this
307
            .on('click', '#select-all-rows', _onSelectAllRows)
308
            .on('click', '.send-email', _onSendEmail)
309
            .on('click', '.forward-email', _onForwardEmail)
310
            .on('click', '.delete-email', _onDeleteEmail)
311
            .on('click', '.preview-email', _onPreviewEmail);
312
        
313
        meta._initFinished();
314
    };
315

    
316
    // Return data to module engine.
317
    return meta;
318
});
(2-2/2)