jira-login-dialog.html 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <link rel="import" href="../../../bower_components/polymer/polymer.html">
  2. <link rel="import" href="../../../bower_components/iron-flex-layout/iron-flex-layout-classes.html">
  3. <link rel="import" href="../../../bower_components/iron-form/iron-form.html">
  4. <link rel="import" href="../../../bower_components/iron-icon/iron-icon.html">
  5. <link rel="import" href="../../../bower_components/paper-button/paper-button.html">
  6. <link rel="import" href="../../../bower_components/paper-dialog/paper-dialog.html">
  7. <link rel="import" href="../../../bower_components/paper-icon-button/paper-icon-button.html">
  8. <link rel="import" href="../../../bower_components/paper-input/paper-input.html">
  9. <link rel="import" href="../../../bower_components/paper-spinner/paper-spinner.html">
  10. <dom-module id="jira-login-dialog">
  11. <style is="custom-style" include="iron-positioning iron-flex iron-flex-alignment"></style>
  12. <style>
  13. .button {
  14. color: white;
  15. }
  16. .button[disabled] {
  17. opacity: 0.25;
  18. }
  19. .login {
  20. background-color: var(--zebra-blue);
  21. }
  22. .close {
  23. background-color: var(--zebra-red);
  24. }
  25. .container {
  26. margin: 8px 0;
  27. }
  28. .success {
  29. color: var(--paper-green-500);
  30. }
  31. .failure {
  32. color: var(--paper-red-500);
  33. }
  34. </style>
  35. <template>
  36. <paper-dialog id="dialog" modal>
  37. <div class="layout vertical">
  38. <div class="layout horizontal end-justified">
  39. <paper-icon-button id="close" class="" icon="close" on-tap="close"></paper-icon-button>
  40. </div>
  41. <div class="layout horizontal center-justified container">
  42. <paper-spinner id="spinner"></paper-spinner>
  43. </div>
  44. <div class="layout horizontal center-justified container">
  45. <iron-icon class="success" icon="check-circle" hidden="{{!response.success}}"></iron-icon>
  46. <iron-icon class="failure" icon="error" hidden="{{!response.failure}}"></iron-icon>
  47. </div>
  48. <!--
  49. <paper-icon-button id="close" class="horizontal end-justified layout" icon="close" on-tap="close"></paper-icon-button>
  50. -->
  51. <form id="form" class="layout vertical" is="iron-form" method="post" action="/api/jira/login">
  52. <paper-input id="username" label="Username (Zebra CoreID)" type="text" required></paper-input>
  53. <paper-input id="password" label="Password" type="password" required></paper-input>
  54. <paper-button id="submit" class="button login" on-tap="_submit" raised>Login</paper-button>
  55. <!--<paper-button id="close" class="button close" on-tap="close" raised>Close</paper-button>-->
  56. </form>
  57. </div>
  58. </paper-dialog>
  59. </template>
  60. <script>
  61. Polymer({
  62. is: 'jira-login-dialog',
  63. properties: {
  64. /**
  65. * The state of the request (i.e. is it in flight or not).
  66. * Has an observer to disable the form if it is in flight.
  67. */
  68. inFlight: {
  69. type: Boolean,
  70. value: false,
  71. observer: '_inFlight'
  72. },
  73. /**
  74. * The state of the response
  75. */
  76. response: {
  77. type: Object,
  78. value: {
  79. "success": false,
  80. "failure": false,
  81. }
  82. }
  83. },
  84. /**
  85. * When this element is attached to the DOM, setup event listeners
  86. */
  87. attached: function () {
  88. var me = this;
  89. //Grab a handle on the form and elements
  90. var form = me.$.form;
  91. var usernameInput = me.$.username;
  92. var passwordInput = me.$.password;
  93. //Before the form is submitted, get the values from the inputs and add them to the body
  94. form.addEventListener('iron-form-presubmit', function (event) {
  95. this.request.contentType = 'application/json';
  96. this.request.body = {username: usernameInput.value, password: passwordInput.value};
  97. });
  98. //When the form is submitted, notify that the request is in flight
  99. form.addEventListener('iron-form-submit', function (event) {
  100. me.inFlight = true;
  101. me.set('response.failure', false);
  102. me.set('response.success', false);
  103. });
  104. //When an error is encountered, notify that the request is no longer in flight.
  105. //Also mark that there was a failure.
  106. form.addEventListener('iron-form-error', function (event) {
  107. me.inFlight = false;
  108. me.set('response.failure', true);
  109. });
  110. //When a response is received, notify that the request is no longer in flight.
  111. //Also create a new cookie, etc.
  112. form.addEventListener('iron-form-response', function (event) {
  113. me.inFlight = false;
  114. var response = event.detail.response;
  115. //Login Success. HTTP 200 (OK)
  116. if (event.detail.status == 200) {
  117. me.set('response.success', true);
  118. //Create a cookie.
  119. me._createCookie(response.session.name, response.session.value);
  120. //Fire an event to let our listeners know a login was successful
  121. me.fire('loggedIn', {loggedIn: true});
  122. //Close the form since the user logged in
  123. me.$.dialog.close();
  124. }
  125. });
  126. },
  127. close: function () {
  128. this.$.dialog.close();
  129. },
  130. open: function () {
  131. this.$.dialog.open();
  132. },
  133. toggle: function () {
  134. this.$.dialog.toggle();
  135. },
  136. _submit: function () {
  137. this.$.form.submit();
  138. },
  139. /**
  140. * Helper method that disables the form elements if the request is mid flight.
  141. * Also enables/disables the spinner.
  142. * @private
  143. */
  144. _inFlight: function () {
  145. var me = this;
  146. var inFlight = me.inFlight;
  147. me.$.spinner.active = inFlight;
  148. me.$.username.disabled = inFlight;
  149. me.$.password.disabled = inFlight;
  150. me.$.submit.disabled = inFlight;
  151. me.$.close.disabled = inFlight;
  152. },
  153. /**
  154. * Helper method to create a cookie using js-cookie.
  155. * @param name The name of the cookie to create
  156. * @param value The value of the cookie
  157. * @private
  158. */
  159. _createCookie: function (name, value) {
  160. //Set the cookie to expire in 12 hours (0.5 * 1 day)
  161. Cookies.set(name, value, {expires: 0.5});
  162. }
  163. });
  164. </script>
  165. </dom-module>