AuditTableLayoutView.js 12 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
    'utils/Utils',
24 25
    'utils/Enums',
    'utils/UrlLinks'
26
], function(require, Backbone, AuditTableLayoutView_tmpl, VEntityList, Utils, Enums, UrlLinks) {
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: {
43
                previousAuditData: "[data-id='previousAuditData']",
44
                nextAuditData: "[data-id='nextAuditData']",
45 46
                pageRecordText: "[data-id='pageRecordText']",
                activePage: "[data-id='activePage']"
47 48 49 50
            },
            /** ui events hash */
            events: function() {
                var events = {};
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
                    includeAtlasTableSorting: true,
73
                    includeFooterRecords: false,
74
                    gridOpts: {
75
                        className: "table table-hover backgrid table-quickMenu",
76 77 78 79 80
                        emptyText: 'No records found!'
                    },
                    filterOpts: {},
                    paginatorOpts: {}
                };
81
                this.currPage = 1;
82 83
            },
            onRender: function() {
84
                $.extend(this.entityCollection.queryParams, { count: this.limit });
85 86 87 88 89
                this.fetchCollection({
                    next: this.ui.nextAuditData,
                    nextClick: false,
                    previous: this.ui.previousAuditData
                });
90 91 92
                this.entityCollection.comparator = function(model) {
                    return -model.get('timestamp');
                }
93
            },
94
            bindEvents: function() {},
95
            getToOffset: function() {
96
                return ((this.limit - 1) * this.currPage);
97
            },
98 99 100
            getFromOffset: function(toOffset) {
                // +2 because of toOffset is alrady in minus and limit is +1;
                return ((toOffset - this.limit) + 2);
101
            },
102
            renderOffset: function(options) {
103
                var entityLength;
104 105
                if (options.nextClick) {
                    options.previous.removeAttr("disabled");
106 107 108
                    if (this.entityCollection.length != 0) {
                        this.currPage++;

109
                    }
110
                } else if (options.previousClick) {
111
                    options.next.removeAttr("disabled");
112 113
                    if (this.currPage > 1 && this.entityCollection.models.length) {
                        this.currPage--;
114 115
                    }
                }
116 117
                if (this.entityCollection.models.length === this.limit) {
                    // Because we have 1 extra record.
118 119 120 121
                    entityLength = this.entityCollection.models.length - 1;
                } else {
                    entityLength = this.entityCollection.models.length
                }
122 123 124 125
                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);
126
            },
127 128 129
            fetchCollection: function(options) {
                var that = this;
                this.$('.fontLoader').show();
130
                this.$('.tableOverlay').show();
131 132 133 134 135 136 137
                if (that.entityCollection.models.length > 1) {
                    if (options.nextClick) {
                        this.pervOld.push(that.entityCollection.first().get('eventKey'));
                    }
                }
                this.entityCollection.fetch({
                    success: function() {
138 139 140
                        if (!(that.ui.pageRecordText instanceof jQuery)) {
                            return;
                        }
141
                        if (that.entityCollection.models.length < that.limit) {
142
                            options.previous.attr('disabled', true);
143 144
                            options.next.attr('disabled', true);
                        }
145
                        that.renderOffset(options);
146
                        that.entityCollection.sort();
147
                        if (that.entityCollection.models.length) {
148
                            if (that.entityCollection && (that.entityCollection.models.length < that.limit && that.currPage == 1) && that.next == that.entityCollection.last().get('eventKey')) {
149 150 151
                                options.next.attr('disabled', true);
                                options.previous.removeAttr("disabled");
                            } else {
152
                                that.next = that.entityCollection.last().get('eventKey');
153
                                if (that.pervOld.length === 0) {
154 155 156 157
                                    options.previous.attr('disabled', true);
                                }
                            }
                        }
158
                        that.renderTableLayoutView();
159 160 161
                        that.$('.fontLoader').hide();
                        that.$('.tableOverlay').hide();
                        that.$('.auditTable').show(); // Only for first time table show because we never hide after first render.
162 163 164 165
                    },
                    silent: true
                });
            },
166 167 168 169 170
            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, {
171
                        columns: cols
172
                    })));
173
                    if (!(that.entityCollection.models.length < that.limit)) {
174
                        that.RAuditTableLayoutView.$el.find('table tr').last().hide();
175
                    }
176 177 178 179 180
                });
            },
            getAuditTableColumns: function() {
                var that = this;
                return this.entityCollection.constructor.getTableCols({
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

                    tool: {
                        label: "",
                        cell: "html",
                        editable: false,
                        sortable: false,
                        fixWidth: "20",
                        cell: Backgrid.ExpandableCell,
                        accordion: false,
                        expand: function(el, model) {
                            el.attr('colspan', '4');
                            require([
                                'views/audit/CreateAuditTableLayoutView',
                            ], function(CreateAuditTableLayoutView) {

                                that.action = model.get('action');
                                // $(el.target).attr('disabled', true);
                                var eventModel = that.entityCollection.fullCollection.findWhere({ 'eventKey': model.get('eventKey') }).toJSON(),
                                    collectionModel = new that.entityCollection.model(eventModel),
                                    view = new CreateAuditTableLayoutView({ guid: that.guid, entityModel: collectionModel, action: that.action, entity: that.entity, entityName: that.entityName, attributeDefs: that.attributeDefs });
                                view.render();
                                $(el).append($('<div>').html(view.$el));
                            });

                        }
                    },
207
                    user: {
208
                        label: "Users",
209 210 211 212 213
                        cell: "html",
                        editable: false,
                    },
                    timestamp: {
                        label: "Timestamp",
214
                        cell: "html",
215 216 217
                        editable: false,
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
218
                                return Utils.formatDate({ date: rawValue });
219 220 221 222
                            }
                        })
                    },
                    action: {
223
                        label: "Actions",
224 225
                        cell: "html",
                        editable: false,
226 227
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
228 229
                                if (Enums.auditAction[rawValue]) {
                                    return Enums.auditAction[rawValue];
230
                                } else {
231
                                    return rawValue;
232 233 234
                                }
                            }
                        })
235
                    }
236
                }, this.entityCollection);
237

238
            },
239 240 241 242 243
            onClickNextAuditData: function() {
                var that = this;
                this.ui.previousAuditData.removeAttr("disabled");
                $.extend(this.entityCollection.queryParams, {
                    startKey: function() {
244
                        return that.next;
245 246 247 248 249 250
                    }
                });
                this.fetchCollection({
                    next: this.ui.nextAuditData,
                    nextClick: true,
                    previous: this.ui.previousAuditData
251
                });
252 253 254 255 256 257
            },
            onClickPreviousAuditData: function() {
                var that = this;
                this.ui.nextAuditData.removeAttr("disabled");
                $.extend(this.entityCollection.queryParams, {
                    startKey: function() {
258
                        return that.pervOld.pop();
259 260 261 262 263 264 265 266
                    }
                });
                this.fetchCollection({
                    next: this.ui.nextAuditData,
                    previousClick: true,
                    previous: this.ui.previousAuditData
                });
            },
267 268
        });
    return AuditTableLayoutView;
269
});