1
0

settings-admin.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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(response) {
  30. hideUndoButton(setting, value);
  31. preview(setting, value, response.data.serverCssUrl);
  32. }).fail(function(response) {
  33. OC.msg.finishedSaving('#theming_settings_msg', response);
  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. var stylesheetsLoaded = 1;
  40. var reloadStylesheets = function(cssFile) {
  41. var queryString = '?reload=' + new Date().getTime();
  42. var url = cssFile + queryString;
  43. var old = $('link[href*="' + cssFile.replace("/","\/") + '"]');
  44. var stylesheet = $("<link/>", {
  45. rel: "stylesheet",
  46. type: "text/css",
  47. href: url
  48. });
  49. stylesheet.load(function () {
  50. $(old).remove();
  51. stylesheetsLoaded--;
  52. if(stylesheetsLoaded === 0) {
  53. $('#theming_settings_loading').hide();
  54. var response = { status: 'success', data: {message: t('theming', 'Saved')}};
  55. OC.msg.finishedSaving('#theming_settings_msg', response);
  56. }
  57. });
  58. stylesheet.appendTo("head");
  59. };
  60. if (serverCssUrl !== undefined) {
  61. stylesheetsLoaded++;
  62. reloadStylesheets(serverCssUrl);
  63. }
  64. reloadStylesheets(OC.generateUrl('/apps/theming/styles'));
  65. if (setting === 'name') {
  66. window.document.title = t('core', 'Admin') + " - " + value;
  67. }
  68. hideUndoButton(setting, value);
  69. }
  70. function hideUndoButton(setting, value) {
  71. var themingDefaults = {
  72. name: 'Nextcloud',
  73. slogan: t('lib', 'a safe home for all your data'),
  74. url: 'https://nextcloud.com',
  75. color: '#0082c9',
  76. logoMime: '',
  77. backgroundMime: '',
  78. imprintUrl: ''
  79. };
  80. if (value === themingDefaults[setting] || value === '') {
  81. $('.theme-undo[data-setting=' + setting + ']').hide();
  82. } else {
  83. $('.theme-undo[data-setting=' + setting + ']').show();
  84. }
  85. if(setting === 'backgroundMime' && value !== 'backgroundColor') {
  86. $('.theme-remove-bg').show();
  87. }
  88. if(setting === 'backgroundMime' && value === 'backgroundColor') {
  89. $('.theme-remove-bg').hide();
  90. $('.theme-undo[data-setting=backgroundMime]').show();
  91. }
  92. }
  93. $(document).ready(function () {
  94. $('#theming [data-toggle="tooltip"]').tooltip();
  95. $('#theming .theme-undo').each(function() {
  96. var setting = $(this).data('setting');
  97. var value = $('#theming-'+setting).val();
  98. hideUndoButton(setting, value);
  99. });
  100. $('.fileupload').fileupload({
  101. pasteZone: null,
  102. dropZone: null,
  103. done: function (e, response) {
  104. var $form = $(e.target).closest('form');
  105. var key = $form.data('image-key');
  106. preview(key + 'Mime', response.result.data.name, response.result.data.serverCssUrl);
  107. $form.find('.image-preview').css('backgroundImage', response.result.data.url + '?v=' + new Date().getTime());
  108. OC.msg.finishedSaving('#theming_settings_msg', response.result);
  109. $form.find('label.button').addClass('icon-upload').removeClass('icon-loading-small');
  110. $form.find('.theme-undo').show();
  111. },
  112. submit: function(e, response) {
  113. var $form = $(e.target).closest('form');
  114. var key = $form.data('image-key');
  115. startLoading();
  116. $form.find('label.button').removeClass('icon-upload').addClass('icon-loading-small');
  117. },
  118. fail: function (e, response){
  119. var $form = $(e.target).closest('form');
  120. OC.msg.finishedError('#theming_settings_msg', response._response.jqXHR.responseJSON.data.message);
  121. $form.find('label.button').addClass('icon-upload').removeClass('icon-loading-small');
  122. $('#theming_settings_loading').hide();
  123. }
  124. });
  125. // clicking preview should also trigger file upload dialog
  126. $('#theming-preview-logo').on('click', function(e) {
  127. e.stopPropagation();
  128. $('#uploadlogo').click();
  129. });
  130. $('#theming-preview').on('click', function() {
  131. $('#upload-login-background').click();
  132. });
  133. function checkName () {
  134. var length = $('#theming-name').val().length;
  135. try {
  136. if (length > 0) {
  137. return true;
  138. } else {
  139. throw t('theming', 'Name cannot be empty');
  140. }
  141. } catch (error) {
  142. $('#theming-name').attr('title', error);
  143. $('#theming-name').tooltip({placement: 'top', trigger: 'manual'});
  144. $('#theming-name').tooltip('fixTitle');
  145. $('#theming-name').tooltip('show');
  146. $('#theming-name').addClass('error');
  147. }
  148. return false;
  149. }
  150. $('#theming-name').keyup(function() {
  151. if (checkName()) {
  152. $('#theming-name').tooltip('hide');
  153. $('#theming-name').removeClass('error');
  154. }
  155. });
  156. $('#theming-name').change(function(e) {
  157. var el = $(this);
  158. });
  159. function onChange(e) {
  160. var el = $(this);
  161. var setting = el.parent().find('div[data-setting]').data('setting');
  162. var value = $(this).val();
  163. if(setting === 'color') {
  164. if (value.indexOf('#') !== 0) {
  165. value = '#' + value;
  166. }
  167. }
  168. if(setting === 'name') {
  169. if(checkName()){
  170. $.when(el.focusout()).then(function() {
  171. setThemingValue('name', value);
  172. });
  173. if (e.keyCode == 13) {
  174. setThemingValue('name', value);
  175. }
  176. }
  177. }
  178. $.when(el.focusout()).then(function() {
  179. setThemingValue(setting, value);
  180. });
  181. if (e.keyCode == 13) {
  182. setThemingValue(setting, value);
  183. }
  184. };
  185. $('#theming input[type="text"]').change(onChange);
  186. $('#theming input[type="url"]').change(onChange);
  187. $('.theme-undo').click(function (e) {
  188. var setting = $(this).data('setting');
  189. var $form = $(this).closest('form');
  190. var image = $form.data('image-key');
  191. startLoading();
  192. $('.theme-undo[data-setting=' + setting + ']').hide();
  193. $.post(
  194. OC.generateUrl('/apps/theming/ajax/undoChanges'), {'setting' : setting}
  195. ).done(function(response) {
  196. if (setting === 'color') {
  197. var colorPicker = document.getElementById('theming-color');
  198. colorPicker.style.backgroundColor = response.data.value;
  199. colorPicker.value = response.data.value.slice(1).toUpperCase();
  200. } else if (!image) {
  201. var input = document.getElementById('theming-'+setting);
  202. input.value = response.data.value;
  203. }
  204. preview(setting, response.data.value, response.data.serverCssUrl);
  205. });
  206. });
  207. $('.theme-remove-bg').click(function() {
  208. startLoading();
  209. $.post(
  210. OC.generateUrl('/apps/theming/ajax/updateStylesheet'), {'setting' : 'backgroundMime', 'value' : 'backgroundColor'}
  211. ).done(function(response) {
  212. preview('backgroundMime', 'backgroundColor', response.data.serverCssUrl);
  213. }).fail(function(response) {
  214. OC.msg.finishedSaving('#theming_settings_msg', response);
  215. $('#theming_settings_loading').hide();
  216. });
  217. });
  218. });