~cdparra/gelee/trunk

« back to all changes in this revision

Viewing changes to webui/ecosystem/workspace/extjs/docs/source/Importer.html

  • Committer: parra
  • Date: 2010-03-15 15:56:56 UTC
  • Revision ID: svn-v4:ac5bba68-f036-4e09-846e-8f32731cc928:trunk/gelee:1448
merged gelee at svn

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<html>
 
2
<head>
 
3
  <title>The source code</title>
 
4
    <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
 
5
    <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
 
6
</head>
 
7
<body  onload="prettyPrint();">
 
8
    <pre class="prettyprint lang-js">tx.Importer = function(){
 
9
        function chooseFile(callback){
 
10
                var file = new air.File(air.File.documentsDirectory.nativePath);
 
11
                var filter = new air.FileFilter("Tasks XML File", "*.xml");
 
12
                
 
13
                file.addEventListener('select', function(e){
 
14
                        doImport(e.target, callback);
 
15
                });
 
16
                
 
17
                file.browseForOpen('Open', [filter]);
 
18
        }
 
19
        
 
20
        
 
21
        /*
 
22
         * This import function used to be clean and simple. The addition of asynchronous import and
 
23
         * a progress bar changed that quickly. :) 
 
24
         */
 
25
        function doImport(file, callback){
 
26
                
 
27
                Ext.Msg.progress('Import', 'Reading file...');
 
28
                
 
29
                var listTable = tx.data.conn.getTable('list', 'listId');
 
30
                var taskTable = tx.data.conn.getTable('task', 'taskId');
 
31
                var taskCount = 0;
 
32
                var visibleIndex = 0;
 
33
                var doUpdate = true;
 
34
                var f = String.format;
 
35
                
 
36
                function getProgress(index){
 
37
                        if(taskCount > 0){
 
38
                                return (.8 * index) / taskCount;
 
39
                        }else{
 
40
                                return .8;
 
41
                        }
 
42
                }
 
43
                
 
44
                function readFile(){
 
45
                        var stream = new air.FileStream();
 
46
                        stream.open(file, air.FileMode.READ);
 
47
                        var xml = stream.readUTFBytes(stream.bytesAvailable);
 
48
                        stream.close();
 
49
                                        
 
50
                        Ext.Msg.updateProgress(.1, 'Parsing...');
 
51
                        parse.defer(10, null, [xml]);
 
52
                }
 
53
                
 
54
                function parse(xml){
 
55
                        try {
 
56
                                var doc = new runtime.flash.xml.XMLDocument();
 
57
                                doc.ignoreWhite = true;
 
58
                                doc.parseXML(xml);
 
59
                                
 
60
                                var lists = doc.firstChild.childNodes;
 
61
                                var count = lists.length;
 
62
                                
 
63
                                for (var i = 0; i < count; i++) {
 
64
                                        taskCount += lists[i].childNodes.length;
 
65
                                }       
 
66
                                Ext.Msg.updateProgress(.15, '', 'Loading Tasks...');
 
67
                                loadLists(lists, 0);
 
68
                        }catch(e){
 
69
                                air.trace(e);
 
70
                                alert('An error occured while trying to import the selected file.');
 
71
                        }                       
 
72
                }
 
73
                
 
74
                function loadLists(lists, index){
 
75
                        if(index < lists.length){
 
76
                                var list = lists[index];
 
77
                                listTable.save(list.attributes);
 
78
                                nextTask(list.childNodes, 0, lists, index);
 
79
                        }
 
80
                        else {
 
81
                                Ext.Msg.updateProgress(1, '', 'Completing import...');
 
82
                                callback.defer(10);
 
83
                        }                               
 
84
                }               
 
85
                
 
86
                function nextTask(tasks, index, lists, listIndex){
 
87
                        if(index < tasks.length){
 
88
                                Ext.Msg.updateProgress(
 
89
                                        getProgress(++visibleIndex),
 
90
                                        f('{0} of {1}', visibleIndex, taskCount)
 
91
                                );
 
92
                                loadTasks.defer(250, window, [tasks, index, lists, listIndex]);
 
93
                        }
 
94
                        else {
 
95
                                loadLists(lists, ++listIndex);
 
96
                        }
 
97
                }
 
98
                
 
99
                function loadTasks(tasks, index, lists, listIndex){
 
100
                        taskTable.save(tasks[index].attributes);
 
101
                        nextTask(tasks, ++index, lists, listIndex);
 
102
                }
 
103
                
 
104
                readFile.defer(10);
 
105
        }
 
106
        
 
107
        this.doImport = function(callback){
 
108
                chooseFile(callback);
 
109
        }
 
110
};
 
111
</pre>    
 
112
</body>
 
113
</html>
 
 
b'\\ No newline at end of file'