Repository navigation
Expand file tree
/
Copy pathSearchcodeModel.js
More file actions
155 lines (139 loc) · 3.81 KB
/
Copy pathSearchcodeModel.js
File metadata and controls
155 lines (139 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var Util = require('Util.js');
var Database = require('model/Database.js');
module.exports = new function () {
var _this = this;
var DB;
var schemaBuilder = Database.schemaBuilder;
var Tables;
var DBEventType = Database.eventType;
var win = $(window);
schemaBuilder
.createTable('SourceCode')
.addColumn('id', lf.Type.INTEGER)
.addColumn('sid', lf.Type.OBJECT)
.addColumn('htm', lf.Type.OBJECT)
.addColumn('create', lf.Type.DATE_TIME)
.addPrimaryKey(['id'], true);
var persistLangsName = 'codelf_langs_selected';
var langs = Util.localStorage.get(persistLangsName), langQuery;
var page = 0;
var lastVal;
var cacheSourceCodes = {};
var cacheSourceCodeHtmls = {};
var afterRequestSearchcode;
genLangQuery(langs);
this.resetPage = function () {
page = 0;
}
this.setLang = function (val) {
langs = val || null;
genLangQuery(val);
this.resetPage();
Util.localStorage[langs ? 'set' : 'del'](persistLangsName, langs);
}
this.getLang = function () {
return langs;
}
function genLangQuery(val) {
if (!!val) {
var arr1 = val.replace(/\s+/g, ',').split(','),
arr2 = [];
arr1.forEach(function (key) {
arr2.push('lan=' + key);
});
langQuery = arr2.join('&');
} else {
langQuery = null;
}
}
win.on('DB:ready', function (ev,db) {
DB = db;
Tables = {
SourceCode: DB.getSchema().table('SourceCode')
};
_this.SourceCodeTable.getAll(function(rows){
rows.forEach(function (key) {
cacheSourceCodeHtmls[key.sid] = key.htm;
});
});
});
this.SourceCodeTable = new function () {
this.add = function (sid, htm, callback) {
if (!sid) {
return;
}
var row = Tables.SourceCode.createRow({
'sid': sid,
'htm': htm,
'create': new Date()
});
DB.insertOrReplace().into(Tables.SourceCode).values([row])
.exec().then(function () {
callback && callback();
win.trigger('DB:Table.SourceCode.onchange', {type: DBEventType.C});
});
}
this.getAll = function (callback) {
DB.select()
.from(Tables.SourceCode)
.orderBy(Tables.SourceCode.id, lf.Order.DESC)
.exec().then(function (rows) {
callback && callback(rows);
});
}
};
this.setCacheSourceCodeHtmlById = function(id,htm){
cacheSourceCodeHtmls[id] = htm;
_this.SourceCodeTable.add(id,htm);
}
this.getCacheSourceCodeHtmlById = function(id){
return cacheSourceCodeHtmls[id];
}
//search code by query
this.request = function (val, callback) {
afterRequestSearchcode = callback;
if (val != lastVal) {
this.resetPage();
}
lastVal = val;
lastVal && $.ajax({
type: 'GET',
dataType: 'jsonp',
//dataType: 'json',
//url: 'https://searchcode.com/api/codesearch_I/' + (langQuery ? ('?' + langQuery) : ''),
url: 'https://searchcode.com/api/jsonp_codesearch_I/' + (langQuery ? ('?' + langQuery) : ''),
data: {
q: lastVal,
p: page,
per_page: 42,
callback: 'afterRequestSearchcode'
},
jsonp: false,
jsonpCallback: false,
success: function (data) {
callback && callback(data, page);
page++;
}
})
};
window.afterRequestSearchcode = function(data){
afterRequestSearchcode && afterRequestSearchcode(data, page);
page++;
}
//get source code by id
this.requestSourceCode = function (id, callback) {
if (cacheSourceCodes[id]) {
callback && callback(cacheSourceCodes[id]);
return;
}
id && $.ajax({
type: 'GET',
dataType: 'json',
url: 'https://searchcode.com/api/result/' + id + '/',
success: function (data) {
cacheSourceCodes[id] = data;
callback && callback(data);
}
});
}
};