init
[garradin.git] / www / admin / static / skel_editor.js
1 (function (){
2 var www_url = document.body.getAttribute('data-url');
3
4 var css = document.createElement('link');
5 css.type = 'text/css';
6 css.rel = 'stylesheet';
7 css.href = www_url + 'static/skel_editor.css';
8 document.head.appendChild(css);
9
10 var save_btn = document.querySelector('input[name=save]');
11 save_btn.type = 'hidden';
12
13 var code = new codeEditor('f_content');
14
15 code.params.lang = {
16 search: "Texte à chercher ?\n(expression régulière autorisée, pour cela commencer par un slash '/')",
17 replace: "Texte pour le remplacement ?\n(utiliser $1, $2... pour les captures d'expression régulière)",
18 search_selection: "Texte à chercher dans la sélection ?\n(expression régulière autorisée, pour cela commencer par un slash '/')",
19 replace_result: "%d occurences trouvées et remplacées.",
20 goto: "Aller à la ligne :",
21 no_search_result: "Aucun résultat trouvé."
22 };
23
24 code.origValue = code.textarea.value;
25 code.saved = true;
26
27 code.onlinechange = function () {
28 if ((p = this.parent.querySelector('nav p')) && this.origValue != code.textarea.value)
29 {
30 toolbar.removeChild(p);
31 }
32
33 var line = this.getLine(this.current_line);
34 var doc = [];
35
36 if (match = line.match(/<BOUCLE(\d+|_[a-zA-Z0-9_-]+)\(([A-Z]+)\)(.*?)>/))
37 {
38 doc.push({link: 'Boucles', title: 'BOUCLE'});
39 doc.push({link: 'Boucle-'+match[2], title: match[2]});
40
41 if (match[3])
42 {
43 if (match[3].match(/\{".*"\}/))
44 doc.push({link: 'Critere-inter', title: 'Critère inter-résultat {"..."}'});
45 if (match[3].match(/\{\d+(,\d+)?\}/))
46 doc.push({link: 'Critere-de-nombre', title: 'Critère de nombre {X,Y}'});
47 if (match[3].match(/\{par\s+.*\}/))
48 doc.push({link: 'Critere-d-ordre', title: 'Critère d\'ordre {par champ}'});
49 if (match[3].match(/\{inverse\}/))
50 doc.push({link: 'Critere-inverse', title: 'Critère {inverse}'});
51 }
52 }
53
54 if (match = line.match(/<INCLURE\{(.*?)\}>/))
55 {
56 doc.push({link: 'Inclure', title: 'Inclusion du fichier ' + match[1]});
57 }
58
59 if (match = line.match(/#[A-Z0-9_]+(\*?(\|.*?)?\).*?\])?/g))
60 {
61 for (var i = 0; i < match.length; i++)
62 {
63 var tag = match[i].match(/(#[A-Z0-9_]+)(\*?(\|(.*?))?\).*?\])?/);
64 doc.push({title: 'Balise ' + tag[1]});
65
66 if (typeof tag[4] != 'undefined')
67 {
68 var tag = tag[4].split('|');
69 for (var j = 0; j < tag.length; j++)
70 {
71 var end = tag[j].indexOf('{');
72 end = (end == -1) ? tag[j].length : end;
73 var f = tag[j].substr(0, end);
74 doc.push({link: 'Filtre-'+f, title: 'Filtre '+f});
75 }
76 }
77 }
78 }
79
80 help.innerHTML = '';
81
82 for (var i = 0; i < doc.length; i++)
83 {
84 help.innerHTML += ' | ';
85
86 if (doc[i].link)
87 help.innerHTML += '<a href="' + doc_url + '#' + doc[i].link + '" onclick="return !window.open(this.href);">' + doc[i].title + '</a>';
88 else if (doc[i].tag)
89 help.innerHTML += '<' + tag + '>' + doc[i].title + '</' + tag + '>';
90 else
91 help.innerHTML += doc[i].title;
92 } return false;
93
94 };
95
96 code.saveFile = function (e)
97 {
98 if (this.fullscreen)
99 this.textarea.form.action += '&fullscreen';
100
101 this.textarea.form.submit();
102 };
103
104 code.loadFile = function (e)
105 {
106 var file = e.target.value;
107
108 if (file == skel_current) return;
109
110 if (code.textarea.value != code.origValue &&
111 !window.confirm("Le fichier a été modifié, abandonner les modifications ?"))
112 {
113 for (var i = 0; i < e.target.options.length; i++)
114 {
115 e.target.options[i].selected = false;
116
117 if (e.target.options[i].value == skel_current)
118 {
119 e.target.options[i].selected = true;
120 }
121 }
122
123 return false;
124 }
125
126 var url = www_url + 'config/site.php?edit=' + encodeURIComponent(file);
127
128 window.location.href = url + (code.fullscreen ? '#fullscreen' : '');
129
130 return true;
131 };
132
133 code.resetFile = function (e)
134 {
135 if (this.textarea.value == this.origValue) return;
136 if (!window.confirm("Le fichier a été modifié, abandonner les modifications ?")) return;
137 this.textarea.form.reset();
138 };
139
140 var help = document.createElement('div');
141 help.className = 'sk_help';
142
143 code.parent.appendChild(help);
144
145 var toolbar = document.createElement('nav');
146 toolbar.className = 'sk_toolbar';
147
148 var appendButton = function (name, title, action)
149 {
150 var btn = document.createElement('input');
151 btn.type = 'button';
152 btn.value = btn.title = title;
153 btn.className = name;
154 btn.onclick = function () { action.call(code); return false; };
155
156 toolbar.appendChild(btn);
157 };
158
159 appendButton('save', 'Enregistrer les modifications', code.saveFile);
160 appendButton('reset', 'Recharger le fichier (effacer les modifications)', code.resetFile);
161
162 appendButton('search', 'Chercher', code.search);
163 appendButton('search_replace', 'Chercher et remplacer', code.searchAndReplace);
164 appendButton('gotoline', 'Aller à la ligne', code.goToLine);
165 appendButton('fullscreen', 'Plein écran', code.toggleFullscreen);
166
167 var sel = document.createElement('select');
168 sel.title = 'Charger un autre fichier';
169 sel.onchange = code.loadFile;
170
171 for (var i in skel_list)
172 {
173 if (!skel_list.hasOwnProperty(i))
174 continue;
175
176 var skel = skel_list[i];
177 var opt = document.createElement('option');
178 opt.value = skel;
179 opt.innerHTML = skel;
180 opt.selected = (skel == skel_current) ? true : false;
181 sel.appendChild(opt);
182 }
183
184 toolbar.appendChild(sel);
185
186 code.parent.insertBefore(toolbar, code.parent.firstChild);
187
188 if (window.location.hash.match(/fullscreen/))
189 {
190 code.toggleFullscreen();
191
192 if (msg = document.querySelector('p.error, p.confirm'))
193 {
194 var m = document.createElement('p');
195 m.innerHTML = msg.innerHTML;
196 m.className = msg.className;
197 toolbar.appendChild(m);
198 msg.parentNode.removeChild(msg);
199 }
200
201 window.location.hash = '';
202 }
203 }());