edit.js 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  1. // Edit plugin, https://github.com/datenstrom/yellow-plugins/tree/master/edit
  2. // Copyright (c) 2013-2018 Datenstrom, https://datenstrom.se
  3. // This file may be used and distributed under the terms of the public license.
  4. var yellow =
  5. {
  6. action: function(action, status, args) { yellow.edit.action(action, status, args); },
  7. onLoad: function() { yellow.edit.load(); },
  8. onClickAction: function(e) { yellow.edit.clickAction(e); },
  9. onClick: function(e) { yellow.edit.click(e); },
  10. onKeydown: function(e) { yellow.edit.keydown(e); },
  11. onUpdate: function() { yellow.edit.updatePane(yellow.edit.paneId, yellow.edit.paneAction, yellow.edit.paneStatus); },
  12. onResize: function() { yellow.edit.resizePane(yellow.edit.paneId, yellow.edit.paneAction, yellow.edit.paneStatus); }
  13. };
  14. yellow.edit =
  15. {
  16. paneId: 0, //visible pane ID
  17. paneActionOld: 0, //previous pane action
  18. paneAction: 0, //current pane action
  19. paneStatus: 0, //current pane status
  20. popupId: 0, //visible popup ID
  21. intervalId: 0, //timer interval ID
  22. // Handle initialisation
  23. load: function()
  24. {
  25. var body = document.getElementsByTagName("body")[0];
  26. if(body && body.firstChild && !document.getElementById("yellow-bar"))
  27. {
  28. this.createBar("yellow-bar");
  29. this.createPane("yellow-pane-edit", "none", "none");
  30. this.action(yellow.page.action, yellow.page.status);
  31. clearInterval(this.intervalId);
  32. }
  33. },
  34. // Handle action
  35. action: function(action, status, args)
  36. {
  37. status = status ? status : "none";
  38. args = args ? args : "none";
  39. switch(action)
  40. {
  41. case "login": this.showPane("yellow-pane-login", action, status); break;
  42. case "logout": this.sendPane("yellow-pane-logout", action); break;
  43. case "signup": this.showPane("yellow-pane-signup", action, status); break;
  44. case "confirm": this.showPane("yellow-pane-signup", action, status); break;
  45. case "approve": this.showPane("yellow-pane-signup", action, status); break;
  46. case "reactivate": this.showPane("yellow-pane-settings", action, status); break;
  47. case "recover": this.showPane("yellow-pane-recover", action, status); break;
  48. case "settings": this.showPane("yellow-pane-settings", action, status); break;
  49. case "reconfirm": this.showPane("yellow-pane-settings", action, status); break;
  50. case "change": this.showPane("yellow-pane-settings", action, status); break;
  51. case "version": this.showPane("yellow-pane-version", action, status); break;
  52. case "update": this.sendPane("yellow-pane-update", action, status, args); break;
  53. case "create": this.showPane("yellow-pane-edit", action, status, true); break;
  54. case "edit": this.showPane("yellow-pane-edit", action, status, true); break;
  55. case "delete": this.showPane("yellow-pane-edit", action, status, true); break;
  56. case "user": this.showPane("yellow-pane-user", action, status); break;
  57. case "send": this.sendPane(this.paneId, this.paneAction); break;
  58. case "close": this.hidePane(this.paneId); break;
  59. case "toolbar": this.processToolbar(status, args); break;
  60. case "help": this.processHelp(); break;
  61. }
  62. },
  63. // Handle action clicked
  64. clickAction: function(e)
  65. {
  66. e.stopPropagation();
  67. e.preventDefault();
  68. var element = e.target;
  69. for(; element; element=element.parentNode)
  70. {
  71. if(element.tagName=="A") break;
  72. }
  73. this.action(element.getAttribute("data-action"), element.getAttribute("data-status"), element.getAttribute("data-args"));
  74. },
  75. // Handle mouse clicked
  76. click: function(e)
  77. {
  78. if(this.popupId && !document.getElementById(this.popupId).contains(e.target)) this.hidePopup(this.popupId, true);
  79. if(this.paneId && !document.getElementById(this.paneId).contains(e.target)) this.hidePane(this.paneId, true);
  80. },
  81. // Handle keyboard
  82. keydown: function(e)
  83. {
  84. if(this.paneId=="yellow-pane-edit") this.processShortcut(e);
  85. if(this.paneId && e.keyCode==27) this.hidePane(this.paneId);
  86. },
  87. // Create bar
  88. createBar: function(barId)
  89. {
  90. if(yellow.config.debug) console.log("yellow.edit.createBar id:"+barId);
  91. var elementBar = document.createElement("div");
  92. elementBar.className = "yellow-bar";
  93. elementBar.setAttribute("id", barId);
  94. if(barId=="yellow-bar")
  95. {
  96. yellow.toolbox.addEvent(document, "click", yellow.onClick);
  97. yellow.toolbox.addEvent(document, "keydown", yellow.onKeydown);
  98. yellow.toolbox.addEvent(window, "resize", yellow.onResize);
  99. }
  100. var elementDiv = document.createElement("div");
  101. elementDiv.setAttribute("id", barId+"-content");
  102. if(yellow.config.userName)
  103. {
  104. elementDiv.innerHTML =
  105. "<div class=\"yellow-bar-left\">"+
  106. "<a href=\"#\" id=\"yellow-pane-edit-link\" data-action=\"edit\">"+this.getText("Edit")+"</a>"+
  107. "</div>"+
  108. "<div class=\"yellow-bar-right\">"+
  109. "<a href=\"#\" id=\"yellow-pane-create-link\" data-action=\"create\">"+this.getText("Create")+"</a>"+
  110. "<a href=\"#\" id=\"yellow-pane-delete-link\" data-action=\"delete\">"+this.getText("Delete")+"</a>"+
  111. "<a href=\"#\" id=\"yellow-pane-user-link\" data-action=\"user\">"+yellow.toolbox.encodeHtml(yellow.config.userName)+"</a>"+
  112. "</div>"+
  113. "<div class=\"yellow-bar-banner\"></div>";
  114. }
  115. elementBar.appendChild(elementDiv);
  116. yellow.toolbox.insertBefore(elementBar, document.getElementsByTagName("body")[0].firstChild);
  117. this.bindActions(elementBar);
  118. },
  119. // Create pane
  120. createPane: function(paneId, paneAction, paneStatus)
  121. {
  122. if(yellow.config.debug) console.log("yellow.edit.createPane id:"+paneId);
  123. var elementPane = document.createElement("div");
  124. elementPane.className = "yellow-pane";
  125. elementPane.setAttribute("id", paneId);
  126. elementPane.style.display = "none";
  127. if(paneId=="yellow-pane-edit")
  128. {
  129. yellow.toolbox.addEvent(elementPane, "input", yellow.onUpdate);
  130. }
  131. if(paneId=="yellow-pane-edit" || paneId=="yellow-pane-user")
  132. {
  133. var elementArrow = document.createElement("span");
  134. elementArrow.className = "yellow-arrow";
  135. elementArrow.setAttribute("id", paneId+"-arrow");
  136. elementPane.appendChild(elementArrow);
  137. }
  138. var elementDiv = document.createElement("div");
  139. elementDiv.setAttribute("id", paneId+"-content");
  140. switch(paneId)
  141. {
  142. case "yellow-pane-login":
  143. elementDiv.innerHTML =
  144. "<form method=\"post\">"+
  145. "<a href=\"#\" class=\"yellow-close\" data-action=\"close\"><i class=\"yellow-icon yellow-icon-close\"></i></a>"+
  146. "<h1>"+this.getText("LoginTitle")+"</h1>"+
  147. "<div id=\"yellow-pane-login-fields\">"+
  148. "<input type=\"hidden\" name=\"action\" value=\"login\" />"+
  149. "<p><label for=\"yellow-pane-login-email\">"+this.getText("LoginEmail")+"</label><br /><input class=\"yellow-form-control\" name=\"email\" id=\"yellow-pane-login-email\" maxlength=\"64\" value=\""+yellow.toolbox.encodeHtml(yellow.config.editLoginEmail)+"\" /></p>"+
  150. "<p><label for=\"yellow-pane-login-password\">"+this.getText("LoginPassword")+"</label><br /><input class=\"yellow-form-control\" type=\"password\" name=\"password\" id=\"yellow-pane-login-password\" maxlength=\"64\" value=\""+yellow.toolbox.encodeHtml(yellow.config.editLoginPassword)+"\" /></p>"+
  151. "<p><input class=\"yellow-btn\" type=\"submit\" value=\""+this.getText("LoginButton")+"\" /></p>"+
  152. "</div>"+
  153. "<div id=\"yellow-pane-login-buttons\">"+
  154. "<p><a href=\"#\" id=\"yellow-pane-login-recover\" data-action=\"recover\">"+this.getText("LoginRecover")+"</a><p>"+
  155. "<p><a href=\"#\" id=\"yellow-pane-login-signup\" data-action=\"signup\">"+this.getText("LoginSignup")+"</a><p>"+
  156. "</div>"+
  157. "</form>";
  158. break;
  159. case "yellow-pane-signup":
  160. elementDiv.innerHTML =
  161. "<form method=\"post\">"+
  162. "<a href=\"#\" class=\"yellow-close\" data-action=\"close\"><i class=\"yellow-icon yellow-icon-close\"></i></a>"+
  163. "<h1>"+this.getText("SignupTitle")+"</h1>"+
  164. "<div id=\"yellow-pane-signup-status\" class=\""+paneStatus+"\">"+this.getText(paneAction+"Status", "", paneStatus)+"</div>"+
  165. "<div id=\"yellow-pane-signup-fields\">"+
  166. "<input type=\"hidden\" name=\"action\" value=\"signup\" />"+
  167. "<p><label for=\"yellow-pane-signup-name\">"+this.getText("SignupName")+"</label><br /><input class=\"yellow-form-control\" name=\"name\" id=\"yellow-pane-signup-name\" maxlength=\"64\" value=\""+yellow.toolbox.encodeHtml(this.getRequest("name"))+"\" /></p>"+
  168. "<p><label for=\"yellow-pane-signup-email\">"+this.getText("SignupEmail")+"</label><br /><input class=\"yellow-form-control\" name=\"email\" id=\"yellow-pane-signup-email\" maxlength=\"64\" value=\""+yellow.toolbox.encodeHtml(this.getRequest("email"))+"\" /></p>"+
  169. "<p><label for=\"yellow-pane-signup-password\">"+this.getText("SignupPassword")+"</label><br /><input class=\"yellow-form-control\" type=\"password\" name=\"password\" id=\"yellow-pane-signup-password\" maxlength=\"64\" value=\"\" /></p>"+
  170. "<p><input class=\"yellow-btn\" type=\"submit\" value=\""+this.getText("SignupButton")+"\" /></p>"+
  171. "</div>"+
  172. "<div id=\"yellow-pane-signup-buttons\">"+
  173. "<p><a href=\"#\" class=\"yellow-btn\" data-action=\"close\">"+this.getText("OkButton")+"</a></p>"+
  174. "</div>"+
  175. "</form>";
  176. break;
  177. case "yellow-pane-recover":
  178. elementDiv.innerHTML =
  179. "<form method=\"post\">"+
  180. "<a href=\"#\" class=\"yellow-close\" data-action=\"close\"><i class=\"yellow-icon yellow-icon-close\"></i></a>"+
  181. "<h1>"+this.getText("RecoverTitle")+"</h1>"+
  182. "<div id=\"yellow-pane-recover-status\" class=\""+paneStatus+"\">"+this.getText(paneAction+"Status", "", paneStatus)+"</div>"+
  183. "<div id=\"yellow-pane-recover-fields-first\">"+
  184. "<input type=\"hidden\" name=\"action\" value=\"recover\" />"+
  185. "<p><label for=\"yellow-pane-recover-email\">"+this.getText("RecoverEmail")+"</label><br /><input class=\"yellow-form-control\" name=\"email\" id=\"yellow-pane-recover-email\" maxlength=\"64\" value=\""+yellow.toolbox.encodeHtml(this.getRequest("email"))+"\" /></p>"+
  186. "<p><input class=\"yellow-btn\" type=\"submit\" value=\""+this.getText("OkButton")+"\" /></p>"+
  187. "</div>"+
  188. "<div id=\"yellow-pane-recover-fields-second\">"+
  189. "<p><label for=\"yellow-pane-recover-password\">"+this.getText("RecoverPassword")+"</label><br /><input class=\"yellow-form-control\" type=\"password\" name=\"password\" id=\"yellow-pane-recover-password\" maxlength=\"64\" value=\"\" /></p>"+
  190. "<p><input class=\"yellow-btn\" type=\"submit\" value=\""+this.getText("OkButton")+"\" /></p>"+
  191. "</div>"+
  192. "<div id=\"yellow-pane-recover-buttons\">"+
  193. "<p><a href=\"#\" class=\"yellow-btn\" data-action=\"close\">"+this.getText("OkButton")+"</a></p>"+
  194. "</div>"+
  195. "</form>";
  196. break;
  197. case "yellow-pane-settings":
  198. var rawDataLanguages = "";
  199. if(yellow.config.serverLanguages && Object.keys(yellow.config.serverLanguages).length>1)
  200. {
  201. rawDataLanguages += "<p>";
  202. for(var language in yellow.config.serverLanguages)
  203. {
  204. var checked = language==this.getRequest("language") ? " checked=\"checked\"" : "";
  205. rawDataLanguages += "<label for=\"yellow-pane-settings-"+language+"\"><input type=\"radio\" name=\"language\" id=\"yellow-pane-settings-"+language+"\" value=\""+language+"\""+checked+"> "+yellow.toolbox.encodeHtml(yellow.config.serverLanguages[language])+"</label><br />";
  206. }
  207. rawDataLanguages += "</p>";
  208. }
  209. elementDiv.innerHTML =
  210. "<form method=\"post\">"+
  211. "<a href=\"#\" class=\"yellow-close\" data-action=\"close\"><i class=\"yellow-icon yellow-icon-close\"></i></a>"+
  212. "<h1 id=\"yellow-pane-settings-title\">"+this.getText("SettingsTitle")+"</h1>"+
  213. "<div id=\"yellow-pane-settings-status\" class=\""+paneStatus+"\">"+this.getText(paneAction+"Status", "", paneStatus)+"</div>"+
  214. "<div id=\"yellow-pane-settings-fields\">"+
  215. "<input type=\"hidden\" name=\"action\" value=\"settings\" />"+
  216. "<p><label for=\"yellow-pane-settings-name\">"+this.getText("SignupName")+"</label><br /><input class=\"yellow-form-control\" name=\"name\" id=\"yellow-pane-settings-name\" maxlength=\"64\" value=\""+yellow.toolbox.encodeHtml(this.getRequest("name"))+"\" /></p>"+
  217. "<p><label for=\"yellow-pane-settings-email\">"+this.getText("SignupEmail")+"</label><br /><input class=\"yellow-form-control\" name=\"email\" id=\"yellow-pane-settings-email\" maxlength=\"64\" value=\""+yellow.toolbox.encodeHtml(this.getRequest("email"))+"\" /></p>"+
  218. "<p><label for=\"yellow-pane-settings-password\">"+this.getText("SignupPassword")+"</label><br /><input class=\"yellow-form-control\" type=\"password\" name=\"password\" id=\"yellow-pane-settings-password\" maxlength=\"64\" value=\"\" /></p>"+rawDataLanguages+
  219. "<p><input class=\"yellow-btn\" type=\"submit\" value=\""+this.getText("OkButton")+"\" /></p>"+
  220. "</div>"+
  221. "<div id=\"yellow-pane-settings-buttons\">"+
  222. "<p><a href=\"#\" class=\"yellow-btn\" data-action=\"close\">"+this.getText("OkButton")+"</a></p>"+
  223. "</div>"+
  224. "</form>";
  225. break;
  226. case "yellow-pane-version":
  227. elementDiv.innerHTML =
  228. "<form method=\"post\">"+
  229. "<a href=\"#\" class=\"yellow-close\" data-action=\"close\"><i class=\"yellow-icon yellow-icon-close\"></i></a>"+
  230. "<h1 id=\"yellow-pane-version-title\">"+yellow.toolbox.encodeHtml(yellow.config.serverVersion)+"</h1>"+
  231. "<div id=\"yellow-pane-version-status\" class=\""+paneStatus+"\">"+this.getText("VersionStatus", "", paneStatus)+"</div>"+
  232. "<div id=\"yellow-pane-version-fields\">"+yellow.page.rawDataOutput+"</div>"+
  233. "<div id=\"yellow-pane-version-buttons\">"+
  234. "<p><a href=\"#\" class=\"yellow-btn\" data-action=\"close\">"+this.getText("OkButton")+"</a></p>"+
  235. "</div>"+
  236. "</form>";
  237. break;
  238. case "yellow-pane-edit":
  239. var rawDataButtons = "";
  240. if(yellow.config.editToolbarButtons && yellow.config.editToolbarButtons!="none")
  241. {
  242. var tokens = yellow.config.editToolbarButtons.split(",");
  243. for(var i=0; i<tokens.length; i++)
  244. {
  245. var token = tokens[i].trim();
  246. if(token!="separator")
  247. {
  248. rawDataButtons += "<li><a href=\"#\" id=\"yellow-toolbar-"+yellow.toolbox.encodeHtml(token)+"\" class=\"yellow-toolbar-btn-icon yellow-toolbar-tooltip\" data-action=\"toolbar\" data-status=\""+yellow.toolbox.encodeHtml(token)+"\" aria-label=\""+this.getText("Toolbar", "", token)+"\"><i class=\"yellow-icon yellow-icon-"+yellow.toolbox.encodeHtml(token)+"\"></i></a></li>";
  249. } else {
  250. rawDataButtons += "<li><a href=\"#\" class=\"yellow-toolbar-btn-separator\"></a></li>";
  251. }
  252. }
  253. if(yellow.config.debug) console.log("yellow.edit.createPane buttons:"+yellow.config.editToolbarButtons);
  254. }
  255. elementDiv.innerHTML =
  256. "<form method=\"post\">"+
  257. "<div id=\"yellow-pane-edit-toolbar\">"+
  258. "<h1 id=\"yellow-pane-edit-toolbar-title\" class=\"yellow-toolbar yellow-toolbar-left\">"+this.getText("Edit")+"</h1>"+
  259. "<ul id=\"yellow-pane-edit-toolbar-buttons\" class=\"yellow-toolbar yellow-toolbar-left\">"+rawDataButtons+"</ul>"+
  260. "<ul id=\"yellow-pane-edit-toolbar-main\" class=\"yellow-toolbar yellow-toolbar-right\">"+
  261. "<li><a href=\"#\" id=\"yellow-pane-edit-cancel\" class=\"yellow-toolbar-btn\" data-action=\"close\">"+this.getText("CancelButton")+"</a></li>"+
  262. "<li><a href=\"#\" id=\"yellow-pane-edit-send\" class=\"yellow-toolbar-btn\" data-action=\"send\">"+this.getText("EditButton")+"</a></li>"+
  263. "</ul>"+
  264. "<ul class=\"yellow-toolbar yellow-toolbar-banner\"></ul>"+
  265. "</div>"+
  266. "<textarea id=\"yellow-pane-edit-text\" class=\"yellow-form-control\"></textarea>"+
  267. "<div id=\"yellow-pane-edit-preview\"></div>"+
  268. "</form>";
  269. break;
  270. case "yellow-pane-user":
  271. elementDiv.innerHTML =
  272. "<ul class=\"yellow-dropdown\">"+
  273. "<li><span>"+yellow.toolbox.encodeHtml(yellow.config.userEmail)+"</span></li>"+
  274. "<li><a href=\"#\" data-action=\"settings\">"+this.getText("SettingsTitle")+"</a></li>" +
  275. "<li><a href=\"#\" data-action=\"help\">"+this.getText("UserHelp")+"</a></li>" +
  276. "<li><a href=\"#\" data-action=\"logout\">"+this.getText("UserLogout")+"</a></li>"+
  277. "</ul>";
  278. break;
  279. }
  280. elementPane.appendChild(elementDiv);
  281. yellow.toolbox.insertAfter(elementPane, document.getElementsByTagName("body")[0].firstChild);
  282. this.bindActions(elementPane);
  283. },
  284. // Update pane
  285. updatePane: function(paneId, paneAction, paneStatus, init)
  286. {
  287. if(yellow.config.debug) console.log("yellow.edit.updatePane id:"+paneId);
  288. var showFields = paneStatus!="next" && paneStatus!="done" && paneStatus!="expired";
  289. switch(paneId)
  290. {
  291. case "yellow-pane-login":
  292. if(yellow.config.editLoginRestrictions)
  293. {
  294. yellow.toolbox.setVisible(document.getElementById("yellow-pane-login-signup"), false);
  295. }
  296. break;
  297. case "yellow-pane-signup":
  298. yellow.toolbox.setVisible(document.getElementById("yellow-pane-signup-fields"), showFields);
  299. yellow.toolbox.setVisible(document.getElementById("yellow-pane-signup-buttons"), !showFields);
  300. break;
  301. case "yellow-pane-recover":
  302. yellow.toolbox.setVisible(document.getElementById("yellow-pane-recover-fields-first"), showFields);
  303. yellow.toolbox.setVisible(document.getElementById("yellow-pane-recover-fields-second"), showFields);
  304. yellow.toolbox.setVisible(document.getElementById("yellow-pane-recover-buttons"), !showFields);
  305. if(showFields)
  306. {
  307. if(this.getRequest("id"))
  308. {
  309. yellow.toolbox.setVisible(document.getElementById("yellow-pane-recover-fields-first"), false);
  310. } else {
  311. yellow.toolbox.setVisible(document.getElementById("yellow-pane-recover-fields-second"), false);
  312. }
  313. }
  314. break;
  315. case "yellow-pane-settings":
  316. yellow.toolbox.setVisible(document.getElementById("yellow-pane-settings-fields"), showFields);
  317. yellow.toolbox.setVisible(document.getElementById("yellow-pane-settings-buttons"), !showFields);
  318. if(paneStatus=="none")
  319. {
  320. document.getElementById("yellow-pane-settings-status").innerHTML = "<a href=\"#\" data-action=\"version\">"+this.getText("VersionTitle")+"</a>";
  321. document.getElementById("yellow-pane-settings-name").value = yellow.config.userName;
  322. document.getElementById("yellow-pane-settings-email").value = yellow.config.userEmail;
  323. document.getElementById("yellow-pane-settings-"+yellow.config.userLanguage).checked = true;
  324. }
  325. break;
  326. case "yellow-pane-version":
  327. if(paneStatus=="none" && this.isPlugin("update"))
  328. {
  329. document.getElementById("yellow-pane-version-status").innerHTML = this.getText("VersionStatusCheck");
  330. document.getElementById("yellow-pane-version-fields").innerHTML = "";
  331. setTimeout("yellow.action('send');", 500);
  332. }
  333. if(paneStatus=="updates" && yellow.config.userWebmaster)
  334. {
  335. document.getElementById("yellow-pane-version-status").innerHTML = "<a href=\"#\" data-action=\"update\">"+this.getText("VersionUpdateNormal")+"</a>";
  336. }
  337. break;
  338. case "yellow-pane-edit":
  339. document.getElementById("yellow-pane-edit-text").focus();
  340. if(init)
  341. {
  342. yellow.toolbox.setVisible(document.getElementById("yellow-pane-edit-text"), true);
  343. yellow.toolbox.setVisible(document.getElementById("yellow-pane-edit-preview"), false);
  344. document.getElementById("yellow-pane-edit-toolbar-title").innerHTML = yellow.toolbox.encodeHtml(yellow.page.title);
  345. document.getElementById("yellow-pane-edit-text").value = paneAction=="create" ? yellow.page.rawDataNew : yellow.page.rawDataEdit;
  346. var matches = document.getElementById("yellow-pane-edit-text").value.match(/^(\xEF\xBB\xBF)?\-\-\-[\r\n]+/);
  347. var position = document.getElementById("yellow-pane-edit-text").value.indexOf("\n", matches ? matches[0].length : 0);
  348. document.getElementById("yellow-pane-edit-text").setSelectionRange(position, position);
  349. if(yellow.config.editToolbarButtons!="none")
  350. {
  351. yellow.toolbox.setVisible(document.getElementById("yellow-pane-edit-toolbar-title"), false);
  352. this.updateToolbar(0, "yellow-toolbar-checked");
  353. }
  354. if(yellow.config.userRestrictions)
  355. {
  356. yellow.toolbox.setVisible(document.getElementById("yellow-pane-edit-send"), false);
  357. document.getElementById("yellow-pane-edit-text").readOnly = true;
  358. }
  359. }
  360. if(!yellow.config.userRestrictions)
  361. {
  362. var key, className;
  363. switch(this.getAction(paneId, paneAction))
  364. {
  365. case "create": key = "CreateButton"; className = "yellow-toolbar-btn yellow-toolbar-btn-create"; break;
  366. case "edit": key = "EditButton"; className = "yellow-toolbar-btn yellow-toolbar-btn-edit"; break;
  367. case "delete": key = "DeleteButton"; className = "yellow-toolbar-btn yellow-toolbar-btn-delete"; break;
  368. }
  369. if(document.getElementById("yellow-pane-edit-send").className != className)
  370. {
  371. document.getElementById("yellow-pane-edit-send").innerHTML = this.getText(key);
  372. document.getElementById("yellow-pane-edit-send").className = className;
  373. this.resizePane(paneId, paneAction, paneStatus);
  374. }
  375. }
  376. break;
  377. }
  378. this.bindActions(document.getElementById(paneId));
  379. },
  380. // Resize pane
  381. resizePane: function(paneId, paneAction, paneStatus)
  382. {
  383. var elementBar = document.getElementById("yellow-bar-content");
  384. var paneLeft = yellow.toolbox.getOuterLeft(elementBar);
  385. var paneTop = yellow.toolbox.getOuterTop(elementBar) + yellow.toolbox.getOuterHeight(elementBar) + 10;
  386. var paneWidth = yellow.toolbox.getOuterWidth(elementBar);
  387. var paneHeight = yellow.toolbox.getWindowHeight() - paneTop - Math.min(yellow.toolbox.getOuterHeight(elementBar) + 10, (yellow.toolbox.getWindowWidth()-yellow.toolbox.getOuterWidth(elementBar))/2);
  388. switch(paneId)
  389. {
  390. case "yellow-pane-login":
  391. case "yellow-pane-signup":
  392. case "yellow-pane-recover":
  393. case "yellow-pane-settings":
  394. case "yellow-pane-version":
  395. yellow.toolbox.setOuterLeft(document.getElementById(paneId), paneLeft);
  396. yellow.toolbox.setOuterTop(document.getElementById(paneId), paneTop);
  397. yellow.toolbox.setOuterWidth(document.getElementById(paneId), paneWidth);
  398. break;
  399. case "yellow-pane-edit":
  400. yellow.toolbox.setOuterLeft(document.getElementById("yellow-pane-edit"), paneLeft);
  401. yellow.toolbox.setOuterTop(document.getElementById("yellow-pane-edit"), paneTop);
  402. yellow.toolbox.setOuterHeight(document.getElementById("yellow-pane-edit"), paneHeight);
  403. yellow.toolbox.setOuterWidth(document.getElementById("yellow-pane-edit"), paneWidth);
  404. var elementWidth = yellow.toolbox.getWidth(document.getElementById("yellow-pane-edit"));
  405. yellow.toolbox.setOuterWidth(document.getElementById("yellow-pane-edit-text"), elementWidth);
  406. yellow.toolbox.setOuterWidth(document.getElementById("yellow-pane-edit-preview"), elementWidth);
  407. var buttonsWidth = 0;
  408. var buttonsWidthMax = yellow.toolbox.getOuterWidth(document.getElementById("yellow-pane-edit-toolbar")) -
  409. yellow.toolbox.getOuterWidth(document.getElementById("yellow-pane-edit-toolbar-main")) - 1;
  410. var element = document.getElementById("yellow-pane-edit-toolbar-buttons").firstChild;
  411. for(; element; element=element.nextSibling)
  412. {
  413. element.removeAttribute("style");
  414. buttonsWidth += yellow.toolbox.getOuterWidth(element);
  415. if(buttonsWidth>buttonsWidthMax) yellow.toolbox.setVisible(element, false);
  416. }
  417. yellow.toolbox.setOuterWidth(document.getElementById("yellow-pane-edit-toolbar-title"), buttonsWidthMax);
  418. var height1 = yellow.toolbox.getHeight(document.getElementById("yellow-pane-edit"));
  419. var height2 = yellow.toolbox.getOuterHeight(document.getElementById("yellow-pane-edit-toolbar"));
  420. yellow.toolbox.setOuterHeight(document.getElementById("yellow-pane-edit-text"), height1 - height2);
  421. yellow.toolbox.setOuterHeight(document.getElementById("yellow-pane-edit-preview"), height1 - height2);
  422. var elementLink = document.getElementById("yellow-pane-"+paneAction+"-link");
  423. var position = yellow.toolbox.getOuterLeft(elementLink) + yellow.toolbox.getOuterWidth(elementLink)/2;
  424. position -= yellow.toolbox.getOuterLeft(document.getElementById("yellow-pane-edit")) + 1;
  425. yellow.toolbox.setOuterLeft(document.getElementById("yellow-pane-edit-arrow"), position);
  426. break;
  427. case "yellow-pane-user":
  428. yellow.toolbox.setOuterLeft(document.getElementById("yellow-pane-user"), paneLeft + paneWidth - yellow.toolbox.getOuterWidth(document.getElementById("yellow-pane-user")));
  429. yellow.toolbox.setOuterTop(document.getElementById("yellow-pane-user"), paneTop);
  430. var elementLink = document.getElementById("yellow-pane-user-link");
  431. var position = yellow.toolbox.getOuterLeft(elementLink) + yellow.toolbox.getOuterWidth(elementLink)/2;
  432. position -= yellow.toolbox.getOuterLeft(document.getElementById("yellow-pane-user"));
  433. yellow.toolbox.setOuterLeft(document.getElementById("yellow-pane-user-arrow"), position);
  434. break;
  435. }
  436. },
  437. // Show or hide pane
  438. showPane: function(paneId, paneAction, paneStatus, modal)
  439. {
  440. if(this.paneId!=paneId || this.paneAction!=paneAction)
  441. {
  442. this.hidePane(this.paneId);
  443. if(!document.getElementById(paneId)) this.createPane(paneId, paneAction, paneStatus);
  444. var element = document.getElementById(paneId);
  445. if(!yellow.toolbox.isVisible(element))
  446. {
  447. if(yellow.config.debug) console.log("yellow.edit.showPane id:"+paneId);
  448. yellow.toolbox.setVisible(element, true);
  449. if(modal)
  450. {
  451. yellow.toolbox.addClass(document.body, "yellow-body-modal-open");
  452. yellow.toolbox.addValue("meta[name=viewport]", "content", ", maximum-scale=1, user-scalable=0");
  453. }
  454. this.paneId = paneId;
  455. this.paneAction = paneAction;
  456. this.paneStatus = paneStatus;
  457. this.updatePane(paneId, paneAction, paneStatus, this.paneActionOld!=this.paneAction);
  458. this.resizePane(paneId, paneAction, paneStatus);
  459. }
  460. } else {
  461. this.hidePane(this.paneId, true);
  462. }
  463. },
  464. // Hide pane
  465. hidePane: function(paneId, fadeout)
  466. {
  467. var element = document.getElementById(paneId);
  468. if(yellow.toolbox.isVisible(element))
  469. {
  470. yellow.toolbox.removeClass(document.body, "yellow-body-modal-open");
  471. yellow.toolbox.removeValue("meta[name=viewport]", "content", ", maximum-scale=1, user-scalable=0");
  472. yellow.toolbox.setVisible(element, false, fadeout);
  473. this.paneId = 0;
  474. this.paneActionOld = this.paneAction;
  475. this.paneAction = 0;
  476. this.paneStatus = 0;
  477. }
  478. this.hidePopup(this.popupId);
  479. },
  480. // Send pane
  481. sendPane: function(paneId, paneAction, paneStatus, paneArgs)
  482. {
  483. if(yellow.config.debug) console.log("yellow.edit.sendPane id:"+paneId);
  484. var args = { "action":paneAction };
  485. if(paneId=="yellow-pane-edit")
  486. {
  487. args.action = this.getAction(paneId, paneAction);
  488. args.rawdatasource = yellow.page.rawDataSource;
  489. args.rawdataedit = document.getElementById("yellow-pane-edit-text").value;
  490. args.rawdataendofline = yellow.page.rawDataEndOfLine;
  491. }
  492. if(paneArgs)
  493. {
  494. var tokens = paneArgs.split("/");
  495. for(var i=0; i<tokens.length; i++)
  496. {
  497. var pair = tokens[i].split(/[:=]/);
  498. if(!pair[0] || !pair[1]) continue;
  499. args[pair[0]] = pair[1];
  500. }
  501. }
  502. yellow.toolbox.submitForm(args);
  503. },
  504. // Process help
  505. processHelp: function()
  506. {
  507. this.hidePane(this.paneId);
  508. window.open(this.getText("HelpUrl", "yellow"), "_self");
  509. },
  510. // Process shortcut
  511. processShortcut: function(e)
  512. {
  513. var shortcut = yellow.toolbox.getEventShortcut(e)
  514. if(shortcut)
  515. {
  516. var tokens = yellow.config.editKeyboardShortcuts.split(",");
  517. for(var i=0; i<tokens.length; i++)
  518. {
  519. var pair = tokens[i].trim().split(" ");
  520. if(shortcut==pair[0] || shortcut.replace("meta+", "ctrl+")==pair[0])
  521. {
  522. e.stopPropagation();
  523. e.preventDefault();
  524. this.processToolbar(pair[1]);
  525. }
  526. }
  527. }
  528. },
  529. // Process toolbar
  530. processToolbar: function(status, args)
  531. {
  532. if(yellow.config.debug) console.log("yellow.edit.processToolbar status:"+status);
  533. var elementText = document.getElementById("yellow-pane-edit-text");
  534. var elementPreview = document.getElementById("yellow-pane-edit-preview");
  535. if(!yellow.config.userRestrictions && this.paneAction!="delete" && !yellow.toolbox.isVisible(elementPreview))
  536. {
  537. switch(status)
  538. {
  539. case "h1": yellow.editor.setMarkdown(elementText, "# ", "insert-multiline-block", true); break;
  540. case "h2": yellow.editor.setMarkdown(elementText, "## ", "insert-multiline-block", true); break;
  541. case "h3": yellow.editor.setMarkdown(elementText, "### ", "insert-multiline-block", true); break;
  542. case "paragraph": yellow.editor.setMarkdown(elementText, "", "remove-multiline-block");
  543. yellow.editor.setMarkdown(elementText, "", "remove-fenced-block"); break;
  544. case "quote": yellow.editor.setMarkdown(elementText, "> ", "insert-multiline-block", true); break;
  545. case "pre": yellow.editor.setMarkdown(elementText, "```\n", "insert-fenced-block", true); break;
  546. case "bold": yellow.editor.setMarkdown(elementText, "**", "insert-inline", true); break;
  547. case "italic": yellow.editor.setMarkdown(elementText, "*", "insert-inline", true); break;
  548. case "strikethrough": yellow.editor.setMarkdown(elementText, "~", "insert-inline", true); break;
  549. case "code": yellow.editor.setMarkdown(elementText, "`", "insert-autodetect", true); break;
  550. case "ul": yellow.editor.setMarkdown(elementText, "* ", "insert-multiline-block", true); break;
  551. case "ol": yellow.editor.setMarkdown(elementText, "1. ", "insert-multiline-block", true); break;
  552. case "tl": yellow.editor.setMarkdown(elementText, "- [ ] ", "insert-multiline-block", true); break;
  553. case "link": yellow.editor.setMarkdown(elementText, "[link](url)", "insert", false, yellow.editor.getMarkdownLink); break;
  554. case "file": yellow.editor.setMarkdown(elementText, "[image picture.jpg]", "insert"); break;
  555. case "text": yellow.editor.setMarkdown(elementText, args, "insert"); break;
  556. case "draft": yellow.editor.setMetaData(elementText, "status", "draft", true); break;
  557. case "undo": yellow.editor.undo(); break;
  558. case "redo": yellow.editor.redo(); break;
  559. }
  560. }
  561. if(status=="preview") this.showPreview(elementText, elementPreview);
  562. if(status=="save" && !yellow.config.userRestrictions && this.paneAction!="delete") this.action("send");
  563. if(status=="help") window.open(this.getText("HelpUrl", "yellow"), "_blank");
  564. if(status=="markdown") window.open(this.getText("MarkdownUrl", "yellow"), "_blank");
  565. if(status=="format" || status=="heading" || status=="list" || status=="emojiawesome" || status=="fontawesome")
  566. {
  567. this.showPopup("yellow-popup-"+status, status);
  568. } else {
  569. this.hidePopup(this.popupId);
  570. }
  571. },
  572. // Update toolbar
  573. updateToolbar: function(status, name)
  574. {
  575. if(status)
  576. {
  577. var element = document.getElementById("yellow-toolbar-"+status);
  578. if(element) yellow.toolbox.addClass(element, name);
  579. } else {
  580. var elements = document.getElementsByClassName(name);
  581. for(var i=0, l=elements.length; i<l; i++)
  582. {
  583. yellow.toolbox.removeClass(elements[i], name);
  584. }
  585. }
  586. },
  587. // Create popup
  588. createPopup: function(popupId)
  589. {
  590. if(yellow.config.debug) console.log("yellow.edit.createPopup id:"+popupId);
  591. var elementPopup = document.createElement("div");
  592. elementPopup.className = "yellow-popup";
  593. elementPopup.setAttribute("id", popupId);
  594. elementPopup.style.display = "none";
  595. var elementDiv = document.createElement("div");
  596. elementDiv.setAttribute("id", popupId+"-content");
  597. switch(popupId)
  598. {
  599. case "yellow-popup-format":
  600. elementDiv.innerHTML =
  601. "<ul class=\"yellow-dropdown yellow-dropdown-menu\">"+
  602. "<li><a href=\"#\" id=\"yellow-popup-format-h1\" data-action=\"toolbar\" data-status=\"h1\">"+this.getText("ToolbarH1")+"</a></li>"+
  603. "<li><a href=\"#\" id=\"yellow-popup-format-h2\" data-action=\"toolbar\" data-status=\"h2\">"+this.getText("ToolbarH2")+"</a></li>"+
  604. "<li><a href=\"#\" id=\"yellow-popup-format-h3\" data-action=\"toolbar\" data-status=\"h3\">"+this.getText("ToolbarH3")+"</a></li>"+
  605. "<li><a href=\"#\" id=\"yellow-popup-format-paragraph\" data-action=\"toolbar\" data-status=\"paragraph\">"+this.getText("ToolbarParagraph")+"</a></li>"+
  606. "<li><a href=\"#\" id=\"yellow-popup-format-pre\" data-action=\"toolbar\" data-status=\"pre\">"+this.getText("ToolbarPre")+"</a></li>"+
  607. "<li><a href=\"#\" id=\"yellow-popup-format-quote\" data-action=\"toolbar\" data-status=\"quote\">"+this.getText("ToolbarQuote")+"</a></li>"+
  608. "</ul>";
  609. break;
  610. case "yellow-popup-heading":
  611. elementDiv.innerHTML =
  612. "<ul class=\"yellow-dropdown yellow-dropdown-menu\">"+
  613. "<li><a href=\"#\" id=\"yellow-popup-heading-h1\" data-action=\"toolbar\" data-status=\"h1\">"+this.getText("ToolbarH1")+"</a></li>"+
  614. "<li><a href=\"#\" id=\"yellow-popup-heading-h2\" data-action=\"toolbar\" data-status=\"h2\">"+this.getText("ToolbarH2")+"</a></li>"+
  615. "<li><a href=\"#\" id=\"yellow-popup-heading-h3\" data-action=\"toolbar\" data-status=\"h3\">"+this.getText("ToolbarH3")+"</a></li>"+
  616. "</ul>";
  617. break;
  618. case "yellow-popup-list":
  619. elementDiv.innerHTML =
  620. "<ul class=\"yellow-dropdown yellow-dropdown-menu\">"+
  621. "<li><a href=\"#\" id=\"yellow-popup-list-ul\" data-action=\"toolbar\" data-status=\"ul\">"+this.getText("ToolbarUl")+"</a></li>"+
  622. "<li><a href=\"#\" id=\"yellow-popup-list-ol\" data-action=\"toolbar\" data-status=\"ol\">"+this.getText("ToolbarOl")+"</a></li>"+
  623. "</ul>";
  624. break;
  625. case "yellow-popup-emojiawesome":
  626. var rawDataEmojis = "";
  627. if(yellow.config.emojiawesomeToolbarButtons && yellow.config.emojiawesomeToolbarButtons!="none")
  628. {
  629. var tokens = yellow.config.emojiawesomeToolbarButtons.split(" ");
  630. for(var i=0; i<tokens.length; i++)
  631. {
  632. var token = tokens[i].replace(/[\:]/g,"");
  633. var className = token.replace("+1", "plus1").replace("-1", "minus1").replace(/_/g, "-");
  634. rawDataEmojis += "<li><a href=\"#\" id=\"yellow-popup-list-"+yellow.toolbox.encodeHtml(token)+"\" data-action=\"toolbar\" data-status=\"text\" data-args=\":"+yellow.toolbox.encodeHtml(token)+":\"><i class=\"ea ea-"+yellow.toolbox.encodeHtml(className)+"\"></i></a></li>";
  635. }
  636. }
  637. elementDiv.innerHTML = "<ul class=\"yellow-dropdown yellow-dropdown-menu\">"+rawDataEmojis+"</ul>";
  638. break;
  639. case "yellow-popup-fontawesome":
  640. var rawDataIcons = "";
  641. if(yellow.config.fontawesomeToolbarButtons && yellow.config.fontawesomeToolbarButtons!="none")
  642. {
  643. var tokens = yellow.config.fontawesomeToolbarButtons.split(" ");
  644. for(var i=0; i<tokens.length; i++)
  645. {
  646. var token = tokens[i].replace(/[\:]/g,"");
  647. rawDataIcons += "<li><a href=\"#\" id=\"yellow-popup-list-"+yellow.toolbox.encodeHtml(token)+"\" data-action=\"toolbar\" data-status=\"text\" data-args=\":"+yellow.toolbox.encodeHtml(token)+":\"><i class=\"fa "+yellow.toolbox.encodeHtml(token)+"\"></i></a></li>";
  648. }
  649. }
  650. elementDiv.innerHTML = "<ul class=\"yellow-dropdown yellow-dropdown-menu\">"+rawDataIcons+"</ul>";
  651. break;
  652. }
  653. elementPopup.appendChild(elementDiv);
  654. yellow.toolbox.insertAfter(elementPopup, document.getElementsByTagName("body")[0].firstChild);
  655. this.bindActions(elementPopup);
  656. },
  657. // Show or hide popup
  658. showPopup: function(popupId, status)
  659. {
  660. if(this.popupId!=popupId)
  661. {
  662. this.hidePopup(this.popupId);
  663. if(!document.getElementById(popupId)) this.createPopup(popupId);
  664. var element = document.getElementById(popupId);
  665. if(yellow.config.debug) console.log("yellow.edit.showPopup id:"+popupId);
  666. yellow.toolbox.setVisible(element, true);
  667. this.popupId = popupId;
  668. this.updateToolbar(status, "yellow-toolbar-selected");
  669. var elementParent = document.getElementById("yellow-toolbar-"+status);
  670. var popupLeft = yellow.toolbox.getOuterLeft(elementParent);
  671. var popupTop = yellow.toolbox.getOuterTop(elementParent) + yellow.toolbox.getOuterHeight(elementParent) - 1;
  672. yellow.toolbox.setOuterLeft(document.getElementById(popupId), popupLeft);
  673. yellow.toolbox.setOuterTop(document.getElementById(popupId), popupTop);
  674. } else {
  675. this.hidePopup(this.popupId, true);
  676. }
  677. },
  678. // Hide popup
  679. hidePopup: function(popupId, fadeout)
  680. {
  681. var element = document.getElementById(popupId);
  682. if(yellow.toolbox.isVisible(element))
  683. {
  684. yellow.toolbox.setVisible(element, false, fadeout);
  685. this.popupId = 0;
  686. this.updateToolbar(0, "yellow-toolbar-selected");
  687. }
  688. },
  689. // Show or hide preview
  690. showPreview: function(elementText, elementPreview)
  691. {
  692. if(!yellow.toolbox.isVisible(elementPreview))
  693. {
  694. var thisObject = this;
  695. var formData = new FormData();
  696. formData.append("action", "preview");
  697. formData.append("rawdataedit", elementText.value);
  698. formData.append("rawdataendofline", yellow.page.rawDataEndOfLine);
  699. var request = new XMLHttpRequest();
  700. request.open("POST", window.location.pathname, true);
  701. request.onload = function() { if(this.status==200) thisObject.updatePreview.call(thisObject, elementText, elementPreview, this.responseText); };
  702. request.send(formData);
  703. } else {
  704. this.updatePreview(elementText, elementPreview, "");
  705. }
  706. },
  707. // Update preview
  708. updatePreview: function(elementText, elementPreview, string)
  709. {
  710. var showPreview = string.length!=0;
  711. yellow.toolbox.setVisible(elementText, !showPreview);
  712. yellow.toolbox.setVisible(elementPreview, showPreview);
  713. if(showPreview)
  714. {
  715. this.updateToolbar("preview", "yellow-toolbar-checked");
  716. elementPreview.innerHTML = string;
  717. dispatchEvent(new Event("load"));
  718. } else {
  719. this.updateToolbar(0, "yellow-toolbar-checked");
  720. elementText.focus();
  721. }
  722. },
  723. // Bind actions to links
  724. bindActions: function(element)
  725. {
  726. var elements = element.getElementsByTagName("a");
  727. for(var i=0, l=elements.length; i<l; i++)
  728. {
  729. if(elements[i].getAttribute("data-action")) elements[i].onclick = yellow.onClickAction;
  730. if(elements[i].getAttribute("data-action")=="toolbar") elements[i].onmousedown = function(e) { e.preventDefault() };
  731. }
  732. },
  733. // Return action
  734. getAction: function(paneId, paneAction)
  735. {
  736. var action = "";
  737. if(paneId=="yellow-pane-edit")
  738. {
  739. switch(paneAction)
  740. {
  741. case "create": action = "create"; break;
  742. case "edit": action = document.getElementById("yellow-pane-edit-text").value.length!=0 ? "edit" : "delete"; break;
  743. case "delete": action = "delete"; break;
  744. }
  745. if(yellow.page.statusCode==434 && paneAction!="delete") action = "create";
  746. }
  747. return action;
  748. },
  749. // Return request string
  750. getRequest: function(key, prefix)
  751. {
  752. if(!prefix) prefix = "request";
  753. key = prefix + yellow.toolbox.toUpperFirst(key);
  754. return (key in yellow.page) ? yellow.page[key] : "";
  755. },
  756. // Return text string
  757. getText: function(key, prefix, postfix)
  758. {
  759. if(!prefix) prefix = "edit";
  760. if(!postfix) postfix = "";
  761. key = prefix + yellow.toolbox.toUpperFirst(key) + yellow.toolbox.toUpperFirst(postfix);
  762. return (key in yellow.text) ? yellow.text[key] : "["+key+"]";
  763. },
  764. // Check if plugin exists
  765. isPlugin: function(name)
  766. {
  767. return name in yellow.config.serverPlugins;
  768. }
  769. };
  770. yellow.editor =
  771. {
  772. // Set Markdown formatting
  773. setMarkdown: function(element, prefix, type, toggle, callback)
  774. {
  775. var information = this.getMarkdownInformation(element, prefix, type);
  776. var selectionStart = (information.type.indexOf("block")!=-1) ? information.top : information.start;
  777. var selectionEnd = (information.type.indexOf("block")!=-1) ? information.bottom : information.end;
  778. if(information.found && toggle) information.type = information.type.replace("insert", "remove");
  779. if(information.type=="remove-fenced-block" || information.type=="remove-inline")
  780. {
  781. selectionStart -= information.prefix.length; selectionEnd += information.prefix.length;
  782. }
  783. var text = information.text;
  784. var textSelectionBefore = text.substring(0, selectionStart);
  785. var textSelection = text.substring(selectionStart, selectionEnd);
  786. var textSelectionAfter = text.substring(selectionEnd, text.length);
  787. var textSelectionNew, selectionStartNew, selectionEndNew;
  788. switch(information.type)
  789. {
  790. case "insert-multiline-block":
  791. textSelectionNew = this.getMarkdownMultilineBlock(textSelection, information);
  792. selectionStartNew = information.start + this.getMarkdownDifference(textSelection, textSelectionNew, true);
  793. selectionEndNew = information.end + this.getMarkdownDifference(textSelection, textSelectionNew);
  794. if(information.start==information.top && information.start!=information.end) selectionStartNew = information.top;
  795. if(information.end==information.top && information.start!=information.end) selectionEndNew = information.top;
  796. break;
  797. case "remove-multiline-block":
  798. textSelectionNew = this.getMarkdownMultilineBlock(textSelection, information);
  799. selectionStartNew = information.start + this.getMarkdownDifference(textSelection, textSelectionNew, true);
  800. selectionEndNew = information.end + this.getMarkdownDifference(textSelection, textSelectionNew);
  801. if(selectionStartNew<=information.top) selectionStartNew = information.top;
  802. if(selectionEndNew<=information.top) selectionEndNew = information.top;
  803. break;
  804. case "insert-fenced-block":
  805. textSelectionNew = this.getMarkdownFencedBlock(textSelection, information);
  806. selectionStartNew = information.start + information.prefix.length;
  807. selectionEndNew = information.end + this.getMarkdownDifference(textSelection, textSelectionNew) - information.prefix.length;
  808. break;
  809. case "remove-fenced-block":
  810. textSelectionNew = this.getMarkdownFencedBlock(textSelection, information);
  811. selectionStartNew = information.start - information.prefix.length;
  812. selectionEndNew = information.end + this.getMarkdownDifference(textSelection, textSelectionNew) + information.prefix.length;
  813. break;
  814. case "insert-inline":
  815. textSelectionNew = information.prefix + textSelection + information.prefix;
  816. selectionStartNew = information.start + information.prefix.length;
  817. selectionEndNew = information.end + information.prefix.length;
  818. break;
  819. case "remove-inline":
  820. textSelectionNew = text.substring(information.start, information.end);
  821. selectionStartNew = information.start - information.prefix.length;
  822. selectionEndNew = information.end - information.prefix.length;
  823. break;
  824. case "insert":
  825. textSelectionNew = callback ? callback(textSelection, information) : information.prefix;
  826. selectionStartNew = information.start + textSelectionNew.length;
  827. selectionEndNew = selectionStartNew;
  828. }
  829. if(textSelection!=textSelectionNew || selectionStart!=selectionStartNew || selectionEnd!=selectionEndNew)
  830. {
  831. element.focus();
  832. element.setSelectionRange(selectionStart, selectionEnd);
  833. document.execCommand("insertText", false, textSelectionNew);
  834. element.value = textSelectionBefore + textSelectionNew + textSelectionAfter;
  835. element.setSelectionRange(selectionStartNew, selectionEndNew);
  836. }
  837. if(yellow.config.debug) console.log("yellow.editor.setMarkdown type:"+information.type);
  838. },
  839. // Return Markdown formatting information
  840. getMarkdownInformation: function(element, prefix, type)
  841. {
  842. var text = element.value;
  843. var start = element.selectionStart;
  844. var end = element.selectionEnd;
  845. var top = start, bottom = end;
  846. while(text.charAt(top-1)!="\n" && top>0) top--;
  847. if(bottom==top && bottom<text.length) bottom++;
  848. while(text.charAt(bottom-1)!="\n" && bottom<text.length) bottom++;
  849. if(type=="insert-autodetect")
  850. {
  851. if(text.substring(start, end).indexOf("\n")!=-1)
  852. {
  853. type = "insert-fenced-block"; prefix = "```\n";
  854. } else {
  855. type = "insert-inline"; prefix = "`";
  856. }
  857. }
  858. var found = false;
  859. if(type.indexOf("multiline-block")!=-1)
  860. {
  861. if(text.substring(top, top+prefix.length)==prefix) found = true;
  862. } else if(type.indexOf("fenced-block")!=-1) {
  863. if(text.substring(top-prefix.length, top)==prefix && text.substring(bottom, bottom+prefix.length)==prefix)
  864. {
  865. found = true;
  866. }
  867. } else {
  868. if(text.substring(start-prefix.length, start)==prefix && text.substring(end, end+prefix.length)==prefix)
  869. {
  870. if(prefix=="*")
  871. {
  872. var lettersBefore = 0, lettersAfter = 0;
  873. for(var index=start-1; text.charAt(index)=="*"; index--) lettersBefore++;
  874. for(var index=end; text.charAt(index)=="*"; index++) lettersAfter++;
  875. found = lettersBefore!=2 && lettersAfter!=2;
  876. } else {
  877. found = true;
  878. }
  879. }
  880. }
  881. return { "text":text, "prefix":prefix, "type":type, "start":start, "end":end, "top":top, "bottom":bottom, "found":found };
  882. },
  883. // Return Markdown length difference
  884. getMarkdownDifference: function(textSelection, textSelectionNew, firstTextLine)
  885. {
  886. var textSelectionLength, textSelectionLengthNew;
  887. if(firstTextLine)
  888. {
  889. var position = textSelection.indexOf("\n");
  890. var positionNew = textSelectionNew.indexOf("\n");
  891. textSelectionLength = position!=-1 ? position+1 : textSelection.length+1;
  892. textSelectionLengthNew = positionNew!=-1 ? positionNew+1 : textSelectionNew.length+1;
  893. } else {
  894. var position = textSelection.indexOf("\n");
  895. var positionNew = textSelectionNew.indexOf("\n");
  896. textSelectionLength = position!=-1 ? textSelection.length : textSelection.length+1;
  897. textSelectionLengthNew = positionNew!=-1 ? textSelectionNew.length : textSelectionNew.length+1;
  898. }
  899. return textSelectionLengthNew - textSelectionLength;
  900. },
  901. // Return Markdown for multiline block
  902. getMarkdownMultilineBlock: function(textSelection, information)
  903. {
  904. var textSelectionNew = "";
  905. var lines = yellow.toolbox.getTextLines(textSelection);
  906. for(var i=0; i<lines.length; i++)
  907. {
  908. var matches = lines[i].match(/^(\s*[\#\*\-\>\s]+)?(\s+\[.\]|\s*\d+\.)?[ \t]+/);
  909. if(matches)
  910. {
  911. textSelectionNew += lines[i].substring(matches[0].length);
  912. } else {
  913. textSelectionNew += lines[i];
  914. }
  915. }
  916. textSelection = textSelectionNew;
  917. if(information.type.indexOf("remove")==-1)
  918. {
  919. textSelectionNew = "";
  920. var linePrefix = information.prefix;
  921. lines = yellow.toolbox.getTextLines(textSelection.length!=0 ? textSelection : "\n");
  922. for(var i=0; i<lines.length; i++)
  923. {
  924. textSelectionNew += linePrefix+lines[i];
  925. if(information.prefix=="1. ")
  926. {
  927. var matches = linePrefix.match(/^(\d+)\.\s/);
  928. if(matches) linePrefix = (parseInt(matches[1])+1)+". ";
  929. }
  930. }
  931. textSelection = textSelectionNew;
  932. }
  933. return textSelection;
  934. },
  935. // Return Markdown for fenced block
  936. getMarkdownFencedBlock: function(textSelection, information)
  937. {
  938. var textSelectionNew = "";
  939. var lines = yellow.toolbox.getTextLines(textSelection);
  940. for(var i=0; i<lines.length; i++)
  941. {
  942. var matches = lines[i].match(/^```/);
  943. if(!matches) textSelectionNew += lines[i];
  944. }
  945. textSelection = textSelectionNew;
  946. if(information.type.indexOf("remove")==-1)
  947. {
  948. if(textSelection.length==0) textSelection = "\n";
  949. textSelection = information.prefix + textSelection + information.prefix;
  950. }
  951. return textSelection;
  952. },
  953. // Return Markdown for link
  954. getMarkdownLink: function(textSelection, information)
  955. {
  956. return textSelection.length!=0 ? information.prefix.replace("link", textSelection) : information.prefix;
  957. },
  958. // Set meta data
  959. setMetaData: function(element, key, value, toggle)
  960. {
  961. var information = this.getMetaDataInformation(element, key);
  962. if(information.bottom!=0)
  963. {
  964. var selectionStart = information.found ? information.start : information.bottom;
  965. var selectionEnd = information.found ? information.end : information.bottom;
  966. var text = information.text;
  967. var textSelectionBefore = text.substring(0, selectionStart);
  968. var textSelection = text.substring(selectionStart, selectionEnd);
  969. var textSelectionAfter = text.substring(selectionEnd, text.length);
  970. var textSelectionNew = yellow.toolbox.toUpperFirst(key)+": "+value+"\n";
  971. if(information.found && information.value==value && toggle) textSelectionNew = "";
  972. var selectionStartNew = selectionStart;
  973. var selectionEndNew = selectionStart + textSelectionNew.trim().length;
  974. element.focus();
  975. element.setSelectionRange(selectionStart, selectionEnd);
  976. document.execCommand("insertText", false, textSelectionNew);
  977. element.value = textSelectionBefore + textSelectionNew + textSelectionAfter;
  978. element.setSelectionRange(selectionStartNew, selectionEndNew);
  979. element.scrollTop = 0;
  980. if(yellow.config.debug) console.log("yellow.editor.setMetaData key:"+key);
  981. }
  982. },
  983. // Return meta data information
  984. getMetaDataInformation: function(element, key)
  985. {
  986. var text = element.value;
  987. var value = "";
  988. var start = 0, end = 0, top = 0, bottom = 0;
  989. var found = false;
  990. var parts = text.match(/^(\xEF\xBB\xBF)?(\-\-\-[\r\n]+)([\s\S]+?)\-\-\-[\r\n]+/);
  991. if(parts)
  992. {
  993. key = yellow.toolbox.toLowerFirst(key);
  994. start = end = top = ((parts[1] ? parts[1] : "")+parts[2]).length;
  995. bottom = ((parts[1] ? parts[1] : "")+parts[2]+parts[3]).length;
  996. var lines = yellow.toolbox.getTextLines(parts[3]);
  997. for(var i=0; i<lines.length; i++)
  998. {
  999. var matches = lines[i].match(/^\s*(.*?)\s*:\s*(.*?)\s*$/);
  1000. if(matches && yellow.toolbox.toLowerFirst(matches[1])==key && matches[2].length!=0)
  1001. {
  1002. value = matches[2];
  1003. end = start + lines[i].length;
  1004. found = true;
  1005. break;
  1006. }
  1007. start = end = start + lines[i].length;
  1008. }
  1009. }
  1010. return { "text":text, "value":value, "start":start, "end":end, "top":top, "bottom":bottom, "found":found };
  1011. },
  1012. // Undo changes
  1013. undo: function()
  1014. {
  1015. document.execCommand("undo");
  1016. },
  1017. // Redo changes
  1018. redo: function()
  1019. {
  1020. document.execCommand("redo");
  1021. }
  1022. };
  1023. yellow.toolbox =
  1024. {
  1025. // Insert element before reference element
  1026. insertBefore: function(element, elementReference)
  1027. {
  1028. elementReference.parentNode.insertBefore(element, elementReference);
  1029. },
  1030. // Insert element after reference element
  1031. insertAfter: function(element, elementReference)
  1032. {
  1033. elementReference.parentNode.insertBefore(element, elementReference.nextSibling);
  1034. },
  1035. // Add element class
  1036. addClass: function(element, name)
  1037. {
  1038. element.classList.add(name);
  1039. },
  1040. // Remove element class
  1041. removeClass: function(element, name)
  1042. {
  1043. element.classList.remove(name);
  1044. },
  1045. // Add attribute information
  1046. addValue: function(selector, name, value)
  1047. {
  1048. var element = document.querySelector(selector);
  1049. element.setAttribute(name, element.getAttribute(name) + value);
  1050. },
  1051. // Remove attribute information
  1052. removeValue: function(selector, name, value)
  1053. {
  1054. var element = document.querySelector(selector);
  1055. element.setAttribute(name, element.getAttribute(name).replace(value, ""));
  1056. },
  1057. // Add event handler
  1058. addEvent: function(element, type, handler)
  1059. {
  1060. element.addEventListener(type, handler, false);
  1061. },
  1062. // Remove event handler
  1063. removeEvent: function(element, type, handler)
  1064. {
  1065. element.removeEventListener(type, handler, false);
  1066. },
  1067. // Return shortcut from keyboard event, alphanumeric only
  1068. getEventShortcut: function(e)
  1069. {
  1070. var shortcut = "";
  1071. if(e.keyCode>=48 && e.keyCode<=90)
  1072. {
  1073. shortcut += (e.ctrlKey ? "ctrl+" : "")+(e.metaKey ? "meta+" : "")+(e.altKey ? "alt+" : "")+(e.shiftKey ? "shift+" : "");
  1074. shortcut += String.fromCharCode(e.keyCode).toLowerCase();
  1075. }
  1076. return shortcut;
  1077. },
  1078. // Return element width in pixel
  1079. getWidth: function(element)
  1080. {
  1081. return element.offsetWidth - this.getBoxSize(element).width;
  1082. },
  1083. // Return element height in pixel
  1084. getHeight: function(element)
  1085. {
  1086. return element.offsetHeight - this.getBoxSize(element).height;
  1087. },
  1088. // Set element width in pixel, including padding and border
  1089. setOuterWidth: function(element, width)
  1090. {
  1091. element.style.width = Math.max(0, width - this.getBoxSize(element).width) + "px";
  1092. },
  1093. // Set element height in pixel, including padding and border
  1094. setOuterHeight: function(element, height)
  1095. {
  1096. element.style.height = Math.max(0, height - this.getBoxSize(element).height) + "px";
  1097. },
  1098. // Return element width in pixel, including padding and border
  1099. getOuterWidth: function(element, includeMargin)
  1100. {
  1101. var width = element.offsetWidth;
  1102. if(includeMargin) width += this.getMarginSize(element).width;
  1103. return width;
  1104. },
  1105. // Return element height in pixel, including padding and border
  1106. getOuterHeight: function(element, includeMargin)
  1107. {
  1108. var height = element.offsetHeight;
  1109. if(includeMargin) height += this.getMarginSize(element).height;
  1110. return height;
  1111. },
  1112. // Set element left position in pixel
  1113. setOuterLeft: function(element, left)
  1114. {
  1115. element.style.left = Math.max(0, left) + "px";
  1116. },
  1117. // Set element top position in pixel
  1118. setOuterTop: function(element, top)
  1119. {
  1120. element.style.top = Math.max(0, top) + "px";
  1121. },
  1122. // Return element left position in pixel
  1123. getOuterLeft: function(element)
  1124. {
  1125. return element.getBoundingClientRect().left + window.pageXOffset;
  1126. },
  1127. // Return element top position in pixel
  1128. getOuterTop: function(element)
  1129. {
  1130. return element.getBoundingClientRect().top + window.pageYOffset;
  1131. },
  1132. // Return window width in pixel
  1133. getWindowWidth: function()
  1134. {
  1135. return window.innerWidth;
  1136. },
  1137. // Return window height in pixel
  1138. getWindowHeight: function()
  1139. {
  1140. return window.innerHeight;
  1141. },
  1142. // Return element CSS property
  1143. getStyle: function(element, property)
  1144. {
  1145. return window.getComputedStyle(element).getPropertyValue(property);
  1146. },
  1147. // Return element CSS padding and border
  1148. getBoxSize: function(element)
  1149. {
  1150. var paddingLeft = parseFloat(this.getStyle(element, "padding-left")) || 0;
  1151. var paddingRight = parseFloat(this.getStyle(element, "padding-right")) || 0;
  1152. var borderLeft = parseFloat(this.getStyle(element, "border-left-width")) || 0;
  1153. var borderRight = parseFloat(this.getStyle(element, "border-right-width")) || 0;
  1154. var width = paddingLeft + paddingRight + borderLeft + borderRight;
  1155. var paddingTop = parseFloat(this.getStyle(element, "padding-top")) || 0;
  1156. var paddingBottom = parseFloat(this.getStyle(element, "padding-bottom")) || 0;
  1157. var borderTop = parseFloat(this.getStyle(element, "border-top-width")) || 0;
  1158. var borderBottom = parseFloat(this.getStyle(element, "border-bottom-width")) || 0;
  1159. var height = paddingTop + paddingBottom + borderTop + borderBottom;
  1160. return { "width":width, "height":height };
  1161. },
  1162. // Return element CSS margin
  1163. getMarginSize: function(element)
  1164. {
  1165. var marginLeft = parseFloat(this.getStyle(element, "margin-left")) || 0;
  1166. var marginRight = parseFloat(this.getStyle(element, "margin-right")) || 0;
  1167. var width = marginLeft + marginRight;
  1168. var marginTop = parseFloat(this.getStyle(element, "margin-top")) || 0;
  1169. var marginBottom = parseFloat(this.getStyle(element, "margin-bottom")) || 0;
  1170. var height = marginTop + marginBottom;
  1171. return { "width":width, "height":height };
  1172. },
  1173. // Set element visibility
  1174. setVisible: function(element, show, fadeout)
  1175. {
  1176. if(fadeout && !show)
  1177. {
  1178. var opacity = 1;
  1179. function renderFrame()
  1180. {
  1181. opacity -= .1;
  1182. if(opacity<=0)
  1183. {
  1184. element.style.opacity = "initial";
  1185. element.style.display = "none";
  1186. } else {
  1187. element.style.opacity = opacity;
  1188. requestAnimationFrame(renderFrame);
  1189. }
  1190. }
  1191. renderFrame();
  1192. } else {
  1193. element.style.display = show ? "block" : "none";
  1194. }
  1195. },
  1196. // Check if element exists and is visible
  1197. isVisible: function(element)
  1198. {
  1199. return element && element.style.display!="none";
  1200. },
  1201. // Convert first letter to lowercase
  1202. toLowerFirst: function(string)
  1203. {
  1204. return string.charAt(0).toLowerCase()+string.slice(1);
  1205. },
  1206. // Convert first letter to uppercase
  1207. toUpperFirst: function(string)
  1208. {
  1209. return string.charAt(0).toUpperCase()+string.slice(1);
  1210. },
  1211. // Return lines from text string, including newline
  1212. getTextLines: function(string)
  1213. {
  1214. var lines = string.split("\n");
  1215. for(var i=0; i<lines.length; i++) lines[i] = lines[i]+"\n";
  1216. if(string.length==0 || string.charAt(string.length-1)=="\n") lines.pop();
  1217. return lines;
  1218. },
  1219. // Encode HTML special characters
  1220. encodeHtml: function(string)
  1221. {
  1222. return string
  1223. .replace(/&/g, "&amp;")
  1224. .replace(/</g, "&lt;")
  1225. .replace(/>/g, "&gt;")
  1226. .replace(/"/g, "&quot;");
  1227. },
  1228. // Submit form with post method
  1229. submitForm: function(args)
  1230. {
  1231. var elementForm = document.createElement("form");
  1232. elementForm.setAttribute("method", "post");
  1233. for(var key in args)
  1234. {
  1235. if(!args.hasOwnProperty(key)) continue;
  1236. var elementInput = document.createElement("input");
  1237. elementInput.setAttribute("type", "hidden");
  1238. elementInput.setAttribute("name", key);
  1239. elementInput.setAttribute("value", args[key]);
  1240. elementForm.appendChild(elementInput);
  1241. }
  1242. document.body.appendChild(elementForm);
  1243. elementForm.submit();
  1244. }
  1245. };
  1246. yellow.edit.intervalId = setInterval("yellow.onLoad()", 1);