CommonViewFunction.js 21.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
                tagModel.deleteTag(options.guid, options.tagName, {
40
                    skipDefaultError: true,
41
                    success: function(data) {
42
                        var msg = "Tag " + name.name + Messages.removeSuccessMessage;
43 44 45 46
                        if (options.tagOrTerm === "term") {
                            msg = "Term " + options.tagName + Messages.removeSuccessMessage;
                        } else if (options.tagOrTerm === "tag") {
                            msg = "Tag " + options.tagName + Messages.removeSuccessMessage;
47
                        }
48
                        Utils.notifySuccess({
49
                            content: msg
50
                        });
51 52 53 54 55 56 57
                        if (options.callback) {
                            options.callback();
                        }
                        if (options.collection) {
                            options.collection.fetch({ reset: true });
                        }

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

150 151 152
                    if (id && inputOutputField) {
                        if (inputOutputField.attributes) {
                            if (inputOutputField.attributes.name) {
153
                                tempLink += '<a href="#!/detailPage/' + id + '">' + _.escape(inputOutputField.attributes.name) + '</a>'
154
                            } else if (inputOutputField.attributes.qualifiedName) {
155
                                tempLink += '<a href="#!/detailPage/' + id + '">' + _.escape(inputOutputField.attributes.qualifiedName) + '</a>'
156
                            } else if (inputOutputField.typeName) {
157
                                tempLink += '<a href="#!/detailPage/' + id + '">' + _.escape(inputOutputField.typeName) + '</a>'
158
                            } else {
159
                                tempLink += '<a href="#!/detailPage/' + id + '">' + _.escape(id) + '</a>'
160
                            }
161
                        } else if (inputOutputField.name) {
162
                            tempLink += '<a href="#!/detailPage/' + id + '">' + _.escape(inputOutputField.name) + '</a>';
163
                        } else if (inputOutputField.qualifiedName) {
164
                            tempLink += '<a href="#!/detailPage/' + id + '">' + _.escape(inputOutputField.qualifiedName) + '</a>'
165
                        } else {
166
                            var fetch = true;
167 168 169
                            var fetchId = (_.isObject(id) ? id.id : id);
                            fetchInputOutputValue(fetchId);
                            tempLink += '<div data-id="' + fetchId + '"></div>';
170
                        }
171 172
                    }
                    if (readOnly) {
173 174 175 176 177 178 179 180
                        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;
                        }

181
                    } else {
182 183
                        if (tempLink.search('href') != -1) {
                            subLink += '<div>' + tempLink + '</div>'
184
                        } else if (tempLink.length) {
185 186
                            subLink += tempLink
                        }
187 188
                    }
                }
189 190 191
                if (valueOfArray.length) {
                    subLink = valueOfArray.join(', ');
                }
192 193 194
                if (searchTable) {
                    table = subLink;
                } else {
195
                    table += '<tr><td>' + _.escape(key) + '</td><td>' + subLink + '</td></tr>';
196
                }
197
            } else {
198 199 200 201
                if (key.indexOf("Time") !== -1 || key == "retention") {
                    if (searchTable) {
                        table = new Date(valueObject[key]);
                    } else {
202
                        table += '<tr><td>' + _.escape(key) + '</td><td>' + new Date(valueObject[key]) + '</td></tr>';
203
                    }
204
                } else {
205 206 207 208 209 210 211
                    if (searchTable) {
                        if (_.isBoolean(valueObject[key])) {
                            table = valueObject[key].toString();
                        } else {
                            table = valueObject[key];
                        }
                    } else {
212
                        table += '<tr><td>' + _.escape(key) + '</td><td>' + _.escape(valueObject[key]) + '</td></tr>';
213
                    }
214 215 216 217 218
                }
            }
        });
        return table;
    }
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    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({
240
                        value: _.escape(splitUrlWithoutTerm[i]),
241 242 243 244 245
                        href: href
                    });
                } else {
                    href += "/terms/" + splitUrlWithoutTerm[i];
                    urlList.push({
246
                        value: _.escape(splitUrlWithoutTerm[i]),
247 248 249 250 251 252 253 254 255 256 257
                        href: href
                    });
                };
            }
            return urlList;
        }
    }
    CommonViewFunction.breadcrumbMaker = function(options) {
        var li = "";
        if (options.urlList) {
            _.each(options.urlList, function(object) {
258
                li += '<li><a class="link" href="#!/taxonomy/detailCatalog/api/atlas/v1/taxonomies/' + object.href + '?load=true">' + _.escape(object.value) + '</a></li>';
259 260 261 262 263 264 265 266
            });
        }
        if (options.scope) {
            options.scope.html(li);
            options.scope.asBreadcrumbs("destroy");
            options.scope.asBreadcrumbs({
                namespace: 'breadcrumb',
                overflow: "left",
267
                responsive: false,
268 269
                toggleIconClass: 'fa fa-ellipsis-h',
                dropdown: function(classes) {
270 271
                    var dropdownClass = 'dropdown';
                    var dropdownMenuClass = 'dropdown-menu popover popoverTerm bottom arrowPosition';
272 273 274 275
                    if (this.options.overflow === 'right') {
                        dropdownMenuClass += ' dropdown-menu-right';
                    }

276 277 278 279 280 281 282 283
                    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>';
284 285 286 287
                }
            });
        }
    }
