admin-add-tool-form.html 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <link rel="import" href="../../bower_components/polymer/polymer.html">
  2. <link rel="import" href="../../bower_components/iron-form/iron-form.html">
  3. <link rel="import" href="../../bower_components/paper-button/paper-button.html">
  4. <link rel="import" href="../../bower_components/paper-input/paper-input.html">
  5. <link rel="import" href="../../bower_components/paper-material/paper-material.html">
  6. <link rel="import" href="../../bower_components/paper-toast/paper-toast.html">
  7. <dom-module id="admin-add-tool-form">
  8. <template>
  9. <style is="custom-style">
  10. .container {
  11. margin: 16px;
  12. padding: 16px;
  13. background-color: white;
  14. }
  15. .button {
  16. margin-top: 16px;
  17. }
  18. .login {
  19. color: white;
  20. background-color: var(--paper-blue-300);
  21. }
  22. .login[disabled] {
  23. background-color: var(--paper-blue-100);
  24. }
  25. .cancel {
  26. color: white;
  27. background-color: var(--paper-red-300);
  28. }
  29. .toast {
  30. }
  31. .success {
  32. background: var(--paper-green-300);
  33. border: 1px solid var(--paper-green-500);
  34. }
  35. .failure {
  36. background: var(--paper-red-300);
  37. border: 1px solid var(--paper-red-500);
  38. }
  39. </style>
  40. <paper-material id="container" class="container">
  41. <form id="addToolForm" is="iron-form" method="post" action="/api/tool/guarded">
  42. <paper-input id="toolId" label="Tool Id" type="number" required auto-validate></paper-input>
  43. <paper-input id="toolName" label="Tool Name" type="text" required auto-validate></paper-input>
  44. <paper-button id="submitButton" class="button login" raised disabled>Add Tool</paper-button>
  45. <paper-button id="cancelButton" class="button cancel" raised>Cancel</paper-button>
  46. </form>
  47. </paper-material>
  48. <paper-toast id="successToast" class="toast success" text="{{successText}}"></paper-toast>
  49. <paper-toast id="failureToast" class="toast failure" text="{{failureText}}"></paper-toast>
  50. </template>
  51. <script>
  52. Polymer({
  53. is: 'admin-add-tool-form',
  54. properties: {
  55. /**
  56. * Message to display on the failure toast
  57. */
  58. failureText: {
  59. type: String,
  60. value: "Failed to add tool!"
  61. },
  62. /**
  63. * Messsage to display on the success toast
  64. */
  65. successText: {
  66. type: String,
  67. value: "Added Tool!"
  68. }
  69. },
  70. //Polymer lifecycle method when element is attached to the DOM
  71. attached: function () {
  72. var me = this;
  73. var addToolForm = me.$.addToolForm;
  74. var toolId = me.$.toolId;
  75. var toolName = me.$.toolName;
  76. var submitButton = me.$.submitButton;
  77. var cancelButton = me.$.cancelButton;
  78. var successToast = me.$.successToast;
  79. successToast.fitInto = me.parentElement;
  80. var failureToast = me.$.failureToast;
  81. failureToast.fitInto = me.parentElement;
  82. submitButton.addEventListener('tap', function () {
  83. addToolForm.submit();
  84. });
  85. addToolForm.addEventListener('change', function (event) {
  86. // Validate the entire form to see if we should enable the `Submit` button.
  87. submitButton.disabled = !addToolForm.validate();
  88. });
  89. //Event that fires before the form is submitted. Adds the inputs to the body of the AJAX request.
  90. addToolForm.addEventListener('iron-form-presubmit', function (event) {
  91. this.request.method = "POST";
  92. this.request.contentType = 'application/json';
  93. this.request.body = {
  94. token: sessionStorage.getItem('token'),
  95. toolId: toolId.value,
  96. toolName: toolName.value
  97. };
  98. });
  99. //Event that fires if an error is encountered
  100. addToolForm.addEventListener('iron-form-error', function (event) {
  101. failureToast.open();
  102. });
  103. //Event the fires when a response is received
  104. addToolForm.addEventListener('iron-form-response', function (event) {
  105. var response = event.detail.response;
  106. //Check that the document was actually added.
  107. //This *shouldn't* be required, but just check anyways
  108. if (response.created == true) {
  109. successToast.open();
  110. }
  111. else {
  112. console.log("HTTP 201 received but document wasn't added... ");
  113. console.log(response);
  114. }
  115. });
  116. },
  117. });
  118. </script>
  119. </dom-module>