admin-markdown-editor.html 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <link rel="import" href="../../../bower_components/polymer/polymer.html">
  2. <link rel="import" href="../../../bower_components/polymer/polymer.html">
  3. <link rel="import" href="../../../bower_components/paper-button/paper-button.html">
  4. <link rel="import" href="../../../bower_components/paper-toast/paper-toast.html">
  5. <link rel="import" href="../../../bower_components/iron-flex-layout/iron-flex-layout-classes.html">
  6. <link rel="import" href="../../../bower_components/vaadin-combo-box/vaadin-combo-box.html">
  7. <link rel="import" href="../../markdown-editor.html">
  8. <dom-module id="admin-markdown-editor">
  9. <template>
  10. <style is="custom-style" include="iron-flex iron-positioning"></style>
  11. <style>
  12. .container {
  13. height: 100%;
  14. }
  15. .row {
  16. margin-top: 16px;
  17. margin-bottom: 16px;
  18. }
  19. vaadin-combo-box {
  20. margin-left: 16px;
  21. margin-right: 16px;
  22. }
  23. .button {
  24. background-color: var(--zebra-blue);
  25. color: white;
  26. }
  27. .editor {
  28. height: 100%;
  29. }
  30. </style>
  31. <div id="container" class="container vertical layout">
  32. <div class="row horizontal layout">
  33. <vaadin-combo-box id="toolList" label="Tools" class="flex" items="{{tools}}"
  34. value="JIRA"></vaadin-combo-box>
  35. <vaadin-combo-box id="sectionList" label="Sections" class="flex"
  36. items="{{sectionNames}}" value="About"></vaadin-combo-box>
  37. <paper-input id="itemList" class="flex" label="Item #" type="number" required></paper-input>
  38. </div>
  39. <div class="row horizontal layout">
  40. <paper-button id="load" raised class="button flex">LOAD</paper-button>
  41. <paper-button id="save" raised class="button flex">SAVE</paper-button>
  42. </div>
  43. <div class="row horizontal layout">
  44. <paper-input id="heading" class="flex" label="Heading" type="text" value="{{heading}}"></paper-input>
  45. </div>
  46. <div class="container vertical layout">
  47. <markdown-editor id="editor" class="editor" markdown="{{markdown}}"></markdown-editor>
  48. </div>
  49. </div>
  50. <paper-toast id="successToast" text="Saved!"></paper-toast>
  51. </template>
  52. <script>
  53. Polymer({
  54. is: 'admin-markdown-editor',
  55. properties: {
  56. heading: {
  57. type: String,
  58. value: "Test Heading"
  59. },
  60. /**
  61. * The markdown string in the editor
  62. */
  63. markdown: {
  64. type: String
  65. },
  66. /**
  67. * Array of tool id/name combos
  68. */
  69. tools: {
  70. type: Array
  71. },
  72. /**
  73. * Array of sections names for the tools
  74. */
  75. sectionNames: {
  76. type: Array,
  77. value: ["About", "Training", "Links"]
  78. }
  79. },
  80. //Polymer lifecycle method when element is attached to the DOM
  81. attached: function () {
  82. var me = this;
  83. var toolList = me.$.toolList;
  84. var sectionList = me.$.sectionList;
  85. var itemList = me.$.itemList;
  86. var loadButton = me.$.load;
  87. var saveButton = me.$.save;
  88. var heading = me.$.heading;
  89. var successToast = me.$.successToast;
  90. successToast.fitInto = me.$.container;
  91. //Load in the tools
  92. var tools_ajax = document.createElement('iron-ajax');
  93. tools_ajax.auto = true;
  94. tools_ajax.method = "GET";
  95. tools_ajax.url = "/api/tools/info";
  96. //When we get a response from our AJAX call
  97. tools_ajax.addEventListener("response", function (e) {
  98. //Grab the tools from the response
  99. var tools = e.detail.response;
  100. //We need to transform the array to fit to <vaadin-combo-box> requirements
  101. me.tools = tools.map(function (obj) {
  102. return rObj = {
  103. value: obj.id,
  104. label: obj.name
  105. };
  106. });
  107. });
  108. loadButton.addEventListener('tap', function () {
  109. me.markdown = me._getMarkdown(toolList.value, sectionList.value.toLowerCase(), itemList.value);
  110. });
  111. saveButton.addEventListener('tap', function () {
  112. me._setMarkdown(heading.value, toolList.value, sectionList.value.toLowerCase());
  113. });
  114. },
  115. /**
  116. * Method for retrieving the markdown for the given tool&section combo
  117. *
  118. * Sends the tool name and section name to the DB and receives a response containing the markdown string
  119. *
  120. * @param toolId The id of the tool to retrieve
  121. * @param sectionName The name of the section to retrieve
  122. * @private
  123. */
  124. _getMarkdown: function (toolId, sectionName, itemNum) {
  125. var me = this;
  126. var markdown_ajax = document.createElement('iron-ajax');
  127. markdown_ajax.auto = true;
  128. markdown_ajax.method = "GET";
  129. markdown_ajax.url = "/api/tool/" + toolId + "/" + sectionName + "/" + itemNum;
  130. //When we get a response from our AJAX call
  131. markdown_ajax.addEventListener("response", function (e) {
  132. //Set the current markdown to the received markdown
  133. me.heading = e.detail.response.heading;
  134. me.markdown = e.detail.response.contents;
  135. });
  136. },
  137. /**
  138. * Method for setting the markdown string of the given tool and section
  139. *
  140. * A `paper-toast` will pop up if the markdown was set successfully
  141. *
  142. * @param markdown The markdown string to be set
  143. * @param toolName The name of the tool to set the markdown for
  144. * @param sectionName The name of the section to set the markdown for
  145. * @private
  146. */
  147. _setMarkdown: function (heading, markdown, toolId, sectionName) {
  148. var me = this;
  149. var markdown_ajax = document.createElement('iron-ajax');
  150. markdown_ajax.auto = true;
  151. markdown_ajax.body = {heading: heading, contents: markdown};
  152. markdown_ajax.contentType = 'application/json';
  153. markdown_ajax.method = "POST";
  154. markdown_ajax.url = "/api/tool/" + toolId + "/" + sectionName;
  155. //When we get a response from our AJAX call
  156. markdown_ajax.addEventListener("response", function (e) {
  157. console.log(e.detail.response);
  158. });
  159. }
  160. });
  161. </script>
  162. </dom-module>