1var svgeditor_path = 'https://cdn.rawgit.com/SVG-Edit/svgedit/stable/editor/';	//online stable
2//var svgeditor_path = 'https://raw.githubusercontent.com/SVG-Edit/svgedit/master/editor/';	//online latest (unstable)
3//var svgeditor_path = DOKU_BASE+'lib/plugins/svgedit/svg-edit/';		//offline
4
5//load embedapi.js
6var head = document.getElementsByTagName("head")[0];
7script = document.createElement('script');
8script.type = 'text/javascript';
9script.src = svgeditor_path + 'embedapi.js';
10head.appendChild(script);
11
12function svgedit_load() {
13	var field = jQuery('#wiki__text');
14	if (!field) return;
15	field = field[0];
16	var timeout = setTimeout('svgedit_load();', 500);	//load ASAP
17	window.svgedit.setSvgString(field.value) (function(a) {
18						  clearTimeout(timeout);
19						  }
20	);
21}
22function svgedit_save(page) {
23	window.svgedit.getSvgString()(function(data) {
24				      var field = jQuery('#wiki__text');
25				      if (!field) return;
26				      field = field[0];
27				      field.value = data;
28				      if (page) {
29				      	field = jQuery('#edbtn__save');
30				      	field.click();
31				      }
32	}) ;
33}
34
35function showhide(elem) {
36	elem.style.display = (elem.style.display == 'none' ? '' : 'none');
37}
38
39function insertAfter(newNode, preNode) {
40	if (preNode.nextSibling)
41		preNode.parentNode.insertBefore(newNode, preNode.nextSibling);
42	else
43		preNode.parentNode(newNode);
44}
45
46var svgedit = null;
47
48function svgedit_init() {
49	var field = jQuery('#wiki__text');
50	if (!field) return;
51	field = field[0];
52
53	//toggle view
54	showhide(field);
55	showhide(jQuery('#tool__bar')[0]);
56	showhide(jQuery('#edbtn__save')[0]);
57
58	//lock
59	if (jQuery('#svg__edit').length) return;
60
61	//create iframe
62
63	var el = document.createElement('iframe');
64	el.setAttribute("src", svgeditor_path + 'svg-editor.html');
65	el.setAttribute("id", "svg__edit");
66	el.setAttribute("name", "svg__edit");
67	el.setAttribute("frameborder", "0");
68	el.setAttribute("width", "100%");
69	el.setAttribute("height", "70%");
70	el.setAttribute("style", "min-height: 600px;");
71	insertAfter(el, field);
72
73	//create save button
74	field = jQuery('#edbtn__save');
75	if (!field) return;
76	field = field[0];
77
78	el = document.createElement('input');
79	el.setAttribute("type", "button");
80	el.setAttribute("onclick", "svgedit_save(true)");
81	el.setAttribute("value", "SVG-SAVE");
82	el.setAttribute("title", "Save SVG to server");
83	el.setAttribute("class", "button");
84	field.parentNode.insertBefore(el, field);
85
86	el = document.createElement('input');
87	el.setAttribute("type", "button");
88	el.setAttribute("onclick", "svgedit_load()");
89	el.setAttribute("value", "TXT->SVG");
90	el.setAttribute("title", "Copy SVG from textarea to svg-editor");
91	el.setAttribute("class", "button");
92	field.parentNode.insertBefore(el, field);
93
94	el = document.createElement('input');
95	el.setAttribute("type", "button");
96	el.setAttribute("onclick", "svgedit_save()");
97	el.setAttribute("value", "SVG->TXT");
98	el.setAttribute("title", "Copy SVG from svg-editor to textarea");
99	el.setAttribute("class", "button");
100	field.parentNode.insertBefore(el, field);
101
102	//create embedapi
103	window.svgedit = new embedded_svg_edit(jQuery('#svg__edit')[0]);
104
105	//load image
106	svgedit_load();
107};
108
109
110jQuery(function() {
111	     if (!jQuery('#wiki__text').length || jQuery('#wiki__text').attr("readOnly")) return;
112	     var field = jQuery('#tool__bar');
113	     if (!field.length) return;
114	     field = field[0];
115	     field.style.float = 'left';
116	     var el = document.createElement('button');
117	     el.setAttribute("id", "TZT");
118	     el.setAttribute("class", "toolbutton");
119	     el.setAttribute("onclick", "svgedit_init();");
120	     el.setAttribute("title", "Edit this page as SVG!");
121	     el.setAttribute("style", "float: left;");
122	     field.parentNode.insertBefore(el, field);
123	     el.appendChild(document.createTextNode("SVG"));
124	     var el = document.createElement('br');
125	     el.setAttribute('style', "clear: left;");
126	     field.appendChild(el);}) ;
127