This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.js
executable file
·202 lines (189 loc) · 6.81 KB
/
init.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright (c) Codiad & Andr3as, distributed
* as-is and without warranty under the MIT License.
* See http://opensource.org/licenses/MIT for more information.
* This information must remain intact.
*/
(function(global, $){
var codiad = global.codiad,
scripts = document.getElementsByTagName('script'),
path = scripts[scripts.length-1].src.split('?')[0],
curpath = path.split('/').slice(0, -1).join('/')+'/';
$(function() {
codiad.Include.init();
});
codiad.Include = {
path: curpath,
files: null,
cache: [],
rules: [],
//////////////////////////////////////////////////////////
//
// Init - Subscribe to amplify publications
//
//////////////////////////////////////////////////////////
init: function() {
var _this = this;
amplify.subscribe("active.onFocus", function(path){
_this.updateFiles(path);
});
amplify.subscribe("Complete.Normal", function(obj){
_this.getSuggestions(obj);
});
amplify.subscribe("Include.Callback", function(obj){
_this.replacePrefix(obj);
});
//Load rules
$.getJSON(this.path + 'rules.json', function(json){
_this.rules = json;
});
//Check requirements
setTimeout(function(){
if (typeof(codiad.Complete) == 'undefined') {
codiad.modal.load(400,_this.path+"dialog.php");
}
}, 2000);
},
//////////////////////////////////////////////////////////
//
// Update file list of the project of the current focused file
//
// Parameters:
//
// path - {String} - Current focused file path
//
//////////////////////////////////////////////////////////
updateFiles: function(path) {
var _this = this;
var project;
if (path.search("/") !== 0) {
project = path.substring(0, path.indexOf("/"));
} else {
project = path.substring(0, path.lastIndexOf("/"));
}
var date = new Date().getTime();
if (typeof(this.cache[project]) != 'undefined') {
if (this.cache[project].time > date - 60000) {
_this.files = this.cache[project].files;
return;
}
}
$.getJSON(this.path+"controller.php?action=getFiles&path="+project, function(data){
_this.files = data;
_this.cache[project] = {files: data, time: new Date().getTime()};
});
},
//////////////////////////////////////////////////////////
//
// Get suggestion
//
// Parameters:
//
// obj - {Object} - CompletePlus publishing object
//
//////////////////////////////////////////////////////////
getSuggestions: function(obj) {
var text = obj.before.replace(new RegExp(" ", "g"), "");
text = text.replace(new RegExp("\t", "g"), "");
var rule = [];
for (var i = 0; i < this.rules.length; i++) {
rule = this.rules[i];
if (rule.scope.indexOf(obj.syntax) !== -1) {
if (new RegExp(rule.regex).test(obj.before)) {
for (var j = 0; j < rule.ext.length; j++) {
console.log(rule.ext[j]);
this.sendSuggestion(rule.ext[j], obj.file);
}
}
}
}
},
//////////////////////////////////////////////////////////
//
// Commit suggestions to CompletePlus
//
// Parameters:
//
// ext - {String} - Extension of the files to commit
// curFile - {String} - File path of the current file
//
//////////////////////////////////////////////////////////
sendSuggestion: function(ext, curFile) {
if (typeof(this.files[ext]) == 'undefined') {
return false;
}
var sugObj, file, buf = [];
for (var i = 0; i < this.files[ext].length; i++) {
//filter the current file
file = this.files[ext][i];
if (file !== curFile.substring(curFile.indexOf("/")+1)) {
sugObj = codiad.Complete.pluginParser(file, file, file, "Include.Callback");
buf.push(sugObj);
}
}
if (buf.length !== 0) {
codiad.Complete.pluginNormal(buf);
}
},
//////////////////////////////////////////////////////////
//
// Test if item is at the end of string
//
// Parameters:
//
// string - {String} - String to search in
// item - {String} - Item to search for
//
//////////////////////////////////////////////////////////
isAtEnd: function(string, item) {
var pos = string.lastIndexOf(item);
if (pos != -1) {
var part = string.substring(pos);
if (part === item) {
return true;
} else {
return false;
}
} else {
return false;
}
},
//////////////////////////////////////////////////////////
//
// Replace prefix with file path
//
// Parameters:
//
// obj - {Object} - Suggestion object
//
//////////////////////////////////////////////////////////
replacePrefix: function(obj) {
var path = prompt("Edit include path:", "/"+obj.suggestion);
if (path === null) {
return false;
}
codiad.Complete.replacePrefix(path);
return true;
},
//////////////////////////////////////////////////////////
//
// Switch to a certain target
//
// Parameters:
//
// target - {string} - Target to switch to
//
//////////////////////////////////////////////////////////
goTo: function(target) {
if (target == "github") {
$.getJSON(this.path+"plugin.json", function(data){
codiad.modal.unload();
window.open(data[0].url, '_newtab');
});
} else {
codiad.modal.unload();
codiad.market.list();
}
}
};
})(this, jQuery);