AuditTableLayoutView.js 12.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

define(['require',
    'backbone',
    'hbs!tmpl/audit/AuditTableLayoutView_tmpl',
    'collection/VEntityList',
23 24 25
    'utils/Enums',
    'utils/UrlLinks'
], function(require, Backbone, AuditTableLayoutView_tmpl, VEntityList, Enums, UrlLinks) {
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    'use strict';

    var AuditTableLayoutView = Backbone.Marionette.LayoutView.extend(
        /** @lends AuditTableLayoutView */
        {
            _viewName: 'AuditTableLayoutView',

            template: AuditTableLayoutView_tmpl,

            /** Layout sub regions */
            regions: {
                RAuditTableLayoutView: "#r_auditTableLayoutView",
            },

            /** ui selector cache */
            ui: {
                auditCreate: "[data-id='auditCreate']",
43
                previousAuditData: "[data-id='previousAuditData']",
44 45
                nextAuditData: "[data-id='nextAuditData']",
                pageRecordText: "[data-id='pageRecordText']"
46 47 48 49 50
            },
            /** ui events hash */
            events: function() {
                var events = {};
                events["click " + this.ui.auditCreate] = "onClickAuditCreate";
51 52
                events["click " + this.ui.nextAuditData] = "onClickNextAuditData";
                events["click " + this.ui.previousAuditData] = "onClickPreviousAuditData";
53 54 55 56 57 58 59
                return events;
            },
            /**
             * intialize a new AuditTableLayoutView Layout
             * @constructs
             */
            initialize: function(options) {
60
                _.extend(this, _.pick(options, 'guid', 'entity', 'entityName', 'attributeDefs'));
61
                this.entityCollection = new VEntityList();
62
                this.limit = 26;
63
                this.entityCollection.url = UrlLinks.entityCollectionaudit(this.guid);
64
                this.entityCollection.modelAttrName = "events";
65
                this.entityModel = new this.entityCollection.model();
66
                this.pervOld = [];
67 68 69
                this.commonTableOptions = {
                    collection: this.entityCollection,
                    includeFilter: false,
70
                    includePagination: false,
71
                    includePageSize: false,
72
                    includeFooterRecords: false,
73
                    gridOpts: {
74
                        className: "table table-hover backgrid table-quickMenu",
75 76 77 78 79
                        emptyText: 'No records found!'
                    },
                    filterOpts: {},
                    paginatorOpts: {}
                };
80
                this.currPage = 1;
81 82
            },
            onRender: function() {
83
                $.extend(this.entityCollection.queryParams, { count: this.limit });
84 85 86 87 88
                this.fetchCollection({
                    next: this.ui.nextAuditData,
                    nextClick: false,
                    previous: this.ui.previousAuditData
                });
89 90 91
                this.entityCollection.comparator = function(model) {
                    return -model.get('timestamp');
                }
92
            },
93
            bindEvents: function() {},
94 95
            getToOffset: function() {
                var toOffset = 0;
96
                if (this.entityCollection.models.length < this.limit) {
97
                    toOffset = (this.getFromOffset() + (this.entityCollection.models.length));
98
                } else {
99
                    toOffset = (this.getFromOffset() + (this.entityCollection.models.length - 1));
100 101 102
                }
                return toOffset;
            },
103
            getFromOffset: function(options) {
104
                var count = (this.currPage - 1) * (this.limit - 1);
105 106 107 108 109 110
                if (options && (options.nextClick || options.previousClick || this.entityCollection.models.length)) {
                    return count + 1;
                } else {
                    return count;
                }
            },
111
            renderOffset: function(options) {
112
                var entityLength;
113 114
                if (options.nextClick) {
                    options.previous.removeAttr("disabled");
115 116 117
                    if (this.entityCollection.length != 0) {
                        this.currPage++;

118
                    }
119
                } else if (options.previousClick) {
120
                    options.next.removeAttr("disabled");
121 122
                    if (this.currPage > 1 && this.entityCollection.models.length) {
                        this.currPage--;
123 124
                    }
                }
125 126 127 128 129 130
                if (this.entityCollection.models.length > 25) {
                    entityLength = this.entityCollection.models.length - 1;
                } else {
                    entityLength = this.entityCollection.models.length
                }
                this.ui.pageRecordText.html("Showing  <u>" + entityLength + " records</u> From " + this.getFromOffset(options) + " - " + this.getToOffset());
131
            },
132 133 134
            fetchCollection: function(options) {
                var that = this;
                this.$('.fontLoader').show();
135
                this.$('.tableOverlay').show();
136 137 138 139 140 141 142
                if (that.entityCollection.models.length > 1) {
                    if (options.nextClick) {
                        this.pervOld.push(that.entityCollection.first().get('eventKey'));
                    }
                }
                this.entityCollection.fetch({
                    success: function() {
143 144 145
                        if (!(that.ui.pageRecordText instanceof jQuery)) {
                            return;
                        }
146
                        if (that.entityCollection.models.length < that.limit) {
147
                            options.previous.attr('disabled', true);
148 149
                            options.next.attr('disabled', true);
                        }
150
                        that.renderOffset(options);
151
                        that.entityCollection.sort();
152
                        if (that.entityCollection.models.length) {
153
                            if (that.entityCollection && (that.entityCollection.models.length < that.limit && that.currPage == 1) && that.next == that.entityCollection.last().get('eventKey')) {
154 155 156
                                options.next.attr('disabled', true);
                                options.previous.removeAttr("disabled");
                            } else {
157
                                that.next = that.entityCollection.last().get('eventKey');
158
                                if (that.pervOld.length === 0) {
159 160
                                    options.previous.attr('disabled', true);
                                }
161
                                that.renderTableLayoutView();
162 163
                            }
                        }
164 165 166
                        that.$('.fontLoader').hide();
                        that.$('.tableOverlay').hide();
                        that.$('.auditTable').show(); // Only for first time table show because we never hide after first render.
167 168 169 170
                    },
                    silent: true
                });
            },
171 172 173 174 175
            renderTableLayoutView: function() {
                var that = this;
                require(['utils/TableLayout'], function(TableLayout) {
                    var cols = new Backgrid.Columns(that.getAuditTableColumns());
                    that.RAuditTableLayoutView.show(new TableLayout(_.extend({}, that.commonTableOptions, {
176
                        columns: cols
177
                    })));
178
                    if (!(that.entityCollection.models.length < that.limit)) {
179
                        that.RAuditTableLayoutView.$el.find('table tr').last().hide();
180
                    }
181 182 183 184 185 186
                });
            },
            getAuditTableColumns: function() {
                var that = this;
                return this.entityCollection.constructor.getTableCols({
                    user: {
187
                        label: "Users",
188 189 190 191 192 193
                        cell: "html",
                        editable: false,
                        sortable: false,
                    },
                    timestamp: {
                        label: "Timestamp",
194
                        cell: "html",
195 196 197 198
                        editable: false,
                        sortable: false,
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
199
                                return new Date(rawValue);
200 201 202 203
                            }
                        })
                    },
                    action: {
204
                        label: "Actions",
205 206
                        cell: "html",
                        editable: false,
207 208 209
                        sortable: false,
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
210 211
                                if (Enums.auditAction[rawValue]) {
                                    return Enums.auditAction[rawValue];
212
                                } else {
213
                                    return rawValue;
214 215 216
                                }
                            }
                        })
217 218
                    },
                    tool: {
219
                        label: "Tools",
220 221 222 223 224
                        cell: "html",
                        editable: false,
                        sortable: false,
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
225
                                return '<div class="label label-success auditDetailBtn" data-id="auditCreate" data-action="' + Enums.auditAction[model.get('action')] + '" data-modalId="' + model.get('eventKey') + '">Detail</div>';
226 227 228 229 230
                            }
                        })
                    },

                }, this.entityCollection);
