Daniel@0
|
1 "use strict";
|
Daniel@0
|
2
|
Daniel@0
|
3 App.module("MainMenuModule", function (MainMenuModule, App, Backbone, Marionette, $, _, Logger) {
|
Daniel@0
|
4
|
Daniel@0
|
5 // Define private variables
|
Daniel@0
|
6
|
Daniel@0
|
7 MainMenuModule.MainMenuBarView = Backbone.View.extend({
|
Daniel@0
|
8
|
Daniel@0
|
9 options: {
|
Daniel@0
|
10 flashingSpeed: 100,
|
Daniel@0
|
11 },
|
Daniel@0
|
12
|
Daniel@0
|
13 el: '.main-menu-bar',
|
Daniel@0
|
14
|
Daniel@0
|
15 _$itemHelp: null, _$aHelp: null,
|
Daniel@0
|
16 _$itemUndo: null, _$aUndo: null,
|
Daniel@0
|
17 _$itemRedo: null, _$aRedo: null,
|
Daniel@0
|
18 _$itemShare: null, _$aShare: null,
|
Daniel@0
|
19 _$itemBookmarks: null, _$aBookmarks: null,
|
Daniel@0
|
20 _$itemDownloads: null, _$aDownloads: null,
|
Daniel@0
|
21
|
Daniel@0
|
22 initialize: function(options) {
|
Daniel@0
|
23 var _this = this;
|
Daniel@0
|
24
|
Daniel@0
|
25 _this.$el.disableSelection();
|
Daniel@0
|
26
|
Daniel@0
|
27 // Enable help link
|
Daniel@0
|
28 _this._$itemHelp = _this.$(".main-menu-bar__item_action_help");
|
Daniel@0
|
29 _this._$aHelp = _this._$itemHelp.children().first();
|
Daniel@0
|
30 _this._$itemHelp.setMod("main-menu-bar__item", "state", "enabled");
|
Daniel@0
|
31 _this._$aHelp.click(function(event) {
|
Daniel@0
|
32 App.showHelp();
|
Daniel@0
|
33 event.preventDefault();
|
Daniel@0
|
34 return false;
|
Daniel@0
|
35 });
|
Daniel@0
|
36
|
Daniel@0
|
37 // Enable undo / redo links
|
Daniel@0
|
38 _this._$itemUndo = _this.$(".main-menu-bar__item_action_undo");
|
Daniel@0
|
39 _this._$aUndo = _this._$itemUndo.children().first();
|
Daniel@0
|
40 _this._$itemRedo = _this.$(".main-menu-bar__item_action_redo");
|
Daniel@0
|
41 _this._$aRedo = _this._$itemRedo.children().first();
|
Daniel@0
|
42
|
Daniel@0
|
43 var stateHistory = App.context.get("stateHistory");
|
Daniel@0
|
44 App.context.get("stateHistory").bind("change", _this.render, _this);
|
Daniel@0
|
45
|
Daniel@0
|
46 _this._$aUndo.click(function(event) {
|
Daniel@0
|
47 App.undo();
|
Daniel@0
|
48 event.preventDefault();
|
Daniel@0
|
49 });
|
Daniel@0
|
50
|
Daniel@0
|
51 _this._$aRedo.click(function(event) {
|
Daniel@0
|
52 App.redo();
|
Daniel@0
|
53 event.preventDefault();
|
Daniel@0
|
54 });
|
Daniel@0
|
55
|
Daniel@0
|
56 // Misc actions
|
Daniel@0
|
57 _this._$itemShare = _this.$(".main-menu-bar__item_action_share");
|
Daniel@0
|
58 _this._$aShare = _this._$itemShare.children().first();
|
Daniel@0
|
59 _this._$itemBookmarks = _this.$(".main-menu-bar__item_action_bookmarks");
|
Daniel@0
|
60 _this._$aBookmarks = _this._$itemBookmarks.children().first();
|
Daniel@0
|
61 _this._$itemDownloads = _this.$(".main-menu-bar__item_action_downloads");
|
Daniel@0
|
62 _this._$aDownloads = _this._$itemDownloads.children().first();
|
Daniel@0
|
63
|
Daniel@0
|
64 _this._$aShare.click(function(event) {
|
Daniel@0
|
65 App.showStateSharing();
|
Daniel@0
|
66 event.preventDefault();
|
Daniel@0
|
67 });
|
Daniel@0
|
68
|
Daniel@0
|
69 _this._$aBookmarks.click(function(event) {
|
Daniel@0
|
70 App.showStateBookmarks();
|
Daniel@0
|
71 event.preventDefault();
|
Daniel@0
|
72 });
|
Daniel@0
|
73
|
Daniel@0
|
74 // Mode changer
|
Daniel@0
|
75 _this._$itemChangeMode = _this.$(".main-menu-bar__item_action_change-mode");
|
Daniel@0
|
76 _this._$aChangeModeToRecordings = _this._$itemChangeMode.children().first();
|
Daniel@0
|
77 _this._$aChangeModeToCollections = _this._$itemChangeMode.children().last();
|
Daniel@0
|
78
|
Daniel@0
|
79 _this._$aChangeModeToRecordings.click(function(event) {
|
Daniel@0
|
80 App.context.attributes.state.set("musicRecordingsGridIsShown", true);
|
Daniel@0
|
81 event.preventDefault();
|
Daniel@0
|
82 });
|
Daniel@0
|
83 _this._$aChangeModeToCollections.click(function(event) {
|
Daniel@0
|
84 App.context.attributes.state.set("musicRecordingsGridIsShown", false);
|
Daniel@0
|
85 event.preventDefault();
|
Daniel@0
|
86 });
|
Daniel@0
|
87
|
Daniel@0
|
88 _this.render(false, true);
|
Daniel@0
|
89 },
|
Daniel@0
|
90
|
Daniel@0
|
91 render: function(deep, instant) {
|
Daniel@0
|
92 var _this = this;
|
Daniel@0
|
93 _this._updateUndoRedo();
|
Daniel@0
|
94 _this._updateModeChanger();
|
Daniel@0
|
95 },
|
Daniel@0
|
96
|
Daniel@0
|
97 /**
|
Daniel@0
|
98 * callback
|
Daniel@0
|
99 * function({bool} interrupted)
|
Daniel@0
|
100 */
|
Daniel@0
|
101 flashItem: function(itemActionToFlash, times, callback) {
|
Daniel@0
|
102 var _this = this;
|
Daniel@0
|
103 var $item = _this.$(".main-menu-bar__item_action_" + itemActionToFlash);
|
Daniel@0
|
104 $item.addClass("main-menu-bar__item_flashing");
|
Daniel@0
|
105 var currentCheckNumber = Math.random();
|
Daniel@0
|
106 $item.data("randomNumberForFlashCheck", currentCheckNumber);
|
Daniel@0
|
107 var timerCounter = times > 1 ? times * 2 - 1 : 1;
|
Daniel@0
|
108 var interval = setInterval(function() {
|
Daniel@0
|
109 if (currentCheckNumber == $item.data("randomNumberForFlashCheck")) {
|
Daniel@0
|
110 if (timerCounter % 2) {
|
Daniel@0
|
111 $item.removeClass("main-menu-bar__item_flashing");
|
Daniel@0
|
112 } else {
|
Daniel@0
|
113 $item.addClass("main-menu-bar__item_flashing");
|
Daniel@0
|
114 }
|
Daniel@0
|
115 } else {
|
Daniel@0
|
116 if (_.isFunction(callback)) {
|
Daniel@0
|
117 callback.call($item, true);
|
Daniel@0
|
118 }
|
Daniel@0
|
119 clearInterval(interval);
|
Daniel@0
|
120 }
|
Daniel@0
|
121 if (!--timerCounter) {
|
Daniel@0
|
122 if (_.isFunction(callback)) {
|
Daniel@0
|
123 callback.call($item, false);
|
Daniel@0
|
124 }
|
Daniel@0
|
125 clearInterval(interval);
|
Daniel@0
|
126 }
|
Daniel@0
|
127 }, _this.options.flashingSpeed);
|
Daniel@0
|
128 },
|
Daniel@0
|
129
|
Daniel@0
|
130 _flashModeChangerThreeTimes: function() {
|
Daniel@0
|
131 var _this = this;
|
Daniel@0
|
132 _this.flashItem("change-mode", 3);
|
Daniel@0
|
133 },
|
Daniel@0
|
134
|
Daniel@0
|
135 _updateUndoRedo: function() {
|
Daniel@0
|
136 var _this = this;
|
Daniel@0
|
137 var stateHistory = App.context.get("stateHistory");
|
Daniel@0
|
138 _this._$itemUndo.setMod("main-menu-bar", "item", "state", stateHistory.canUndo() ? "enabled" : false);
|
Daniel@0
|
139 _this._$itemRedo.setMod("main-menu-bar", "item", "state", stateHistory.canRedo() ? "enabled" : false);
|
Daniel@0
|
140 },
|
Daniel@0
|
141
|
Daniel@0
|
142 _updateModeChanger: function() {
|
Daniel@0
|
143 var _this = this;
|
Daniel@0
|
144 var state = App.context.get("state");
|
Daniel@0
|
145 var musicRecordingsGridIsShown = !! state.get("musicRecordingsGridIsShown");
|
Daniel@0
|
146 if (_this._cachedMusicRecordingsGridIsShown !== musicRecordingsGridIsShown) {
|
Daniel@0
|
147 _this._$itemChangeMode.setMod("main-menu-bar", "item", "mode", musicRecordingsGridIsShown ? "2collections" : "2recordings");
|
Daniel@0
|
148
|
Daniel@0
|
149 if (musicRecordingsGridIsShown) {
|
Daniel@0
|
150 _this.stopListening(state.get("musicRecordingGrid"));
|
Daniel@0
|
151 _this.listenTo(state.get("musicCollectionGrid"), "change", _this._flashModeChangerThreeTimes);
|
Daniel@0
|
152 } else {
|
Daniel@0
|
153 _this.stopListening(state.get("musicCollectionGrid"));
|
Daniel@0
|
154 _this.listenTo(state.get("musicRecordingGrid"), "change", _this._flashModeChangerThreeTimes);
|
Daniel@0
|
155 }
|
Daniel@0
|
156 _this._cachedMusicRecordingsGridIsShown = musicRecordingsGridIsShown;
|
Daniel@0
|
157 }
|
Daniel@0
|
158 }
|
Daniel@0
|
159
|
Daniel@0
|
160 });
|
Daniel@0
|
161 }, Logger); |