BusinessCatalogLayoutView.js 26.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/**
 * 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',
    'hbs!tmpl/business_catalog/BusinessCatalogLayoutView_tmpl',
    'utils/Utils',
    'collection/VCatalogList',
    'utils/CommonViewFunction'
], function(require, Backbone, BusinessCatalogLayoutViewTmpl, Utils, VCatalogList, CommonViewFunction) {
    'use strict';

    var BusinessCatalogLayoutView = Backbone.Marionette.LayoutView.extend(
        /** @lends BusinessCatalogLayoutView */
        {
            _viewName: 'BusinessCatalogLayoutView',

            template: BusinessCatalogLayoutViewTmpl,

            /** Layout sub regions */
            regions: {},
            /** ui selector cache */
            ui: {
                Parent: '[data-id="Parent"]',
40
                childList: '[data-id="childList"]',
41 42
                liClick: 'li a[data-href]',
                backTaxanomy: '[data-id="backTaxanomy"]',
43
                expandArrow: '[data-id="expandArrow"]',
44 45
                searchTermInput: '[data-id="searchTermInput"]',
                refreshTaxanomy: '[data-id="refreshTaxanomy"]'
46 47 48 49 50 51 52 53 54 55 56 57 58
            },
            /** ui events hash */
            events: function() {
                var events = {};
                events['dblclick ' + this.ui.liClick] = function(e) {
                    $(e.currentTarget).parent('li').find('.tools .taxanomyloader').show();
                    this.singleClick = false;
                    this.forwardClick(e, true);
                };
                events['click ' + this.ui.liClick] = function(e) {
                    this.dblClick = false;
                    this.forwardClick(e);
                };
59
                events['click ' + this.ui.backTaxanomy] = 'backButtonTaxanomy';
60
                events['click ' + this.ui.refreshTaxanomy] = 'refreshButtonTaxanomy';
61
                events['click ' + this.ui.expandArrow] = 'changeArrowState';
62 63 64 65 66
                events["change " + this.ui.searchTermInput] = function() {
                    this.singleClick = false;
                    var termUrl = this.termCollection.url.split("/", 5).join("/") + "/" + this.ui.searchTermInput.val().split(".").join("/terms/");
                    this.forwardClick(undefined, true, termUrl);
                };
67 68 69 70 71 72 73 74 75
                return events;
            },
            /**
             * intialize a new BusinessCatalogLayoutView Layout
             * @constructs
             */
            initialize: function(options) {
                _.extend(this, _.pick(options, 'globalVent', 'url'));
                this.parentCollection = new VCatalogList();
76 77 78
                this.childCollection = new VCatalogList();
                this.taxanomy = new VCatalogList();
                this.termCollection = new VCatalogList();
79 80 81 82 83 84 85
                this.dblClick = false;
                this.singleClick = false;
            },
            bindEvents: function() {
                var that = this;
                this.listenTo(this.parentCollection, 'reset', function() {
                    this.dblClick = false;
86 87 88 89 90 91 92
                    if (this.parentCollection.fullCollection.models.length) {
                        this.generateTree(true);
                    } else {
                        if (Utils.getUrlState.isTaxonomyTab()) {
                            this.createDefaultTaxonomy();
                        }
                    }
93
                }, this);
94
                this.listenTo(this.childCollection, 'reset', function() {
95 96 97
                    this.dblClick = false;
                    this.generateTree();
                }, this);
98 99 100 101 102 103 104
                this.listenTo(this.taxanomy, 'reset', function() {
                    this.searchResult();
                }, this);
                this.listenTo(this.termCollection, 'reset', function() {
                    this.termSearchData();
                }, this);
                this.listenTo(this.childCollection, 'error', function(model, response) {
105 106 107 108 109
                    if (response && response.responseJSON && response.responseJSON.message) {
                        Utils.notifyError({
                            content: response.responseJSON.message
                        });
                    }
110
                    this.hideLoader();
111 112 113 114 115 116 117
                }, this);
                this.listenTo(this.parentCollection, 'error', function(model, response) {
                    if (response && response.responseJSON && response.responseJSON.message) {
                        Utils.notifyError({
                            content: response.responseJSON.message
                        });
                    }
118
                    this.hideLoader();
119 120 121 122 123 124
                }, this);
            },
            onRender: function() {
                var that = this;
                this.bindEvents();
                that.ui.backTaxanomy.hide();
125

126
                this.fetchCollection(this.url, true);
127

128 129 130 131 132 133 134 135 136 137 138
                $('body').on("click", '.termPopoverList li', function(e) {
                    that[$(this).find("a").data('fn')](e);
                });
                $('body').click(function(e) {
                    if ($('.termPopoverList').length) {
                        if ($(e.target).hasClass('termPopover')) {
                            return;
                        }
                        that.$('.termPopover').popover('hide');
                    }
                });
139
                this.fetchTaxanomyCollections();
140
            },
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
            manualRender: function(url, isParent, back) {
                if (back) {
                    this.backButton = back;
                }
                if (this.ui.Parent.children().length <= 0 || this.backButton) {
                    this.fetchCollection(url, isParent);
                }
                if (url && isParent && !this.backButton) {
                    this.fetchCollection(url, isParent);
                }
                if (!url && !back && isParent) {
                    var url = this.$('.taxonomyTree').find('.active a').data('href');
                    this.forwardClick(undefined, undefined, url);
                }
                if (this.backButton) {
                    this.backButton = false;
                }
            },
            changeArrowState: function(e) {
                var scope = this.$('[data-id="expandArrow"]');
                if (e) {
                    scope = $(e.currentTarget);
                }
                if (scope.hasClass('fa-chevron-down')) {
                    scope.removeClass('fa-chevron-down');
                    scope.addClass('fa-chevron-right');
                    this.addActiveClass(scope[0]);
168
                    this.ui.childList.hide();
169
                } else {
170
                    if (e && $(e.currentTarget).parents('li.parentChild').length) {
171 172 173 174 175 176
                        scope.parent('li').find('.tools .taxanomyloader').show();
                        this.forwardClick(e, true);
                    } else {
                        scope.addClass('fa-chevron-down');
                        scope.removeClass('fa-chevron-right');
                        this.singleClick = false;
177
                        this.ui.childList.show();
178 179
                    }
                }
180 181 182 183 184 185 186 187 188 189 190 191 192
            },
            fetchCollection: function(url, isParent) {
                if (url) {
                    this.url = url;
                } else {
                    var parentURL = this.ui.Parent.find('a').data('href');
                    if (parentURL) {
                        this.url = parentURL;
                    } else {
                        this.url = "api/atlas/v1/taxonomies";
                    }

                }
193
                this.showLoader();
194 195 196 197 198
                if (isParent) {
                    this.parentCollection.url = this.url;
                    this.parentCollection.fullCollection.reset(undefined, { silent: true });
                    this.parentCollection.fetch({ reset: true });
                } else {
199 200 201
                    this.childCollection.url = this.url + "?hierarchy/path:.";
                    this.childCollection.fullCollection.reset(undefined, { silent: true });
                    this.childCollection.fetch({ reset: true });
202 203
                }
            },
204 205 206 207 208 209 210 211
            showLoader() {
                this.$('.taxonomyTree').find('li.active .tools .taxanomyloader').show();
                this.$('.contentLoading').show();
            },
            hideLoader() {
                this.$('.taxanomyloader').hide();
                this.$('.contentLoading').hide();
            },
212 213 214 215 216 217 218 219
            forwardClick: function(e, forward, url) {
                var hrefUrl = "";
                if (e) {
                    hrefUrl = $(e.currentTarget).data('href');
                }
                if (url) {
                    hrefUrl = url;
                }
220 221 222 223 224 225
                if (!e && !url) {
                    var dataHref = this.ui.Parent.find('a').data('href');
                    if (dataHref) {
                        hrefUrl = dataHref;
                    }
                }
226 227
                if (forward) {
                    this.dblClick = true;
228
                    this.ui.childList.show();
229 230 231 232
                    this.fetchCollection(hrefUrl, true);
                } else {
                    this.singleClick = true;
                }
233 234 235 236 237 238 239 240 241 242
                if (hrefUrl.length > 1) {
                    Utils.setUrl({
                        url: '#!/taxonomy/detailCatalog' + hrefUrl,
                        mergeBrowserUrl: false,
                        updateTabState: function() {
                            return { taxonomyUrl: this.url, stateChanged: false };
                        },
                        trigger: true
                    });
                }
243 244 245
                if (e) {
                    this.addActiveClass(e);
                }
246 247 248 249 250 251 252
            },
            addActiveClass: function(e) {
                $(e.currentTarget).parents('ul.taxonomyTree').find('li').removeClass('active');
                $(e.currentTarget).parent('li').addClass('active');
            },
            generateTree: function(isParent) {
                var parentLi = "",
253
                    childLi = "",
254 255 256 257 258 259 260 261
                    that = this;

                function createTaxonomy(url) {
                    var href = false;
                    _.each(that.parentCollection.fullCollection.models, function(model, key) {

                        if (model.get('terms')) {
                            href = model.get('terms').href;
262 263
                        } else if (model.get('href')) {
                            href = model.get('href') + "/terms";
264 265 266 267 268 269 270 271 272 273 274 275
                        }
                        var hrefUrl = "/api" + model.get('href').split("/api")[1];
                        if (hrefUrl) {
                            var backUrlCheck = hrefUrl.split("taxonomies/");
                            if (backUrlCheck.length > 1) {
                                if (backUrlCheck[1].split("/terms").length <= 1) {
                                    that.ui.backTaxanomy.hide();
                                } else {
                                    that.ui.backTaxanomy.show();
                                }
                            }
                        }
276 277
                        var name = Utils.checkTagOrTerm(model.get('name'));
                        parentLi = '<div class="tools"><i class="fa fa-refresh fa-spin-custom taxanomyloader"></i><i class="fa fa-ellipsis-h termPopover"></i></div><i class="fa fa-chevron-right toggleArrow" data-id="expandArrow" data-href="' + hrefUrl + '"></i><a href="javascript:void(0)" data-href="' + hrefUrl + '" data-name="`' + model.get('name') + '`">' + name.name + '</a>';
278 279
                    });
                    if (href) {
280 281
                        var hrefUrl = "/api" + href.split("/api")[1];
                        that.fetchCollection(hrefUrl);
282
                    }
283
                    that.ui.childList.html('');
284 285 286 287 288
                    that.ui.Parent.addClass('active');
                    that.ui.Parent.html(parentLi);
                }

                function createTerm() {
289
                    _.each(that.childCollection.fullCollection.models, function(model, key) {
290 291
                        var name = Utils.checkTagOrTerm(model.get('name'));
                        var hrefUrl = "/api" + model.get('href').split("/api")[1]
292
                        childLi += '<li class="children"><div class="tools"><i class="fa fa-refresh fa-spin-custom taxanomyloader"></i><i class="fa fa-ellipsis-h termPopover" ></i></div><i class="fa fa-chevron-right toggleArrow" data-id="expandArrow" data-href="' + hrefUrl + '"></i><a href="javascript:void(0)" data-href="' + hrefUrl + '" data-name="`' + model.get('name') + '`">' + name.name + '</a></li>';
293
                    });
294
                    that.ui.childList.html(childLi);
295 296 297 298 299
                }

                if (isParent) {
                    createTaxonomy();
                } else {
300
                    this.changeArrowState();
301
                    createTerm();
302 303 304 305
                }
                if (this.refresh) {
                    this.$('.taxonomyTree').find('a[data-href="' + this.refresh + '"]').parent().addClass('active');
                    this.refresh = undefined;
306
                }
307
                this.hideLoader();
308 309 310 311 312 313
                this.$('.termPopover').popover({
                    placement: 'bottom',
                    html: true,
                    trigger: 'manual',
                    container: 'body',
                    content: function() {
314 315 316 317 318 319 320 321 322
                        var li = "<li class='listTerm' ><i class='fa fa-search'></i> <a href='javascript:void(0)' data-fn='onSearchTerm'>Search Assets</a></li>" +
                            "<li class='listTerm'><i class='fa fa-plus'></i> <a href='javascript:void(0)' data-fn='onAddTerm'>Add Subterm</a></li>";
                        /* "<li class='listTerm' ><i class='fa fa-arrow-right'></i> <a href='javascript:void(0)' data-fn='moveTerm'>Move Term</a></li>" +
                         "<li class='listTerm' ><i class='fa fa-edit'></i> <a href='javascript:void(0)' data-fn='onEditTerm'>Edit Term</a></li>" +*/
                        var termDataURL = Utils.getUrlState.getQueryUrl().hash.split("terms");
                        if (termDataURL.length > 1) {
                            li += "<li class='listTerm'><i class='fa fa-trash'></i> <a href='javascript:void(0)' data-fn='deleteTerm'>Delete Term</a></li>"
                        }
                        return "<ul class='termPopoverList'>" + li + "</ul>";
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
                    }
                });
                this.$('.termPopover').off('click').on('click', function(e) {
                    // if any other popovers are visible, hide them
                    e.preventDefault();
                    that.$('.termPopover').not(this).popover('hide');
                    $(this).popover('toggle');
                });
            },
            onAddTerm: function(e) {
                var that = this;
                require([
                    'views/business_catalog/AddTermLayoutView',
                    'modules/Modal'
                ], function(AddTermLayoutView, Modal) {
                    var view = new AddTermLayoutView({
                        url: that.$('.taxonomyTree').find('li.active').find("a").data("href"),
                        model: new that.parentCollection.model()
                    });
                    var modal = new Modal({
                        title: 'Add Term',
                        content: view,
                        okCloses: true,
                        showFooter: true,
                        allowCancel: true,
                        okText: 'Create',
                    }).open();
350
                    modal.$el.find('button.ok').attr('disabled', true);
351 352 353
                    modal.on('ok', function() {
                        that.saveAddTerm(view);
                    });
354 355 356 357 358 359 360 361
                    view.ui.termName.on('keyup', function() {
                        if (this.value.indexOf(' ') >= 0) {
                            modal.$el.find('button.ok').prop('disabled', true);
                        } else {
                            modal.$el.find('button.ok').prop('disabled', false);
                        }

                    });
362 363 364 365 366 367 368 369
                    view.on('closeModal', function() {
                        modal.trigger('cancel');
                    });

                });
            },
            saveAddTerm: function(view) {
                var that = this;
370 371
                var url = view.url;
                view.model.url = url + "/terms/" + view.ui.termName.val();
372
                this.showLoader();
373 374
                view.model.set({ description: view.ui.termDetail.val() }).save(null, {
                    success: function(model, response) {
375
                        that.create = true;
376
                        that.fetchTaxanomyCollections();
377 378
                        that.forwardClick(undefined, true, url);
                        //that.fetchCollection(that.url);
379
                        Utils.notifySuccess({
380
                            content: "Term " + view.ui.termName.val() + " Created successfully"
381 382 383 384
                        });
                    },
                    error: function(model, response) {
                        Utils.notifyError({
385
                            content: "Term " + view.ui.termName.val() + " could not be Created"
386
                        });
387 388 389
                    },
                    complete: function() {
                        that.hideLoader();
390 391 392 393
                    }
                });
            },
            deleteTerm: function(e) {
394
                var termName = this.$('.taxonomyTree').find('li.active a').data("name"),
395
                    that = this,
396
                    modal = CommonViewFunction.deleteTagModel(termName);
397
                modal.on('ok', function() {
398
                    that.deleteTermData(e);
399 400 401 402 403
                });
                modal.on('closeModal', function() {
                    modal.trigger('cancel');
                });
            },
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
            deleteTermData: function(e) {
                var that = this;
                this.showLoader();
                require(['models/VCatalog'], function(VCatalog) {
                    var termModel = new VCatalog(),
                        url = that.$('.taxonomyTree').find('li.active a').data('href');
                    var termName = that.$('.taxonomyTree').find('li.active a').text();
                    termModel.deleteTerm(url, {
                        beforeSend: function() {},
                        success: function(data) {
                            Utils.notifySuccess({
                                content: "Term " + termName + " has been deleted successfully"
                            });
                            var termURL = url.split("/").slice(0, -2).join("/");
                            that.forwardClick(undefined, true, termURL);
                        },
                        error: function(error, data, status) {
                            var message = "Term " + termName + " could not be deleted";
                            if (data.error) {
                                message = data.error;
                            }
                            Utils.notifyError({
                                content: message
                            });
                        },
                        complete: function() {
                            that.hideLoader();
                        }
                    });
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
                });
            },
            moveTerm: function() {
                var that = this;
                require([
                    'views/business_catalog/MoveTermLayoutView',
                    'modules/Modal'
                ], function(MoveTermLayoutView, Modal) {
                    var view = new MoveTermLayoutView({
                        taxanomyCollection: that.collection
                    });
                    var modal = new Modal({
                        title: 'Move Term',
                        content: view,
                        okCloses: true,
                        showFooter: true,
                        allowCancel: true,
                        okText: 'Move',
                    }).open();
                    // modal.on('ok', function() {
                    //     that.saveAddTerm(view);
                    // });
                    view.on('closeModal', function() {
                        modal.trigger('cancel');
                    });
                });
            },
            onSearchTerm: function() {
461
                this.showLoader();
462 463 464
                Utils.setUrl({
                    url: '#!/search/searchResult',
                    urlParams: {
465 466 467
                        query: this.$('.taxonomyTree').find('li.active').find("a").data('name'),
                        searchType: "dsl",
                        dslChecked: true
468 469 470 471 472 473 474 475 476 477 478
                    },
                    updateTabState: function() {
                        return { searchUrl: this.url, stateChanged: true };
                    },
                    mergeBrowserUrl: false,
                    trigger: true
                });
            },
            backButtonTaxanomy: function(e) {
                var that = this;
                this.dblClick = false;
479
                this.backButton = true;
480 481 482 483 484 485 486 487 488 489 490 491 492 493
                var dataURL = this.$('.taxonomyTree').find('li[data-id="Parent"]').find("a").data('href').split("/terms");
                var backUrl = dataURL.pop();
                if (dataURL.join("/terms").length) {
                    this.ui.backTaxanomy.show();
                    var currentURL = "!/taxonomy/detailCatalog" + dataURL.join("/terms");
                    Utils.setUrl({
                        url: currentURL,
                        mergeBrowserUrl: false,
                        trigger: true,
                        updateTabState: function() {
                            return { taxonomyUrl: currentURL, stateChanged: false };
                        }
                    });
                }
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
            },
            fetchTaxanomyCollections: function() {
                this.taxanomy.fetch({ reset: true });
            },
            searchResult: function() {
                var that = this;
                _.each(this.taxanomy.models, function(model, key) {
                    var name = model.get('name');
                    that.termCollection.url = "/api/atlas/v1/taxonomies/" + name + "/terms";
                });
                this.termCollection.fetch({ reset: true });
            },
            termSearchData: function() {
                var that = this;
                var str = '<option></option>';
                for (var j = 0; j < this.termCollection.models.length; j++) {
                    var terms = this.termCollection.models[j].attributes.name;
                    str += '<option>' + terms + '</option>';
                    this.ui.searchTermInput.html(str);
                }
                // this.ui.searchTermInput.setAttribute('data-href' : that.termCollection.url);
                this.ui.searchTermInput.select2({
                    placeholder: "Search Term",
                    allowClear: true
                });
519 520 521 522 523 524
            },
            refreshButtonTaxanomy: function() {
                this.fetchTaxanomyCollections();
                this.refresh = this.$('.taxonomyTree').find('.active a').data('href');
                this.fetchCollection(this.url);
                this.changeArrowState();
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
            },
            createDefaultTaxonomy: function() {
                var that = this;
                require([
                    'views/business_catalog/AddTermLayoutView',
                    'modules/Modal'
                ], function(AddTermLayoutView, Modal) {
                    var view = new AddTermLayoutView({
                        url: "/api/atlas/v1/taxonomies",
                        model: new that.parentCollection.model()
                    });
                    var modal = new Modal({
                        title: 'Default taxonomy',
                        content: view,
                        okCloses: true,
                        showFooter: true,
                        allowCancel: true,
                        okText: 'Create',
                    }).open();
                    modal.$el.find('button.ok').attr('disabled', true);
                    modal.on('ok', function() {
                        that.saveDefaultTaxonomy(view);
                    });
                    view.ui.termName.attr("placeholder", "Default taxonomy name");
                    view.ui.termName.on('keyup', function() {
                        if (this.value.indexOf(' ') >= 0) {
                            modal.$el.find('button.ok').prop('disabled', true);
                        } else {
                            modal.$el.find('button.ok').prop('disabled', false);
                        }

                    });
                    view.on('closeModal', function() {
                        modal.trigger('cancel');
                    });
                });
            },
            saveDefaultTaxonomy: function(view) {
                var that = this;
                var url = view.url;
                view.model.url = url + "/" + view.ui.termName.val();
                this.showLoader();
                view.model.set({ description: view.ui.termDetail.val() }).save(null, {
                    success: function(model, response) {
                        that.fetchCollection(view.model.url, true);
                        that.forwardClick(undefined, undefined, view.model.url);
                        Utils.notifySuccess({
                            content: "Default taxonomy" + view.ui.termName.val() + " Created successfully"
                        });
                    },
                    error: function(error, data, status) {
                        Utils.notifyError({
                            content: "Default taxonomy " + view.ui.termName.val() + " could not be Created"
                        });
                    },
                    complete: function() {
                        that.hideLoader();
                    }
                });
584
            }
585 586 587
        });
    return BusinessCatalogLayoutView;
});