-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileRenderer.js
193 lines (154 loc) · 5.37 KB
/
FileRenderer.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
var BookmarkFile = require('./BookmarkFile.js'),
DesktopFile = require('./DesktopFile.js'),
IconPathFetcher = require('./IconPathFetcher.js'),
execSync = require("child_process").execSync;
var folderIconMapping = {
'/home/leo': ['places', 'user-home'],
'/home/leo/Downloads': ['places', 'folder-download'],
'/home/leo/Videos': ['places', 'folder-videos'],
'/home/leo/Documents': ['places', 'folder-documents'],
'/home/leo/Projects': ['places', 'folder-templates'],
'/home/leo/Desktop': ['places', 'user-desktop'],
'/media/Share': ['devices', 'drive-harddisk']
};
var iconTheme = 'Flattr';
var iconDirectory = '/usr/share/icons/';
function FileRenderer( options ) {
var document = options.document,
folderIconMappingList = Object.keys(folderIconMapping),
iconPathFetcher = options.iconPathFetcher;
function getIconPath( category, size, iconName ) {
return iconDirectory + iconTheme + '/' + category + '/' + size + '/' + (iconName.replace(/\//g, '-')) + '.svg';
}
// function mimeTypeToPath( mimeType ) {
// return ;
// }
// function getFileTypeIconPath( file, size, callback ) {
// var iconName = '';
// function mimeCallback( iconName ) {
// if ( size )
// iconName = iconName.replace(/\/\d+\//, '/' + size + '/');
// callback( getIconPath( iconName ) );
// }
// file.isDirectory(function( err, isDir ) {
// if ( isDir ) {
// var mappedPathIndex = folderIconMappingList.indexOf(file.absolutePath);
// if ( mappedPathIndex === -1 ) {
// iconName = 'places/64/folder';
// } else {
// var mappedPath = folderIconMappingList[mappedPathIndex];
// var mapData = folderIconMapping[mappedPath]
// category = mapData[0];
// iconName = mapData[1];
// }
// mimeCallback(iconName);
// } else {
// getMimeTypeIconName( file.fileName, mimeCallback );
// }
// });
// }
this.renderShell = function( file ) {
if ( file instanceof BookmarkFile ) {
var fileElement = document.createElement('div');
fileElement.className = 'item file';
var iconElement = new window.Image();
var fileNameElement = document.createElement('p');
fileElement.appendChild(iconElement);
fileElement.appendChild(fileNameElement);
fileElement.obj = file;
file.element = fileElement;
file.iconElement = iconElement;
file.fileNameElement = fileNameElement;
return fileElement;
} else {
var fileElement = document.createElement('nde-file');
fileElement.className = 'item file';
fileElement.obj = file;
file.element = fileElement;
return fileElement;
}
};
var escapeShell = function(cmd) {
return '"'+cmd.replace(/(["\s'$`\\])/g,'\\$1')+'"';
};
function getDesktopFileIconName( path ) {
var command = 'grep -E ^Icon= ' + path.replace(/(["\s'$`\\])/g,'\\$1');
var matchedLine = execSync( command ) + '';
var iconName = matchedLine.replace(/^Icon=\s*/, '').trim();
if ( matchedLine === '' ) return null;
return iconName;
}
this.renderIcon = function(file, size) {
size = size || 64;
file.isDirectory(function(err, isDir) {
if ( ! isDir ) {
if ( file instanceof DesktopFile ) {
file.getIconPath(function( err, iconPath ) {
if ( err )
iconPath = '/usr/share/icons/Flattr/mimetypes/48/text-plain.svg';
file.element.setAttribute('icon', iconPath);
});
} else {
file.getMimeType(function( err, mimeType ){
if ( mimeType )
iconPathFetcher.getIconPath( mimeType.replace(/\//g, '-'), 48, function( err, iconPath ) {
if ( err )
iconPath = '/usr/share/icons/Flattr/mimetypes/48/text-plain.svg';
if ( file instanceof BookmarkFile ) {
file.iconElement.src = iconPath;
} else {
file.element.setAttribute('icon', iconPath);
}
});
});
}//END if Desktop file
} else {
file.getAbsolutePath(function( err, absolutePath ) {
var mappedPathIndex = folderIconMappingList.indexOf(absolutePath),
iconName,
iconCategory;
if ( mappedPathIndex === -1 ) {
iconName = 'folder';
iconCategory = 'places';
} else {
var mappedPath = folderIconMappingList[mappedPathIndex];
var mapData = folderIconMapping[mappedPath]
iconCategory = mapData[0];
iconName = mapData[1];
}
iconPathFetcher.getIconPath( iconName.replace(/\//g, '-'), size, function( err, iconPath ) {
if ( err )
iconPath = '/usr/share/icons/Flattr/mimetypes/48/text-plain.svg';
if ( file instanceof BookmarkFile ) {
file.iconElement.src = iconPath;
} else {
file.element.setAttribute('icon', iconPath);
}
});
});
}
});
};
this.renderFileName = function(file) {
file.getDisplayName(function( err, displayName ){
if ( file instanceof BookmarkFile ) {
file.fileNameElement.textContent = displayName;
} else {
file.element.setAttribute( 'name', displayName );
}
});
};
this.render = function( file, callback, iconSize ) {
var fileElement = this.renderShell(file);
callback(fileElement);
this.renderFileName(file);
this.renderIcon(file, iconSize);
}.bind(this);
this.renderInline = function( file, callback ) {
this.render( file, function( element ){
element.classList.add('inline-item');
callback(element);
}, 16);
}.bind(this);
}
module.exports = FileRenderer;