123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <link rel="import" href="elements/elements.html">
  2. <dom-module id="app-shell">
  3. <style is="custom-style" include="iron-flex iron-flex-alignment iron-positioning"></style>
  4. <style>
  5. .yellow {
  6. background-color: var(--zebra-yellow);
  7. }
  8. .title {
  9. text-align: center;
  10. }
  11. .black {
  12. background-color: var(--zebra-black);
  13. }
  14. .tabs-container {
  15. /*width: 15%;*/
  16. }
  17. .page-tabs {
  18. height: 32px;
  19. color: var(--zebra-yellow);
  20. background-color: var(--zebra-black);
  21. --paper-tabs-selection-bar-color: var(--zebra-yellow);
  22. }
  23. .container {
  24. background-color: white;
  25. }
  26. .pages {
  27. margin: 8px 16px;
  28. }
  29. .name {
  30. font-size: 14px;
  31. }
  32. .avatar {
  33. vertical-align: middle;
  34. }
  35. </style>
  36. <template>
  37. <app-header-layout has-scrolling-region fullbleed>
  38. <app-header class="yellow" fixed>
  39. <!-- First Row -->
  40. <app-toolbar>
  41. <img class="" src="images/zebra_logo_64.png">
  42. <div class="title" main-title>{{title}}</div>
  43. <div hidden="{{loggedIn}}">
  44. <paper-icon-button icon="account-box" on-tap="_openLoginDialog"></paper-icon-button>
  45. </div>
  46. <div hidden="{{!loggedIn}}">
  47. <!--<span class="name">{{displayName}}</span>-->
  48. <iron-image id="avatar" class="avatar" src="{{avatarLink}}"
  49. on-tap="_openNameDialog"></iron-image>
  50. <paper-icon-button id="logout" icon="close" on-tap="_logout"></paper-icon-button>
  51. </div>
  52. </app-toolbar>
  53. <!-- Second Row -->
  54. <div class="black">
  55. <div class="tabs-container">
  56. <paper-tabs class="page-tabs" selected="{{selected}}" sticky>
  57. <paper-tab>Home</paper-tab>
  58. <paper-tab>News</paper-tab>
  59. <paper-tab>Forms</paper-tab>
  60. <paper-tab>Tools</paper-tab>
  61. </paper-tabs>
  62. </div>
  63. </div>
  64. </app-header>
  65. <div class="main-content">
  66. <iron-pages class="pages" selected="{{selected}}">
  67. <!-- Home -->
  68. <portal-home></portal-home>
  69. <!-- News -->
  70. <portal-news-list></portal-news-list>
  71. <!-- Forms -->
  72. <portal-forms logged-in="{{loggedIn}}"></portal-forms>
  73. <!-- Tools -->
  74. <portal-tools></portal-tools>
  75. </iron-pages>
  76. </div>
  77. </app-header-layout>
  78. <jira-login-dialog id="loginDialog"></jira-login-dialog>
  79. <paper-dialog id="nameDialog" no-overlap horizontal-align="right" vertical-align="auto">
  80. <h4>Currently signed in as:</h4>
  81. <p>{{user.displayName}} ({{user.username}}@zebra.com)</p>
  82. </paper-dialog>
  83. </template>
  84. <script>
  85. Polymer({
  86. is: 'app-shell',
  87. properties: {
  88. /**
  89. * The URL to the avatar of the user.
  90. */
  91. avatarLink: {
  92. type: String
  93. },
  94. /**
  95. * The display name (i.e. full name) of the user.
  96. */
  97. displayName: {
  98. type: String
  99. },
  100. /**
  101. * Logged in state of the user.
  102. * Has an observer to watch for changes.
  103. * Will not show user details if the user is not logged in.
  104. */
  105. loggedIn: {
  106. type: Boolean,
  107. value: false,
  108. observer: '_loggedIn'
  109. },
  110. /**
  111. * The currently selected 'page'.
  112. * Defaults to the 'Home' page (0).
  113. */
  114. selected: {
  115. type: Number,
  116. value: 0
  117. },
  118. /**
  119. * The title that appears in the toolbar at the top of the page.
  120. */
  121. title: {
  122. type: String,
  123. value: "EMC Engineering Tools Portal"
  124. },
  125. user: {
  126. type: Object,
  127. value: {}
  128. }
  129. },
  130. attached: function () {
  131. var me = this;
  132. //Check if the user is logged in by checking the cookie.
  133. me.loggedIn = me._checkCookie();
  134. //If the cookie is invalid, delete it
  135. if (me.loggedIn == false)
  136. me._deleteCookie();
  137. //Setup a listener for login events
  138. //<jira-login-dialog> will fire a 'loggedIn' event when the user logs in
  139. me.$.loginDialog.addEventListener('loggedIn', function (event) {
  140. me.loggedIn = event.detail.loggedIn;
  141. });
  142. },
  143. /**
  144. * Function for retrieving the details of the currently signed in user, such as avatar and display name.
  145. *
  146. * @param username The username of the user to retrieve details for.
  147. */
  148. getUserDetails: function (username) {
  149. var me = this;
  150. var ajax = document.createElement('iron-ajax');
  151. ajax.auto = true;
  152. ajax.method = "GET";
  153. ajax.url = "/api/jira/user/";
  154. ajax.params = {cookie: Cookies.get("JSESSIONID"), username: username};
  155. ajax.addEventListener("response", function (e) {
  156. var response = e.detail.response;
  157. me.avatarLink = response.avatar;
  158. me.displayName = response.displayName;
  159. me.set('user.displayName', response.displayName);
  160. me.set('user.username', username);
  161. });
  162. },
  163. /**
  164. * Function for retrieving the username of the user.
  165. * Uses the current session cookie to get the current user's username.
  166. * Then, calls helper method to get user details.
  167. *
  168. * @see {@link getUserDetails} for the actual details retrieval.
  169. */
  170. getUser: function () {
  171. var me = this;
  172. var ajax = document.createElement('iron-ajax');
  173. ajax.auto = true;
  174. ajax.method = "GET";
  175. ajax.url = "/api/jira/login/";
  176. ajax.params = {cookie: Cookies.get("JSESSIONID")};
  177. ajax.addEventListener("response", function (e) {
  178. //Call helper method now that we know the username
  179. me.getUserDetails(e.detail.response.name);
  180. });
  181. },
  182. /**
  183. * Helper method for checking the validity of the cookie.
  184. * @returns {boolean} The validity of the cookie.
  185. * @private
  186. */
  187. _checkCookie: function () {
  188. if (typeof Cookies.get("JSESSIONID") === typeof undefined)
  189. return false;
  190. else
  191. return true;
  192. },
  193. /**
  194. * Helper method for deleting the JIRA session cookie.
  195. * Uses js-cookie.
  196. * @private
  197. */
  198. _deleteCookie: function () {
  199. Cookies.remove('JSESSIONID');
  200. },
  201. /**
  202. * Helper method for opening the JIRA login dialog.
  203. * @private
  204. */
  205. _openLoginDialog: function () {
  206. this.$.loginDialog.open();
  207. },
  208. /**
  209. * Helper method for opening the name dialog.
  210. * @private
  211. */
  212. _openNameDialog: function () {
  213. this.$.nameDialog.positionTarget = this.$.avatar;
  214. this.$.nameDialog.open();
  215. },
  216. /**
  217. * Observer function tied to loggedIn prop.
  218. * When the user logs in, the prop is updated to 'true' and the user detail retrieval process begins.
  219. * @private
  220. */
  221. _loggedIn: function () {
  222. var me = this;
  223. if (me.loggedIn == true)
  224. me.getUser();
  225. },
  226. /**
  227. * Helper method for logging out the user.
  228. * Simply sends a request to the server to relay a logout to JIRA.
  229. * Once complete, deletes the session cookie and sets the loggedIn state to false.
  230. * @private
  231. */
  232. _logout: function () {
  233. var me = this;
  234. var ajax = document.createElement('iron-ajax');
  235. ajax.auto = true;
  236. ajax.method = "DELETE";
  237. ajax.url = "/api/jira/login";
  238. ajax.params = {cookie: Cookies.get('JSESSIONID')};
  239. //If there were no HTTP errors encountered, the logout was successful
  240. ajax.addEventListener("response", function (e) {
  241. me.loggedIn = false;
  242. me._deleteCookie();
  243. });
  244. }
  245. });
  246. </script>
  247. </dom-module>