AuditTableLayoutView.js 10.6 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
    'utils/Globals'
], function(require, Backbone, AuditTableLayoutView_tmpl, VEntityList, Globals) {
25 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: {
                auditValue: "[data-id='auditValue']",
                auditCreate: "[data-id='auditCreate']",
43 44
                previousAuditData: "[data-id='previousAuditData']",
                nextAuditData: "[data-id='nextAuditData']"
45 46 47 48 49
            },
            /** ui events hash */
            events: function() {
                var events = {};
                events["click " + this.ui.auditCreate] = "onClickAuditCreate";
50 51
                events["click " + this.ui.nextAuditData] = "onClickNextAuditData";
                events["click " + this.ui.previousAuditData] = "onClickPreviousAuditData";
52 53 54 55 56 57 58 59 60
                return events;
            },
            /**
             * intialize a new AuditTableLayoutView Layout
             * @constructs
             */
            initialize: function(options) {
                _.extend(this, _.pick(options, 'globalVent', 'guid'));
                this.entityCollection = new VEntityList();
61
                this.count = 25;
62
                this.entityCollection.url = "/api/atlas/entities/" + this.guid + "/audit";
63
                this.entityCollection.modelAttrName = "events"
64
                this.entityModel = new this.entityCollection.model();
65
                this.pervOld = [];
66 67 68
                this.commonTableOptions = {
                    collection: this.entityCollection,
                    includeFilter: false,
69
                    includePagination: false,
70
                    includePageSize: false,
71
                    includeFooterRecords: false,
72
                    gridOpts: {
73
                        className: "table table-hover backgrid table-quickMenu",
74 75 76 77 78 79 80
                        emptyText: 'No records found!'
                    },
                    filterOpts: {},
                    paginatorOpts: {}
                };
            },
            onRender: function() {
81 82 83 84 85 86
                $.extend(this.entityCollection.queryParams, { count: this.count });
                this.fetchCollection({
                    next: this.ui.nextAuditData,
                    nextClick: false,
                    previous: this.ui.previousAuditData
                });
87 88
                this.renderTableLayoutView();
            },
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
            fetchCollection: function(options) {
                var that = this;
                this.$('.fontLoader').show();
                this.$('.auditTable').hide();
                if (that.entityCollection.models.length > 1) {
                    if (options.nextClick) {
                        this.pervOld.push(that.entityCollection.first().get('eventKey'));
                    }
                }
                this.entityCollection.fetch({
                    success: function() {
                        that.$('.fontLoader').hide();
                        that.$('.auditTable').show();
                        options.previous.attr('disabled', true);
                        if (that.entityCollection.models.length <= 1) {
                            that.pervOld.pop();
                            options.next.attr('disabled', true);
                        }
                        if (that.entityCollection.models.length == 1 && that.next == that.entityCollection.last().get('eventKey')) {
                            options.next.attr('disabled', true);
                            options.previous.removeAttr("disabled");
                        } else {
                            if (that.entityCollection.models.length > 0) {
                                that.next = that.entityCollection.last().get('eventKey');
                                if (options.nextClick) {
                                    options.previous.removeAttr("disabled");
                                }
                                if (options.previousClick) {
                                    options.previous.removeAttr("disabled");
                                }
                                if (that.pervOld.length == 0) {
                                    options.previous.attr('disabled', true);
                                }
                            }
                            that.renderTableLayoutView();
                        }
                    },
                    silent: true
                });
            },
129 130 131 132 133 134
            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, {
                        globalVent: that.globalVent,
135
                        columns: cols
136 137 138 139 140 141 142
                    })));
                });
            },
            getAuditTableColumns: function() {
                var that = this;
                return this.entityCollection.constructor.getTableCols({
                    user: {
143
                        label: "Users",
144 145 146 147 148 149
                        cell: "html",
                        editable: false,
                        sortable: false,
                    },
                    timestamp: {
                        label: "Timestamp",
150
                        cell: "html",
151 152 153 154
                        editable: false,
                        sortable: false,
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
155
                                return new Date(rawValue);
156 157 158 159
                            }
                        })
                    },
                    action: {
160
                        label: "Actions",
161 162
                        cell: "html",
                        editable: false,
163 164 165 166 167
                        sortable: false,
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
                                that.detailBtnDisable = false;
                                if (Globals.auditAction[rawValue]) {
168
                                    return Globals.auditAction[rawValue];
169
                                } else {
170
                                    return rawValue;
171 172 173
                                }
                            }
                        })
174 175
                    },
                    tool: {
176
                        label: "Tools",
177 178 179 180 181
                        cell: "html",
                        editable: false,
                        sortable: false,
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
182
                                return '<div class="label label-success auditDetailBtn" data-id="auditCreate" data-action="' + Globals.auditAction[model.attributes.action] + '" disabled="' + that.detailBtnDisable + '" data-modalId="' + model.get('eventKey') + '">Detail</div>';
183 184 185 186 187
                            }
                        })
                    },

                }, this.entityCollection);
188

189 190 191 192 193 194 195
            },
            onClickAuditCreate: function(e) {
                var that = this;
                require([
                    'modules/Modal',
                    'views/audit/CreateAuditTableLayoutView',
                ], function(Modal, CreateAuditTableLayoutView) {
196 197 198
                    var collectionModel = that.entityCollection.findWhere({ 'eventKey': $(e.currentTarget).data('modalid') });
                    that.action = $(e.target).data("action");
                    var view = new CreateAuditTableLayoutView({ guid: that.guid, model: collectionModel, action: that.action });
199
                    var modal = new Modal({
200
                        title: that.action,
201 202 203 204
                        content: view,
                        okCloses: true,
                        showFooter: true,
                    }).open();
205 206 207
                    view.on('closeModal', function() {
                        modal.trigger('cancel');
                    });
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
                    view.$el.on('click', 'td a', function() {
                        modal.trigger('cancel');
                    })
                });
            },
            onClickNextAuditData: function() {
                var that = this;
                this.ui.previousAuditData.removeAttr("disabled");
                $.extend(this.entityCollection.queryParams, {
                    startKey: function() {
                        return that.next
                    }
                });
                this.fetchCollection({
                    next: this.ui.nextAuditData,
                    nextClick: true,
                    previous: this.ui.previousAuditData
225
                });
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
            },
            onClickPreviousAuditData: function() {
                var that = this;
                this.ui.nextAuditData.removeAttr("disabled");
                $.extend(this.entityCollection.queryParams, {
                    startKey: function() {
                        return that.pervOld.pop()
                    }
                });
                this.fetchCollection({
                    next: this.ui.nextAuditData,
                    previousClick: true,
                    previous: this.ui.previousAuditData
                });
            },
241 242 243
        });
    return AuditTableLayoutView;
});