Router.js 10.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 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
            '!/': 'tagAttributePageLoad',
            '!/tag/tagAttribute/(*name)': 'tagAttributePageLoad',
            '!/search/searchResult': 'searchResult',
            '!/detailPage/:id': 'detailPage',
            '!/tag': 'commonAction',
            '!/search': 'commonAction',
39 40 41
            // Default
            '*actions': 'defaultAction'
        },
42
        initialize: function(options) {
43
            _.extend(this, _.pick(options, 'entityDefCollection', 'typeHeaders', 'enumDefCollection', 'classificationDefCollection'));
44 45 46
            this.showRegions();
            this.bindCommonEvents();
            this.listenTo(this, 'route', this.postRouteExecute, this);
47
            this.searchVent = new Backbone.Wreqr.EventAggregator();
48 49 50
            this.preFetchedCollectionLists = {
                'entityDefCollection': this.entityDefCollection,
                'typeHeaders': this.typeHeaders,
51 52
                'enumDefCollection': this.enumDefCollection,
                'classificationDefCollection': this.classificationDefCollection
53
            }
54
            this.sharedObj = {
55 56 57 58 59
                searchTableColumns: {},
                searchTableFilters: {
                    tagFilters: {},
                    entityFilters: {}
                }
60
            }
61 62 63 64
        },
        bindCommonEvents: function() {
            var that = this;
            $('body').on('click', 'li.aboutAtlas', function() {
65 66 67 68
                require([
                    'views/common/AboutAtlas',
                ], function(AboutAtlas) {
                    new AboutAtlas();
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
                });
            });
        },
        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);
        },
93
        detailPage: function(id) {
94 95 96 97
            var that = this;
            if (id) {
                require([
                    'views/site/Header',
98
                    'views/detail_page/DetailPageLayoutView',
99
                    'views/site/SideNavLayoutView',
100 101
                    'collection/VEntityList'
                ], function(Header, DetailPageLayoutView, SideNavLayoutView, VEntityList) {
102
                    this.entityCollection = new VEntityList([], {});
103
                    var paramObj = Utils.getUrlState.getQueryParams();
104
                    App.rNHeader.show(new Header());
105
                    if (!App.rSideNav.currentView) {
106
                        App.rSideNav.show(new SideNavLayoutView(
107
                            _.extend({}, that.preFetchedCollectionLists, that.sharedObj)
108
                        ));
109 110 111
                    } else {
                        App.rSideNav.currentView.selectTab();
                    }
112
                    App.rNContent.show(new DetailPageLayoutView(_.extend({
113
                        'collection': this.entityCollection,
114
                        'id': id,
115
                        'value': paramObj
116
                    }, that.preFetchedCollectionLists, that.sharedObj)));
117
                    this.entityCollection.url = UrlLinks.entitiesApiUrl(id);
118
                    this.entityCollection.fetch({ reset: true });
119 120 121
                });
            }
        },
