edit.js 82 KB

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