3
* Copyright(c) 2006-2009, Ext JS, LLC.
6
* http://extjs.com/license
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");
14
file.addEventListener('select', function(e){
15
doImport(e.target, callback);
18
file.browseForOpen('Open', [filter]);
23
* This import function used to be clean and simple. The addition of asynchronous import and
24
* a progress bar changed that quickly. :)
26
function doImport(file, callback){
28
Ext.Msg.progress('Import', 'Reading file...');
30
var listTable = tx.data.conn.getTable('list', 'listId');
31
var taskTable = tx.data.conn.getTable('task', 'taskId');
35
var f = String.format;
37
function getProgress(index){
39
return (.8 * index) / taskCount;
46
var stream = new air.FileStream();
47
stream.open(file, air.FileMode.READ);
48
var xml = stream.readUTFBytes(stream.bytesAvailable);
51
Ext.Msg.updateProgress(.1, 'Parsing...');
52
parse.defer(10, null, [xml]);
57
var doc = new runtime.flash.xml.XMLDocument();
58
doc.ignoreWhite = true;
61
var lists = doc.firstChild.childNodes;
62
var count = lists.length;
64
for (var i = 0; i < count; i++) {
65
taskCount += lists[i].childNodes.length;
67
Ext.Msg.updateProgress(.15, '', 'Loading Tasks...');
71
alert('An error occured while trying to import the selected file.');
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);
82
Ext.Msg.updateProgress(1, '', 'Completing import...');
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)
93
loadTasks.defer(250, window, [tasks, index, lists, listIndex]);
96
loadLists(lists, ++listIndex);
100
function loadTasks(tasks, index, lists, listIndex){
101
taskTable.save(tasks[index].attributes);
102
nextTask(tasks, ++index, lists, listIndex);
108
this.doImport = function(callback){
109
chooseFile(callback);