PropagationPropertyModal.js 16.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
/**
 * 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',
    'hbs!tmpl/graph/PropagationPropertyModalView_tmpl',
    'models/VRelationship',
22
    'models/VEntity',
23 24 25 26
    'modules/Modal',
    'utils/Utils',
    'utils/UrlLinks',
    'utils/Messages'
27
], function(require, PropagationPropertyModalViewTmpl, VRelationship, VEntity, Modal, Utils, UrlLinks, Messages) {
28 29 30 31 32 33 34 35
    'use strict';

    var PropogationPropertyModal = Backbone.Marionette.CompositeView.extend({
        template: PropagationPropertyModalViewTmpl,
        templateHelpers: function() {},
        regions: {},
        ui: {
            propagationOptions: '[data-id="propagationOptions"]',
36 37
            edgeDetailName: '[data-id="edgeDetailName"]',
            propagationState: "[data-id='propagationState']",
38
            entityClick: "[data-id='entityClick']",
39 40 41
            editPropagationType: 'input[name="editPropagationType"]',
            PropagatedClassificationTable: "[data-id='PropagatedClassificationTable']"

42 43
        },
        events: function() {
44 45
            var events = {},
                that = this;
46
            events["change " + this.ui.propagationOptions] = function() {
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
                this.modalEdited = true;
                this.modal.$el.find('button.ok').attr("disabled", false);
            };
            events["click " + this.ui.editPropagationType] = function(e) {
                if (this.modalEdited === true) {
                    e.preventDefault();
                    that.notifyModal();
                }
            };
            events["change " + this.ui.editPropagationType] = function(e) {
                if (e.target.checked) {
                    this.showPropagatedClassificationTable();
                    this.viewType = "table";
                } else {
                    this.showEditPropagation();
                    this.viewType = "flow";
                }
            };
65
            events["click " + this.ui.entityClick] = function(e) {
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
                var that = this,
                    url = "",
                    notifyObj = {
                        modal: true,
                        text: "Are you sure you want to navigate away from this page ?",
                        ok: function(argument) {
                            that.modal.trigger('cancel');
                            Utils.setUrl({
                                url: url,
                                mergeBrowserUrl: false,
                                trigger: true
                            });

                        },
                        cancel: function(argument) {}
81 82 83 84 85
                    },
                    $el = $(e.currentTarget),
                    guid = $el.parents('tr').data('entityguid');
                if ($el.hasClass('entityName')) {
                    url = '#!/detailPage/' + guid + '?tabActive=lineage';
86
                } else {
87
                    url = '#!/tag/tagAttribute/' + $el.data('name');
88 89 90 91 92
                }
                Utils.notifyConfirm(notifyObj);
            };
            events["change " + this.ui.propagationState] = function(e) {
                this.modalEdited = true;
93
                this.modal.$el.find('button.ok').attr("disabled", false);
94 95 96
                var $el = $(e.currentTarget).parents('tr'),
                    entityguid = $el.data("entityguid"),
                    classificationName = $el.find('[data-name]').data('name');
97 98
                if (e.target.checked) {
                    this.propagatedClassifications = _.reject(this.propagatedClassifications, function(val, key) {
99
                        if (val.entityGuid == entityguid && classificationName == val.typeName) {
100 101 102 103 104 105
                            that.blockedPropagatedClassifications.push(val);
                            return true;
                        }
                    });
                } else {
                    this.blockedPropagatedClassifications = _.reject(this.blockedPropagatedClassifications, function(val, key) {
106
                        if (val.entityGuid == entityguid && classificationName == val.typeName) {
107 108 109 110 111
                            that.propagatedClassifications.push(val);
                            return true;
                        }
                    });
                }
112 113 114 115 116 117 118 119
            };
            return events;
        },
        /**
         * intialize a new PropogationPropertyModal Layout
         * @constructs
         */
        initialize: function(options) {
120
            _.extend(this, _.pick(options, 'edgeInfo', 'relationshipId', 'lineageData', 'apiGuid', 'detailPageFetchCollection'));
121
            this.entityModel = new VRelationship();
122 123 124
            this.VEntityModel = new VEntity();
            this.modalEdited = false;
            this.viewType = 'flow';
125 126
            var that = this,
                modalObj = {
127
                    title: 'Enable/Disable Propagation',
128 129 130 131
                    content: this,
                    okText: 'Update',
                    okCloses: false,
                    cancelText: "Cancel",
132
                    mainClass: 'modal-lg',
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
                    allowCancel: true,
                };

            this.modal = new Modal(modalObj)
            this.modal.open();
            this.modal.$el.find('button.ok').attr("disabled", true);
            this.on('ok', function() {
                that.updateRelation();
            });
            this.on('closeModal', function() {
                this.modal.trigger('cancel');
            });
            this.updateEdgeView(this.edgeInfo);
        },

