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>
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");
13
file.addEventListener('select', function(e){
14
doImport(e.target, callback);
17
file.browseForOpen('Open', [filter]);
22
* This import function used to be clean and simple. The addition of asynchronous import and
23
* a progress bar changed that quickly. :)
25
function doImport(file, callback){
27
Ext.Msg.progress('Import', 'Reading file...');
29
var listTable = tx.data.conn.getTable('list', 'listId');
30
var taskTable = tx.data.conn.getTable('task', 'taskId');
34
var f = String.format;
36
function getProgress(index){
38
return (.8 * index) / taskCount;
45
var stream = new air.FileStream();
46
stream.open(file, air.FileMode.READ);
47
var xml = stream.readUTFBytes(stream.bytesAvailable);
50
Ext.Msg.updateProgress(.1, 'Parsing...');
51
parse.defer(10, null, [xml]);
56
var doc = new runtime.flash.xml.XMLDocument();
57
doc.ignoreWhite = true;
60
var lists = doc.firstChild.childNodes;
61
var count = lists.length;
63
for (var i = 0; i < count; i++) {
64
taskCount += lists[i].childNodes.length;
66
Ext.Msg.updateProgress(.15, '', 'Loading Tasks...');
70
alert('An error occured while trying to import the selected file.');
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);
81
Ext.Msg.updateProgress(1, '', 'Completing import...');
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)
92
loadTasks.defer(250, window, [tasks, index, lists, listIndex]);
95
loadLists(lists, ++listIndex);
99
function loadTasks(tasks, index, lists, listIndex){
100
taskTable.save(tasks[index].attributes);
101
nextTask(tasks, ++index, lists, listIndex);
107
this.doImport = function(callback){
108
chooseFile(callback);
b'\\ No newline at end of file'