build.html 19 KB

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