OLD | NEW |
---|---|
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 /** | 5 /** |
6 * Namespace for utility functions. | 6 * Namespace for utility functions. |
7 */ | 7 */ |
8 var util = { | 8 var util = { |
9 /** | 9 /** |
10 * Returns a function that console.log's its arguments, prefixed by |msg|. | 10 * Returns a function that console.log's its arguments, prefixed by |msg|. |
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
507 (event.shiftKey ? 'Shift-' : '') + | 507 (event.shiftKey ? 'Shift-' : '') + |
508 (event.metaKey ? 'Meta-' : ''); | 508 (event.metaKey ? 'Meta-' : ''); |
509 }, | 509 }, |
510 | 510 |
511 /** | 511 /** |
512 * A wrapper for navigator.onLine that allows for easy debug override. | 512 * A wrapper for navigator.onLine that allows for easy debug override. |
513 * @return {boolean} True if offline. | 513 * @return {boolean} True if offline. |
514 */ | 514 */ |
515 isOffline: function() { | 515 isOffline: function() { |
516 return !navigator.onLine; | 516 return !navigator.onLine; |
517 }, | |
518 | |
519 /* | |
520 * Tests if |path| references special, internaly used directory in which | |
521 * creating new entries is not allowed. | |
522 * Currently, only paths used for gdata content search match this description | |
523 * (gdata content search root directory and directories that contain gdata | |
524 * content search results). | |
525 * | |
526 * @param {string} path Path which is being tested. | |
527 * @return {boolean} Test result. | |
528 */ | |
529 isSpecialReadonlyDirectory: function(path) { | |
530 // If the path is not search root or it's child, we're fine. | |
531 if (path.search(util.GDATA_SEARCH_ROOT_PATH) != 0 && | |
532 path + '/' != util.GDATA_SEARCH_ROOT_PATH) { | |
533 return false; | |
534 } | |
535 | |
536 var pathComponents = path.split('/'); | |
537 | |
538 // We should not create entries on path if it's either gdata search root, | |
539 // or its immediate child. | |
540 var lengthDifference = | |
541 pathComponents.length - util.GDATA_SEARCH_ROOT_COMPONENTS.length; | |
542 return lengthDifference == 0 || lengthDifference == 1; | |
543 }, | |
544 | |
545 /** | |
546 * Checks if the provided path is under gdata search. | |
547 * | |
548 * @param {string} path Path to be tested. | |
549 * @return {boolean} Is the path gdata search path. | |
550 */ | |
551 isGDataSearchPath: function(path) { | |
SeRya
2012/05/10 08:44:16
Most of code for analyzing file paths placed into
tbarzic
2012/05/10 23:28:02
Done.
| |
552 return path == util.GDATA_SEARCH_ROOT_PATH || | |
553 path.search(util.GDATA_SEARCH_ROOT_PATH + '/') == 0; | |
554 }, | |
555 | |
556 /* | |
557 * Root path used for displaying gdata content search results. | |
558 * Search results will be shown in directory 'GDATA_SEARCH_ROOT_PATH/query'. | |
559 * | |
560 * @const | |
561 * @type {string} | |
562 */ | |
563 GDATA_SEARCH_ROOT_PATH: '/gdata/.search', | |
564 | |
565 /* | |
566 * @const | |
567 * @type {Array.<string>} | |
568 */ | |
569 GDATA_SEARCH_ROOT_COMPONENTS: ['', 'gdata', '.search'], | |
570 | |
571 /* | |
572 * Creates directory path in which gdata content search results for |query| | |
573 * should be displayed. | |
574 * | |
575 * @param {string} query Search query. | |
576 * @return {string} Virtual directory path for search results. | |
577 */ | |
578 createGDataSearchPath: function(query) { | |
579 return util.GDATA_SEARCH_ROOT_PATH + '/' + query; | |
580 }, | |
581 | |
582 /* | |
583 * Tests if the given path is a gdata search result path, and if it is, | |
584 * returns file's fileName in virtual search file system, its gdata resourceId | |
585 * and the display name that should be used when the file is shown in file | |
586 * browser. | |
587 * | |
588 * @param {string} path The potential gdata search result path. | |
589 * @return {object.<string, stringi, string>} Object that will contain file's | |
590 * fileName, displayName and resourceId; or null if the path is not gdata | |
591 * search result path. | |
592 */ | |
593 getFileAndDisplayNameForGDataSearchResult: function(path) { | |
594 // Nothing to do if the path is not under gdata search root path. | |
595 if (path.search(util.GDATA_SEARCH_ROOT_PATH) != 0) | |
596 return null; | |
597 | |
598 var pathComponents = path.split('/'); | |
599 | |
600 // Search result should be formatted like: | |
601 // gdataSearchRoot/query/result | |
602 if (pathComponents.length != util.GDATA_SEARCH_ROOT_COMPONENTS.length + 2) | |
603 return null; | |
604 for (var i = 0; i < util.GDATA_SEARCH_ROOT_COMPONENTS.length; i++) { | |
605 if (pathComponents[i] != util.GDATA_SEARCH_ROOT_COMPONENTS[i]) | |
606 return null; | |
607 } | |
608 | |
609 // Search result file name should be formatted like: | |
610 // resource_id.referenced_file_name | |
611 // We should display referenced file name only. | |
612 var result = {}; | |
613 result.fileName = pathComponents.pop(); | |
614 result.displayName = | |
615 result.fileName.slice(result.fileName.indexOf('.') + 1); | |
616 result.resourceId = | |
617 result.fileName.substr(0, result.fileName.indexOf('.')); | |
618 | |
619 if (result.fileName.length > 0 && result.displayName.length > 0) { | |
620 return result; | |
621 } else { | |
622 return null; | |
623 } | |
517 } | 624 } |
518 }; | 625 }; |
OLD | NEW |