BusinessMetadataTableLayoutView.js 22.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**
 * 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',
21
    'hbs!tmpl/business_metadata/BusinessMetadataTableLayoutView_tmpl',
22
    'utils/Utils',
23 24
    'utils/Messages'
], function(require, Backbone, BusinessMetadataTableLayoutView_tmpl, Utils, Messages) {
25 26
    'use strict';

27 28
    var BusinessMetadataTableLayoutView = Backbone.Marionette.LayoutView.extend(
        /** @lends BusinessMetadataTableLayoutView */
29
        {
30
            _viewName: 'BusinessMetadataTableLayoutView',
31

32
            template: BusinessMetadataTableLayoutView_tmpl,
33 34 35

            /** Layout sub regions */
            regions: {
36
                RBusinessMetadataTableLayoutView: "#r_businessMetadataTableLayoutView",
37 38 39 40 41
                RModal: "#r_modal"
            },

            /** ui selector cache */
            ui: {
42 43 44 45
                businessMetadataAttrPage: "[data-id='businessMetadataAttrPage']",
                businessMetadataAttrPageTitle: "[data-id='businessMetadataAttrPageTitle']",
                businessMetadataDetailPage: "[data-id='businessMetadataDetailPage']",
                createBusinessMetadata: "[data-id='createBusinessMetadata']",
46 47
                attributeEdit: "[data-id='attributeEdit']",
                addAttribute: '[data-id="addAttribute"]',
48
                businessMetadataAttrPageOk: '[data-id="businessMetadataAttrPageOk"]',
49
                colManager: "[data-id='colManager']",
50
                deleteBusinessMetadata: '[data-id="deleteBusinessMetadata"]'
51 52 53 54 55
            },
            /** ui events hash */
            events: function() {
                var events = {},
                    that = this;
56
                events["click " + this.ui.createBusinessMetadata] = "onClickCreateBusinessMetadata";
57 58
                events["click " + this.ui.addAttribute] = "onEditAttr";
                events["click " + this.ui.attributeEdit] = "onEditAttr";
59
                events["click " + this.ui.deleteBusinessMetadata] = function(e) {
60
                    that.guid = e.target.dataset.guid;
61
                    that.deleteBusinessMetadataElement();
62 63 64 65
                };
                return events;
            },
            /**
66
             * intialize a new BusinessMetadataTableLayoutView Layout
67 68 69
             * @constructs
             */
            initialize: function(options) {
70
                _.extend(this, _.pick(options, 'guid', 'entity', 'entityName', 'attributeDefs', 'typeHeaders', 'businessMetadataDefCollection', 'entityDefCollection', 'businessMetadataAttr', 'selectedBusinessMetadata'));
71 72 73
                this.limit = 10;
                this.newAttr = false;
                this.commonTableOptions = {
74
                    collection: this.businessMetadataDefCollection,
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
                    includeFilter: false,
                    includePagination: true,
                    includeFooterRecords: true,
                    includePageSize: true,
                    includeGotoPage: true,
                    includeAtlasTableSorting: true,
                    includeTableLoader: true,
                    includeColumnManager: true,
                    gridOpts: {
                        className: "table table-hover backgrid table-quickMenu",
                        emptyText: 'No records found!'
                    },
                    columnOpts: {
                        opts: {
                            initialColumnsVisible: null,
                            saveState: false
                        },
                        visibilityControlOpts: {
                            buttonTemplate: _.template("<button class='btn btn-action btn-sm pull-right'>Columns&nbsp<i class='fa fa-caret-down'></i></button>")
                        },
                        el: this.ui.colManager
                    },
                    filterOpts: {},
                    paginatorOpts: {}
                };
                this.guid = null;
                this.showDetails = true; // toggle between sttribute page and detail page
            },
            onRender: function() {
104 105 106
                this.toggleBusinessMetadataDetailsAttrView();
                $.extend(this.businessMetadataDefCollection.queryParams, { count: this.limit });
                this.businessMetadataDefCollection.fullCollection.sort({ silent: true });
107 108 109
                this.renderTableLayoutView();
                this.$('.tableOverlay').hide();
                this.$('.auditTable').show(); // Only for first time table show because we never hide after first render.
110
                this.businessMetadataDefCollection.comparator = function(model) {
111 112 113
                    return -model.get('timestamp');
                }
            },
114
            toggleBusinessMetadataDetailsAttrView: function() {
115 116
                var that = this;
                if (that.showDetails) {
117 118
                    that.ui.businessMetadataAttrPage.hide();
                    that.ui.businessMetadataDetailPage.show();
119
                } else {
120 121
                    that.ui.businessMetadataAttrPage.show();
                    that.ui.businessMetadataDetailPage.hide();
122 123 124 125 126 127
                }
            },
            bindEvents: function() {},
            loaderStatus: function(isActive) {
                var that = this;
                if (isActive) {
128 129
                    that.$('.businessMetadata-attr-tableOverlay').show();
                    that.$('.business-metadata-attr-fontLoader').show();
130
                } else {
131 132
                    that.$('.businessMetadata-attr-tableOverlay').hide();
                    that.$('.business-metadata-attr-fontLoader').hide();
133 134 135 136
                }
            },
            onEditAttr: function(e) {
                var that = this,
137 138
                    isAttrEdit = e.currentTarget.dataset && e.currentTarget.dataset.id === 'attributeEdit' ? true : false,
                    guid = e.currentTarget.dataset && e.currentTarget.dataset.guid ? e.currentTarget.dataset.guid : null,
139 140
                    selectedBusinessMetadata = that.businessMetadataDefCollection.fullCollection.findWhere({ guid: guid }),
                    attrributes = selectedBusinessMetadata ? selectedBusinessMetadata.get('attributeDefs') : null,
141
                    attrName = e.currentTarget.dataset.name ? e.currentTarget.dataset.name : null,
142
                    attrDetails = { name: attrName };
143 144
                if (selectedBusinessMetadata) {
                    that.ui.businessMetadataAttrPageOk.text("Save");
145
                    that.newAttr = e.currentTarget && e.currentTarget.dataset.action === "createAttr" ? true : false;
146 147 148
                    that.guid = guid;
                    _.each(attrributes, function(attrObj) {
                        if (attrObj.name === attrName) {
149
                            attrDetails = $.extend(true, {}, attrObj);
150
                            if (attrObj.typeName.includes('array')) {
151
                                attrDetails.typeName = attrObj.typeName.replace("array<", "").replace(">", "");
152 153 154 155 156 157
                                attrDetails.multiValued = true;
                            }
                        }
                    });

                    that.showDetails = false;
158 159 160 161
                    that.toggleBusinessMetadataDetailsAttrView();
                    that.ui.businessMetadataAttrPageOk.attr('data-action', e.currentTarget.dataset.id);
                    require(["views/business_metadata/CreateBusinessMetadataLayoutView"], function(CreateBusinessMetadataLayoutView) {
                        that.view = new CreateBusinessMetadataLayoutView({
162
                            onEditCallback: function() {
163
                                that.businessMetadataDefCollection.fullCollection.sort({ silent: true });
164 165
                                that.renderTableLayoutView();
                            },
166
                            onUpdateBusinessMetadata: function(fetch) {
167
                                that.showDetails = true;
168
                                that.toggleBusinessMetadataDetailsAttrView();
169 170 171 172
                                if (fetch) {
                                    enumDefCollection.fetch({ reset: true });
                                    that.entityDefCollection.fetch({ silent: true });
                                }
173 174
                            },
                            parent: that.$el,
175
                            businessMetadataDefCollection: that.businessMetadataDefCollection,
176 177 178 179
                            enumDefCollection: enumDefCollection,
                            isAttrEdit: isAttrEdit,
                            typeHeaders: typeHeaders,
                            attrDetails: attrDetails,
180
                            selectedBusinessMetadata: selectedBusinessMetadata,
181 182 183 184
                            guid: that.guid,
                            isNewAttr: that.newAttr
                        });
                        if (isAttrEdit) {
185
                            that.ui.businessMetadataAttrPageTitle.text("Update Attribute of: " + selectedBusinessMetadata.get('name'));
186
                        } else {
187
                            that.ui.businessMetadataAttrPageTitle.text("Add Business Metadata Attribute for: " + selectedBusinessMetadata.get('name'));
188 189 190 191 192 193
                        }

                        that.RModal.show(that.view);
                    });
                }
            },
194
            onClickCreateBusinessMetadata: function(e) {
195
                var that = this,
196
                    isNewBusinessMetadata = true;
197
                that.showDetails = false;
198 199 200 201 202 203
                that.ui.businessMetadataAttrPageOk.text("Create");
                that.ui.businessMetadataAttrPageOk.attr('data-action', 'createBusinessMetadata');
                that.ui.businessMetadataAttrPageTitle.text("Create Business Metadata");
                that.toggleBusinessMetadataDetailsAttrView();
                require(["views/business_metadata/CreateBusinessMetadataLayoutView"], function(CreateBusinessMetadataLayoutView) {
                    that.view = new CreateBusinessMetadataLayoutView({
204
                        onUpdateBusinessMetadata: function(fetch) {
205
                            that.showDetails = true;
206
                            that.toggleBusinessMetadataDetailsAttrView();
207 208 209 210
                            if (fetch) {
                                enumDefCollection.fetch({ reset: true });
                                that.entityDefCollection.fetch({ silent: true });
                            }
211
                        },
212
                        businessMetadataDefCollection: that.businessMetadataDefCollection,
213 214
                        enumDefCollection: enumDefCollection,
                        typeHeaders: typeHeaders,
215
                        isNewBusinessMetadata: isNewBusinessMetadata
216 217 218 219 220 221 222
                    });
                    that.RModal.show(that.view);
                });
            },
            renderTableLayoutView: function() {
                var that = this;
                require(['utils/TableLayout'], function(TableLayout) {
223 224
                    var cols = new Backgrid.Columns(that.getBusinessMetadataTableColumns());
                    that.RBusinessMetadataTableLayoutView.show(new TableLayout(_.extend({}, that.commonTableOptions, {
225 226
                        columns: cols
                    })));
227 228
                    if (!(that.businessMetadataDefCollection.models.length < that.limit)) {
                        that.RBusinessMetadataTableLayoutView.$el.find('table tr').last().hide();
229 230 231 232
                    }

                });
            },
233
            getBusinessMetadataTableColumns: function() {
234
                var that = this;
235
                return this.businessMetadataDefCollection.constructor.getTableCols({
236 237 238 239 240 241
                    attributeDefs: {
                        label: "",
                        cell: "html",
                        editable: false,
                        sortable: false,
                        cell: Backgrid.ExpandableCell,
242
                        fixWidth: "20",
243 244 245 246 247 248 249
                        accordion: false,
                        alwaysVisible: true,
                        expand: function(el, model) {
                            el.attr('colspan', '8');
                            var attrValues = '',
                                attrTable = $('table'),
                                attrTableBody = $('tbody'),
250
                                attrTableHeading = "<thead><td style='display:table-cell'><b>Attribute</b></td><td style='display:table-cell'><b>Type</b></td><td style='display:table-cell'><b>Search Weight</b></td><td style='display:table-cell'><b>Enable Multivalues</b></td><td style='display:table-cell'><b>Max Length</b></td><td style='display:table-cell'><b>Applicable Type(s)</b></td><td style='display:table-cell'><b>Action</b></td></thead>",
251 252 253 254 255
                                attrRow = '',
                                attrTableDetails = '';
                            if (model.attributes && model.attributes.attributeDefs.length) {
                                _.each(model.attributes.attributeDefs, function(attrObj) {
                                    var applicableEntityTypes = '',
256 257 258
                                        typeName = attrObj.typeName,
                                        multiSelect = '',
                                        maxString = 'NA';
259
                                    if (attrObj.options && attrObj.options.applicableEntityTypes) {
260 261 262 263 264 265 266
                                        var entityTypes = JSON.parse(attrObj.options.applicableEntityTypes);
                                        _.each(entityTypes, function(values) {
                                            applicableEntityTypes += '<label class="btn btn-action btn-xs btn-blue no-pointer">' + values + '</label>';
                                        })
                                    }
                                    if (typeName.includes('array')) {
                                        typeName = _.escape(typeName);
267
                                        multiSelect = 'checked';
268
                                    }
269 270 271 272 273
                                    if (typeName.includes('string') && attrObj.options && attrObj.options.maxStrLength) {
                                        maxString = attrObj.options.maxStrLength;
                                    }

                                    attrRow += "<tr> <td style='display:table-cell'>" + _.escape(attrObj.name) + "</td><td style='display:table-cell'>" + typeName + "</td><td style='display:table-cell'>" + _.escape(attrObj.searchWeight) + "</td><td style='display:table-cell'><input type='checkbox' class='form-check-input multi-value-select' " + multiSelect + " disabled='disabled'> </td><td style='display:table-cell'>" + maxString + "</td><td style='display:table-cell'>" + applicableEntityTypes + "</td><td style='display:table-cell'> <div class='btn btn-action btn-sm' style='margin-left:0px;' data-id='attributeEdit' data-guid='" + model.get('guid') + "' data-name ='" + _.escape(attrObj.name) + "' data-action='attributeEdit' >Edit</div> </td></tr> ";
274
                                });
275 276
                                var adminText = '<div class="row"><div class="col-sm-12 attr-details"><table style="padding: 50px;">' + attrTableHeading + attrRow + '</table></div></div>';
                                $(el).append($('<div>').html(adminText));
277
                            } else {
278 279
                                var adminText = '<div class="row"><div class="col-sm-12 attr-details"><h5 class="text-center"> No attributes to show.</h5></div></div>';
                                $(el).append($('<div>').html(adminText));
280 281 282 283 284 285 286 287 288
                            }
                        }
                    },
                    name: {
                        label: "Name",
                        cell: "html",
                        editable: false,
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
289
                                return '<a title= "' + model.get('name') + '" href ="#!/administrator/businessMetadata/' + model.get('guid') + '?from=bm">' + model.get('name') + '</a>';
290 291 292 293 294 295 296 297 298
                            }
                        })
                    },
                    description: {
                        label: "Description",
                        cell: "html",
                        editable: false,
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
299
                                return _.escape(model.get('description'));
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
                            }
                        })
                    },
                    createdBy: {
                        label: "Created by",
                        cell: "html",
                        renderable: false,
                        editable: false,
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
                                return model.get('updatedBy');
                            }
                        })
                    },
                    createTime: {
                        label: "Created on",
                        cell: "html",
                        renderable: false,
                        editable: false,
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
321
                                return Utils.formatDate({ date: model.get('createTime') });
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
                            }
                        })
                    },
                    updatedBy: {
                        label: "Updated by",
                        cell: "html",
                        renderable: false,
                        editable: false,
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
                                return model.get('updatedBy');
                            }
                        })
                    },
                    updateTime: {
                        label: "Updated on",
                        cell: "html",
                        renderable: false,
                        editable: false,
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
343
                                return Utils.formatDate({ date: model.get('updateTime') });
344 345 346 347 348 349 350 351 352 353
                            }
                        })
                    },
                    tools: {
                        label: "Action",
                        cell: "html",
                        sortable: false,
                        editable: false,
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
354
                                return "<button type='button' data-id='addAttribute' data-guid='" + model.get('guid') + "' class='btn btn-action btn-sm' style='margin-bottom: 10px;' data-action='createAttr' data-original-title='Add Business Metadata attribute'><i class='fa fa-plus'></i> Attributes</button>";
355 356 357
                            }
                        })
                    }
