Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(997)

Side by Side Diff: chrome/browser/resources/file_manager/common/js/path_util.js

Issue 148193002: Removed most of path utility functions in Files app. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased. Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 'use strict'; 5 'use strict';
6 6
7 /** 7 /**
8 * Type of a root directory. 8 * Type of a root directory.
9 * @enum {string} 9 * @enum {string}
10 * @const 10 * @const
(...skipping 21 matching lines...) Expand all
32 // Fake root for offline available files on the drive. 32 // Fake root for offline available files on the drive.
33 DRIVE_OFFLINE: 'drive_offline', 33 DRIVE_OFFLINE: 'drive_offline',
34 34
35 // Fake root for shared files on the drive. 35 // Fake root for shared files on the drive.
36 DRIVE_SHARED_WITH_ME: 'drive_shared_with_me', 36 DRIVE_SHARED_WITH_ME: 'drive_shared_with_me',
37 37
38 // Fake root for recent files on the drive. 38 // Fake root for recent files on the drive.
39 DRIVE_RECENT: 'drive_recent' 39 DRIVE_RECENT: 'drive_recent'
40 }); 40 });
41 41
42 /**
43 * Top directory for each root type.
44 * TODO(mtomasz): Deprecated. Remove this.
45 * @enum {string}
46 * @const
47 */
48 var RootDirectory = Object.freeze({
49 DOWNLOADS: '/Downloads',
50 ARCHIVE: '/archive',
51 REMOVABLE: '/removable',
52 DRIVE: '/drive',
53 CLOUD_DEVICE: '/privet',
54 DRIVE_OFFLINE: '/drive_offline', // A fake root. Not the actual filesystem.
55 DRIVE_SHARED_WITH_ME: '/drive_shared_with_me', // A fake root.
56 DRIVE_RECENT: '/drive_recent' // A fake root.
57 });
58
59 var PathUtil = {}; 42 var PathUtil = {};
60 43
61 /** 44 /**
62 * Checks if the given path represents a special search. Fake entries in
63 * RootDirectory correspond to special searches.
64 * @param {string} path Path to check.
65 * @return {boolean} True if the given path represents a special search.
66 */
67 PathUtil.isSpecialSearchRoot = function(path) {
68 var type = PathUtil.getRootType(path);
69 return type == RootType.DRIVE_OFFLINE ||
70 type == RootType.DRIVE_SHARED_WITH_ME ||
71 type == RootType.DRIVE_RECENT;
72 };
73
74 /**
75 * Checks |path| and return true if it is under Google Drive or a special
76 * search root which represents a special search from Google Drive.
77 * @param {string} path Path to check.
78 * @return {boolean} True if the given path represents a Drive based path.
79 */
80 PathUtil.isDriveBasedPath = function(path) {
81 var rootType = PathUtil.getRootType(path);
82 return rootType === RootType.DRIVE ||
83 rootType === RootType.DRIVE_SHARED_WITH_ME ||
84 rootType === RootType.DRIVE_RECENT ||
85 rootType === RootType.DRIVE_OFFLINE;
86 };
87
88 /**
89 * @param {string} path Path starting with '/'.
90 * @return {string} Top directory (starting with '/').
91 */
92 PathUtil.getTopDirectory = function(path) {
93 var i = path.indexOf('/', 1);
94 return i === -1 ? path : path.substring(0, i);
95 };
96
97 /**
98 * Obtains the parent path of the specified path.
99 * @param {string} path Path string.
100 * @return {string} Parent path.
101 */
102 PathUtil.getParentDirectory = function(path) {
103 if (path[path.length - 1] == '/')
104 return PathUtil.getParentDirectory(path.substring(0, path.length - 1));
105 var index = path.lastIndexOf('/');
106 if (index == 0)
107 return '/';
108 else if (index == -1)
109 return '.';
110 return path.substring(0, index);
111 };
112
113 /**
114 * @param {string} path Any unix-style path (may start or not start from root).
115 * @return {Array.<string>} Path components.
116 */
117 PathUtil.split = function(path) {
118 var fromRoot = false;
119 if (path[0] === '/') {
120 fromRoot = true;
121 path = path.substring(1);
122 }
123
124 var components = path.split('/');
125 if (fromRoot)
126 components[0] = '/' + components[0];
127 return components;
128 };
129
130 /**
131 * Returns a directory part of the given |path|. In other words, the path
132 * without its base name.
133 *
134 * Examples:
135 * PathUtil.dirname('abc') -> ''
136 * PathUtil.dirname('a/b') -> 'a'
137 * PathUtil.dirname('a/b/') -> 'a/b'
138 * PathUtil.dirname('a/b/c') -> 'a/b'
139 * PathUtil.dirname('/') -> '/'
140 * PathUtil.dirname('/abc') -> '/'
141 * PathUtil.dirname('/abc/def') -> '/abc'
142 * PathUtil.dirname('') -> ''
143 *
144 * @param {string} path The path to be parsed.
145 * @return {string} The directory path.
146 */
147 PathUtil.dirname = function(path) {
148 var index = path.lastIndexOf('/');
149 if (index < 0)
150 return '';
151 if (index == 0)
152 return '/';
153 return path.substring(0, index);
154 };
155
156 /**
157 * Returns the base name (the last component) of the given |path|. If the
158 * |path| ends with '/', returns an empty component.
159 *
160 * Examples:
161 * PathUtil.basename('abc') -> 'abc'
162 * PathUtil.basename('a/b') -> 'b'
163 * PathUtil.basename('a/b/') -> ''
164 * PathUtil.basename('a/b/c') -> 'c'
165 * PathUtil.basename('/') -> ''
166 * PathUtil.basename('/abc') -> 'abc'
167 * PathUtil.basename('/abc/def') -> 'def'
168 * PathUtil.basename('') -> ''
169 *
170 * @param {string} path The path to be parsed.
171 * @return {string} The base name.
172 */
173 PathUtil.basename = function(path) {
174 var index = path.lastIndexOf('/');
175 return index >= 0 ? path.substring(index + 1) : path;
176 };
177
178 /**
179 * Join path components into a single path. Can be called either with a list of
180 * components as arguments, or with an array of components as the only argument.
181 *
182 * Examples:
183 * Path.join('abc', 'def') -> 'abc/def'
184 * Path.join('/', 'abc', 'def/ghi') -> '/abc/def/ghi'
185 * Path.join(['/abc/def', 'ghi']) -> '/abc/def/ghi'
186 *
187 * @return {string} Resulting path.
188 */
189 PathUtil.join = function() {
190 var components;
191
192 if (arguments.length === 1 && typeof(arguments[0]) === 'object') {
193 components = arguments[0];
194 } else {
195 components = arguments;
196 }
197
198 var path = '';
199 for (var i = 0; i < components.length; i++) {
200 if (components[i][0] === '/') {
201 path = components[i];
202 continue;
203 }
204 if (path.length === 0 || path[path.length - 1] !== '/')
205 path += '/';
206 path += components[i];
207 }
208 return path;
209 };
210
211 /**
212 * @param {string} path Path starting with '/'.
213 * @return {RootType} RootType.DOWNLOADS, RootType.DRIVE etc.
214 */
215 PathUtil.getRootType = function(path) {
216 var rootDir = PathUtil.getTopDirectory(path);
217 for (var type in RootDirectory) {
218 if (rootDir === RootDirectory[type])
219 return RootType[type];
220 }
221 };
222
223 /**
224 * @param {string} path Any path.
225 * @return {string} The root path.
226 */
227 PathUtil.getRootPath = function(path) {
228 var type = PathUtil.getRootType(path);
229
230 if (type == RootType.DOWNLOADS || type == RootType.DRIVE_OFFLINE ||
231 type == RootType.DRIVE_SHARED_WITH_ME || type == RootType.DRIVE_RECENT)
232 return PathUtil.getTopDirectory(path);
233
234 if (type == RootType.DRIVE || type == RootType.ARCHIVE ||
235 type == RootType.REMOVABLE) {
236 var components = PathUtil.split(path);
237 if (components.length > 1) {
238 return PathUtil.join(components[0], components[1]);
239 } else {
240 return components[0];
241 }
242 }
243
244 return '/';
245 };
246
247 /**
248 * @param {string} path A path.
249 * @return {boolean} True if it is a path to the root.
250 */
251 PathUtil.isRootPath = function(path) {
252 return PathUtil.getRootPath(path) === path;
253 };
254
255 /**
256 * Returns the localized name for the root type. If not available, then returns 45 * Returns the localized name for the root type. If not available, then returns
257 * null. 46 * null.
258 * 47 *
259 * @param {RootType} rootType The root type. 48 * @param {RootType} rootType The root type.
260 * @return {?string} The localized name, or null if not available. 49 * @return {?string} The localized name, or null if not available.
261 */ 50 */
262 PathUtil.getRootTypeLabel = function(rootType) { 51 PathUtil.getRootTypeLabel = function(rootType) {
263 var str = function(id) { 52 var str = function(id) {
264 return loadTimeData.getString(id); 53 return loadTimeData.getString(id);
265 }; 54 };
(...skipping 30 matching lines...) Expand all
296 */ 85 */
297 PathUtil.splitExtension = function(path) { 86 PathUtil.splitExtension = function(path) {
298 var dotPosition = path.lastIndexOf('.'); 87 var dotPosition = path.lastIndexOf('.');
299 if (dotPosition <= path.lastIndexOf('/')) 88 if (dotPosition <= path.lastIndexOf('/'))
300 dotPosition = -1; 89 dotPosition = -1;
301 90
302 var filename = dotPosition != -1 ? path.substr(0, dotPosition) : path; 91 var filename = dotPosition != -1 ? path.substr(0, dotPosition) : path;
303 var extension = dotPosition != -1 ? path.substr(dotPosition) : ''; 92 var extension = dotPosition != -1 ? path.substr(dotPosition) : '';
304 return [filename, extension]; 93 return [filename, extension];
305 }; 94 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698