148
        onRender: function() {},
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
        updateEdgeView: function(options) {
            var obj = options.obj,
                fromEntity = this.lineageData.guidEntityMap[obj.fromEntityId],
                toEntity = this.lineageData.guidEntityMap[obj.toEntityId];
            if (fromEntity && toEntity) {
                this.ui.edgeDetailName.html(fromEntity.displayText + " <span class='navigation-font'><i class='fa fa-long-arrow-right fa-color'></i></span> " + toEntity.displayText);
            }
            if (obj && obj.relationshipId) {
                this.showLoader();
                this.getEdgeEntity({ id: obj.relationshipId, from: fromEntity, to: toEntity });
            }
        },
        getPropagationFlow: function(options) {
            var relationshipData = options.relationshipData,
                graphData = options.graphData,
                propagateTags = relationshipData.propagateTags;
            if (relationshipData.end1) {
                if (relationshipData.end1.guid == graphData.from.guid || propagateTags == "BOTH" || propagateTags == "NONE") {
                    return propagateTags;
                } else {
                    return propagateTags == "ONE_TO_TWO" ? "TWO_TO_ONE" : "ONE_TO_TWO";
                }
            } else {
                return propagateTags;
            }
        },
        getEdgeEntity: function(options) {
            var that = this,
                id = options.id,
                from = options.from,
                to = options.to,
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
                enableOtherFlow = function(relationshipObj) {
                    var isTwoToOne = false;
                    if (relationshipObj.propagateTags == "BOTH") {
                        that.ui.propagationOptions.find('.both').show();
                    } else {
                        that.ui.propagationOptions.find('.both').hide();
                        if (that.edgeInfo.obj.fromEntityId != relationshipObj.end1.guid && relationshipObj.propagateTags == "ONE_TO_TWO") {
                            isTwoToOne = true;
                        } else if (that.edgeInfo.obj.fromEntityId == relationshipObj.end1.guid && relationshipObj.propagateTags == "TWO_TO_ONE") {
                            isTwoToOne = true;
                        }
                        if (isTwoToOne) {
                            that.ui.propagationOptions.find('.TWO_TO_ONE').show();
                        } else {
                            that.ui.propagationOptions.find('.TWO_TO_ONE').hide();
                        }
                    }
                },
                updateValue = function(relationshipData) {
                    var relationshipObj = relationshipData.relationship;
                    if (relationshipObj) {
                        that.$("input[name='propagateRelation'][value=" + that.getPropagationFlow({
                            "relationshipData": relationshipObj,
                            "graphData": options
                        }) + "]").prop('checked', true);
                        enableOtherFlow(relationshipObj);
                        that.showBlockedClassificationTable(relationshipData);
                        that.hideLoader({ buttonDisabled: true });
                    }
                }
            this.ui.propagationOptions.find('li label>span.fromName').text(from.typeName);
            this.ui.propagationOptions.find('li label>span.toName').text(to.typeName);

213 214 215 216 217
            if (id === this.ui.propagationOptions.attr("entity-id")) {
                return;
            }
            this.ui.propagationOptions.attr("entity-id", id);
            if (this.apiGuid[id]) {
218
                updateValue(this.apiGuid[id]);
219 220 221 222 223 224
            } else {
                if (this.edgeCall && this.edgeCall.readyState != 4) {
                    this.edgeCall.abort();
                }
                this.edgeCall = this.entityModel.getRelationship(id, {
                    success: function(relationshipData) {
225 226
                        that.apiGuid[relationshipData.relationship.guid] = relationshipData;
                        updateValue(relationshipData);
227 228 229 230 231 232 233 234 235 236
                    },
                    cust_error: function() {
                        that.hideLoader();
                    }
                });
            }
        },
        updateRelation: function() {
            var that = this,
                entityId = that.ui.propagationOptions.attr('entity-id'),
237 238
                PropagationValue = this.$("input[name='propagateRelation']:checked").val(),
                relationshipProp = {};
239 240 241 242
            if (PropagationValue === this.ui.propagationOptions.attr("propagation")) {
                return;
            }
            this.ui.propagationOptions.attr("propagation", PropagationValue);
243 244 245
            if (this.viewType == "flow") {
                relationshipProp = {
                    "propagateTags": that.getPropagationFlow({
246
                        "relationshipData": _.extend({}, this.apiGuid[entityId].relationship, { 'propagateTags': PropagationValue }),
247 248 249 250 251
                        "graphData": { from: { guid: this.edgeInfo.obj.fromEntityId } }
                    })
                }
            } else {
                relationshipProp = {
252 253
                    "blockedPropagatedClassifications": this.blockedPropagatedClassifications,
                    "propagatedClassifications": this.propagatedClassifications
254 255
                };
            }
256 257
            this.showLoader();
            this.entityModel.saveRelationship({
258
                data: JSON.stringify(_.extend({}, that.apiGuid[entityId].relationship, relationshipProp)),
259 260 261 262 263
                success: function(relationshipData) {
                    if (relationshipData) {
                        that.hideLoader({ buttonDisabled: true });
                        that.modal.trigger('cancel');
                        that.apiGuid[relationshipData.guid] = relationshipData;
264
                        that.detailPageFetchCollection();
265 266 267 268 269 270 271 272 273 274
                        Utils.notifySuccess({
                            content: "Propagation flow updated succesfully."
                        });
                    }
                },
                cust_error: function() {
                    that.hideLoader();
                }
            });
        },