231

232 233 234 235 236 237 238
            },
            onClickAuditCreate: function(e) {
                var that = this;
                require([
                    'modules/Modal',
                    'views/audit/CreateAuditTableLayoutView',
                ], function(Modal, CreateAuditTableLayoutView) {
239
                    that.action = $(e.target).data("action");
240
                    var eventModel = that.entityCollection.fullCollection.findWhere({ 'eventKey': $(e.currentTarget).data('modalid') }).toJSON(),
241
                        collectionModel = new that.entityCollection.model(eventModel),
242
                        view = new CreateAuditTableLayoutView({ guid: that.guid, entityModel: collectionModel, action: that.action, entity: that.entity, entityName: that.entityName, attributeDefs: that.attributeDefs });
243
                    var modal = new Modal({
244
                        title: that.action,
245 246 247 248
                        content: view,
                        okCloses: true,
                        showFooter: true,
                    }).open();
249 250 251
                    view.on('closeModal', function() {
                        modal.trigger('cancel');
                    });
252 253
                    view.$el.on('click', 'td a', function() {
                        modal.trigger('cancel');
254
                    });
255 256 257 258 259 260 261
                });
            },
            onClickNextAuditData: function() {
                var that = this;
                this.ui.previousAuditData.removeAttr("disabled");
                $.extend(this.entityCollection.queryParams, {
                    startKey: function() {
262
                        return that.next;
263 264 265 266 267 268
                    }
                });
                this.fetchCollection({
                    next: this.ui.nextAuditData,
                    nextClick: true,
                    previous: this.ui.previousAuditData
269
                });
270 271 272 273 274 275
            },
            onClickPreviousAuditData: function() {
                var that = this;
                this.ui.nextAuditData.removeAttr("disabled");
                $.extend(this.entityCollection.queryParams, {
                    startKey: function() {
276
                        return that.pervOld.pop();
277 278 279 280 281 282 283 284
                    }
                });
                this.fetchCollection({
                    next: this.ui.nextAuditData,
                    previousClick: true,
                    previous: this.ui.previousAuditData
                });
            },
285 286 287
        });
    return AuditTableLayoutView;
});