settings-admin.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * @author Björn Schießle <bjoern@schiessle.org>
  3. *
  4. * @copyright Copyright (c) 2016, Bjoern Schiessle
  5. * @license AGPL-3.0
  6. *
  7. * This code is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your opinion) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. function startLoading() {
  22. OC.msg.startSaving('#theming_settings_msg');
  23. $('#theming_settings_loading').show();
  24. }
  25. function setThemingValue(setting, value) {
  26. startLoading();
  27. $.post(
  28. OC.generateUrl('/apps/theming/ajax/updateStylesheet'), {'setting' : setting, 'value' : value}
  29. ).done(function() {
  30. hideUndoButton(setting, value);
  31. preview(setting, value);
  32. }).fail(function(response) {
  33. OC.msg.finishedSaving('#theming_settings_msg', response.responseJSON);
  34. $('#theming_settings_loading').hide();
  35. });
  36. }
  37. function preview(setting, value, serverCssUrl) {
  38. OC.msg.startAction('#theming_settings_msg', t('theming', 'Loading preview…'));
  39. // Get all theming themes css links and force reload them
  40. [...document.querySelectorAll('link.theme')]
  41. .forEach(theme => {
  42. // Only edit the clone to not remove applied one
  43. var clone = theme.cloneNode()
  44. var url = new URL(clone.href)
  45. // Set current timestamp as cache buster
  46. url.searchParams.set('v', Date.now())
  47. clone.href = url.toString()
  48. clone.onload = function() {
  49. theme.remove()
  50. }
  51. document.head.append(clone)
  52. })
  53. if (setting === 'name') {
  54. window.document.title = t('core', 'Admin') + " - " + value;
  55. }
  56. // Finish
  57. $('#theming_settings_loading').hide();
  58. var response = { status: 'success', data: {message: t('theming', 'Saved')}};
  59. OC.msg.finishedSaving('#theming_settings_msg', response);
  60. hideUndoButton(setting, value);
  61. }
  62. function hideUndoButton(setting, value) {
  63. var themingDefaults = {
  64. name: 'Nextcloud',
  65. slogan: t('lib', 'a safe home for all your data'),
  66. url: 'https://nextcloud.com',
  67. color: '#0082c9',
  68. logoMime: '',
  69. backgroundMime: '',
  70. imprintUrl: '',
  71. privacyUrl: ''
  72. };
  73. if (value === themingDefaults[setting] || value === '') {
  74. $('.theme-undo[data-setting=' + setting + ']').hide();
  75. } else {
  76. $('.theme-undo[data-setting=' + setting + ']').show();
  77. }
  78. if(setting === 'backgroundMime' && value !== 'backgroundColor') {
  79. $('.theme-remove-bg').show();
  80. }
  81. if(setting === 'backgroundMime' && value === 'backgroundColor') {
  82. $('.theme-remove-bg').hide();
  83. $('.theme-undo[data-setting=backgroundMime]').show();
  84. }
  85. }
  86. window.addEventListener('DOMContentLoaded', function () {
  87. $('#theming [data-toggle="tooltip"]').tooltip();
  88. // manually instantiate jscolor to work around new Function call which violates strict CSP
  89. var colorElement = $('#theming-color')[0];
  90. colorElement.setAttribute('aria-expanded', 'false');
  91. var jscolor = new window.jscolor(colorElement, {hash: true});
  92. $('#theming .theme-undo').each(function() {
  93. var setting = $(this).data('setting');
  94. var value = $('#theming-'+setting).val();
  95. hideUndoButton(setting, value);
  96. });
  97. $('.fileupload').fileupload({
  98. pasteZone: null,
  99. dropZone: null,
  100. done: function (e, response) {
  101. var $form = $(e.target).closest('form');
  102. var key = $form.data('image-key');
  103. preview(key + 'Mime', response.result.data.name, response.result.data.serverCssUrl);
  104. $form.find('.image-preview').css('backgroundImage', response.result.data.url + '?v=' + new Date().getTime());
  105. OC.msg.finishedSaving('#theming_settings_msg', response.result);
  106. $form.find('label.button').addClass('icon-upload').removeClass('icon-loading-small');
  107. $form.find('.theme-undo').show();
  108. },
  109. submit: function(e, response) {
  110. var $form = $(e.target).closest('form');
  111. var key = $form.data('image-key');
  112. startLoading();
  113. $form.find('label.button').removeClass('icon-upload').addClass('icon-loading-small');
  114. },
  115. fail: function (e, response){
  116. var $form = $(e.target).closest('form');
  117. const responseJSON = response._response.jqXHR.responseJSON;
  118. OC.msg.finishedError('#theming_settings_msg', responseJSON && responseJSON.data && responseJSON.data.message ? responseJSON.data.message : t('theming', 'Error uploading the file'));
  119. $form.find('label.button').addClass('icon-upload').removeClass('icon-loading-small');
  120. $('#theming_settings_loading').hide();
  121. }
  122. });
  123. // clicking preview should also trigger file upload dialog
  124. $('#theming-preview-logo').on('click', function(e) {
  125. e.stopPropagation();
  126. $('#uploadlogo').click();
  127. });
  128. $('#theming-preview').on('click', function() {
  129. $('#upload-login-background').click();
  130. });
  131. function checkName () {
  132. var length = $('#theming-name').val().length;
  133. try {
  134. if (length > 0) {
  135. return true;
  136. } else {
  137. throw t('theming', 'Name cannot be empty');
  138. }
  139. } catch (error) {
  140. $('#theming-name').attr('title', error);
  141. $('#theming-name').tooltip({placement: 'top', trigger: 'manual'});
  142. $('#theming-name').tooltip('_fixTitle');
  143. $('#theming-name').tooltip('show');
  144. $('#theming-name').addClass('error');
  145. }
  146. return false;
  147. }
  148. $('#theming-name').keyup(function() {
  149. if (checkName()) {
  150. $('#theming-name').tooltip('hide');
  151. $('#theming-name').removeClass('error');
  152. }
  153. });
  154. $('#theming-name').change(function(e) {
  155. var el = $(this);
  156. });
  157. $('#userThemingDisabled').change(function(e) {
  158. var checked = e.target.checked
  159. setThemingValue('disable-user-theming', checked ? 'yes' : 'no')
  160. });
  161. function onChange(e) {
  162. var el = $(this);
  163. var setting = el.parent().find('div[data-setting]').data('setting');
  164. var value = $(this).val();
  165. if(setting === 'color') {
  166. if (value.indexOf('#') !== 0) {
  167. value = '#' + value;
  168. }
  169. }
  170. if(setting === 'name') {
  171. if(checkName()){
  172. $.when(el.focusout()).then(function() {
  173. setThemingValue('name', value);
  174. });
  175. if (e.keyCode == 13) {
  176. setThemingValue('name', value);
  177. }
  178. }
  179. }
  180. $.when(el.focusout()).then(function() {
  181. setThemingValue(setting, value);
  182. });
  183. if (e.keyCode == 13) {
  184. setThemingValue(setting, value);
  185. }
  186. };
  187. $('#theming input[type="text"]').change(onChange);
  188. $('#theming input[type="url"]').change(onChange);
  189. $('.theme-undo').click(function (e) {
  190. var setting = $(this).data('setting');
  191. var $form = $(this).closest('form');
  192. var image = $form.data('image-key');
  193. startLoading();
  194. $('.theme-undo[data-setting=' + setting + ']').hide();
  195. $.post(
  196. OC.generateUrl('/apps/theming/ajax/undoChanges'), {'setting' : setting}
  197. ).done(function(response) {
  198. if (setting === 'color') {
  199. var colorPicker = document.getElementById('theming-color');
  200. colorPicker.style.backgroundColor = response.data.value;
  201. colorPicker.value = response.data.value.slice(1).toUpperCase();
  202. } else if (!image) {
  203. var input = document.getElementById('theming-'+setting);
  204. input.value = response.data.value;
  205. }
  206. preview(setting, response.data.value, response.data.serverCssUrl);
  207. });
  208. });
  209. $('.theme-remove-bg').click(function() {
  210. startLoading();
  211. $.post(
  212. OC.generateUrl('/apps/theming/ajax/updateStylesheet'), {'setting' : 'backgroundMime', 'value' : 'backgroundColor'}
  213. ).done(function(response) {
  214. preview('backgroundMime', 'backgroundColor', response.data.serverCssUrl);
  215. }).fail(function(response) {
  216. OC.msg.finishedSaving('#theming_settings_msg', response);
  217. $('#theming_settings_loading').hide();
  218. });
  219. });
  220. });