122
        tagAttributePageLoad: function(tagName) {
123 124 125
            var that = this;
            require([
                'views/site/Header',
126
                'views/site/SideNavLayoutView',
127
                'views/tag/TagDetailLayoutView',
128
            ], function(Header, SideNavLayoutView, TagDetailLayoutView) {
129 130
                var paramObj = Utils.getUrlState.getQueryParams(),
                    url = Utils.getUrlState.getQueryUrl().queyParams[0];
131
                App.rNHeader.show(new Header());
132
                if (!App.rSideNav.currentView) {
133 134 135 136
                    if (paramObj && paramObj.dlttag) {
                        Utils.setUrl({
                            url: url,
                            trigger: false,
137
                            updateTabState: true
138 139
                        });
                    }
140 141
                    App.rSideNav.show(new SideNavLayoutView(
                        _.extend({
142 143
                            'tag': tagName,
                            'value': paramObj
144
                        }, that.preFetchedCollectionLists, that.sharedObj)
145
                    ));
146
                } else {
147 148 149 150
                    if (paramObj && paramObj.dlttag) {
                        Utils.setUrl({
                            url: url,
                            trigger: false,
151
                            updateTabState: true
152 153
                        });
                    }
154
                    App.rSideNav.currentView.RTagLayoutView.currentView.manualRender(_.extend({}, paramObj, { 'tagName': tagName }));
155 156 157
                    App.rSideNav.currentView.selectTab();
                }
                if (tagName) {
158 159 160 161 162
                    // updating paramObj to check for new queryparam.
                    paramObj = Utils.getUrlState.getQueryParams();
                    if (paramObj && paramObj.dlttag) {
                        return false;
                    }
163 164
                    App.rNContent.show(new TagDetailLayoutView(
                        _.extend({
165 166
                            'tag': tagName,
                            'value': paramObj
167
                        }, that.preFetchedCollectionLists, that.sharedObj)
168
                    ));
169 170 171 172 173 174 175
                }
            });
        },
        commonAction: function() {
            var that = this;
            require([
                'views/site/Header',
176
                'views/site/SideNavLayoutView',
177
                'views/search/SearchDetailLayoutView',
178
            ], function(Header, SideNavLayoutView, SearchDetailLayoutView) {
179
                var paramObj = Utils.getUrlState.getQueryParams();
180
                App.rNHeader.show(new Header());
181
                if (!App.rSideNav.currentView) {
182 183
                    App.rSideNav.show(new SideNavLayoutView(
                        _.extend({
184
                            'searchVent': that.searchVent
185
                        }, that.preFetchedCollectionLists, that.sharedObj)
186
                    ));
187 188 189 190 191 192
                } else {
                    App.rSideNav.currentView.selectTab();
                    if (Utils.getUrlState.isTagTab()) {
                        App.rSideNav.currentView.RTagLayoutView.currentView.manualRender();
                    }
                }
193
                if (Globals.entityCreate && Utils.getUrlState.isSearchTab()) {
194 195 196
                    App.rNContent.show(new SearchDetailLayoutView(
                        _.extend({
                            'value': paramObj,
197 198
                            'initialView': true,
                            'searchVent': that.searchVent
199
                        }, that.preFetchedCollectionLists, that.sharedObj)
200
                    ));
201 202 203 204
                } else {
                    App.rNContent.$el.html("");
                    App.rNContent.destroy();
                }
205 206 207 208 209 210
            });
        },
        searchResult: function() {
            var that = this;
            require([
                'views/site/Header',
211
                'views/site/SideNavLayoutView',
212
                'views/search/SearchDetailLayoutView'
213
            ], function(Header, SideNavLayoutView, SearchDetailLayoutView) {
214
                var paramObj = Utils.getUrlState.getQueryParams();
215 216 217
                var isinitialView = true,
                    isTypeTagNotExists = false,
                    tempParam = _.extend({}, paramObj);
218
                App.rNHeader.show(new Header());
219
                if (!App.rSideNav.currentView) {
220 221 222
                    App.rSideNav.show(new SideNavLayoutView(
                        _.extend({
                            'value': paramObj,
223
                            'searchVent': that.searchVent
224
                        }, that.preFetchedCollectionLists, that.sharedObj)
225
                    ));
226 227 228 229
                } else {
                    App.rSideNav.currentView.RSearchLayoutView.currentView.manualRender(paramObj);
                }
                App.rSideNav.currentView.selectTab();
230 231 232
                if (paramObj) {
                    isinitialView = (paramObj.type || (paramObj.dslChecked == "true" ? "" : paramObj.tag) || (paramObj.query ? paramObj.query.trim() : "")).length === 0;
                }
233 234 235 236
                App.rNContent.show(new SearchDetailLayoutView(
                    _.extend({
                        'value': paramObj,
                        'searchVent': that.searchVent,
237
                        'initialView': isinitialView,
238
                        'isTypeTagNotExists': ((paramObj.type != tempParam.type) || (tempParam.tag != paramObj.tag))
239
                    }, that.preFetchedCollectionLists, that.sharedObj)
240
                ));
241 242 243 244
            });
        },
        defaultAction: function(actions) {
            // We have no matching route, lets just log what the URL was
245 246 247
            Utils.setUrl({
                url: '#!/search',
                mergeBrowserUrl: false,
248 249
                trigger: true,
                updateTabState: true
250
            });
251

252 253 254 255
            console.log('No route:', actions);
        }
    });
    return AppRouter;
256
});