~dongpo-deng/sahana-eden/test

« back to all changes in this revision

Viewing changes to static/scripts/ext-2.2.1/air/samples/tasks/js/Importer.js

  • Committer: Deng Dongpo
  • Date: 2010-08-01 09:29:44 UTC
  • Revision ID: dongpo@dhcp-21193.iis.sinica.edu.tw-20100801092944-8t9obt4xtl7otesb
initial

Show diffs side-by-side

added added

removed removed

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