Created
May 10, 2014 09:28
-
-
Save hyperNURb/832ae12ccccf2c8d370d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| define(function(require, exports, module) { | |
| var preloaderClass = 'loading', | |
| $ = require('jquery'), | |
| _ = require('underscore'), | |
| BB = require('backbone'), | |
| DashboardRouter = require('widgets/routers/dashboard'), | |
| DashboardSubmenu = require('widgets/views/dashboard/submenu'), | |
| SitesCollection = require('widgets/collections/sites'), | |
| syncMixin = require('libs/backbone/plugins/sync_processing'), | |
| CSRFMixin = require('libs/backbone/plugins/csrf_protection'), | |
| SwitchableFeaturesMixin = require('libs/backbone/plugins/switchablefeatures'), | |
| DropDownsView = require('widgets/views/dashboard_drop_downs'), | |
| Informer = require('widgets/views/informer'), | |
| LayoutManager = require('libs/backbone/plugins/layout_manager'), | |
| StatsMixin = require('libs/backbone/plugins/stats_tracking'), | |
| device = require('utils/device'), | |
| settings = require('settings'), | |
| MenuView = require('widgets/managers/menu'), | |
| SettingsMethods = require('utils/settings_methods'), | |
| IndexView = BB.View.extend({ | |
| el: $('body'), | |
| tabField: $('#tab-data'), | |
| initialize: function () { | |
| var showBodyScroll = function () { | |
| $('body').removeClass('noscroll'); | |
| }, | |
| hideBodyScroll = function () { | |
| $('body').addClass('noscroll'); | |
| }, | |
| extraDetails; | |
| this.on('change:current', this.switchSite, this); | |
| this.on('add:site', this.addSite, this); | |
| this.on('change:theme', this.setTheme, this); | |
| this.on('change:layout', this.setLayout, this); | |
| this.on('change:use_giant_splash', this.setGiantSplash, this); | |
| this.on('change:use_fixed_header', this.setFixedHeader, this); | |
| this.collection.on('change:current', this.switchSite, this); | |
| DashboardRouter.on('route:activatePage', this.navigateTab, this); | |
| DashboardRouter.on('route:setPage', this.activateTab, this); | |
| DashboardRouter.on('route:setDefault', this.activateDefaultTab, this); | |
| DashboardRouter.on('route:scrollOn', showBodyScroll, this); | |
| DashboardRouter.on('route:scrollOff', hideBodyScroll, this); | |
| // settings.site = settings.sites[0]; | |
| // tracking user on KissMetric & Intercom.io | |
| extraDetails = { | |
| 'email': settings.user.email, | |
| 'name': settings.user.display_name, | |
| 'created_at': settings.user.created_at | |
| }; | |
| if (settings.site.owner_id === settings.user.id) { | |
| extraDetails.site_name = settings.site.name; | |
| extraDetails.site_title = settings.site.title; | |
| } | |
| settings.is('autorized') && this.trackIdentify(settings.user.id, extraDetails); | |
| if (device.isiPad) { | |
| var $iPadBookmark = $('#ipad-bookmark'), | |
| $normalBookmark = $iPadBookmark.prev(), | |
| bookmarkContent = $normalBookmark.attr('href'); | |
| $normalBookmark.addClass('hide'); | |
| $iPadBookmark.removeClass('hide'); | |
| $iPadBookmark.on('click', function(e) { | |
| e.preventDefault(); | |
| //display informer for textarea + instruction | |
| new Informer({ | |
| type: 'info', | |
| message: '<p>Add this page as a bookmark named "Stick" and replace its address by following code:</p><textarea rows="7" class="input-xlarge">{{bookmark}}</textarea>', | |
| messageVars: { | |
| bookmark: bookmarkContent | |
| }, | |
| headline: 'Add iPad bookmark', | |
| controls: [ | |
| { | |
| name: 'OK', | |
| callback: $.noop | |
| } | |
| ] | |
| }).render(); | |
| }); | |
| } | |
| }, | |
| addSite: function () { | |
| window.location.reload(); | |
| }, | |
| switchSite: function (model, siteId) { | |
| window.location.href = '/core/community/change_current_site/' + model.get('id'); | |
| }, | |
| setTheme: function (model, themeId) { | |
| if(_.isNumber(themeId)) { | |
| var siteModel = this.collection.getCurrent(); | |
| siteModel instanceof BB.Model && siteModel.save({theme_id: themeId}); | |
| } | |
| }, | |
| setLayout: function (model, layoutId) { | |
| if(_.isNumber(layoutId)) { | |
| var siteModel = this.collection.getCurrent(); | |
| siteModel instanceof BB.Model && siteModel.save({layout_id: layoutId}); | |
| } | |
| }, | |
| setGiantSplash: function (model, giantSplashStatus) { | |
| model.save({use_giant_splash: giantSplashStatus}); | |
| }, | |
| setFixedHeader: function (model, fixedHeaderStatus) { | |
| model.save({use_fixed_header: fixedHeaderStatus}); | |
| }, | |
| activateDefaultTab: function () { | |
| var defaultModel = _.find(settings.menu, function (model) { | |
| return model['default']; | |
| }); | |
| defaultModel && this.activateTab(defaultModel.id); | |
| }, | |
| navigateTab: function (name, subtabname) { | |
| DashboardRouter.navigate(name, true); | |
| }, | |
| drawSubmenu: function(name, subtab) { | |
| var model = this.options.menu_collection.get(name); | |
| if (!model) { | |
| return; | |
| } | |
| if (this.last_tab === name) { | |
| return; | |
| } | |
| this.last_tab = name; | |
| this.submenu_view = this.makeInstance(DashboardSubmenu, {baseTab: model.get('name'), collection: new BB.Collection(model.get('submenu'))}).render(); | |
| this.submenu_view.setActive(subtab); | |
| }, | |
| activateTab: function (name, subtabname) { | |
| var current_tab = _.find(settings.menu, function (item) { return item.id === name; }); | |
| this.drawSubmenu(name, subtabname); | |
| if (!current_tab) { | |
| this.activateDefaultTab(); | |
| return; | |
| } | |
| var view = this, | |
| options = _.defaults({ | |
| className: 'dash-tab-content ' + name | |
| , router: DashboardRouter | |
| }, this.options); | |
| this._activeTab instanceof BB.View && this._activeTab.remove(); | |
| view.tabField.addClass(preloaderClass); | |
| this.active_tab_name = name + '_' + subtabname; | |
| require([this.getTabView(name, subtabname)], function (TabView) { | |
| //prevent race conditions, when user quickly switch tabs | |
| if (view.active_tab_name !== name + '_' + subtabname) { | |
| return; | |
| } | |
| view.tabField.removeClass(preloaderClass); | |
| view._activeTab = view.makeInstance(TabView, options); | |
| view.tabField.html(view._activeTab.render().el); | |
| view._activeTab.on('all', function () { | |
| this.trigger.apply(view, arguments); | |
| }, view); | |
| }); | |
| }, | |
| reDrawHeader: function () { | |
| this._activeTab instanceof BB.View && this._activeTab.render(); | |
| }, | |
| getTabView: function (tabId, subtab) { | |
| var url; | |
| switch(tabId) { | |
| case 'content_feeds': | |
| url = 'content_feeds_in'; | |
| break; | |
| case 'facebook': | |
| url = settings.providers.facebook.status ? 'facebook_in' : 'facebook'; | |
| break; | |
| default: | |
| url = tabId; | |
| } | |
| //looking for a default subtab if subtab is not specified | |
| var submenu = this.options.menu_collection.get(tabId).get('submenu'); | |
| subtab && (subtab = _.find(submenu, function(subtab_item) { return subtab_item['id'] === subtab; })); | |
| subtab || (subtab = _.find(submenu, function(subtab_item) { return subtab_item['active']; })); | |
| subtab = _.isObject(subtab) ? subtab.id : null; | |
| if (subtab) { | |
| return 'widgets/views/dashboard/' + url + '/' + subtab; | |
| } | |
| return 'widgets/views/dashboard_' + url; | |
| }, | |
| render: function () { | |
| var view = this; | |
| if (settings.providers.twitter.token_expired) { | |
| require('utils/token_handler').showTwitterInformer(); | |
| } | |
| if (!settings.email_confirmed) { | |
| if (settings.err_msg) { | |
| this.notify({ | |
| message: settings.err_msg, | |
| timeout: 8000 | |
| }); | |
| } else { | |
| this.notify({ | |
| message: 'You need to confirm your email. <a href="/core/users/send_email_confirmation">Resend email</a> (from [email protected])', | |
| timeout: 8000, | |
| safeHTML: false | |
| }); | |
| } | |
| } | |
| this.collection.fetch({add: false, success: function (collection, response) { | |
| var dropDownsViewInst = view.makeInstance(DropDownsView, {collection: collection, siteList: response}).render(); | |
| dropDownsViewInst.on('all', function () { | |
| this.trigger.apply(this, arguments); | |
| }, view); | |
| } | |
| }); | |
| this.makeInstance(MenuView, { el: this.$el[0], router: DashboardRouter}).render(); | |
| return this; | |
| } | |
| }); | |
| BB = BB.noConflict(); | |
| syncMixin.call(BB.Collection.prototype); | |
| syncMixin.call(BB.Model.prototype); | |
| CSRFMixin.call(BB); | |
| SwitchableFeaturesMixin.call(BB.Model.prototype); | |
| LayoutManager.call(BB.View.prototype); | |
| StatsMixin.call(BB.View.prototype); | |
| SettingsMethods.call(settings); | |
| return new (BB.View.extend({ | |
| init: function (options) { | |
| settings.err_msg = options.err_msg; | |
| this.makeInstance(IndexView, { | |
| collection: new SitesCollection(), | |
| menu_collection: new BB.Collection(settings.menu) | |
| }).render(); | |
| if (options.open_power_your_site) { | |
| var PowerYourSiteView = require('widgets/views/payment/power_site'); | |
| $('.main').append('<div class="automatically-opened-lightboxes"></div>'); | |
| new PowerYourSiteView({site_id: settings.site.id, raised_from: 'automatic_link_dashboard'}).setElement($('.main .automatically-opened-lightboxes')).render(); | |
| } | |
| BB.history.start({pushState: true, root: '/core/dashboard/'}); | |
| } | |
| }))(); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment