-
Notifications
You must be signed in to change notification settings - Fork 6
/
userInterface.js
136 lines (110 loc) · 3.46 KB
/
userInterface.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'use strict';
// Dependencies
//
var path = require('path');
// Insert's the current folder
// into the current folder UI element
//
// @param folderPath String The path of the folder
//
function updateCurrentFolder (folderPath) {
window.$('#current-folder').html(convertFolderPathIntoLinks(folderPath));
}
// Converts a string into a collection of links
//
// @param folderPath String The path of the folder
//
function convertFolderPathIntoLinks (folderPath) {
var folders = folderPath.split(path.sep);
var contents = [];
var pathAtFolder = '';
folders.forEach(function (folder) {
pathAtFolder += folder + path.sep;
contents.push('<span class="path" data-path="' + pathAtFolder.slice(0,-1) + '">' + folder + '</span>');
});
return contents.join(path.sep).toString();
}
// Appends the file to the main area in the app
//
// @param file {Object} The file
//
function addFileToMainArea (file) {
var mainArea = window.document.getElementById('main-area');
var template = window.document.querySelector('#item-template');
template.content.querySelector('img').src = 'images/' + file.type + '.svg';
template.content.querySelector('img').className = 'icon';
template.content.querySelector('img').classList.add(file.type);
template.content.querySelector('img').setAttribute('data-filePath',file.path);
template.content.querySelector('.filename').innerText = file.file;
var clone = window.document.importNode(template.content, true);
mainArea.appendChild(clone);
}
// Makes the folders clickable
//
// @param cb {Function} The function to execute when the double-click happens
//
function makeFoldersClickable (cb) {
window.$('.directory').on('dblclick', cb);
}
// Makes the files clickable
//
// @param cb {Function} The function to execute when the double-click happens
//
function makeFilesClickable (cb) {
window.$('.file').on('dblclick', cb);
}
// Clears the main area of any content
//
function clearMainArea () {
window.$('#main-area').html('');
}
// Attaches to the search field in the toolbar
//
// @param cb {Function} The function to execute when the key is pressed
//
function bindSearchField (cb) {
window.$('#search').on('keyup', cb);
}
// Binds the clickable folders in the current folder path area
//
// @param cb {Function} The function to execute on the folder path
//
function bindCurrentFolderPath (cb) {
window.$('span.path').on('click', function (event) {
var folderPath = window.$(event.target).attr('data-path');
cb(folderPath);
});
}
// Filters the results in the main area
//
// @param results {Array} The list of results
//
function filterResults (results) {
var validFilePaths = results.map(function (result) { return result.ref; });
window.$('.item').each(function (index, item) {
var filePath = window.$(window.$(item).find('img')).attr('data-filePath');
if (validFilePaths.indexOf(filePath) !== -1) {
window.$(item).show();
} else {
window.$(item).hide();
}
});
}
// Resets the display of the files in the main area
//
function resetFilter () {
window.$('.item').show();
}
// Expose the functions as the public API
//
module.exports = {
addFileToMainArea : addFileToMainArea,
updateCurrentFolder : updateCurrentFolder,
makeFoldersClickable : makeFoldersClickable,
makeFilesClickable : makeFilesClickable,
clearMainArea : clearMainArea,
bindSearchField : bindSearchField,
bindCurrentFolderPath : bindCurrentFolderPath,
filterResults : filterResults,
resetFilter : resetFilter
};