288 289
    CommonViewFunction.termTableBreadcrumbMaker = function(obj) {
        if (!obj) {
290 291
            return "";
        }
292
        var traits = obj.classificationNames,
293
            url = "",
294 295
            deleteHtml = "",
            html = "",
296 297 298 299 300 301 302 303 304 305 306 307 308 309
            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
                    });
                }
            });
        }
310 311 312 313
        _.each(terms, function(obj, i) {
            var className = "";
            if (i >= 1) {
                className += "showHideDiv hide";
314
            }
315
            obj['valueUrl'] = CommonViewFunction.breadcrumbUrlMaker(obj.url);
316
            html += '<div class="' + className + '" dataterm-name="' + _.escape(obj.name) + '"><div class="liContent"></div>' + obj.deleteHtml + '</div>';
317 318 319 320
        })
        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>'
        }
321 322 323 324 325 326
        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>'
            }
327 328 329 330
        }
        return {
            html: '<div class="termTableBreadcrumb" dataterm-id="' + id + '">' + html + '</div>',
            object: { scopeId: id, value: terms }
331
        }
332

333
    }
334 335
    CommonViewFunction.tagForTable = function(obj) {
        var traits = obj.classificationNames,
336 337
            atags = "",
            addTag = "",
338
            popTag = "",
339 340 341 342 343 344 345 346 347 348 349 350
            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;
351
                }
352 353 354 355 356 357 358
            });
        }
        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>';
359 360 361
            }
        }
        if (count > 1) {
362
            addTag += '<div data-id="showMoreLess" class="inputTagAdd assignTag tagDetailPopover"><i class="fa fa-ellipsis-h" aria-hidden="true"></i></div>'
363
        }
364
        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>';
365
    }
366
    CommonViewFunction.saveTermToAsset = function(options, that) {
367 368 369
        require(['models/VCatalog'], function(Vcatalog) {
            var VCatalog = new Vcatalog();
            var name = options.termName;
370
            ++that.asyncFetchCounter;
371 372 373 374 375 376 377 378 379 380 381 382
            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 });
                    }
                },
383 384 385 386 387 388
                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
                    }
                }
389 390 391
            });
        })
    }
392 393 394 395 396 397 398 399 400 401 402 403 404
    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;
405
    CommonViewFunction.userDataFetch = function(options) {
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
        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;
        }
421 422 423 424
        if (options.url) {
            $.ajax({
                url: options.url,
                success: function(response) {
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
                    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) {
447
                    if (options.callback) {
448
                        options.callback(response.responseJSON);
449 450 451 452 453
                    }
                }
            });
        }
    }
454 455
    return CommonViewFunction;
});