UNPKG

3.43 kBJavaScriptView Raw
1const assign = require('object-assign')
2const createStore = require('redux').createStore
3const combineReducers = require('redux').combineReducers
4const BrowserWindow = require('electron').BrowserWindow
5
6const sharedState = combineReducers({
7 focusedWindow: focusedWindow,
8 windows: windows
9})
10
11var store = createStore(sharedState)
12
13function focusedWindowAction (action) {
14 var fw = BrowserWindow.getFocusedWindow()
15
16 if (typeof action.windowId === 'undefined' && fw) {
17 action.windowId = fw.id
18 }
19
20 return action
21}
22
23const actions = {
24
25 setFocusedWindow: function (windowId) {
26 store.dispatch(focusedWindowAction({
27 type: 'SET_FOCUSED_WINDOW',
28 windowId: windowId
29 }))
30 },
31
32 setFilePath: function (windowId, filePath) {
33 store.dispatch(focusedWindowAction({
34 type: 'SET_FILE_PATH',
35 windowId: windowId,
36 filePath: filePath
37 }))
38 },
39
40 setSelection: function (windowId, selection) {
41 store.dispatch(focusedWindowAction({
42 type: 'SET_SELECTION',
43 windowId: windowId,
44 selection: selection
45 }))
46 },
47
48 clearSelection: function (windowId) {
49 store.dispatch(focusedWindowAction({
50 type: 'CLEAR_SELECTION',
51 windowId: windowId
52 }))
53 },
54
55 setHistoryStatus: function (windowId, data) {
56 store.dispatch(focusedWindowAction({
57 type: 'SET_HISTORY_STATUS',
58 windowId: windowId,
59 canGoBack: data.canGoBack,
60 canGoForward: data.canGoForward
61 }))
62 }
63}
64
65function focusedWindow (state, action) {
66 if (typeof state === 'undefined') {
67 return null
68 }
69
70 switch (action.type) {
71 case 'SET_FOCUSED_WINDOW':
72 return action.windowId
73
74 default:
75 return state
76 }
77}
78
79function win (state, action) {
80 if (typeof state === 'undefined') {
81 return {
82 id: action.windowId
83 }
84 }
85
86 if (state.id !== action.windowId) {
87 return state
88 }
89
90 switch (action.type) {
91 case 'SET_FILE_PATH':
92 return assign({}, state, {
93 filePath: action.filePath
94 })
95
96 case 'SET_HISTORY_STATUS':
97 return assign({}, state, {
98 history: {
99 canGoBack: action.canGoBack,
100 canGoForward: action.canGoForward
101 }
102 })
103
104 case 'SET_SELECTION':
105 return assign({}, state, {
106 selection: action.selection
107 })
108
109 case 'CLEAR_SELECTION':
110 return assign({}, state, {
111 selection: null
112 })
113
114 default:
115 return state
116 }
117}
118
119function windows (state, action) {
120 if (typeof state === 'undefined') {
121 return []
122 }
123
124 switch (action.type) {
125 case 'SET_FILE_PATH':
126 case 'SET_SELECTION':
127 case 'CLEAR_SELECTION':
128 case 'SET_HISTORY_STATUS':
129 var newState = state.slice(0)
130
131 var index = state.findIndex(function (w) {
132 return w.id === action.windowId
133 })
134
135 if (index === -1) {
136 newState = newState.concat(win(undefined, action))
137 }
138
139 return newState.map(function (w) {
140 return win(w, action)
141 })
142
143 default:
144 return state
145 }
146}
147
148function getFocusedWindowState () {
149 var fw = BrowserWindow.getFocusedWindow()
150 return fw && getWindowState(fw.id)
151}
152
153function getWindowState (windowId) {
154 return store.getState().windows
155 .find(function (w) {
156 return w.id === windowId
157 })
158}
159
160module.exports = assign({}, actions, {
161 subscribe: store.subscribe,
162 getState: store.getState,
163 getWindowState: getWindowState,
164 getFocusedWindowState: getFocusedWindowState
165})