TreeLayoutView.js 31.8 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
/**
 * 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/TreeLayoutView_tmpl',
    'utils/Utils',
    'collection/VCatalogList',
    'utils/CommonViewFunction',
25 26 27
    'utils/Messages',
    'utils/UrlLinks'
], function(require, Backbone, TreeLayoutView_tmpl, Utils, VCatalogList, CommonViewFunction, Messages, UrlLinks) {
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    'use strict';

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

            template: TreeLayoutView_tmpl,

            /** Layout sub regions */
            regions: {},
            /** ui selector cache */
            ui: {
                Parent: '[data-id="Parent"]',
                childList: '[data-id="childList"]',
                liClick: 'li a[data-href]',
                backTaxanomy: '[data-id="backTaxanomy"]',
                expandArrow: '[data-id="expandArrow"]',
                searchTermInput: '[data-id="searchTermInput"]',
47 48
                refreshTaxanomy: '[data-id="refreshTaxanomy"]',
                descriptionAssign: '[data-id="descriptionAssign"]',
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
            },
            /** ui events hash */
            events: function() {
                var events = {};
                events['dblclick ' + this.ui.liClick] = function(e) {
                    this.changeArrowState(e);
                };
                events['click ' + this.ui.liClick] = function(e) {
                    var that = this;
                    that.addActiveClass(e);
                    that.url = $(e.currentTarget).data('href');
                    if (this.viewBased) {
                        Utils.setUrl({
                            url: '#!/taxonomy/detailCatalog' + that.url,
                            mergeBrowserUrl: false,
                            updateTabState: function() {
                                return { taxonomyUrl: this.url, stateChanged: true };
                            },
                            trigger: true
                        });
                    }
                };
                events['click ' + this.ui.backTaxanomy] = 'backButtonTaxanomy';
                events['click ' + this.ui.refreshTaxanomy] = 'refreshButtonTaxanomy';
                events['click ' + this.ui.expandArrow] = 'changeArrowState';
                events["change " + this.ui.searchTermInput] = function() {
                    var termUrl = this.termCollection.url.split("/", 5).join("/") + "/" + this.ui.searchTermInput.val().split(".").join("/terms/");
                    this.fetchCollection(termUrl, true);
                    if (this.viewBased) {
                        Utils.setUrl({
                            url: "#!/taxonomy/detailCatalog" + termUrl,
                            mergeBrowserUrl: false,
                            trigger: true,
                            updateTabState: function() {
                                return { taxonomyUrl: this.url, stateChanged: true };
                            }
                        });
                    }
                };
                return events;
            },
            /**
             * intialize a new TreeLayoutView Layout
             * @constructs
             */
            initialize: function(options) {
95
                _.extend(this, _.pick(options, 'url', 'viewBased'));
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
                this.parentCollection = new VCatalogList();
                this.childCollection = new VCatalogList();
                this.taxanomy = new VCatalogList();
                this.termCollection = new VCatalogList();
            },
            bindEvents: function() {
                var that = this;
                this.listenTo(this.parentCollection, 'reset', function() {
                    if (this.parentCollection.fullCollection.models.length) {
                        this.generateTree(true);
                    } else {
                        if (Utils.getUrlState.isTaxonomyTab() || Utils.getUrlState.isInitial()) {
                            this.createDefaultTaxonomy();
                        }
                    }
                }, this);
                this.listenTo(this.childCollection, 'reset', function() {
                    this.generateTree();
                }, this);
                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) {
                    this.hideLoader();
                }, this);
                this.listenTo(this.parentCollection, 'error', function(model, response) {
                    this.hideLoader();
                }, this);
            },
            onRender: function() {
                var that = this;
                this.bindEvents();
                that.ui.backTaxanomy.hide();
                this.fetchCollection(this.url, true);
                this.$el.on("click", '.termPopoverList li', function(e) {
                    that[$(this).find("a").data('fn')](e);
                });
                $('body').click(function(e) {
                    if (that.$('.termPopoverList').length) {
                        if ($(e.target).hasClass('termPopover')) {
                            return;
                        }
                        that.$('.termPopover').popover('hide');
                    }
                });
                this.fetchTaxanomyCollections();
145 146 147 148 149
                if (!this.viewBased) {
                    this.ui.descriptionAssign.show();
                } else {
                    this.ui.descriptionAssign.hide();
                }
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 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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
            },
            backButtonTaxanomy: function(e) {
                var that = this;
                this.backButton = true;
                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");
                    this.fetchCollection(dataURL.join("/terms"), true);
                    this.url = dataURL.join("/terms");
                    if (this.viewBased) {
                        Utils.setUrl({
                            url: currentURL,
                            mergeBrowserUrl: false,
                            trigger: true,
                            updateTabState: function() {
                                return { taxonomyUrl: this.url, stateChanged: true };
                            }
                        });
                    }
                }
            },
            manualRender: function(url) {
                var splitUrl = this.url.split('/terms');
                if (url && this.url != url) {
                    if (splitUrl.length > 1 && splitUrl[splitUrl.length - 1] == "") {
                        var checkUrl = splitUrl;
                        checkUrl.pop();
                        if (url != checkUrl) {
                            this.fetchCollection(url, true);
                        }
                    } else if (Utils.getUrlState.getQueryParams() && Utils.getUrlState.getQueryParams().load == "true") {
                        if (this.viewBased) {
                            Utils.setUrl({
                                url: "#!/taxonomy/detailCatalog" + url,
                                mergeBrowserUrl: false,
                                trigger: false,
                                updateTabState: function() {
                                    return { taxonomyUrl: this.url, stateChanged: true };
                                }
                            });
                        }
                        this.fetchCollection(url, true);
                    } else {
                        this.fetchCollection(url, true);
                    }
                }
                if (!url && Utils.getUrlState.isTaxonomyTab()) {
                    this.selectFirstElement();
                }
            },
            changeArrowState: function(e) {
                var scope = this.$('[data-id="expandArrow"]');
                if (e) {
                    scope = $(e.currentTarget);
                }
                if (scope.is('a')) {
                    var url = scope.data('href');
                    scope = scope.parent().find("i.toggleArrow");
                } else if (scope.is('i')) {
                    var url = scope.parent().find("a").data('href');
                }
                if (scope.hasClass('fa-angle-down')) {
                    scope.toggleClass('fa-angle-right fa-angle-down');
                    this.ui.childList.hide();
                } else {
                    if (e && $(e.currentTarget).parents('li.parentChild').length) {
                        scope.parent('li').find('.tools .taxanomyloader').show();
                        this.url = url;
                        this.fetchCollection(url, true);
                        if (this.viewBased) {
                            Utils.setUrl({
                                url: "#!/taxonomy/detailCatalog" + url,
                                mergeBrowserUrl: false,
                                trigger: true,
                                updateTabState: function() {
                                    return { taxonomyUrl: this.url, stateChanged: true };
                                }
                            });
                        }
                    } else {
                        scope.toggleClass('fa-angle-right fa-angle-down');
                        this.ui.childList.show();
                    }
                }
            },
            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 {
245
                        this.url = UrlLinks.taxonomiesApiUrl();
246 247 248 249 250 251
                    }
                }
                this.showLoader();
                if (isParent) {
                    this.parentCollection.url = this.url;
                    this.parentCollection.fullCollection.reset(undefined, { silent: true });
252
                    this.parentCollection.fetch({ reset: true, cache: true });
253 254 255
                } else {
                    this.childCollection.url = this.url + "?hierarchy/path:.";
                    this.childCollection.fullCollection.reset(undefined, { silent: true });
256
                    this.childCollection.fetch({ reset: true, cache: true });
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
                }
            },
            showLoader: function() {
                this.$('.taxonomyTree').find('li.active .tools .taxanomyloader').show();
                this.$('.contentLoading').show();
            },
            hideLoader: function() {
                this.$('.taxanomyloader').hide();
                this.$('.contentLoading').hide();
            },
            addActiveClass: function(e) {
                this.$('ul.taxonomyTree').find('li').removeClass('active');
                if (e.jquery) {
                    e.parent('li').addClass('active');
                } else {
                    if (e.currentTarget) {
                        $(e.currentTarget).parent('li').addClass('active');
                    } else {
                        if ($(e).parent.length) {
                            $(e).parent('li').addClass('active');
                        } else {
                            $(e).parents('li').addClass('active');
                        }
                    }
                }
            },
            fetchTaxanomyCollections: function() {
                this.taxanomy.fetch({ reset: true });
            },
            refreshButtonTaxanomy: function() {
                this.fetchTaxanomyCollections();
                var url = "";
                if (this.$('.taxonomyTree').find('.active').parents('.parentChild').length) {
                    url = this.$('.taxonomyTree').find('.active a').data('href').split("/").slice(0, -2).join("/");
                    this.refresh = this.$('.taxonomyTree').find('.active a').data('href');
                } else {
                    url = this.$('.taxonomyTree').find('.active a').data('href');
                    this.refresh = this.$('.taxonomyTree').find('.active a').data('href');
                }
                this.fetchCollection(url, true);
            },
            searchResult: function() {
                var that = this;
                _.each(this.taxanomy.models, function(model, key) {
                    var name = model.get('name');
302
                    that.termCollection.url = UrlLinks.taxonomiesTermsApiUrl(name)
303 304 305 306 307 308
                });
                this.termCollection.fetch({ reset: true });
            },
            termSearchData: function() {
                var that = this;
                var str = '<option></option>';
309 310
                this.termCollection.fullCollection.comparator = function(model) {
                    return model.get('name');
311
                };
312 313 314 315
                this.termCollection.fullCollection.sort().each(function(model) {
                    str += '<option>' + model.get('name') + '</option>';
                });
                this.ui.searchTermInput.html(str);
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
                // this.ui.searchTermInput.setAttribute('data-href' : that.termCollection.url);
                this.ui.searchTermInput.select2({
                    placeholder: "Search Term",
                    allowClear: true
                });
            },
            onSearchTerm: function() {
                Utils.setUrl({
                    url: '#!/search/searchResult',
                    urlParams: {
                        query: this.$('.taxonomyTree').find('li.active').find("a").data('name'),
                        searchType: "dsl",
                        dslChecked: true
                    },
                    updateTabState: function() {
                        return { searchUrl: this.url, stateChanged: true };
                    },
                    mergeBrowserUrl: false,
                    trigger: true
                });
            },
            selectFirstElement: function() {
338
                var dataURL = this.$('.taxonomyTree').find('li[data-id="Parent"]').find("a").data('href');
339 340
                if (dataURL) {
                    this.url = dataURL;
341
                    if (this.viewBased && Utils.getUrlState.isTaxonomyTab()) {
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
                        Utils.setUrl({
                            url: "#!/taxonomy/detailCatalog" + dataURL,
                            mergeBrowserUrl: false,
                            trigger: true,
                            updateTabState: function() {
                                return { taxonomyUrl: this.url, stateChanged: true };
                            }
                        });
                    }
                }
            },
            generateTree: function(isParent) {
                var parentLi = "",
                    childLi = "",
                    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;
                        } else if (model.get('href')) {
                            href = model.get('href') + "/terms";
                        }
                        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();
                                }
                            }
                        }
