forked from Seldaek/php-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnippet.js
216 lines (205 loc) · 7.27 KB
/
snippet.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*jslint browser: true */
/*global ace, jQuery */
/**
* Snippet Manager
*
* Copyright (C) 2015, Tim Curzon
*
* Licensed under the new BSD License
* See the LICENSE file for details
*/
(function ($, ace) {
"use strict";
// private instance methods
var initialize, log;
// private instance vars
var editor, options, logLevel,
ls,
tagger, tags = [], tagListKey = 'taglist';
logLevel = {
EMERG: 0,
ALERT: 1,
CRIT: 2,
ERR: 3,
WARN: 4,
NOTICE: 5,
INFO: 6,
DEBUG: 7
};
options = {
editor: 'editor',
editorKey: 'phpCode',
log: true,
logLevel: 3
};
/**
* initialise snippet manager
*/
initialize = function () {
editor = ace.edit(options.editor);
tagger.setup();
};
/**
* log helper
*/
log = function(level, msg, data) {
data = data || '';
if (options.log && logLevel[level] <= options.logLevel) {
console.log('[' + level + ']', msg, data);
}
};
/**
* simple localStorage wrapper
*/
ls = {
/**
* stores editor content in localStorage
*/
setItem: function (key, type, content) {
key = type + '_' + key;
localStorage.setItem(key, content);
var stored = localStorage.getItem(key);
log('INFO', 'Content stored in key "' + key + '", type "' + type + '"');
log('DEBUG', 'Content was: ' + stored);
return stored;
},
/**
* get stored editor content from localStorage
*/
getItem: function (key, type) {
key = type + '_' + key;
var content = localStorage.getItem(key);
log('INFO', 'Getting stored content for key "' + key + '", type "' + type + '"');
log('DEBUG', 'Content was: ' + content);
return content;
},
/**
* clear a snippet
*/
clearItem: function(key, type) {
key = type + '_' + key;
localStorage.removeItem(key);
log('INFO', 'Cleared stored content for key "' + key + '", type "' + type + '"');
},
/**
* setup UI save button functionality
*/
clearItems: function(type) {
$.each(keys, function(idx, key) {
ls.clearItem(key, type);
});
},
};
/**
* setup tag bar
*/
// TODO:
// - UI > Sorting
// - UI > Quick update
// - Rename snippet js/css files (tab something or another)
// - Rename a tag (?)
tagger = {
setup: function() {
var persistedTags = ls.getItem(tagListKey, 'config');
if (persistedTags != null && persistedTags != '') {
tags = persistedTags.split(',');
tags = tags.map(function(tag) { return tag.replace('tag_', ''); })
log('DEBUG', 'Attempting initialisation of tags "' + tags.toString() + '"');
tagger.addAllToUi(tags);
log('INFO', 'Tags initialised (tags:' + tags.toString() + ')');
}
$('#tagadd').click(function(e) {
tagger.saveHandler();
});
$('#tagconfig').click(function(e) {
$('#tagconfig').addClass('active');
$('#tagconfigpane').show();
});
$('#tagbar #tagconfigpane').click(function(e) {
if (e.target.id == 'tagconfigpane-close') {
$('#tagconfig').removeClass('active');
$('#tagconfigpane').hide();
}
});
$('#tagbar #taglist').click(function(e) {
log('DEBUG', 'Click logged: ', e);
var tagEl = $(e.target).parents('li').filter('.tag');
var tagName = $('.name', tagEl).text();
if ($(e.target).hasClass('name')) {
tagger.loadHandler(tagName, tagEl);
} else {
var menuEl = $(e.target).parents('li');
log('DEBUG', 'tagEl: ', tagEl, ' / tagName: ', tagName, ' / menuEl: ', menuEl);
if ($(menuEl).hasClass('up')) {
tagger.updateHandler(tagName, tagEl);
} else if ($(menuEl).hasClass('del')) {
tagger.deleteHandler(tagName, tagEl);
}
}
});
},
addAllToUi: function(tagsToAddToUi) {
tagsToAddToUi.forEach(function(tag) {
tagger.addToUi(tag);
});
},
addToUi: function(tag) {
var tagTemplate = '<li class="tag">' +
'<div class="data">' +
'<a class="name" href="#">__tagname__</a>' +
'<a class="showmenu" href="#" title="options">▼</a>' +
'<div class="menu">' +
'<ul>' +
'<li class="up"><a title="Update" href="#">Update<span>↻</span></a></li>' +
'<li class="del"><a title="Delete" href="#">Delete<span>x</span></a></li>' +
'</ul>' +
'</div>' +
'</div>' +
'</li>';
$('#tagbar #taglist').append(tagTemplate.replace('__tagname__', tag));
},
loadHandler: function(tag, tagEl) {
log('DEBUG', 'Attempting loading of tag "' + tag + '" (element: ', tagEl, ')');
var content = ls.getItem(tag, 'tag');
editor.getSession().setValue(content);
log('INFO', 'Tag "' + tag + '" loaded');
},
saveHandler: function() {
var tag = prompt('Enter a tag name');
log('DEBUG', 'Attempting creating of tag "' + tag + '"');
if (tag) {
if (tags.indexOf(tag) == -1) {
tags.push(tag);
ls.setItem(tagListKey, 'config', tags.toString());
ls.setItem(tag, 'tag', editor.getSession().getValue());
tagger.addToUi(tag);
log('INFO', 'Tag "' + tag + '" created (tags:' + tags.toString() + ')');
}
}
},
updateHandler: function(tag, tagEl) {
log('DEBUG', 'Attempting update of tag "' + tag + '" (element: ', tagEl, ')');
if (tags.indexOf(tag) != -1) {
ls.setItem(tag, 'tag', editor.getSession().getValue());
log('INFO', 'Tag "' + tag + '" updated (tags:' + tags.toString() + ')');
}
},
deleteHandler: function(tag, tagEl) {
log('DEBUG', 'Attempting deletion of tag "' + tag + '" (element: ', tagEl, ')');
var del = confirm('Really delete the tag "' + tag + '"?');
if (del && tags.indexOf(tag) != -1) {
tags.splice(tags.indexOf(tag), 1);
ls.clearItem(tag, 'tag');
ls.setItem(tagListKey, 'config', tags.toString());
$(tagEl).remove();
log('INFO', 'Tag "' + tag + '" deleted (tags:' + tags.toString() + ')');
}
}
};
/**
* fire up module, but wait a mo for php-console to init
*/
$(document).ready(function() {
setTimeout(initialize, 1);
});
}(jQuery, ace));