build.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. ---
  2. layout: default
  3. # This is where most of the code for CommunityRule lives
  4. # Follow comments below in various sections for further explanation
  5. ---
  6. <script>
  7. // Enter JavaScript-land!
  8. // First, some functions, followed by initialization commands
  9. // Begin BUILDER functions
  10. // source: https://www.codecanal.com/html5-drag-and-copy/
  11. function allowDrop(ev) {
  12. ev.preventDefault();
  13. }
  14. function drag(ev) {
  15. ev.dataTransfer.setData("text", ev.target.id);
  16. }
  17. function drop(ev) {
  18. ev.preventDefault();
  19. var target = ev.target;
  20. // Prevents dropping into text field
  21. if (target.id == "drag-directions") {
  22. target = target.parentElement;
  23. }
  24. // Set up transfer
  25. var data = ev.dataTransfer.getData("text");
  26. // Iff module is from the menu clone it
  27. var module = document.getElementById(data);
  28. if (module.parentElement.id == "module-menu") {
  29. module = module.cloneNode(true);
  30. // Add a corresponding text field
  31. var name = module.innerHTML.replace(/(<([^>]+)>)/ig,'');
  32. var query = "Explain how the <strong>" + name + "</strong> module works.";
  33. addField(query);
  34. }
  35. // append name
  36. module.id += "-dropped";
  37. // display the deletion button
  38. module.children[2].style.display = "inline";
  39. // pop it in!
  40. target.appendChild(module);
  41. // be sure the dummy text is gone
  42. document.getElementById("drag-directions").style.display = "none";
  43. // Send field contents to console to aid in database stuff
  44. console.log(document.getElementById("module-input"));
  45. }
  46. // Adds a new question field
  47. function addField(content) {
  48. // if input is null, add an empty field to the list
  49. // include a field removal button
  50. var destination = document.getElementById("builder-field");
  51. if (content == null) { content = ""; }
  52. var output = '\n<div><a onclick="this.parentNode.remove()"><img src="{% link assets/tabler_icons/x.svg %}" class="delete-module" align="right" /></a>';
  53. output += '<span class="question">' + content + '</span>';
  54. output += '<p contenteditable="true" class="editable output" id="custom-field"></p></div>\n';
  55. destination.innerHTML += output;
  56. }
  57. // end BUILDER functions
  58. // toggleVisible(id)
  59. // Toggles the visibility of a given element by given ID
  60. function toggleVisible(id) {
  61. var x = document.getElementById(id);
  62. if (x.style.display === "none") {
  63. x.style.display = "block";
  64. } else {
  65. x.style.display = "none";
  66. }
  67. }
  68. // classDisplayAll(className, value)
  69. // Assigns given display value to all elements with a given className
  70. function classDisplayAll(className, value) {
  71. var elements = document.getElementsByClassName(className);
  72. for (var i = 0; i < elements.length; i++) {
  73. elements[i].style.display = value;
  74. }
  75. }
  76. // toggleEditMode()
  77. // Toggles whether editable fields are editable or not
  78. // and removes editing tools.
  79. function toggleEditMode() {
  80. if (editMode === true) { // switch to preview mode
  81. editMode = false;
  82. classDisplayAll("section","block");
  83. classDisplayAll("button","none");
  84. classDisplayAll("question","none");
  85. classDisplayAll("delete-module","none");
  86. var editableFields = document.getElementsByClassName("editable");
  87. // de-editable-ize the editable fields
  88. for (var i = 0; i < editableFields.length; i++) {
  89. editableFields[i].contentEditable = "false";
  90. editableFields[i].style.borderStyle = "none";
  91. // Remove empty fields entirely
  92. var content = editableFields[i].innerHTML;
  93. content = content.replace(/(<([^>]+)>)/ig,''); // strips stray tags
  94. if (content === "") {
  95. editableFields[i].style.display = "none";
  96. }
  97. }
  98. // Remove headers of empty sections
  99. // Inefficient! Might be merged with the above iteration
  100. var sections = document.getElementsByClassName("section");
  101. for (var i = 0; i < sections.length; i++) {
  102. var sectionQuestions = sections[i].getElementsByClassName("editable");
  103. var blanks = 0;
  104. for (var x = 0; x < sectionQuestions.length; x++) {
  105. var content = sectionQuestions[x].innerHTML;
  106. content = content.replace(/(<([^>]+)>)/ig,''); // strips stray tags
  107. if (content == "") { blanks++; }
  108. if (blanks == sectionQuestions.length) {
  109. var headerID = "header-s" + (i + 1);
  110. document.getElementById(headerID).style.display = "none";
  111. }
  112. }
  113. }
  114. // Handle links
  115. // TKTK
  116. // Handle author link
  117. var authorName = document.getElementById("author-text").value;
  118. var authorURL = document.getElementById("author-url").value;
  119. if (authorName != "") {
  120. document.getElementById("authorship-words").style.display = "inline";
  121. if (authorURL != "") { // both author and URL present
  122. document.getElementById("authorship-result").innerHTML = "<a href='" + authorURL +"'>" + authorName + "</a>";
  123. document.getElementById("authorship-result").style.display = "inline";
  124. } else { // only authorName present
  125. document.getElementById("authorship-result").innerHTML = authorName;
  126. document.getElementById("authorship-result").style.display = "inline";
  127. }
  128. } else {
  129. document.getElementById("authorship").style.display = "none";
  130. }
  131. // Finally, change button name
  132. document.getElementById("editToggle").innerHTML = "Customize";
  133. } else { // Switch to editMode
  134. editMode = true;
  135. classDisplayAll("button","block");
  136. classDisplayAll("question","block");
  137. classDisplayAll("editable","block");
  138. classDisplayAll("header","block");
  139. classDisplayAll("section","none");
  140. classDisplayAll("link-text","inline");
  141. classDisplayAll("link-url","inline");
  142. classDisplayAll("delete-module","inline");
  143. // link handling TKTK
  144. // author handling
  145. document.getElementById("authorship-result").style.display = "none";
  146. document.getElementById("authorship-words").style.display = "none";
  147. document.getElementById("authorship").style.display = "block";
  148. // make all editable fields visible
  149. var editableFields = document.getElementsByClassName("editable");
  150. for (var i = 0; i < editableFields.length; i++) {
  151. editableFields[i].style.borderStyle = "none none dashed none";
  152. editableFields[i].contentEditable = "true";
  153. }
  154. // Change button name
  155. document.getElementById("editToggle").innerHTML = "Preview";
  156. }
  157. }
  158. // toggleDisplayMode()
  159. // toggles full displayMode, the Rule-only display for a published Rule
  160. // first, initialize variable:
  161. var displayMode = false;
  162. function toggleDisplayMode() {
  163. if (displayMode == false) {
  164. editMode = true;
  165. toggleEditMode(); // turns off editMode
  166. classDisplayAll("site-nav","none");
  167. classDisplayAll("post-header","none");
  168. classDisplayAll("site-footer","none");
  169. document.getElementById("attribution").style.display = "block";
  170. document.getElementById("toggleDisplayMode").style.display = "inline-block";
  171. document.getElementById("publishRule").style.display = "none";
  172. document.getElementById("trash").style.display = "inline-block";
  173. displayMode = true;
  174. } else {
  175. toggleEditMode() // turns on editMode
  176. classDisplayAll("site-nav","block");
  177. classDisplayAll("post-header","block");
  178. classDisplayAll("site-footer","block");
  179. document.getElementById("attribution").style.display = "none";
  180. document.getElementById("toggleDisplayMode").style.display = "none";
  181. document.getElementById("publishRule").style.display = "inline-block";
  182. document.getElementById("trash").style.display = "none";
  183. displayMode = false;
  184. }
  185. }
  186. // textOutput()
  187. // Produces Markdown rendition of Rule from Export button
  188. function textOutput() {
  189. var filename = 'GOVERNANCE.md';
  190. // First, add title, whether there is one or not
  191. var content = '# '+ document.getElementById('communityname').innerHTML + '\n\n';
  192. content = content.replace(/(<([^>]+)>)/ig,''); // strips stray tags
  193. // Now, begin adding other elements
  194. var elements = document.getElementsByClassName('output');
  195. for (var i = 1; i < elements.length; i++) {
  196. var thisBit = elements[i].innerHTML;
  197. thisBit = thisBit.replace(/(<([^>]+)>)/ig,''); // strips stray tags
  198. if (thisBit != "") {
  199. if (elements[i].classList.contains("subhead")) {
  200. // Before printing subhead, make sure it's not empty
  201. var i2 = i + 1;
  202. while ((i2 < elements.length) &&
  203. (!(elements[i2].classList.contains("subhead")))) {
  204. if (elements[i2].innerHTML != "") {
  205. // in this case, it's not empty, so print and move on
  206. content += '## ';
  207. content += thisBit + '\n\n';
  208. break;
  209. } else { i2++; }
  210. } // won't print anything if a subhead has only empty children
  211. } else {
  212. // Non-subhead elements can just go ahead and print
  213. content += thisBit + '\n\n';
  214. }
  215. }
  216. }
  217. // Add authorship block
  218. var authorName = document.getElementById("author-text").value;
  219. var authorURL = document.getElementById("author-url").value;
  220. var authorshipBlock = "---\n\nCreated by ";
  221. if (authorName != "") {
  222. if (authorURL != "") { // both author and URL present
  223. authorshipBlock += ("[" + authorName + "](" + authorURL + ")");
  224. } else { // only authorName present
  225. authorshipBlock += authorName;
  226. }
  227. content += (authorshipBlock + "\n");
  228. }
  229. // Add attribution block
  230. content += document.getElementById('attributionMD').innerHTML;
  231. // Starting here, see https://stackoverflow.com/a/33542499
  232. var blob = new Blob([content], {type: 'text/plain'});
  233. if(window.navigator.msSaveOrOpenBlob) {
  234. window.navigator.msSaveBlob(blob, filename);
  235. }
  236. else{
  237. var elem = window.document.createElement('a');
  238. elem.href = window.URL.createObjectURL(blob);
  239. elem.download = filename;
  240. document.body.appendChild(elem);
  241. elem.click();
  242. document.body.removeChild(elem);
  243. URL.revokeObjectURL(); // This needs an arg but I can't figure out what
  244. }
  245. var myFile = new Blob([fileContent], {type: 'text/plain'});
  246. window.URL = window.URL || window.webkitURL; document.getElementById('download').setAttribute('href',window.URL.createObjectURL(myFile));
  247. document.getElementById('download').setAttribute('download', fileName);
  248. }
  249. // BEGIN Publish tools, via SteinHQ.com
  250. // publishRule()
  251. // Publishes existing fields to new page, /create/?rule=[ruleID]
  252. // Opens new page in Display mode
  253. function publishRule() {
  254. // Confirm user knows what they're getting into
  255. var r = confirm("Publish to the public Library?");
  256. if (r == false) { return; }
  257. // Proceed with publication
  258. var now = new Date();
  259. // Numerical ID for published Rule
  260. var timeID = now.getTime();
  261. // Readable UTC timestamp
  262. var dateTime = now.getUTCFullYear()+'.'+(now.getUTCMonth()+1)+'.'+now.getUTCDate()
  263. +' '+now.getUTCHours()+":"+ now.getUTCMinutes()+":"+now.getUTCSeconds()
  264. + ' UTC';
  265. // TKTK: Check if ruleID exists; while yes, replace and repeat
  266. var rule = [{
  267. ruleID: timeID,
  268. timestamp: dateTime,
  269. }];
  270. var fields = document.getElementsByClassName("editable");
  271. for (var i = 0; i < fields.length; i++) {
  272. var key = fields[i].id;
  273. var value = "";
  274. if (fields[i].nodeName == "INPUT") { // for <input>
  275. value = fields[i].value.replace(/(<([^>]+)>)/ig,"");
  276. } else { // for other fields
  277. value = fields[i].innerHTML.replace(/(<([^>]+)>)/ig,"");
  278. }
  279. rule[0][key] = value;
  280. }
  281. const store = new SteinStore(
  282. "https://api.steinhq.com/v1/storages/5e8b937ab88d3d04ae0816a5"
  283. );
  284. store.append("rules", rule).then(data => {
  285. window.open("/create/?r=" + timeID, "_self", false);
  286. });
  287. }
  288. // displayRule(ID)
  289. // Displays content based on ID
  290. function displayRule(ID) {
  291. const store = new SteinStore(
  292. "https://api.steinhq.com/v1/storages/5e8b937ab88d3d04ae0816a5"
  293. );
  294. store.read("rules", { search: { ruleID: ID } }).then(data => {
  295. // only runs when we have the data from Goog:
  296. var rule = data[0];
  297. var fields = document.getElementsByClassName("editable");
  298. for (var i = 0; i < fields.length; i++) {
  299. var key = fields[i].id;
  300. var value = rule[key];
  301. if (typeof value === "undefined") {
  302. value = "";
  303. } else if (key.includes("-")) { // links
  304. document.getElementById(key).value = value;
  305. } else {
  306. document.getElementById(key).innerHTML = value;
  307. }
  308. }
  309. // Publish timestamp to Rule
  310. document.getElementById('dateTime').innerHTML = rule['timestamp'];
  311. // Finish
  312. displayMode = false;
  313. toggleDisplayMode();
  314. document.title = rule['communityname'] + " / CommunityRule";
  315. });
  316. }
  317. // deleteRule()
  318. // A temporary placeholder that sends an email requesting rule deletion
  319. function deleteRule() {
  320. var urlParamz = new URLSearchParams(window.location.search);
  321. var rID = urlParamz.get('r');
  322. window.open("mailto:medlab@colorado.edu?subject=Delete Rule request ("
  323. + rID + ")&body=Please explain your rationale:\n");
  324. }
  325. // END Publish tools
  326. // FINALLY, Page loading
  327. // First, grab the current URL
  328. var urlParams = new URLSearchParams(window.location.search);
  329. // Determine if it is a published Rule
  330. if (urlParams.has('r')) {
  331. // If so, grab the Rule from database and enter displayMode
  332. var rID = urlParams.get('r');
  333. displayRule(rID);
  334. } else {
  335. // Otherwise, open in editMode as default
  336. var editMode = true;
  337. // switch out of editMode in special cases
  338. window.onload = function() {
  339. if ((window.location.href.indexOf("/templates/") != -1) ||
  340. (window.location.href.indexOf("/about/") != -1)) {
  341. toggleEditMode();
  342. }
  343. }
  344. }
  345. </script>
  346. <article class="post">
  347. <header class="post-header">
  348. <h1 class="post-title" id="title">
  349. {{ page.title }}
  350. </h1>
  351. <button class="pushButton" id="editToggle" onclick="toggleEditMode()">
  352. Preview</button>
  353. <div class="post-content">
  354. {{ content }}
  355. </div>
  356. </header>
  357. <div id="rulebox">
  358. <span class="question">What is the community’s name?</span>
  359. <h1 contenteditable="true" class="editable output" id="communityname">{{ page.community-name }}</h1>
  360. <!-- BUILDER -->
  361. <div ondrop="drop(event)" ondragover="allowDrop(event)"
  362. class="modulebox" id="module-input">
  363. <span class="question" id="drag-directions">
  364. Drag modules from the icon at right</span>
  365. <div class="button">
  366. <img src="{% link assets/tabler_icons/tool.svg %}" title="Modules" />
  367. <div class="tooltiptext" id="module-menu">
  368. <!-- Customizable module -->
  369. <span class="module" id="module-custom"
  370. draggable="true" ondragstart="drag(event)">
  371. <input contenteditable="true" placeholder="Custom..." />
  372. <img src="{% link assets/tabler_icons/bulb.svg %}"
  373. draggable="false" />
  374. <a onclick="this.parentNode.remove()" class="delete-module"
  375. style="display:none">
  376. <img src="{% link assets/tabler_icons/x.svg %}" /></a>
  377. </span>
  378. <!-- Load preset modules from _data/modules.csv -->
  379. {% for module in site.data.modules %}
  380. <span class="module fixed" id="module-{{ module.id }}"
  381. draggable="true" ondragstart="drag(event)">
  382. <span>{{ module.name }}</span>
  383. <a target="_blank" href="{{ module.url }}">
  384. <img src="{% link assets/tabler_icons/link.svg %}"
  385. draggable="false" /></a>
  386. <a onclick="this.parentNode.remove()" class="delete-module"
  387. style="display:none">
  388. <img src="{% link assets/tabler_icons/x.svg %}" /></a>
  389. </span>
  390. {% endfor %}
  391. </div>
  392. </div>
  393. </div>
  394. <div id="builder-field">
  395. </div>
  396. <div id="authorship" class="linkbox">
  397. <span id="authorship-words">Created by</span>
  398. <input contenteditable="true" class="editable link-text" id="author-text" placeholder="Created by" />
  399. <span class="link-divider"><img src="{% link assets/tabler_icons/pencil.svg %}" title="Add link" /></span>
  400. <input contenteditable="true" class="editable link-url" id="author-url" placeholder="Creator URL (http://, https://)" type="url" pattern="http://.*|https://.*" />
  401. <span id="authorship-result"></span>
  402. </div>
  403. </div><!--#rulebox-->
  404. <div id="attribution" style="display:none;">
  405. <br />
  406. <p><a href="https://communityrule.info">
  407. <img src="https://communityrule.info{% link assets/CommunityRule-derived-000000.svg %}" alt="CommunityRule derived"></a></p>
  408. <p id="dateTime"></p>
  409. <p>Created with <a href="https://communityrule.info">CommunityRule</a><br />
  410. <a href="https://creativecommons.org/licenses/by-sa/4.0/">Creative Commons BY-SA</a></p>
  411. <p><strong>The Publish feature is experimental. Rules may be removed without notice</strong></p>
  412. </div>
  413. <div id="attributionMD" style="display:none;">
  414. ---
  415. [![CommunityRule derived](https://communityrule.info{% link assets/CommunityRule-derived-000000.svg %})](https://communityrule.info)
  416. [Creative Commons BY-SA](https://creativecommons.org/licenses/by-sa/4.0/)</div>
  417. <!--
  418. <button class="pushButton" id="publishRule" onclick="publishRule()"
  419. title="Add to the public Library">Publish</button>
  420. <button class="pushButton" id="toggleDisplayMode" onclick="toggleDisplayMode()"
  421. title="Edit this Rule into a new one">Fork</button>
  422. <button class="pushButton" onclick="textOutput()"
  423. title="Download this Rule as a Markdown text file">Export</button>
  424. <button class="pushButton" id="trash" onclick="deleteRule()">
  425. <img src="{% link assets/tabler_icons/trash.svg %}" title="Rule deletion request" />
  426. </button>
  427. <button class="pushButton"
  428. onclick="javascript:location.href='https://www.colorado.edu/lab/medlab/content/communityrule-user-feedback'">
  429. Feedback
  430. </button>
  431. -->
  432. <span class="question">Publish and Export are not yet available for the RuleBuilder</span>
  433. </article>