CommonViewFunction.js 22.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/**
 * 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.
 */

19
define(['require', 'utils/Utils', 'modules/Modal', 'utils/Messages', 'utils/Enums'], function(require, Utils, Modal, Messages, Enums) {
20 21 22
    'use strict';

    var CommonViewFunction = {};
23
    CommonViewFunction.deleteTagModel = function(options) {
24
        var modal = new Modal({
25 26 27
            title: options.titleMessage,
            okText: options.buttonText,
            htmlContent: options.msg,
28 29 30 31 32 33 34 35 36 37
            cancelText: "Cancel",
            allowCancel: true,
            okCloses: true,
            showFooter: true,
        }).open();
        return modal;
    };
    CommonViewFunction.deleteTag = function(options) {
        require(['models/VTag'], function(VTag) {
            var tagModel = new VTag();
38
            if (options && options.guid && options.tagName) {
39 40 41
                if (options.showLoader) {
                    options.showLoader();
                }
42
                tagModel.deleteTag(options.guid, options.tagName, {
43
                    skipDefaultError: true,
44
                    success: function(data) {
45
                        var msg = "Tag " + name.name + Messages.removeSuccessMessage;
46 47 48 49
                        if (options.tagOrTerm === "term") {
                            msg = "Term " + options.tagName + Messages.removeSuccessMessage;
                        } else if (options.tagOrTerm === "tag") {
                            msg = "Tag " + options.tagName + Messages.removeSuccessMessage;
50
                        }
51
                        Utils.notifySuccess({
52
                            content: msg
53
                        });
54 55 56 57 58 59 60
                        if (options.callback) {
                            options.callback();
                        }
                        if (options.collection) {
                            options.collection.fetch({ reset: true });
                        }

61
                    },
62
                    cust_error: function(model, response) {
63
                        var message = options.tagName + Messages.deleteErrorMessage;
64 65
                        if (response && response.responseJSON) {
                            message = response.responseJSON.errorMessage;
66
                        }
67 68 69
                        if (options.hideLoader) {
                            options.hideLoader();
                        }
70 71 72
                        Utils.notifyError({
                            content: message
                        });
73
                    }
74
                });
75
            }
76 77
        });
    };
78 79 80 81 82 83 84 85 86 87 88 89 90
    CommonViewFunction.findAndmergeRefEntity = function(attributeObject, referredEntities) {
        _.each(attributeObject, function(obj, key) {
            if (_.isObject(obj)) {
                if (_.isArray(obj)) {
                    _.each(obj, function(value) {
                        _.extend(value, referredEntities[value.guid]);
                    });
                } else {
                    _.extend(obj, referredEntities[obj.guid]);
                }
            }
        });
    }
91
    CommonViewFunction.propertyTable = function(valueObject, scope, searchTable) {
92 93 94
        var table = "",
            fetchInputOutputValue = function(id) {
                var that = this;
95 96 97 98
                if (searchTable) {
                    ++scope.fetchList
                }
                scope.entityModel.getEntity(id, {
99
                    success: function(serverData) {
100
                        var value = "",
101 102
                            deleteButton = "",
                            data = serverData.entity;
103 104 105 106 107 108 109 110
                        if (data && data.attributes) {
                            if (data.attributes.name) {
                                value = data.attributes.name;
                            } else if (data.attributes.qualifiedName) {
                                value = data.attributes.qualifiedName;
                            } else if (data.typeName) {
                                value = data.typeName;
                            }
111 112
                        }
                        var id = "";
113
                        if (data.guid) {
114
                            if (Enums.entityStateReadOnly[data.status]) {
115
                                deleteButton += '<button title="Deleted" class="btn btn-atlasAction btn-atlas deleteBtn"><i class="fa fa-trash"></i></button>';
116
                            }
117
                            id = data.guid;
118 119
                        }
                        if (value.length > 1) {
120
                            scope.$('td div[data-id="' + id + '"]').html('<a href="#!/detailPage/' + id + '">' + _.escape(value) + '</a>');
121
                        } else {
122
                            scope.$('td div[data-id="' + id + '"]').html('<a href="#!/detailPage/' + id + '">' + _.escape(id) + '</a>');
123
                        }
124 125 126 127
                        if (deleteButton.length) {
                            scope.$('td div[data-id="' + id + '"]').addClass('block readOnlyLink');
                            scope.$('td div[data-id="' + id + '"]').append(deleteButton);
                        }
128
                    },
129 130 131 132 133 134
                    complete: function() {
                        if (searchTable) {
                            --scope.fetchList;
                            scope.checkTableFetch();
                        }
                    }
135 136 137
                });
            }
        _.keys(valueObject).map(function(key) {
138
            key = _.escape(key)
139 140
            var keyValue = valueObject[key],
                valueOfArray = [];
141
            if (_.isObject(keyValue)) {
142 143 144
                if (!_.isArray(keyValue) && _.isObject(keyValue)) {
                    keyValue = [keyValue];
                }
145 146 147
                var subLink = "";
                for (var i = 0; i < keyValue.length; i++) {
                    var inputOutputField = keyValue[i],
148
                        id = inputOutputField.guid || (_.isObject(inputOutputField.id) ? inputOutputField.id.id : inputOutputField.id),
149
                        tempLink = "",
150 151 152 153
                        status = inputOutputField.status || (_.isObject(inputOutputField.id) ? inputOutputField.id.state : inputOutputField.state),
                        readOnly = Enums.entityStateReadOnly[status];
                    if (!inputOutputField.attributes && inputOutputField.values) {
                        inputOutputField['attributes'] = inputOutputField.values;
154
                    }
155 156
                    if (_.isString(inputOutputField) || _.isBoolean(inputOutputField) || _.isNumber(inputOutputField)) {
                        if (inputOutputField.indexOf("$") == -1) {
157
                            valueOfArray.push('<span>' + _.escape(inputOutputField) + '</span>');
158
                        }
159
                    } else if (_.isObject(inputOutputField) && !id) {
160 161 162 163 164 165
                        _.each(inputOutputField, function(objValue, objKey) {
                            var value = objValue;
                            if (objKey.indexOf("$") == -1) {
                                if (_.isObject(value)) {
                                    value = JSON.stringify(value);
                                }
166
                                valueOfArray.push('<span>' + _.escape(objKey) + ':' + _.escape(value) + '</span>');
167 168
                            }
                        });
169
                    }
170

171 172 173
                    if (id && inputOutputField) {
                        if (inputOutputField.attributes) {
                            if (inputOutputField.attributes.name) {
174
                                tempLink += '<a href="#!/detailPage/' + id + '">' + _.escape(inputOutputField.attributes.name) + '</a>'
175
                            } else if (inputOutputField.attributes.qualifiedName) {
176
                                tempLink += '<a href="#!/detailPage/' + id + '">' + _.escape(inputOutputField.attributes.qualifiedName) + '</a>'
177
                            } else if (inputOutputField.typeName) {
178
                                tempLink += '<a href="#!/detailPage/' + id + '">' + _.escape(inputOutputField.typeName) + '</a>'
179
                            } else {
180
                                tempLink += '<a href="#!/detailPage/' + id + '">' + _.escape(id) + '</a>'
181
                            }
182
                        } else if (inputOutputField.name) {
183
                            tempLink += '<a href="#!/detailPage/' + id + '">' + _.escape(inputOutputField.name) + '</a>';
184
                        } else if (inputOutputField.qualifiedName) {
185
                            tempLink += '<a href="#!/detailPage/' + id + '">' + _.escape(inputOutputField.qualifiedName) + '</a>'
186
                        } else {
187
                            var fetch = true;
188 189 190
                            var fetchId = (_.isObject(id) ? id.id : id);
                            fetchInputOutputValue(fetchId);
                            tempLink += '<div data-id="' + fetchId + '"></div>';
191
                        }
192 193
                    }
                    if (readOnly) {
194 195 196 197 198 199 200 201
                        if (!fetch) {
                            tempLink += '<button title="Deleted" class="btn btn-atlasAction btn-atlas deleteBtn"><i class="fa fa-trash"></i></button>';
                            subLink += '<div class="block readOnlyLink">' + tempLink + '</div>';
                        } else {
                            fetch = false;
                            subLink += tempLink;
                        }

202
                    } else {
203 204
                        if (tempLink.search('href') != -1) {
                            subLink += '<div>' + tempLink + '</div>'
205
                        } else if (tempLink.length) {
206 207
                            subLink += tempLink
                        }
208 209
                    }
                }
210 211 212
                if (valueOfArray.length) {
                    subLink = valueOfArray.join(', ');
                }
213 214 215
                if (searchTable) {
                    table = subLink;
                } else {
216
                    table += '<tr><td>' + _.escape(key) + '</td><td>' + subLink + '</td></tr>';
217
                }
218
            } else {
219 220 221 222
                if (key.indexOf("Time") !== -1 || key == "retention") {
                    if (searchTable) {
                        table = new Date(valueObject[key]);
                    } else {
223
                        table += '<tr><td>' + _.escape(key) + '</td><td>' + new Date(valueObject[key]) + '</td></tr>';
224
                    }
225
                } else {
226 227 228 229 230 231 232
                    if (searchTable) {
                        if (_.isBoolean(valueObject[key])) {
                            table = valueObject[key].toString();
                        } else {
                            table = valueObject[key];
                        }
                    } else {
233
                        table += '<tr><td>' + _.escape(key) + '</td><td>' + _.escape(valueObject[key]) + '</td></tr>';
234
                    }
235 236 237 238 239
                }
            }
        });
        return table;
    }
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    CommonViewFunction.breadcrumbUrlMaker = function(url) {
        if (url) {
            var urlList = [];
            var splitURL = url.split("api/atlas/v1/taxonomies/");
            if (splitURL.length > 1) {
                var splitUrlWithoutTerm = splitURL[1].split("/terms/");
                if (splitUrlWithoutTerm.length == 1) {
                    splitUrlWithoutTerm = splitUrlWithoutTerm[0].split("/");
                }
            } else {
                var splitUrlWithoutTerm = splitURL[0].split("/terms/");
                if (splitUrlWithoutTerm.length == 1) {
                    splitUrlWithoutTerm = splitUrlWithoutTerm[0].split("/");
                }
            }

            var href = "";
            for (var i in splitUrlWithoutTerm) {
                if (i == 0) {
                    href = splitUrlWithoutTerm[i];
                    urlList.push({
261
                        value: _.escape(splitUrlWithoutTerm[i]),
262 263 264 265 266
                        href: href
                    });
                } else {
                    href += "/terms/" + splitUrlWithoutTerm[i];
                    urlList.push({
267
                        value: _.escape(splitUrlWithoutTerm[i]),
268 269 270 271 272 273 274 275 276 277 278
                        href: href
                    });
                };
            }
            return urlList;
        }
    }
    CommonViewFunction.breadcrumbMaker = function(options) {
        var li = "";
        if (options.urlList) {
            _.each(options.urlList, function(object) {
279
                li += '<li><a class="link" href="#!/taxonomy/detailCatalog/api/atlas/v1/taxonomies/' + object.href + '?load=true">' + _.escape(object.value) + '</a></li>';
280 281 282 283 284 285 286 287
            });
        }
        if (options.scope) {
            options.scope.html(li);
            options.scope.asBreadcrumbs("destroy");
            options.scope.asBreadcrumbs({
                namespace: 'breadcrumb',
                overflow: "left",
288
                responsive: false,
289 290
                toggleIconClass: 'fa fa-ellipsis-h',
                dropdown: function(classes) {
291 292
                    var dropdownClass = 'dropdown';
                    var dropdownMenuClass = 'dropdown-menu popover popoverTerm bottom arrowPosition';
293 294 295 296
                    if (this.options.overflow === 'right') {
                        dropdownMenuClass += ' dropdown-menu-right';
                    }

297 298 299 300 301 302 303 304
                    return '<li class="' + dropdownClass + ' ' + classes.dropdownClass + '">' +
                        '<a href="javascript:void(0);" class="' + classes.toggleClass + '" data-toggle="dropdown">' +
                        '<i class="' + classes.toggleIconClass + '"></i>' +
                        '</a>' +
                        '<ul class="' + dropdownMenuClass + ' ' + classes.dropdownMenuClass + '">' +
                        '<div class="arrow"></div>' +
                        '</ul>' +
                        '</li>';
305 306 307 308
                }
            });
        }
    }
309 310
    CommonViewFunction.termTableBreadcrumbMaker = function(obj) {
        if (!obj) {
311 312
            return "";
        }
313
        var traits = obj.classificationNames || _.pluck(obj.classifications, 'typeName'),
314
            url = "",
315 316
            deleteHtml = "",
            html = "",
317 318 319 320 321 322 323 324 325 326 327 328 329 330
            id = obj.guid,
            terms = [],
            entityName = (_.escape(obj.attributes && obj.attributes.name ? obj.attributes.name : null) || _.escape(obj.displayText) || obj.guid);
        if (traits) {
            traits.map(function(term) {
                if (term.split(".").length > 1) {
                    terms.push({
                        deleteHtml: '<a class="pull-left" title="Remove Term"><i class="fa fa-trash" data-id="tagClick" data-type="term" data-assetname="' + entityName + '" data-name="' + term + '" data-guid="' + obj.guid + '" ></i></a>',
                        url: _.unescape(term).split(".").join("/"),
                        name: term
                    });
                }
            });
        }
331 332 333 334
        _.each(terms, function(obj, i) {
            var className = "";
            if (i >= 1) {
                className += "showHideDiv hide";
335
            }
336
            obj['valueUrl'] = CommonViewFunction.breadcrumbUrlMaker(obj.url);
337
            html += '<div class="' + className + '" dataterm-name="' + _.escape(obj.name) + '"><div class="liContent"></div>' + obj.deleteHtml + '</div>';
338 339 340 341
        })
        if (terms.length > 1) {
            html += '<div><a  href="javascript:void(0)" data-id="showMoreLessTerm" class="inputTag inputTagGreen"><span>Show More </span><i class="fa fa-angle-right"></i></a></div>'
        }
342 343 344 345 346 347
        if (!Enums.entityStateReadOnly[obj.status]) {
            if (obj.guid) {
                html += '<div><a href="javascript:void(0)" class="inputAssignTag" data-id="addTerm" data-guid="' + (obj.guid) + '"><i class="fa fa-folder-o"></i>' + " " + 'Assign Term</a></div>'
            } else {
                html += '<div><a href="javascript:void(0)" class="inputAssignTag" data-id="addTerm"><i class="fa fa-folder-o"></i>' + " " + 'Assign Term</a></div>'
            }
348 349 350 351
        }
        return {
            html: '<div class="termTableBreadcrumb" dataterm-id="' + id + '">' + html + '</div>',
            object: { scopeId: id, value: terms }
352
        }
353

354
    }
355
    CommonViewFunction.tagForTable = function(obj) {
356
        var traits = obj.classificationNames || _.pluck(obj.classifications, 'typeName'),
357 358
            atags = "",
            addTag = "",
359
            popTag = "",
360 361 362 363 364 365 366 367 368 369 370 371
            count = 0,
            entityName = (_.escape(obj.attributes && obj.attributes.name ? obj.attributes.name : null) || _.escape(obj.displayText) || obj.guid);
        if (traits) {
            traits.map(function(tag) {
                if (tag.split(".").length === 1) {
                    var className = "inputTag";
                    if (count >= 1) {
                        popTag += '<a class="' + className + '" data-id="tagClick"><span class="inputValue">' + tag + '</span><i class="fa fa-times" data-id="delete"  data-assetname="' + entityName + '"data-name="' + tag + '" data-type="tag" data-guid="' + obj.guid + '" ></i></a>';
                    } else {
                        atags += '<a class="' + className + '" data-id="tagClick"><span class="inputValue">' + tag + '</span><i class="fa fa-times" data-id="delete" data-assetname="' + entityName + '" data-name="' + tag + '"  data-type="tag" data-guid="' + obj.guid + '" ></i></a>';
                    }
                    ++count;
372
                }
373 374 375 376 377 378 379
            });
        }
        if (!Enums.entityStateReadOnly[obj.status]) {
            if (obj.guid) {
                addTag += '<a href="javascript:void(0)" data-id="addTag" class="inputTagAdd assignTag" data-guid="' + obj.guid + '" ><i class="fa fa-plus"></i></a>';
            } else {
                addTag += '<a href="javascript:void(0)" data-id="addTag" class="inputTagAdd assignTag"><i style="right:0" class="fa fa-plus"></i></a>';
380 381 382
            }
        }
        if (count > 1) {
383
            addTag += '<div data-id="showMoreLess" class="inputTagAdd assignTag tagDetailPopover"><i class="fa fa-ellipsis-h" aria-hidden="true"></i></div>'
384
        }
385
        return '<div class="tagList">' + atags + addTag + '<div class="popover popoverTag bottom" style="display:none"><div class="arrow"></div><div class="popover-content popoverContainer">' + popTag + '</div></div></div>';
386
    }
387
    CommonViewFunction.saveTermToAsset = function(options, that) {
388 389 390
        require(['models/VCatalog'], function(Vcatalog) {
            var VCatalog = new Vcatalog();
            var name = options.termName;
391
            ++that.asyncFetchCounter;
392 393 394 395 396 397 398 399 400 401 402 403
            VCatalog.url = function() {
                return "api/atlas/v1/entities/" + options.guid + "/tags/" + name;
            };
            VCatalog.save(null, {
                success: function(data) {
                    Utils.notifySuccess({
                        content: "Term " + name + Messages.addTermToEntitySuccessMessage
                    });
                    if (options.collection) {
                        options.collection.fetch({ reset: true });
                    }
                },
404 405 406 407 408 409
                complete: function() {
                    --that.asyncFetchCounter
                    if (that.callback && that.asyncFetchCounter === 0) {
                        that.callback(); // It will call to parent of parent Callback i.e callback of searchLayoutView
                    }
                }
410 411 412
            });
        })
    }
413 414 415 416 417 418 419 420 421 422 423 424 425
    CommonViewFunction.addRestCsrfCustomHeader = function(xhr, settings) {
        //    if (settings.url == null || !settings.url.startsWith('/webhdfs/')) {
        if (settings.url == null) {
            return;
        }
        var method = settings.type;
        if (CommonViewFunction.restCsrfCustomHeader != null && !CommonViewFunction.restCsrfMethodsToIgnore[method]) {
            // The value of the header is unimportant.  Only its presence matters.
            xhr.setRequestHeader(CommonViewFunction.restCsrfCustomHeader, '""');
        }
    }
    CommonViewFunction.restCsrfCustomHeader = null;
    CommonViewFunction.restCsrfMethodsToIgnore = null;
426
    CommonViewFunction.userDataFetch = function(options) {
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
        var csrfEnabled = false,
            header = null,
            methods = [];

        function getTrimmedStringArrayValue(string) {
            var str = string,
                array = [];
            if (str) {
                var splitStr = str.split(',');
                for (var i = 0; i < splitStr.length; i++) {
                    array.push(splitStr[i].trim());
                }
            }
            return array;
        }
442 443 444 445
        if (options.url) {
            $.ajax({
                url: options.url,
                success: function(response) {
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
                    if (response) {
                        if (response['atlas.rest-csrf.enabled']) {
                            var str = "" + response['atlas.rest-csrf.enabled'];
                            csrfEnabled = (str.toLowerCase() == 'true');
                        }
                        if (response['atlas.rest-csrf.custom-header']) {
                            header = response['atlas.rest-csrf.custom-header'].trim();
                        }
                        if (response['atlas.rest-csrf.methods-to-ignore']) {
                            methods = getTrimmedStringArrayValue(response['atlas.rest-csrf.methods-to-ignore']);
                        }
                        if (csrfEnabled) {
                            CommonViewFunction.restCsrfCustomHeader = header;
                            CommonViewFunction.restCsrfMethodsToIgnore = {};
                            methods.map(function(method) { CommonViewFunction.restCsrfMethodsToIgnore[method] = true; });
                            Backbone.$.ajaxSetup({
                                beforeSend: CommonViewFunction.addRestCsrfCustomHeader
                            });
                        }
                    }
                },
                complete: function(response) {
468
                    if (options.callback) {
469
                        options.callback(response.responseJSON);
470 471 472 473 474
                    }
                }
            });
        }
    }
475 476
    return CommonViewFunction;
});