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

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

                }, this.entityCollection);
224

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