3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js">tx.Importer = function(){
\r
9 function chooseFile(callback){
\r
10 var file = new air.File(air.File.documentsDirectory.nativePath);
\r
11 var filter = new air.FileFilter("Tasks XML File", "*.xml");
\r
13 file.addEventListener('select', function(e){
\r
14 doImport(e.target, callback);
\r
17 file.browseForOpen('Open', [filter]);
\r
22 * This import function used to be clean and simple. The addition of asynchronous import and
\r
23 * a progress bar changed that quickly. :)
\r
25 function doImport(file, callback){
\r
27 Ext.Msg.progress('Import', 'Reading file...');
\r
29 var listTable = tx.data.conn.getTable('list', 'listId');
\r
30 var taskTable = tx.data.conn.getTable('task', 'taskId');
\r
32 var visibleIndex = 0;
\r
33 var doUpdate = true;
\r
34 var f = String.format;
\r
36 function getProgress(index){
\r
38 return (.8 * index) / taskCount;
\r
44 function readFile(){
\r
45 var stream = new air.FileStream();
\r
46 stream.open(file, air.FileMode.READ);
\r
47 var xml = stream.readUTFBytes(stream.bytesAvailable);
\r
50 Ext.Msg.updateProgress(.1, 'Parsing...');
\r
51 parse.defer(10, null, [xml]);
\r
54 function parse(xml){
\r
56 var doc = new runtime.flash.xml.XMLDocument();
\r
57 doc.ignoreWhite = true;
\r
60 var lists = doc.firstChild.childNodes;
\r
61 var count = lists.length;
\r
63 for (var i = 0; i < count; i++) {
\r
64 taskCount += lists[i].childNodes.length;
\r
66 Ext.Msg.updateProgress(.15, '', 'Loading Tasks...');
\r
67 loadLists(lists, 0);
\r
70 alert('An error occured while trying to import the selected file.');
\r
74 function loadLists(lists, index){
\r
75 if(index < lists.length){
\r
76 var list = lists[index];
\r
77 listTable.save(list.attributes);
\r
78 nextTask(list.childNodes, 0, lists, index);
\r
81 Ext.Msg.updateProgress(1, '', 'Completing import...');
\r
86 function nextTask(tasks, index, lists, listIndex){
\r
87 if(index < tasks.length){
\r
88 Ext.Msg.updateProgress(
\r
89 getProgress(++visibleIndex),
\r
90 f('{0} of {1}', visibleIndex, taskCount)
\r
92 loadTasks.defer(250, window, [tasks, index, lists, listIndex]);
\r
95 loadLists(lists, ++listIndex);
\r
99 function loadTasks(tasks, index, lists, listIndex){
\r
100 taskTable.save(tasks[index].attributes);
\r
101 nextTask(tasks, ++index, lists, listIndex);
\r
104 readFile.defer(10);
\r
107 this.doImport = function(callback){
\r
108 chooseFile(callback);
\r