921b48660d071eef33b781d7abff6badf1df379c
[test/tct/web/api.git] /
1 cordova.define("cordova-plugin-file.DirectoryReader", function(require, exports, module) { /*
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20 */
21
22 var exec = require('cordova/exec'),
23     FileError = require('./FileError') ;
24
25 /**
26  * An interface that lists the files and directories in a directory.
27  */
28 function DirectoryReader(localURL) {
29     this.localURL = localURL || null;
30     this.hasReadEntries = false;
31 }
32
33 /**
34  * Returns a list of entries from a directory.
35  *
36  * @param {Function} successCallback is called with a list of entries
37  * @param {Function} errorCallback is called with a FileError
38  */
39 DirectoryReader.prototype.readEntries = function(successCallback, errorCallback) {
40     // If we've already read and passed on this directory's entries, return an empty list.
41     if (this.hasReadEntries) {
42         successCallback([]);
43         return;
44     }
45     var reader = this;
46     var win = typeof successCallback !== 'function' ? null : function(result) {
47         var retVal = [];
48         for (var i=0; i<result.length; i++) {
49             var entry = null;
50             if (result[i].isDirectory) {
51                 entry = new (require('./DirectoryEntry'))();
52             }
53             else if (result[i].isFile) {
54                 entry = new (require('./FileEntry'))();
55             }
56             entry.isDirectory = result[i].isDirectory;
57             entry.isFile = result[i].isFile;
58             entry.name = result[i].name;
59             entry.fullPath = result[i].fullPath;
60             entry.filesystem = new (require('./FileSystem'))(result[i].filesystemName);
61             entry.nativeURL = result[i].nativeURL;
62             retVal.push(entry);
63         }
64         reader.hasReadEntries = true;
65         successCallback(retVal);
66     };
67     var fail = typeof errorCallback !== 'function' ? null : function(code) {
68         errorCallback(new FileError(code));
69     };
70     exec(win, fail, "File", "readEntries", [this.localURL]);
71 };
72
73 module.exports = DirectoryReader;
74
75 });