Completed
Push — master ( e88c19...17669b )
by Rain
03:00
created

Folder.js ➔ ???   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 39
rs 8.8571

1 Function

Rating   Name   Duplication   Size   Complexity  
A Folder.js ➔ ... ➔ ??? 0 5 2
1
2
import ko from 'ko';
3
import _ from '_';
4
5
import {FolderType} from 'Common/Enums';
6
import {UNUSED_OPTION_VALUE} from 'Common/Consts';
7
import {isArray, folderListOptionsBuilder} from 'Common/Utils';
8
import {getFolderInboxName, getFolderFromCacheList} from 'Common/Cache';
9
10
class FolderUserStore
11
{
12
	constructor() {
13
		this.displaySpecSetting = ko.observable(true);
14
15
		this.sentFolder = ko.observable('');
16
		this.draftFolder = ko.observable('');
17
		this.spamFolder = ko.observable('');
18
		this.trashFolder = ko.observable('');
19
		this.archiveFolder = ko.observable('');
20
21
		this.namespace = '';
22
23
		this.folderList = ko.observableArray([]);
24
		this.folderList.optimized = ko.observable(false);
25
		this.folderList.error = ko.observable('');
26
27
		this.foldersLoading = ko.observable(false);
28
		this.foldersCreating = ko.observable(false);
29
		this.foldersDeleting = ko.observable(false);
30
		this.foldersRenaming = ko.observable(false);
31
32
		this.foldersInboxUnreadCount = ko.observable(0);
33
34
		this.currentFolder = ko.observable(null).extend({toggleSubscribe: [
35
			null,
36
			(prev) => {
37
				if (prev) {
38
					prev.selected(false);
39
				}
40
			},
41
			(next) => {
42
				if (next) {
43
					next.selected(true);
44
				}
45
			}
46
		]});
47
48
		this.computers();
49
		this.subscribers();
50
	}
51
52
	computers() {
53
54
		this.draftFolderNotEnabled = ko.computed(
55
			() => ('' === this.draftFolder() || UNUSED_OPTION_VALUE === this.draftFolder())
56
		);
57
58
		this.foldersListWithSingleInboxRootFolder = ko.computed(
59
			() => !_.find(this.folderList(), (folder) => (folder && !folder.isSystemFolder() && folder.visible()))
60
		);
61
62
		this.currentFolderFullNameRaw = ko.computed(
63
			() => (this.currentFolder() ? this.currentFolder().fullNameRaw : '')
0 ignored issues
show
introduced by
This code is unreachable and can thus be removed without consequences.
Loading history...
64
		);
65
66
		this.currentFolderFullName = ko.computed(() => (this.currentFolder() ? this.currentFolder().fullName : ''));
0 ignored issues
show
introduced by
This code is unreachable and can thus be removed without consequences.
Loading history...
67
		this.currentFolderFullNameHash = ko.computed(() => (this.currentFolder() ? this.currentFolder().fullNameHash : ''));
0 ignored issues
show
introduced by
This code is unreachable and can thus be removed without consequences.
Loading history...
68
69
		this.foldersChanging = ko.computed(() => {
70
			const
71
				loading = this.foldersLoading(),
72
				creating = this.foldersCreating(),
73
				deleting = this.foldersDeleting(),
74
				renaming = this.foldersRenaming();
75
76
			return loading || creating || deleting || renaming;
77
		});
78
79
		this.folderListSystemNames = ko.computed(() => {
80
81
			const
82
				list = [getFolderInboxName()],
83
				folders = this.folderList(),
84
				sentFolder = this.sentFolder(),
85
				draftFolder = this.draftFolder(),
86
				spamFolder = this.spamFolder(),
87
				trashFolder = this.trashFolder(),
88
				archiveFolder = this.archiveFolder();
89
90
			if (isArray(folders) && 0 < folders.length)
91
			{
92
				if ('' !== sentFolder && UNUSED_OPTION_VALUE !== sentFolder)
93
				{
94
					list.push(sentFolder);
95
				}
96
				if ('' !== draftFolder && UNUSED_OPTION_VALUE !== draftFolder)
97
				{
98
					list.push(draftFolder);
99
				}
100
				if ('' !== spamFolder && UNUSED_OPTION_VALUE !== spamFolder)
101
				{
102
					list.push(spamFolder);
103
				}
104
				if ('' !== trashFolder && UNUSED_OPTION_VALUE !== trashFolder)
105
				{
106
					list.push(trashFolder);
107
				}
108
				if ('' !== archiveFolder && UNUSED_OPTION_VALUE !== archiveFolder)
109
				{
110
					list.push(archiveFolder);
111
				}
112
			}
113
114
			return list;
115
		});
116
117
		this.folderListSystem = ko.computed(
118
			() => _.compact(_.map(this.folderListSystemNames(), (name) => getFolderFromCacheList(name)))
119
		);
120
121
		this.folderMenuForMove = ko.computed(
122
			() => folderListOptionsBuilder(
123
				this.folderListSystem(), this.folderList(),
124
				[this.currentFolderFullNameRaw()], null, null, null, null, (item) => (item ? item.localName() : ''))
0 ignored issues
show
Bug introduced by
The variable item seems to be never initialized.
Loading history...
introduced by
This code is unreachable and can thus be removed without consequences.
Loading history...
125
		);
126
127
		this.folderMenuForFilters = ko.computed(
128
			() => folderListOptionsBuilder(
129
				this.folderListSystem(), this.folderList(),
130
				['INBOX'], [['', '']], null, null, null, (item) => (item ? item.localName() : ''))
0 ignored issues
show
Bug introduced by
The variable item seems to be never initialized.
Loading history...
introduced by
This code is unreachable and can thus be removed without consequences.
Loading history...
131
		);
132
	}
133
134
	subscribers() {
135
		const
136
			fRemoveSystemFolderType = (observable) => () => {
137
				const folder = getFolderFromCacheList(observable());
138
				if (folder)
139
				{
140
					folder.type(FolderType.User);
141
				}
142
			},
143
			fSetSystemFolderType = (type) => (value) => {
144
				const folder = getFolderFromCacheList(value);
145
				if (folder)
146
				{
147
					folder.type(type);
148
				}
149
			};
150
151
		this.sentFolder.subscribe(fRemoveSystemFolderType(this.sentFolder), this, 'beforeChange');
152
		this.draftFolder.subscribe(fRemoveSystemFolderType(this.draftFolder), this, 'beforeChange');
153
		this.spamFolder.subscribe(fRemoveSystemFolderType(this.spamFolder), this, 'beforeChange');
154
		this.trashFolder.subscribe(fRemoveSystemFolderType(this.trashFolder), this, 'beforeChange');
155
		this.archiveFolder.subscribe(fRemoveSystemFolderType(this.archiveFolder), this, 'beforeChange');
156
157
		this.sentFolder.subscribe(fSetSystemFolderType(FolderType.SentItems), this);
158
		this.draftFolder.subscribe(fSetSystemFolderType(FolderType.Draft), this);
159
		this.spamFolder.subscribe(fSetSystemFolderType(FolderType.Spam), this);
160
		this.trashFolder.subscribe(fSetSystemFolderType(FolderType.Trash), this);
161
		this.archiveFolder.subscribe(fSetSystemFolderType(FolderType.Archive), this);
162
	}
163
164
	/**
165
	 * @returns {Array}
166
	 */
167
	getNextFolderNames() {
168
169
		const
170
			result = [],
171
			limit = 5,
172
			utc = require('Common/Momentor').momentNowUnix(),
173
			timeout = utc - 60 * 5,
174
			timeouts = [],
175
			inboxFolderName = getFolderInboxName(),
176
			fSearchFunction = (list) => {
177
				_.each(list, (folder) => {
178
					if (folder && inboxFolderName !== folder.fullNameRaw &&
179
						folder.selectable && folder.existen && timeout > folder.interval &&
180
						(folder.isSystemFolder() || (folder.subScribed() && folder.checkable()))
181
					)
182
					{
183
						timeouts.push([folder.interval, folder.fullNameRaw]);
184
					}
185
186
					if (folder && 0 < folder.subFolders().length)
187
					{
188
						fSearchFunction(folder.subFolders());
189
					}
190
				});
191
			};
192
193
		fSearchFunction(this.folderList());
194
195
		timeouts.sort((a, b) => {
196
			if (a[0] < b[0])
197
			{
198
				return -1;
199
			}
200
			else if (a[0] > b[0])
201
			{
202
				return 1;
203
			}
204
205
			return 0;
206
		});
207
208
		_.find(timeouts, (aItem) => {
209
			const folder = getFolderFromCacheList(aItem[1]);
210
			if (folder)
211
			{
212
				folder.interval = utc;
213
				result.push(aItem[1]);
214
			}
215
216
			return limit <= result.length;
217
		});
218
219
		return _.uniq(result);
220
	}
221
}
222
223
module.exports = new FolderUserStore();
224