275 276 277
        showBlockedClassificationTable: function(options) {
            var that = this,
                propagationStringValue = "",
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
                classificationTableValue = "",
                relationship = options.relationship,
                referredEntities = options.referredEntities,
                getEntityName = function(guid) {
                    var entityObj = referredEntities[guid],
                        name = guid;
                    if (entityObj) {
                        name = Utils.getName(entityObj) + " (" + entityObj.typeName + ")";
                    }
                    return "<a class='entityName' data-id='entityClick'>" + name + "</a>";
                },
                getTableRow = function(options) {
                    var val = options.val,
                        fromBlockClassification = options.fromBlockClassification;
                    return "<tr data-entityguid=" + val.entityGuid + "><td class='text-center w30'><a class='classificationName' data-id='entityClick' title='" + val.typeName + "' data-name='" + val.typeName + "''>" + val.typeName + "</a></td><td class='text-center'>" + getEntityName(val.entityGuid) + "</td><td class='text-center w30'><input type='checkbox' " + (fromBlockClassification ? "checked" : "") + " data-id='propagationState' class='input'></td></tr>";
                };

            this.blockedPropagatedClassifications = _.isUndefined(relationship.blockedPropagatedClassifications) ? [] : _.clone(relationship.blockedPropagatedClassifications);
            this.propagatedClassifications = _.isUndefined(relationship.propagatedClassifications) ? [] : _.clone(relationship.propagatedClassifications);
297
            _.each(this.blockedPropagatedClassifications, function(val, key) {
298
                propagationStringValue += getTableRow({ "val": val, fromBlockClassification: true });
299 300
            });
            _.each(this.propagatedClassifications, function(val, key) {
301
                propagationStringValue += getTableRow({ "val": val, fromBlockClassification: false });
302 303
            });

304
            classificationTableValue = "<table class='attriTable'><tr><th class='w30'>Classification</th><th>Entity Name</th><th class='w30'>Block Propagatation</th>" + propagationStringValue + "</table>";
305 306 307

            this.ui.PropagatedClassificationTable.append(_.isEmpty(propagationStringValue) ? "No Records Found." : classificationTableValue);
        },
308 309 310 311 312 313 314 315
        showLoader: function() {
            this.modal.$el.find('button.ok').attr("disabled", true);
            this.$('.overlay').removeClass('hide').addClass('show');
        },
        hideLoader: function(options) {
            var buttonDisabled = options && options.buttonDisabled;
            this.modal.$el.find('button.ok').attr("disabled", buttonDisabled ? buttonDisabled : false);
            this.$('.overlay').removeClass('show').addClass('hide');
316 317 318 319 320
        },
        notifyModal: function(options) {
            var that = this,
                notifyObj = {
                    modal: true,
321
                    text: "It looks like you have edited something. If you leave before saving, your changes will be lost.",
322 323 324 325 326 327 328 329 330 331 332 333 334 335
                    ok: function(argument) {
                        that.viewType = that.ui.editPropagationType.is(":checked") ? "flow" : "table";
                        that.ui.editPropagationType.prop("checked", that.viewType === "flow" ? false : true).trigger("change");
                        that.modal.$el.find('button.ok').attr("disabled", true);
                    },
                    cancel: function(argument) {
                        that.viewType = that.ui.editPropagationType.is(":checked") ? "table" : "flow";
                    }
                };
            Utils.notifyConfirm(notifyObj);
        },
        showEditPropagation: function() {
            this.$('.editPropagation').show();
            this.$('.propagatedClassificationTable').hide();
336
            this.modal.$el.find('.modal-title').text("Enable/Disable Propagation");
337 338 339 340
        },
        showPropagatedClassificationTable: function() {
            this.$('.editPropagation').hide();
            this.$('.propagatedClassificationTable').show();
341
            this.modal.$el.find('.modal-title').text("Select Classifications to Block Propagation");
342
        }
343

344 345 346
    });
    return PropogationPropertyModal;
});