|
| 1 | +/* |
| 2 | + * Copyright 2018 Google Inc. |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify it under |
| 5 | + * the terms of the GNU General Public License version 2 as published by the |
| 6 | + * Free Software Foundation. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 10 | + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 11 | + * License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU General Public License along |
| 14 | + * with this program; if not, write to the Free Software Foundation, Inc., 51 |
| 15 | + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * @file |
| 20 | + * Script for the Secret element. |
| 21 | + */ |
| 22 | +(function ($, Drupal, drupalSettings) { |
| 23 | + |
| 24 | + 'use strict'; |
| 25 | + |
| 26 | + Drupal.behaviors.SecretElement = { |
| 27 | + attach: function (context, settings) { |
| 28 | + let $secret = $('.secret', context); |
| 29 | + if ($secret.length) { |
| 30 | + $secret.each(function (i, element) { |
| 31 | + let $this = $(this); |
| 32 | + let $el = $this.find('.secret__value'); |
| 33 | + let hClass = 'secret--hidden'; |
| 34 | + let appElWrapper = '.app-credential'; |
| 35 | + let $wrapper = $this.closest(appElWrapper); |
| 36 | + let loader = '<img src="' + drupalSettings.path.baseUrl + 'core/misc/throbber-active.gif" border="0" />'; |
| 37 | + |
| 38 | + // Hide the value. |
| 39 | + $this.addClass(hClass); |
| 40 | + |
| 41 | + // Toggle secret. |
| 42 | + $(this).find('.secret__toggle').on('click', function (event) { |
| 43 | + let index = $(this).closest(appElWrapper).find('.secret__toggle').index(this); |
| 44 | + let wrapperIndex = $wrapper.data('app-container-index'); |
| 45 | + event.preventDefault(); |
| 46 | + $this.toggleClass(hClass); |
| 47 | + if ($this.hasClass(hClass)) { |
| 48 | + $el.html(''); |
| 49 | + } |
| 50 | + else { |
| 51 | + $el.html(loader); |
| 52 | + callEndpoint($wrapper.data('team'), $wrapper.data('app'), function(data) { |
| 53 | + $el.html(data[wrapperIndex][index]); |
| 54 | + }); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + // Copy to clipboard. |
| 59 | + let $copy = $(this).find('.secret__copy'); |
| 60 | + $copy.find('button').on('click', function (event) { |
| 61 | + let index = $(this).closest(appElWrapper).find('.secret__copy button').index(this); |
| 62 | + let wrapperIndex = $wrapper.closest('fieldset').parent().find('fieldset').index($(this).closest('fieldset')); |
| 63 | + callEndpoint($wrapper.data('team'), $wrapper.data('app'), function(data) { |
| 64 | + copyToClipboard(data[wrapperIndex][index]); |
| 65 | + $copy.find('.badge').fadeIn().delay(1000).fadeOut(); |
| 66 | + }); |
| 67 | + }) |
| 68 | + }); |
| 69 | + } |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + /** |
| 74 | + * Cross browser helper to copy to clipboard. |
| 75 | + */ |
| 76 | + function copyToClipboard(text) { |
| 77 | + if (window.clipboardData && window.clipboardData.setData) { |
| 78 | + // IE specific code path to prevent textarea being shown while dialog is visible. |
| 79 | + return clipboardData.setData("Text", text); |
| 80 | + |
| 81 | + } else if (document.queryCommandSupported && document.queryCommandSupported("copy")) { |
| 82 | + var textarea = document.createElement("textarea"); |
| 83 | + textarea.textContent = text; |
| 84 | + // Prevent scrolling to bottom of page in MS Edge. |
| 85 | + textarea.style.position = "fixed"; |
| 86 | + document.body.appendChild(textarea); |
| 87 | + textarea.select(); |
| 88 | + try { |
| 89 | + // Security exception may be thrown by some browsers. |
| 90 | + return document.execCommand("copy"); |
| 91 | + } catch (ex) { |
| 92 | + return false; |
| 93 | + } finally { |
| 94 | + document.body.removeChild(textarea); |
| 95 | + } |
| 96 | + } |
| 97 | + }; |
| 98 | + |
| 99 | + /** |
| 100 | + * Get credentials based on the app name. |
| 101 | + */ |
| 102 | + function callEndpoint(teamApp, app, callback) { |
| 103 | + var endpoint = drupalSettings.path.baseUrl + 'user/' + drupalSettings.currentUser + '/apps/' + app + '/api-keys'; |
| 104 | + if (teamApp !== undefined && teamApp !== 0 && teamApp !== '') { |
| 105 | + endpoint = drupalSettings.path.baseUrl + 'teams/' + teamApp + '/apps/' + app + '/api-keys'; |
| 106 | + } |
| 107 | + $.get(endpoint, function(data) { |
| 108 | + callback(data); |
| 109 | + }); |
| 110 | + }; |
| 111 | + |
| 112 | +})(jQuery, Drupal, drupalSettings); |
0 commit comments