0
|
1 var LoaderBase = function(){};
|
|
2 LoaderBase.prototype = {
|
|
3 init: function(loadingBox, loadingMessage, database, callback) {
|
|
4 this.loadingBox = loadingBox;
|
|
5 this.loadingMessage = loadingMessage;
|
|
6 this.database = database;
|
|
7 this.callback = callback || function() {};
|
|
8
|
|
9 try {
|
|
10 this.insertHandler = new UpdateHandler(this.database, this.insertSql);
|
|
11 } catch(e) {
|
|
12 this.error(e.message || e);
|
|
13 throw e;
|
|
14 }
|
|
15 this.load();
|
|
16 },
|
|
17 insertSql: "INSERT INTO bookmark VALUES(:id, :url, :title, :info, :tags, :time, UPPER(:title||' '||:info||' '||:tags))",
|
|
18 truncateSql: 'DELETE FROM bookmark',
|
|
19
|
|
20 load: function() {
|
|
21 this.dispStart();
|
|
22
|
|
23 this.bookmarks = [];
|
|
24 this.total = null;
|
|
25
|
|
26 this._load();
|
|
27 },
|
|
28 dispStart: function() {
|
|
29 Glayer.showBox(this.loadingBox);
|
|
30 this.loadingMessage.innerHTML = 'Bookmarks Loading...';
|
|
31 },
|
|
32 dispLoading: function(count) {
|
|
33 this.loadingMessage.innerHTML = 'Bookmarks Loading... ' + count + '/' + this.total;
|
|
34 },
|
|
35 dispEnd: function(count) {
|
|
36 this.loadingBox.style.display = 'none';
|
|
37 Glayer.showAlert('Finish!! loaded ' + count + ' bookmarks', {callback: function(){ incsearch.input.focus(); Glayer.hideAlert(); }});
|
|
38 document.getElementById(Glayer.defaultAlert.okId).focus();
|
|
39
|
|
40 incsearch.reset();
|
|
41 },
|
|
42 error: function(errMsg) {
|
|
43 this.loadingBox.style.display = 'none';
|
|
44
|
|
45 var self = this;
|
|
46 Glayer.showConfirm(
|
|
47 errMsg,
|
|
48 function(result){
|
|
49 Glayer.hideConfirm();
|
|
50 if (result) {
|
|
51 self.dispStart();
|
|
52 self._load();
|
|
53 } else {
|
|
54 incsearch.input.focus();
|
|
55 }
|
|
56 },
|
|
57 {okLabel: 'Retry', cancelLabel: 'cancel'}
|
|
58 );
|
|
59 document.getElementById(Glayer.defaultConfirm.cancelId).focus();
|
|
60
|
|
61 throw errMsg;
|
|
62 },
|
|
63
|
|
64 truncate: function() {
|
|
65 this.database.execute(this.truncateSql);
|
|
66 },
|
|
67 insert: function(bookmark) {
|
|
68 this.insertHandler.execute(bookmark);
|
|
69 },
|
|
70
|
|
71 update: function(bookmarks) {
|
|
72
|
|
73 var self = this;
|
|
74
|
|
75 var conn = this.database.connection;
|
|
76 try {
|
|
77 conn.beginTransaction();
|
|
78 this.truncate();
|
|
79
|
|
80 for (var i = 0, len = bookmarks.length; i < len; i++) {
|
|
81 if (i % 100 == 0) yield i;
|
|
82 this.insert(bookmarks[i]);
|
|
83 }
|
|
84
|
|
85 conn.commitTransaction();
|
|
86 } catch(e) {
|
|
87 conn.rollbackTransaction();
|
|
88
|
|
89 this.error(e.message || e);
|
|
90 throw e;
|
|
91 }
|
|
92 }
|
|
93 }
|
|
94
|
|
95
|
|
96 var Executer = function(generator, interval, func, callback) {
|
|
97 this.generator = generator;
|
|
98 this.func = func || function(){};
|
|
99 this.interval = interval;
|
|
100 this.callback = callback;
|
|
101
|
|
102 var self = this;
|
|
103 this.run = function() {
|
|
104 try{
|
|
105 self.func(self.generator.next());
|
|
106 setTimeout(function(){self.run()}, self.interval);
|
|
107 } catch (e) {
|
|
108 if (e instanceof StopIteration) {
|
|
109 self.callback();
|
|
110 } else {
|
|
111 throw e;
|
|
112 }
|
|
113 }
|
|
114 }
|
|
115 };
|
|
116
|