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';
|
5
|
37 // Glayer.showAlert('Finish!! loaded ' + count + ' bookmarks', {callback: function(){ incsearch.input.focus(); Glayer.hideAlert(); }});
|
|
38 // document.getElementById(Glayer.defaultAlert.okId).focus();
|
|
39
|
|
40 Glayer.hide();
|
|
41 incsearch.input.focus();
|
0
|
42
|
|
43 incsearch.reset();
|
|
44 },
|
|
45 error: function(errMsg) {
|
|
46 this.loadingBox.style.display = 'none';
|
|
47
|
|
48 var self = this;
|
|
49 Glayer.showConfirm(
|
|
50 errMsg,
|
|
51 function(result){
|
|
52 Glayer.hideConfirm();
|
|
53 if (result) {
|
|
54 self.dispStart();
|
|
55 self._load();
|
|
56 } else {
|
|
57 incsearch.input.focus();
|
|
58 }
|
|
59 },
|
|
60 {okLabel: 'Retry', cancelLabel: 'cancel'}
|
|
61 );
|
|
62 document.getElementById(Glayer.defaultConfirm.cancelId).focus();
|
|
63
|
|
64 throw errMsg;
|
|
65 },
|
|
66
|
|
67 truncate: function() {
|
|
68 this.database.execute(this.truncateSql);
|
|
69 },
|
|
70 insert: function(bookmark) {
|
|
71 this.insertHandler.execute(bookmark);
|
|
72 },
|
|
73
|
|
74 update: function(bookmarks) {
|
|
75
|
|
76 var self = this;
|
|
77
|
|
78 var conn = this.database.connection;
|
|
79 try {
|
|
80 conn.beginTransaction();
|
|
81 this.truncate();
|
|
82
|
|
83 for (var i = 0, len = bookmarks.length; i < len; i++) {
|
|
84 if (i % 100 == 0) yield i;
|
|
85 this.insert(bookmarks[i]);
|
|
86 }
|
|
87
|
|
88 conn.commitTransaction();
|
|
89 } catch(e) {
|
|
90 conn.rollbackTransaction();
|
|
91
|
|
92 this.error(e.message || e);
|
|
93 throw e;
|
|
94 }
|
|
95 }
|
|
96 }
|
|
97
|
|
98
|
|
99 var Executer = function(generator, interval, func, callback) {
|
|
100 this.generator = generator;
|
|
101 this.func = func || function(){};
|
|
102 this.interval = interval;
|
|
103 this.callback = callback;
|
|
104
|
|
105 var self = this;
|
|
106 this.run = function() {
|
|
107 try{
|
|
108 self.func(self.generator.next());
|
|
109 setTimeout(function(){self.run()}, self.interval);
|
|
110 } catch (e) {
|
|
111 if (e instanceof StopIteration) {
|
|
112 self.callback();
|
|
113 } else {
|
|
114 throw e;
|
|
115 }
|
|
116 }
|
|
117 }
|
|
118 };
|
|
119
|