378 379 380
                        var name = Utils.checkTagOrTerm(model.get('name'), true);
                        if (name.name) {
                            if (that.viewBased) {
381
                                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-angle-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>';
382
                            } else {
383
                                parentLi = '<div class="tools"><i class="fa fa-refresh fa-spin-custom taxanomyloader"></i></div><i class="fa fa-angle-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>';
384
                            }
385 386 387 388 389 390 391 392 393 394 395 396
                        }
                    });
                    if (href) {
                        var hrefUrl = "/api" + href.split("/api")[1];
                        that.fetchCollection(hrefUrl);
                    }
                    that.ui.childList.html('');
                    that.ui.Parent.addClass('active');
                    that.ui.Parent.html(parentLi);
                }

                function createTerm() {
397 398
                    that.childCollection.fullCollection.comparator = function(model) {
                        return model.get('name').toLowerCase();
399
                    };
400
                    that.childCollection.fullCollection.sort().each(function(model, key) {
401
                        var name = Utils.checkTagOrTerm(model.get('name'), true);
402
                        var hrefUrl = "/api" + model.get('href').split("/api")[1];
403 404 405 406 407 408
                        if (name.name) {
                            if (that.viewBased) {
                                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-angle-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>';
                            } else {
                                childLi += '<li class="children"><div class="tools"><i class="fa fa-refresh fa-spin-custom taxanomyloader"></i></div><i class="fa fa-angle-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>';
                            }
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
                        }
                    });
                    that.ui.childList.html(childLi);
                }
                if (isParent) {
                    createTaxonomy();
                } else {
                    this.changeArrowState();
                    createTerm();
                    if (Utils.getUrlState.isInitial() || Utils.getUrlState.getQueryUrl().lastValue == "taxonomy") {
                        this.selectFirstElement();
                    }
                    if (this.refresh) {
                        this.addActiveClass(this.$('.taxonomyTree').find('a[data-href="' + this.refresh + '"]'));
                        this.refresh = undefined;
                    }
                }
                this.hideLoader();
                if (this.viewBased) {
                    this.$('.termPopover').popover({
                        placement: 'bottom',
                        html: true,
                        trigger: 'manual',
                        container: this.$el,
                        template: '<div class="popover fixedPopover fade bottom in"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div></div>',
                        content: function() {
435
                            var li = "<li class='listTerm'><i class='fa fa-plus'></i> <a href='javascript:void(0)' data-fn='onAddTerm'>Create Subterm</a></li>";
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 461 462 463 464
                            /* "<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-search'></i> <a href='javascript:void(0)' data-fn='onSearchTerm'>Search Assets</a></li>" + li;
                                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>";
                        }
                    });
                    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('show');
                    });
                }
            },
            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({
465
                        title: 'Create Sub-term',
466 467 468 469
                        content: view,
                        okCloses: true,
                        showFooter: true,
                        allowCancel: true,
470
                        okText: 'Create',
471 472 473 474 475 476 477 478
                    }).open();
                    modal.$el.find('button.ok').attr('disabled', true);
                    modal.on('ok', function() {
                        that.saveAddTerm(view);
                    });
                    view.ui.termName.on('keyup', function() {
                        if (this.value.indexOf(' ') >= 0) {
                            modal.$el.find('button.ok').prop('disabled', true);
479
                            view.ui.termName.addClass("addTermDisable");
480 481 482
                            view.$('.alertTerm').show();
                        } else {
                            modal.$el.find('button.ok').prop('disabled', false);
483
                            view.ui.termName.removeClass("addTermDisable");
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
                            view.$('.alertTerm').hide();
                        }
                    });
                    view.on('closeModal', function() {
                        modal.trigger('cancel');
                    });

                });
            },
            saveAddTerm: function(view) {
                var that = this;
                var url = view.url;
                view.model.url = url + "/terms/" + view.ui.termName.val();
                this.showLoader();
                view.model.set({ description: view.ui.termDetail.val() }).save(null, {
                    success: function(model, response) {
                        that.create = true;
                        that.fetchTaxanomyCollections();
                        that.fetchCollection(url, true);
                        Utils.notifySuccess({
                            content: "Term " + view.ui.termName.val() + Messages.addSuccessMessage
                        });
                    },
                    complete: function() {
                        that.hideLoader();
                    }
                });
            },
            deleteTerm: function(e) {
                var termName = this.$('.taxonomyTree').find('li.active a').data("name"),
514
                    assetName = $(e.target).data("assetname"),
515
                    that = this,
516
                    modal = CommonViewFunction.deleteTagModel({
517
                        msg: "<div class='ellipsis'>Delete: " + "<b>" + _.escape(termName) + "?</b></div>" +
518 519 520 521
                            "<p class='termNote'>Assets mapped to this term will be unclassified.</p>",
                        titleMessage: Messages.deleteTerm,
                        buttonText: "Delete"
                    });
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
                modal.on('ok', function() {
                    that.deleteTermData(e);
                });
                modal.on('closeModal', function() {
                    modal.trigger('cancel');
                });
            },
            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, {
537
                        skipDefaultError: true,
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
                        success: function(data) {
                            Utils.notifySuccess({
                                content: "Term " + termName + Messages.deleteSuccessMessage
                            });
                            var termURL = url.split("/").slice(0, -2).join("/");
                            if (that.viewBased) {
                                Utils.setUrl({
                                    url: "#!/taxonomy/detailCatalog" + termURL,
                                    mergeBrowserUrl: false,
                                    trigger: true,
                                    updateTabState: function() {
                                        return { taxonomyUrl: this.url, stateChanged: true };
                                    }
                                });
                            }
                            that.fetchCollection(termURL, true);
                        },
555
                        cust_error: function(model, response) {
556
                            var message = "Term " + termName + Messages.deleteErrorMessage;
557 558
                            if (response && response.responseJSON) {
                                message = response.responseJSON.errorMessage;
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 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
                            }
                            Utils.notifyError({
                                content: message
                            });
                        },
                        complete: function() {
                            that.hideLoader();
                        }
                    });
                });
            },
            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');
                    });
                });
            },
            createDefaultTaxonomy: function() {
                var that = this;
                require([
                    'views/business_catalog/AddTermLayoutView',
                    'modules/Modal'
                ], function(AddTermLayoutView, Modal) {
                    var view = new AddTermLayoutView({
602
                        url: UrlLinks.taxonomiesApiUrl(),
603
                        model: new that.parentCollection.model(),
604
                        defaultTerm: true
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
                    });
                    var modal = new Modal({
                        title: '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.on('keyup', function() {
                        if (this.value.indexOf(' ') >= 0) {
                            modal.$el.find('button.ok').prop('disabled', true);
621
                            view.ui.termName.addClass("addTermDisable");
622 623 624
                            view.$('.alertTerm').show();
                        } else {
                            modal.$el.find('button.ok').prop('disabled', false);
625
                            view.ui.termName.removeClass("addTermDisable");
626 627 628 629 630 631 632 633 634 635 636 637 638 639
                            view.$('.alertTerm').hide();
                        }
                    });
                    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, {
640
                    skipDefaultError: true,
641 642 643 644 645 646
                    success: function(model, response) {
                        that.fetchCollection(view.model.url, true);
                        Utils.notifySuccess({
                            content: "Default taxonomy " + view.ui.termName.val() + Messages.addSuccessMessage
                        });
                    },
647
                    cust_error: function(model, response) {
648 649 650 651 652 653 654 655 656 657 658 659
                        Utils.notifyError({
                            content: "Default taxonomy " + view.ui.termName.val() + Messages.addErrorMessage
                        });
                    },
                    complete: function() {
                        that.hideLoader();
                    }
                });
            }
        });
    return TreeLayoutView;
});