358
                }, this.businessMetadataDefCollection);
359
            },
360
            deleteBusinessMetadataElement: function(businessMetadataName) {
361 362 363 364 365 366 367 368
                var that = this,
                    notifyObj = {
                        modal: true,
                        ok: function(argument) {
                            that.onNotifyDeleteOk();
                        },
                        cancel: function(argument) {}
                    };
369
                var text = "Are you sure you want to delete the business metadata";
370 371 372 373 374
                notifyObj["text"] = text;
                Utils.notifyConfirm(notifyObj);
            },
            onNotifyDeleteOk: function(data) {
                var that = this,
375
                    deleteBusinessMetadataData = that.businessMetadataDefCollection.fullCollection.findWhere({ guid: that.guid });
376
                that.$('.tableOverlay').show();
377 378 379 380
                if (deleteBusinessMetadataData) {
                    var businessMetadataName = deleteBusinessMetadataData.get("name");
                    deleteBusinessMetadataData.deleteBusinessMetadata({
                        typeName: businessMetadataName,
381 382
                        success: function() {
                            Utils.notifySuccess({
383
                                content: "Business Metadata " + businessMetadataName + Messages.getAbbreviationMsg(false, 'deleteSuccessMessage')
384
                            });
385 386
                            that.businessMetadataDefCollection.fullCollection.remove(deleteBusinessMetadataData);
                            that.businessMetadataDefCollection.fullCollection.sort({ silent: true });
387 388
                            that.renderTableLayoutView();
                            that.showDetails = true;
389
                            that.toggleBusinessMetadataDetailsAttrView();
390 391 392 393 394 395 396 397 398 399 400 401 402 403
                            that.loaderStatus(false);
                        },
                        complete: function() {
                            that.$('.tableOverlay').hide();
                            that.$('.position-relative .fontLoader').removeClass('show');
                        }
                    });
                } else {
                    Utils.notifyError({
                        content: Messages.defaultErrorMessage
                    });
                }
            }
        });
404
    return BusinessMetadataTableLayoutView;
405
});