Router.js 12.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([
    'jquery',
    'underscore',
    'backbone',
    'App',
    'utils/Globals',
25 26
    'utils/Utils',
    'utils/UrlLinks',
27 28
    'collection/VGlossaryList'
], function($, _, Backbone, App, Globals, Utils, UrlLinks, VGlossaryList) {
29 30 31
    var AppRouter = Backbone.Router.extend({
        routes: {
            // Define some URL routes
32
            '': 'defaultAction',
33 34 35 36 37 38
            '!/': 'tagAttributePageLoad',
            '!/tag/tagAttribute/(*name)': 'tagAttributePageLoad',
            '!/search/searchResult': 'searchResult',
            '!/detailPage/:id': 'detailPage',
            '!/tag': 'commonAction',
            '!/search': 'commonAction',
39 40
            '!/glossary': 'commonAction',
            '!/glossary/:id': 'glossaryDetailPage',
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
            this.glossaryCollection = new VGlossaryList([], {
                comparator: function(item) {
52
                    return item.get("name");
53 54
                }
            });
55 56 57
            this.preFetchedCollectionLists = {
                'entityDefCollection': this.entityDefCollection,
                'typeHeaders': this.typeHeaders,
58
                'enumDefCollection': this.enumDefCollection,
59 60
                'classificationDefCollection': this.classificationDefCollection,
                'glossaryCollection': this.glossaryCollection
61
            }
62
            this.sharedObj = {
63
                searchTableColumns: {},
64 65 66
                glossary: {
                    selectedItem: {}
                },
67 68 69 70
                searchTableFilters: {
                    tagFilters: {},
                    entityFilters: {}
                }
71
            }
72 73 74 75
        },
        bindCommonEvents: function() {
            var that = this;
            $('body').on('click', 'li.aboutAtlas', function() {
76 77 78 79
                require([
                    'views/common/AboutAtlas',
                ], function(AboutAtlas) {
                    new AboutAtlas();
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
                });
            });
        },
        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);
        },
104
        detailPage: function(id) {
105 106 107 108
            var that = this;
            if (id) {
                require([
                    'views/site/Header',
109
                    'views/detail_page/DetailPageLayoutView',
110
                    'views/site/SideNavLayoutView',
111 112
                    'collection/VEntityList'
                ], function(Header, DetailPageLayoutView, SideNavLayoutView, VEntityList) {
113
                    this.entityCollection = new VEntityList([], {});
114
                    var paramObj = Utils.getUrlState.getQueryParams();
115
                    App.rNHeader.show(new Header());
116
                    if (!App.rSideNav.currentView) {
117
                        App.rSideNav.show(new SideNavLayoutView(
118
                            _.extend({}, that.preFetchedCollectionLists, that.sharedObj)
119
                        ));
120 121 122
                    } else {
                        App.rSideNav.currentView.selectTab();
                    }
123
                    App.rNContent.show(new DetailPageLayoutView(_.extend({
124
                        'collection': this.entityCollection,
125
                        'id': id,
126
                        'value': paramObj
127
                    }, that.preFetchedCollectionLists, that.sharedObj)));
128
                    this.entityCollection.url = UrlLinks.entitiesApiUrl({ guid: id, minExtInfo: true });
129
                    this.entityCollection.fetch({ reset: true });
130 131 132
                });
            }
        },
133
        tagAttributePageLoad: function(tagName) {
134 135 136
            var that = this;
            require([
                'views/site/Header',
137
                'views/site/SideNavLayoutView',
138
                'views/tag/TagDetailLayoutView',
139
            ], function(Header, SideNavLayoutView, TagDetailLayoutView) {
140 141
                var paramObj = Utils.getUrlState.getQueryParams(),
                    url = Utils.getUrlState.getQueryUrl().queyParams[0];
142
                App.rNHeader.show(new Header());
143
                if (!App.rSideNav.currentView) {
144 145 146 147
                    if (paramObj && paramObj.dlttag) {
                        Utils.setUrl({
                            url: url,
                            trigger: false,
148
                            updateTabState: true
149 150
                        });
                    }
151 152
                    App.rSideNav.show(new SideNavLayoutView(
                        _.extend({
153 154
                            'tag': tagName,
                            'value': paramObj
155
                        }, that.preFetchedCollectionLists, that.sharedObj)
156
                    ));
157
                } else {
158 159 160 161
                    if (paramObj && paramObj.dlttag) {
                        Utils.setUrl({
                            url: url,
                            trigger: false,
162
                            updateTabState: true
163 164
                        });
                    }
165
                    App.rSideNav.currentView.RTagLayoutView.currentView.manualRender(_.extend({}, paramObj, { 'tagName': tagName }));
166 167 168
                    App.rSideNav.currentView.selectTab();
                }
                if (tagName) {
169 170 171 172 173
                    // updating paramObj to check for new queryparam.
                    paramObj = Utils.getUrlState.getQueryParams();
                    if (paramObj && paramObj.dlttag) {
                        return false;
                    }
174 175
                    App.rNContent.show(new TagDetailLayoutView(
                        _.extend({
176 177
                            'tag': tagName,
                            'value': paramObj
178
                        }, that.preFetchedCollectionLists, that.sharedObj)
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
        glossaryDetailPage: function(id) {
            var that = this;
            if (id) {
                require([
                    'views/site/Header',
                    'views/glossary/GlossaryDetailLayoutView',
                    'views/site/SideNavLayoutView'
                ], function(Header, GlossaryDetailLayoutView, SideNavLayoutView) {
                    var paramObj = Utils.getUrlState.getQueryParams();
                    App.rNHeader.show(new Header());
                    if (!App.rSideNav.currentView) {
                        App.rSideNav.show(new SideNavLayoutView(
                            _.extend({}, that.preFetchedCollectionLists, that.sharedObj, { 'guid': id, 'value': paramObj })
                        ));
                    } else {
                        App.rSideNav.currentView.RGlossaryLayoutView.currentView.manualRender(_.extend({}, { 'guid': id, 'value': paramObj }));
                        App.rSideNav.currentView.selectTab();
                    }
                    App.rNContent.show(new GlossaryDetailLayoutView(_.extend({
                        'guid': id,
                        'value': paramObj
                    }, that.preFetchedCollectionLists, that.sharedObj)));
                });
            }
        },
208 209 210 211
        commonAction: function() {
            var that = this;
            require([
                'views/site/Header',
212
                'views/site/SideNavLayoutView',
213
                'views/search/SearchDetailLayoutView',
214
            ], function(Header, SideNavLayoutView, SearchDetailLayoutView) {
215
                var paramObj = Utils.getUrlState.getQueryParams();
216
                App.rNHeader.show(new Header());
217
                if (!App.rSideNav.currentView) {
218 219
                    App.rSideNav.show(new SideNavLayoutView(
                        _.extend({
220
                            'searchVent': that.searchVent
221
                        }, that.preFetchedCollectionLists, that.sharedObj)
222
                    ));
223 224 225 226
                } else {
                    App.rSideNav.currentView.selectTab();
                    if (Utils.getUrlState.isTagTab()) {
                        App.rSideNav.currentView.RTagLayoutView.currentView.manualRender();
227
                    } else if (Utils.getUrlState.isGlossaryTab()) {
228
                        App.rSideNav.currentView.RGlossaryLayoutView.currentView.manualRender(_.extend({ "isTrigger": true }, { "value": paramObj }));
229 230
                    }
                }
231

232
                if (Globals.entityCreate && Utils.getUrlState.isSearchTab()) {
233 234 235
                    App.rNContent.show(new SearchDetailLayoutView(
                        _.extend({
                            'value': paramObj,
236 237
                            'initialView': true,
                            'searchVent': that.searchVent
238
                        }, that.preFetchedCollectionLists, that.sharedObj)
239
                    ));
240 241 242 243
                } else {
                    App.rNContent.$el.html("");
                    App.rNContent.destroy();
                }
244 245 246 247 248 249
            });
        },
        searchResult: function() {
            var that = this;
            require([
                'views/site/Header',
250
                'views/site/SideNavLayoutView',
251
                'views/search/SearchDetailLayoutView'
252
            ], function(Header, SideNavLayoutView, SearchDetailLayoutView) {
253
                var paramObj = Utils.getUrlState.getQueryParams();
254 255 256
                var isinitialView = true,
                    isTypeTagNotExists = false,
                    tempParam = _.extend({}, paramObj);
257
                App.rNHeader.show(new Header());
258
                if (!App.rSideNav.currentView) {
259 260 261
                    App.rSideNav.show(new SideNavLayoutView(
                        _.extend({
                            'value': paramObj,
262
                            'searchVent': that.searchVent
263
                        }, that.preFetchedCollectionLists, that.sharedObj)
264
                    ));
265 266 267 268
                } else {
                    App.rSideNav.currentView.RSearchLayoutView.currentView.manualRender(paramObj);
                }
                App.rSideNav.currentView.selectTab();
269
                if (paramObj) {
270
                    isinitialView = (paramObj.type || (paramObj.dslChecked == "true" ? "" : (paramObj.tag || paramObj.term)) || (paramObj.query ? paramObj.query.trim() : "")).length === 0;
271
                }
272 273 274 275
                App.rNContent.show(new SearchDetailLayoutView(
                    _.extend({
                        'value': paramObj,
                        'searchVent': that.searchVent,
276
                        'initialView': isinitialView,
277
                        'isTypeTagNotExists': ((paramObj.type != tempParam.type) || (tempParam.tag != paramObj.tag))
278
                    }, that.preFetchedCollectionLists, that.sharedObj)
279
                ));
280 281 282 283
            });
        },
        defaultAction: function(actions) {
            // We have no matching route, lets just log what the URL was
284 285 286
            Utils.setUrl({
                url: '#!/search',
                mergeBrowserUrl: false,
287 288
                trigger: true,
                updateTabState: true
289
            });
290

291 292 293 294
            console.log('No route:', actions);
        }
    });
    return AppRouter;
295
});