Daniel@0: "use strict"; Daniel@0: Daniel@0: App.module("MainMenuModule", function (MainMenuModule, App, Backbone, Marionette, $, _, Logger) { Daniel@0: Daniel@0: // Define private variables Daniel@0: Daniel@0: MainMenuModule.MainMenuBarView = Backbone.View.extend({ Daniel@0: Daniel@0: options: { Daniel@0: flashingSpeed: 100, Daniel@0: }, Daniel@0: Daniel@0: el: '.main-menu-bar', Daniel@0: Daniel@0: _$itemHelp: null, _$aHelp: null, Daniel@0: _$itemUndo: null, _$aUndo: null, Daniel@0: _$itemRedo: null, _$aRedo: null, Daniel@0: _$itemShare: null, _$aShare: null, Daniel@0: _$itemBookmarks: null, _$aBookmarks: null, Daniel@0: _$itemDownloads: null, _$aDownloads: null, Daniel@0: Daniel@0: initialize: function(options) { Daniel@0: var _this = this; Daniel@0: Daniel@0: _this.$el.disableSelection(); Daniel@0: Daniel@0: // Enable help link Daniel@0: _this._$itemHelp = _this.$(".main-menu-bar__item_action_help"); Daniel@0: _this._$aHelp = _this._$itemHelp.children().first(); Daniel@0: _this._$itemHelp.setMod("main-menu-bar__item", "state", "enabled"); Daniel@0: _this._$aHelp.click(function(event) { Daniel@0: App.showHelp(); Daniel@0: event.preventDefault(); Daniel@0: return false; Daniel@0: }); Daniel@0: Daniel@0: // Enable undo / redo links Daniel@0: _this._$itemUndo = _this.$(".main-menu-bar__item_action_undo"); Daniel@0: _this._$aUndo = _this._$itemUndo.children().first(); Daniel@0: _this._$itemRedo = _this.$(".main-menu-bar__item_action_redo"); Daniel@0: _this._$aRedo = _this._$itemRedo.children().first(); Daniel@0: Daniel@0: var stateHistory = App.context.get("stateHistory"); Daniel@0: App.context.get("stateHistory").bind("change", _this.render, _this); Daniel@0: Daniel@0: _this._$aUndo.click(function(event) { Daniel@0: App.undo(); Daniel@0: event.preventDefault(); Daniel@0: }); Daniel@0: Daniel@0: _this._$aRedo.click(function(event) { Daniel@0: App.redo(); Daniel@0: event.preventDefault(); Daniel@0: }); Daniel@0: Daniel@0: // Misc actions Daniel@0: _this._$itemShare = _this.$(".main-menu-bar__item_action_share"); Daniel@0: _this._$aShare = _this._$itemShare.children().first(); Daniel@0: _this._$itemBookmarks = _this.$(".main-menu-bar__item_action_bookmarks"); Daniel@0: _this._$aBookmarks = _this._$itemBookmarks.children().first(); Daniel@0: _this._$itemDownloads = _this.$(".main-menu-bar__item_action_downloads"); Daniel@0: _this._$aDownloads = _this._$itemDownloads.children().first(); Daniel@0: Daniel@0: _this._$aShare.click(function(event) { Daniel@0: App.showStateSharing(); Daniel@0: event.preventDefault(); Daniel@0: }); Daniel@0: Daniel@0: _this._$aBookmarks.click(function(event) { Daniel@0: App.showStateBookmarks(); Daniel@0: event.preventDefault(); Daniel@0: }); Daniel@0: Daniel@0: // Mode changer Daniel@0: _this._$itemChangeMode = _this.$(".main-menu-bar__item_action_change-mode"); Daniel@0: _this._$aChangeModeToRecordings = _this._$itemChangeMode.children().first(); Daniel@0: _this._$aChangeModeToCollections = _this._$itemChangeMode.children().last(); Daniel@0: Daniel@0: _this._$aChangeModeToRecordings.click(function(event) { Daniel@0: App.context.attributes.state.set("musicRecordingsGridIsShown", true); Daniel@0: event.preventDefault(); Daniel@0: }); Daniel@0: _this._$aChangeModeToCollections.click(function(event) { Daniel@0: App.context.attributes.state.set("musicRecordingsGridIsShown", false); Daniel@0: event.preventDefault(); Daniel@0: }); Daniel@0: Daniel@0: _this.render(false, true); Daniel@0: }, Daniel@0: Daniel@0: render: function(deep, instant) { Daniel@0: var _this = this; Daniel@0: _this._updateUndoRedo(); Daniel@0: _this._updateModeChanger(); Daniel@0: }, Daniel@0: Daniel@0: /** Daniel@0: * callback Daniel@0: * function({bool} interrupted) Daniel@0: */ Daniel@0: flashItem: function(itemActionToFlash, times, callback) { Daniel@0: var _this = this; Daniel@0: var $item = _this.$(".main-menu-bar__item_action_" + itemActionToFlash); Daniel@0: $item.addClass("main-menu-bar__item_flashing"); Daniel@0: var currentCheckNumber = Math.random(); Daniel@0: $item.data("randomNumberForFlashCheck", currentCheckNumber); Daniel@0: var timerCounter = times > 1 ? times * 2 - 1 : 1; Daniel@0: var interval = setInterval(function() { Daniel@0: if (currentCheckNumber == $item.data("randomNumberForFlashCheck")) { Daniel@0: if (timerCounter % 2) { Daniel@0: $item.removeClass("main-menu-bar__item_flashing"); Daniel@0: } else { Daniel@0: $item.addClass("main-menu-bar__item_flashing"); Daniel@0: } Daniel@0: } else { Daniel@0: if (_.isFunction(callback)) { Daniel@0: callback.call($item, true); Daniel@0: } Daniel@0: clearInterval(interval); Daniel@0: } Daniel@0: if (!--timerCounter) { Daniel@0: if (_.isFunction(callback)) { Daniel@0: callback.call($item, false); Daniel@0: } Daniel@0: clearInterval(interval); Daniel@0: } Daniel@0: }, _this.options.flashingSpeed); Daniel@0: }, Daniel@0: Daniel@0: _flashModeChangerThreeTimes: function() { Daniel@0: var _this = this; Daniel@0: _this.flashItem("change-mode", 3); Daniel@0: }, Daniel@0: Daniel@0: _updateUndoRedo: function() { Daniel@0: var _this = this; Daniel@0: var stateHistory = App.context.get("stateHistory"); Daniel@0: _this._$itemUndo.setMod("main-menu-bar", "item", "state", stateHistory.canUndo() ? "enabled" : false); Daniel@0: _this._$itemRedo.setMod("main-menu-bar", "item", "state", stateHistory.canRedo() ? "enabled" : false); Daniel@0: }, Daniel@0: Daniel@0: _updateModeChanger: function() { Daniel@0: var _this = this; Daniel@0: var state = App.context.get("state"); Daniel@0: var musicRecordingsGridIsShown = !! state.get("musicRecordingsGridIsShown"); Daniel@0: if (_this._cachedMusicRecordingsGridIsShown !== musicRecordingsGridIsShown) { Daniel@0: _this._$itemChangeMode.setMod("main-menu-bar", "item", "mode", musicRecordingsGridIsShown ? "2collections" : "2recordings"); Daniel@0: Daniel@0: if (musicRecordingsGridIsShown) { Daniel@0: _this.stopListening(state.get("musicRecordingGrid")); Daniel@0: _this.listenTo(state.get("musicCollectionGrid"), "change", _this._flashModeChangerThreeTimes); Daniel@0: } else { Daniel@0: _this.stopListening(state.get("musicCollectionGrid")); Daniel@0: _this.listenTo(state.get("musicRecordingGrid"), "change", _this._flashModeChangerThreeTimes); Daniel@0: } Daniel@0: _this._cachedMusicRecordingsGridIsShown = musicRecordingsGridIsShown; Daniel@0: } Daniel@0: } Daniel@0: Daniel@0: }); Daniel@0: }, Logger);