admin-markdown-add.html 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <link rel="import" href="../../../bower_components/polymer/polymer.html">
  2. <link rel="import" href="../../../bower_components/iron-ajax/iron-ajax.html">
  3. <link rel="import" href="../../../bower_components/iron-flex-layout/iron-flex-layout-classes.html">
  4. <link rel="import" href="../../../bower_components/paper-button/paper-button.html">
  5. <link rel="import" href="../../../bower_components/paper-input/paper-input.html">
  6. <link rel="import" href="../../../bower_components/paper-material/paper-material.html">
  7. <link rel="import" href="../../../bower_components/paper-toast/paper-toast.html">
  8. <link rel="import" href="../../../bower_components/vaadin-combo-box/vaadin-combo-box.html">
  9. <link rel="import" href="../../markdown-editor.html">
  10. <dom-module id="admin-markdown-add">
  11. <template>
  12. <style is="custom-style" include="iron-flex iron-positioning"></style>
  13. <style>
  14. paper-material {
  15. padding: 8px;
  16. margin: 8px;
  17. background-color: white;
  18. }
  19. markdown-editor {
  20. background-color: white;
  21. }
  22. .separator {
  23. margin: 0 4px;
  24. }
  25. .button {
  26. color: white;
  27. background-color: var(--paper-blue-300);
  28. }
  29. .markdown-container {
  30. margin: 8px;
  31. }
  32. .toast {
  33. }
  34. .success {
  35. background: var(--paper-green-300);
  36. border: 1px solid var(--paper-green-500);
  37. }
  38. .failure {
  39. background: var(--paper-red-300);
  40. border: 1px solid var(--paper-red-500);
  41. }
  42. </style>
  43. <paper-material class="header">
  44. This form adds a new item to the markdown list
  45. </paper-material>
  46. <paper-material class="options horizontal layout">
  47. <vaadin-combo-box id="toolList" label="Tool" class="flex" items="[[tools]]">
  48. </vaadin-combo-box>
  49. <div class="separator"></div>
  50. <vaadin-combo-box id="sectionList" label="Sections" class="flex"
  51. items="{{sectionNames}}" value="About">
  52. </vaadin-combo-box>
  53. <paper-button id="save" class="button primary flex" on-tap="_addMarkdown" raised>ADD</paper-button>
  54. </paper-material>
  55. <paper-material class="horizontal layout">
  56. <paper-input id="heading" class="flex" label="Heading" type="text"></paper-input>
  57. </paper-material>
  58. <div class="markdown-container vertical layout">
  59. <markdown-editor id="editor"></markdown-editor>
  60. </div>
  61. <paper-toast id="successToast" class="toast success" text="{{message.success}}"></paper-toast>
  62. <paper-toast id="failureToast" class="toast failure" text="{{message.failure}}"></paper-toast>
  63. </template>
  64. <script>
  65. Polymer({
  66. is: 'admin-markdown-add',
  67. properties: {
  68. /**
  69. * Object for success/failure messages
  70. */
  71. message: {
  72. type: Object,
  73. value: {
  74. failure: "Failed to add tool!",
  75. success: "Markdown added successfully!"
  76. }
  77. },
  78. /**
  79. * Names of the possible sections to add to
  80. */
  81. sectionNames: {
  82. type: Array,
  83. value: ['About', 'Training', 'Links']
  84. },
  85. /**
  86. * Array of tool names
  87. */
  88. tools: {
  89. type: Array,
  90. }
  91. },
  92. ready: function () {
  93. var me = this;
  94. //Tell the paper-toast's to fit into the parent element
  95. me.$.successToast.fitInto = me.parentElement;
  96. me.$.failureToast.fitInto = me.parentElement;
  97. //Load in the tools to populate the <vaadin-combo-box>
  98. me.tools = me._getTools();
  99. },
  100. /**
  101. * Helper function for getting the tools to populate the tool list
  102. * @private
  103. */
  104. _getTools: function () {
  105. var me = this;
  106. var tools_ajax = document.createElement('iron-ajax');
  107. tools_ajax.auto = true;
  108. tools_ajax.method = "GET";
  109. tools_ajax.url = "/api/tools/info";
  110. tools_ajax.addEventListener("response", function (e) {
  111. //Grab the tools from the response
  112. var tools = e.detail.response;
  113. //We need to transform the array to fit to <vaadin-combo-box> requirements
  114. me.tools = tools.map(function (obj) {
  115. return rObj = {
  116. value: obj.id,
  117. label: obj.name
  118. };
  119. });
  120. });
  121. },
  122. /**
  123. * Helper method for adding the markdown to the section
  124. * @private
  125. */
  126. _addMarkdown: function () {
  127. var me = this;
  128. var toolListVal = me.$.toolList.value;
  129. var sectionListVal = me.$.sectionList.value;
  130. var successToast = me.$.successToast;
  131. var failureToast = me.$.failureToast;
  132. //Check that the values were supplied
  133. if (toolListVal && sectionListVal) {
  134. var heading = me.$.heading.value;
  135. var contents = me.$.editor.markdown;
  136. //Check that the values were supplied
  137. if (heading && contents) {
  138. //Get the token for authentication
  139. var token = sessionStorage.getItem('token');
  140. var ajax = document.createElement('iron-ajax');
  141. ajax.auto = true;
  142. ajax.body = {token: token, heading: heading, contents: contents};
  143. ajax.contentType = 'application/json';
  144. ajax.method = "POST";
  145. ajax.url = "/api/tool/guarded/" + toolListVal + "/" + sectionListVal.toLowerCase();
  146. //When there is an error (HTTP 4XX)
  147. ajax.addEventListener('error', function (event) {
  148. me.set('message.failure', "Failed to add markdown...");
  149. failureToast.open();
  150. });
  151. ajax.addEventListener('response', function (event) {
  152. successToast.open();
  153. });
  154. }
  155. //Missing heading or contents
  156. else {
  157. if (!heading)
  158. me.set('message.failure', "Missing heading. Make sure you enter a heading.");
  159. else
  160. me.set('message.failure', "Missing contents. Make sure you entered some markdown.");
  161. failureToast.open();
  162. }
  163. }
  164. //Missing Tool or Section
  165. else {
  166. if (!toolListVal)
  167. me.set('message.failure', "Missing tool. Make sure you selected a tool.");
  168. else
  169. me.set('message.failure', "Missing section. Make sure you selected a section.");
  170. failureToast.open();
  171. }
  172. },
  173. });
  174. </script>
  175. </dom-module>