BusinessMetadataAttrTableLayoutView.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
/**
 * 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 22 23
    'hbs!tmpl/business_metadata/BusinessMetadataAttrTableLayoutView_tmpl',
    'collection/VEntityList'
], function(require, Backbone, BusinessMetadataAttrTableLayoutView_tmpl, VEntityList) {
24 25
    'use strict';

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

31
            template: BusinessMetadataAttrTableLayoutView_tmpl,
32 33 34

            /** Layout sub regions */
            regions: {
35
                RBusinessMetadataAttrTableLayoutView: "#r_businessMetadataAttrTableLayoutView",
36 37 38 39 40 41 42
                RModal: "#r_modal"
            },

            /** ui selector cache */
            ui: {
                attributeEdit: "[data-id='attributeEdit']",
                addAttribute: '[data-id="addAttribute"]',
43 44 45
                businessMetadataAttrPage: "[data-id='businessMetadataAttrPage']",
                businessMetadataAttrPageTitle: "[data-id='businessMetadataAttrPageTitle']",
                businessMetadataDetailPage: "[data-id='businessMetadataDetailPage']",
46 47 48 49 50 51 52 53 54
            },
            /** ui events hash */
            events: function() {
                var events = {};
                events["click " + this.ui.attributeEdit] = "onEditAttr";
                events["click " + this.ui.addAttribute] = "onEditAttr";
                return events;
            },
            /**
55
             * intialize a new BusinessMetadataAttrTableLayoutView Layout
56 57 58
             * @constructs
             */
            initialize: function(options) {
59 60
                _.extend(this, _.pick(options, 'guid', 'model', 'typeHeaders', 'businessMetadataDefCollection', 'entityDefCollection'));
                this.businessMetadataAttr = new VEntityList(this.model.get("attributeDefs") || []);
61
                this.commonTableOptions = {
62
                    collection: this.businessMetadataAttr,
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
                    includeFilter: false,
                    includePagination: false,
                    includePageSize: false,
                    includeAtlasTableSorting: true,
                    includeFooterRecords: false,
                    gridOpts: {
                        className: "table table-hover backgrid table-quickMenu",
                        emptyText: 'No records found!'
                    },
                    filterOpts: {},
                    paginatorOpts: {}
                };
                this.showDetails = true;
            },
            onRender: function() {
78 79
                this.renderTableLayoutView();
                this.toggleBusinessMetadataDetailsAttrView();
80 81
            },
            bindEvents: function() {},
82
            toggleBusinessMetadataDetailsAttrView: function() {
83 84
                var that = this;
                if (that.showDetails) {
85 86
                    that.ui.businessMetadataAttrPage.hide();
                    that.ui.businessMetadataDetailPage.show();
87
                } else {
88 89
                    that.ui.businessMetadataAttrPage.show();
                    that.ui.businessMetadataDetailPage.hide();
90 91 92 93 94
                }
            },
            onEditAttr: function(e) {
                var that = this,
                    isAttrEdit = false,
95 96
                    selectedBusinessMetadata = that.model,
                    attrributes = selectedBusinessMetadata ? selectedBusinessMetadata.get('attributeDefs') : null,
97 98 99 100 101
                    attrName = e.target.dataset.name ? e.target.dataset.name : null,
                    attrDetails = { name: attrName };
                if (e.target.dataset.action == 'attributeEdit') {
                    isAttrEdit = true
                }
102
                if (selectedBusinessMetadata) {
103 104 105
                    that.newAttr = isAttrEdit ? false : true;
                    _.each(attrributes, function(attrObj) {
                        if (attrObj.name === attrName) {
106
                            attrDetails = $.extend(true, {}, attrObj);
107
                            if (attrObj.typeName.includes('array')) {
108
                                attrDetails.typeName = attrObj.typeName.replace("array<", "").replace(">", "");
109 110 111 112 113
                                attrDetails.multiValued = true;
                            }
                        }
                    });
                    this.showDetails = false;
114 115 116
                    that.toggleBusinessMetadataDetailsAttrView();
                    require(["views/business_metadata/CreateBusinessMetadataLayoutView"], function(CreateBusinessMetadataLayoutView) {
                        that.view = new CreateBusinessMetadataLayoutView({
117 118
                            onEditCallback: function() {
                                enumDefCollection.fetch({ reset: true });
119
                                that.businessMetadataAttr.reset(that.model.get("attributeDefs"));
120
                            },
121
                            onUpdateBusinessMetadata: function(fetch) {
122
                                that.showDetails = true;
123
                                that.toggleBusinessMetadataDetailsAttrView();
124 125 126
                                if (fetch) {
                                    that.entityDefCollection.fetch({ silent: true });
                                }
127 128
                            },
                            parent: that.$el,
129
                            businessMetadataDefCollection: that.businessMetadataDefCollection,
130 131 132 133
                            enumDefCollection: enumDefCollection,
                            isAttrEdit: isAttrEdit,
                            attrDetails: attrDetails,
                            typeHeaders: typeHeaders,
134
                            selectedBusinessMetadata: that.model,
135 136 137 138
                            guid: that.guid,
                            isNewAttr: that.newAttr
                        });
                        if (isAttrEdit) {
139
                            that.ui.businessMetadataAttrPageTitle.text("Update Attribute of: " + selectedBusinessMetadata.get('name'));
140
                        } else {
141
                            that.ui.businessMetadataAttrPageTitle.text("Add Business Metadata Attribute for: " + selectedBusinessMetadata.get('name'));
142 143 144 145 146 147 148 149 150
                        }
                        that.RModal.show(that.view);
                    });
                }

            },
            renderTableLayoutView: function() {
                var that = this;
                require(['utils/TableLayout'], function(TableLayout) {
151 152
                    var cols = new Backgrid.Columns(that.getBusinessMetadataTableColumns());
                    that.RBusinessMetadataAttrTableLayoutView.show(new TableLayout(_.extend({}, that.commonTableOptions, {
153 154 155 156
                        columns: cols
                    })));
                });
            },
