Router.js 14.9 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([
    'jquery',
    'underscore',
    'backbone',
    'App',
    'utils/Globals',
25 26 27 28
    'utils/Utils',
    'utils/UrlLinks',
    'collection/VTagList'
], function($, _, Backbone, App, Globals, Utils, UrlLinks, VTagList) {
29 30 31
    var AppRouter = Backbone.Router.extend({
        routes: {
            // Define some URL routes
32
            '': 'defaultAction',
33 34 35 36 37 38 39 40
            '!/': 'tagAttributePageLoad',
            '!/tag/tagAttribute/(*name)': 'tagAttributePageLoad',
            '!/taxonomy/detailCatalog/(*url)': 'detailCatalog',
            '!/search/searchResult': 'searchResult',
            '!/detailPage/:id': 'detailPage',
            '!/tag': 'commonAction',
            '!/taxonomy': 'commonAction',
            '!/search': 'commonAction',
41 42 43
            // Default
            '*actions': 'defaultAction'
        },
44
        initialize: function(options) {
45
            _.extend(this, _.pick(options, 'entityDefCollection', 'typeHeaders', 'enumDefCollection', 'classificationDefCollection'));
46 47 48
            this.showRegions();
            this.bindCommonEvents();
            this.listenTo(this, 'route', this.postRouteExecute, this);
49
            this.searchVent = new Backbone.Wreqr.EventAggregator();
50 51 52
            this.preFetchedCollectionLists = {
                'entityDefCollection': this.entityDefCollection,
                'typeHeaders': this.typeHeaders,
53 54
                'enumDefCollection': this.enumDefCollection,
                'classificationDefCollection': this.classificationDefCollection
55
            }
56 57 58 59
            this.filterObj = {
                'tagFilters': JSON.parse(Utils.localStorage.getValue('tagFilters')),
                'entityFilters': JSON.parse(Utils.localStorage.getValue('entityFilters'))
            }
60 61 62 63 64 65 66 67 68 69 70
        },
        bindCommonEvents: function() {
            var that = this;
            $('body').on('click', 'li.aboutAtlas', function() {
                that.aboutAtlas();
            });
        },
        aboutAtlas: function() {
            var that = this;
            require([
                'hbs!tmpl/common/aboutAtlas_tmpl',
71 72 73 74
                'modules/Modal',
                'views/common/aboutAtlas',
            ], function(aboutAtlasTmpl, Modal, aboutAtlasView) {
                var view = new aboutAtlasView();
75
                var modal = new Modal({
76
                    title: 'Apache Atlas',
77 78 79
                    content: view,
                    okCloses: true,
                    showFooter: true,
80
                    allowCancel: false,
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
                }).open();

                view.on('closeModal', function() {
                    modal.trigger('cancel');
                });

            });
        },
        showRegions: function() {},

        /**
         * @override
         * Execute a route handler with the provided parameters. This is an
         * excellent place to do pre-route setup or post-route cleanup.
         * @param  {Function} callback - route handler
         * @param  {Array}   args - route params
         */
        execute: function(callback, args) {
            this.preRouteExecute();
            if (callback) callback.apply(this, args);
            this.postRouteExecute();
        },
        preRouteExecute: function() {
            // console.log("Pre-Route Change Operations can be performed here !!");
        },
        postRouteExecute: function(name, args) {
            // console.log("Post-Route Change Operations can be performed here !!");
            // console.log("Route changed: ", name);
        },
110
        detailCatalog: function(url) {
111 112
            var that = this;
            require([
113 114 115 116 117
                'views/business_catalog/BusinessCatalogHeader',
                'views/business_catalog/BusinessCatalogDetailLayoutView',
                'views/business_catalog/SideNavLayoutView',
                'collection/VCatalogList'
            ], function(BusinessCatalogHeader, BusinessCatalogDetailLayoutView, SideNavLayoutView, VCatalogList) {
118 119 120 121
                if (Globals.taxonomy) {
                    var paramObj = Utils.getUrlState.getQueryParams();
                    this.collection = new VCatalogList();
                    this.collection.url = url;
122 123 124 125 126 127
                    App.rNHeader.show(new BusinessCatalogHeader(
                        _.extend({
                            'url': url,
                            'collection': this.collection
                        }, that.preFetchedCollectionLists)
                    ));
128
                    if (!App.rSideNav.currentView) {
129 130 131
                        App.rSideNav.show(new SideNavLayoutView(
                            _.extend({
                                'url': url,
132
                                'filterObj': that.filterObj
133 134
                            }, that.preFetchedCollectionLists)
                        ));
135 136 137 138
                    } else {
                        App.rSideNav.currentView.RBusinessCatalogLayoutView.currentView.manualRender("/" + url);
                        App.rSideNav.currentView.selectTab();
                    }
139 140 141 142 143 144
                    App.rNContent.show(new BusinessCatalogDetailLayoutView(
                        _.extend({
                            'url': url,
                            'collection': this.collection
                        }, that.preFetchedCollectionLists)
                    ));
145
                    this.collection.fetch({ reset: true });
146
                } else {
147
                    that.defaultAction()
148
                }
149 150
            });
        },
151
        detailPage: function(id) {
152 153 154 155
            var that = this;
            if (id) {
                require([
                    'views/site/Header',
156 157 158 159
                    'views/detail_page/DetailPageLayoutView',
                    'views/business_catalog/SideNavLayoutView',
                    'collection/VEntityList'
                ], function(Header, DetailPageLayoutView, SideNavLayoutView, VEntityList) {
160
                    this.entityCollection = new VEntityList([], {});
161
                    App.rNHeader.show(new Header());
162
                    if (!App.rSideNav.currentView) {
163
                        App.rSideNav.show(new SideNavLayoutView(
164
                            _.extend({ 'filterObj': that.filterObj }, that.preFetchedCollectionLists)
165
                        ));
166 167 168
                    } else {
                        App.rSideNav.currentView.selectTab();
                    }
169
                    App.rNContent.show(new DetailPageLayoutView(_.extend({
170
                        'collection': this.entityCollection,
171
                        'id': id,
172
                    }, that.preFetchedCollectionLists)));
173
                    this.entityCollection.url = UrlLinks.entitiesApiUrl(id);
174
                    this.entityCollection.fetch({ reset: true });
175 176 177
                });
            }
        },
178
        tagAttributePageLoad: function(tagName) {
179 180 181
            var that = this;
            require([
                'views/site/Header',
182 183 184 185
                'views/business_catalog/BusinessCatalogLayoutView',
                'views/business_catalog/SideNavLayoutView',
                'views/tag/TagDetailLayoutView',
            ], function(Header, BusinessCatalogLayoutView, SideNavLayoutView, TagDetailLayoutView) {
186 187
                var paramObj = Utils.getUrlState.getQueryParams(),
                    url = Utils.getUrlState.getQueryUrl().queyParams[0];
188
                App.rNHeader.show(new Header());
189
                if (!App.rSideNav.currentView) {
190 191 192 193 194 195 196 197 198
                    if (paramObj && paramObj.dlttag) {
                        Utils.setUrl({
                            url: url,
                            trigger: false,
                            updateTabState: function() {
                                return { tagUrl: this.url, stateChanged: true };
                            }
                        });
                    }
199 200
                    App.rSideNav.show(new SideNavLayoutView(
                        _.extend({
201 202
                            'tag': tagName,
                            'filterObj': that.filterObj
203 204
                        }, that.preFetchedCollectionLists)
                    ));
205
                } else {
206 207 208 209 210 211 212 213 214
                    if (paramObj && paramObj.dlttag) {
                        Utils.setUrl({
                            url: url,
                            trigger: false,
                            updateTabState: function() {
                                return { tagUrl: this.url, stateChanged: true };
                            }
                        });
                    }
215 216 217 218
                    App.rSideNav.currentView.RTagLayoutView.currentView.manualRender(tagName);
                    App.rSideNav.currentView.selectTab();
                }
                if (tagName) {
219 220 221 222 223
                    // updating paramObj to check for new queryparam.
                    paramObj = Utils.getUrlState.getQueryParams();
                    if (paramObj && paramObj.dlttag) {
                        return false;
                    }
224 225
                    App.rNContent.show(new TagDetailLayoutView(
                        _.extend({
226
                            'tag': tagName
227 228
                        }, that.preFetchedCollectionLists)
                    ));
229 230 231 232 233 234 235 236 237
                }
            });
        },
        commonAction: function() {
            var that = this;
            require([
                'views/site/Header',
                'views/business_catalog/BusinessCatalogLayoutView',
                'views/business_catalog/SideNavLayoutView',
238 239
                'views/search/SearchDetailLayoutView',
            ], function(Header, BusinessCatalogLayoutView, SideNavLayoutView, SearchDetailLayoutView) {
240
                var paramObj = Utils.getUrlState.getQueryParams();
241
                App.rNHeader.show(new Header());
242
                if (!App.rSideNav.currentView) {
243 244
                    App.rSideNav.show(new SideNavLayoutView(
                        _.extend({
245
                            'searchVent': that.searchVent,
246
                            'filterObj': that.filterObj
247 248
                        }, that.preFetchedCollectionLists)
                    ));
249 250 251 252 253
                } else {
                    App.rSideNav.currentView.selectTab();
                    if (Utils.getUrlState.isTagTab()) {
                        App.rSideNav.currentView.RTagLayoutView.currentView.manualRender();
                    } else if (Utils.getUrlState.isTaxonomyTab()) {
254
                        App.rSideNav.currentView.RBusinessCatalogLayoutView.currentView.manualRender(undefined, true);
255 256
                    }
                }
257
                if (Globals.entityCreate && Utils.getUrlState.isSearchTab()) {
258 259 260
                    App.rNContent.show(new SearchDetailLayoutView(
                        _.extend({
                            'value': paramObj,
261
                            'initialView': true,
262
                            'filterObj': that.filterObj,
263
                            'searchVent': that.searchVent
264 265
                        }, that.preFetchedCollectionLists)
                    ));
266 267 268 269
                } else {
                    App.rNContent.$el.html("");
                    App.rNContent.destroy();
                }
270 271 272 273 274 275 276 277 278 279
            });
        },
        searchResult: function() {
            var that = this;
            require([
                'views/site/Header',
                'views/business_catalog/BusinessCatalogLayoutView',
                'views/business_catalog/SideNavLayoutView',
                'views/search/SearchDetailLayoutView'
            ], function(Header, BusinessCatalogLayoutView, SideNavLayoutView, SearchDetailLayoutView) {
280
                var paramObj = Utils.getUrlState.getQueryParams(),
281
                    filterObj = that.filterObj
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
                if (paramObj && paramObj.searchType === "basic") {
                    if (paramObj.type) {
                        if (_.has(filterObj.entityFilters, paramObj.type)) {
                            _.extend(paramObj, {
                                'entityFilters': +new Date()
                            })
                        }
                    }
                    if (paramObj.tag) {
                        if (_.has(filterObj.entityFilters, paramObj.type)) {
                            _.extend(paramObj, {
                                'tagFilters': +new Date()
                            })
                        }
                    }
                    Utils.setUrl({
                        url: '#!/search/searchResult',
                        trigger: false,
                        urlParams: paramObj,
                        updateTabState: function() {
                            return { searchUrl: this.url, stateChanged: true };
                        },
                    });
                }
306
                App.rNHeader.show(new Header());
307
                if (!App.rSideNav.currentView) {
308 309 310
                    App.rSideNav.show(new SideNavLayoutView(
                        _.extend({
                            'value': paramObj,
311
                            'searchVent': that.searchVent,
312
                            'filterObj': that.filterObj
313 314
                        }, that.preFetchedCollectionLists)
                    ));
315 316 317 318
                } else {
                    App.rSideNav.currentView.RSearchLayoutView.currentView.manualRender(paramObj);
                }
                App.rSideNav.currentView.selectTab();
319 320 321 322
                App.rNContent.show(new SearchDetailLayoutView(
                    _.extend({
                        'value': paramObj,
                        'searchVent': that.searchVent,
323
                        'filterObj': that.filterObj,
324 325 326
                        'initialView': (paramObj.type || (paramObj.dslChecked == "true" ? "" : paramObj.tag) || (paramObj.query ? paramObj.query.trim() : "")).length === 0
                    }, that.preFetchedCollectionLists)
                ));
327 328 329 330
            });
        },
        defaultAction: function(actions) {
            // We have no matching route, lets just log what the URL was
331 332 333 334 335 336 337 338
            Utils.setUrl({
                url: '#!/search',
                mergeBrowserUrl: false,
                updateTabState: function() {
                    return { searchUrl: this.url, stateChanged: false };
                },
                trigger: true
            });
339

340 341 342 343
            console.log('No route:', actions);
        }
    });
    return AppRouter;
344
});