157
            getBusinessMetadataTableColumns: function() {
158
                var that = this;
159
                return this.businessMetadataAttr.constructor.getTableCols({
160 161 162 163 164 165
                    name: {
                        label: "Attribute Name",
                        cell: "html",
                        editable: false,
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
166
                                return _.escape(model.get('name'));
167 168 169 170
                            }
                        })
                    },
                    typeName: {
171
                        label: "Type Name",
172 173 174 175 176 177 178 179
                        cell: "html",
                        editable: false,
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
                                return _.escape(model.get('typeName'));
                            }
                        })
                    },
180 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 207 208 209 210 211 212
                    searchWeight: {
                        label: "Search Weight",
                        cell: "String",
                        editable: false
                    },
                    enableMultipleValue: {
                        label: "Enable Multivalues",
                        cell: "html",
                        editable: false,
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
                                var enableMultipleValue = '';
                                if (model.get('typeName').indexOf('array<') > -1) {
                                    enableMultipleValue = 'checked';
                                }
                                return '<input type="checkbox" class="form-check-input multi-value-select" data-id="multiValueSelectStatus" ' + enableMultipleValue + ' disabled="disabled">';
                            }
                        })
                    },
                    maxStrLength: {
                        label: "Max Length",
                        cell: "html",
                        editable: false,
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
                                var maxString = "NA";
                                if (model.get('typeName').indexOf('string') > -1) {
                                    maxString = model.get('options').maxStrLength || maxString;
                                }
                                return maxString;
                            }
                        })
                    },
213 214 215 216 217 218
                    options: {
                        label: "Entity Type(s)",
                        cell: "html",
                        editable: false,
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
219 220 221 222 223 224 225 226 227
                                var options = model.get('options')
                                if (options && options.applicableEntityTypes) {
                                    var applicableEntityTypes = '',
                                        attrEntityTypes = JSON.parse(options.applicableEntityTypes);
                                    _.each(attrEntityTypes, function(values) {
                                        applicableEntityTypes += '<label class="btn btn-action btn-xs btn-blue no-pointer">' + values + '</label>';
                                    });
                                    return applicableEntityTypes;
                                }
228 229
                            }
                        })
230 231 232 233 234 235 236 237 238 239 240 241
                    },
                    tool: {
                        label: "Action",
                        cell: "html",
                        editable: false,
                        sortable: false,
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
                                return '<div class="btn btn-action btn-sm" data-id="attributeEdit" data-action="attributeEdit" data-name="' + model.get('name') + '">Edit</div>';
                            }
                        })
                    }
242
                }, this.businessMetadataAttr);
243 244
            }
        });
245
    return BusinessMetadataAttrTableLayoutView;
246
});