You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
7.5 KiB

10 years ago
  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. // Preview images
  66. var timestamp = new Date().getTime();
  67. if (setting === 'logoMime') {
  68. var previewImageLogo = document.getElementById('theming-preview-logo');
  69. if (value !== '') {
  70. previewImageLogo.src = OC.generateUrl('/apps/theming/logo') + "?v" + timestamp;
  71. } else {
  72. previewImageLogo.src = OC.getRootPath() + '/core/img/logo.svg?v' + timestamp;
  73. }
  74. }
  75. if (setting === 'name') {
  76. window.document.title = t('core', 'Admin') + " - " + value;
  77. }
  78. hideUndoButton(setting, value);
  79. }
  80. function hideUndoButton(setting, value) {
  81. var themingDefaults = {
  82. name: 'Nextcloud',
  83. slogan: t('lib', 'a safe home for all your data'),
  84. url: 'https://nextcloud.com',
  85. color: '#0082c9',
  86. logoMime: '',
  87. backgroundMime: ''
  88. };
  89. if (value === themingDefaults[setting] || value === '') {
  90. $('.theme-undo[data-setting=' + setting + ']').hide();
  91. } else {
  92. $('.theme-undo[data-setting=' + setting + ']').show();
  93. }
  94. if(setting === 'backgroundMime' && value !== 'backgroundColor') {
  95. $('.theme-remove-bg').show();
  96. }
  97. if(setting === 'backgroundMime' && value === 'backgroundColor') {
  98. $('.theme-remove-bg').hide();
  99. $('.theme-undo[data-setting=backgroundMime]').show();
  100. }
  101. }
  102. $(document).ready(function () {
  103. $('#theming [data-toggle="tooltip"]').tooltip();
  104. $('#theming .theme-undo').each(function() {
  105. var setting = $(this).data('setting');
  106. var value = $('#theming-'+setting).val();
  107. if(setting === 'logoMime' || setting === 'backgroundMime') {
  108. var value = $('#current-'+setting).val();
  109. }
  110. hideUndoButton(setting, value);
  111. });
  112. var uploadParamsLogo = {
  113. pasteZone: null,
  114. dropZone: null,
  115. done: function (e, response) {
  116. preview('logoMime', response.result.data.name);
  117. OC.msg.finishedSaving('#theming_settings_msg', response.result);
  118. $('label#uploadlogo').addClass('icon-upload').removeClass('icon-loading-small');
  119. $('.theme-undo[data-setting=logoMime]').show();
  120. },
  121. submit: function(e, response) {
  122. startLoading();
  123. $('label#uploadlogo').removeClass('icon-upload').addClass('icon-loading-small');
  124. },
  125. fail: function (e, response){
  126. OC.msg.finishedError('#theming_settings_msg', response._response.jqXHR.responseJSON.data.message);
  127. $('label#uploadlogo').addClass('icon-upload').removeClass('icon-loading-small');
  128. $('#theming_settings_loading').hide();
  129. }
  130. };
  131. var uploadParamsLogin = {
  132. pasteZone: null,
  133. dropZone: null,
  134. done: function (e, response) {
  135. preview('backgroundMime', response.result.data.name);
  136. OC.msg.finishedSaving('#theming_settings_msg', response.result);
  137. $('label#upload-login-background').addClass('icon-upload').removeClass('icon-loading-small');
  138. $('.theme-undo[data-setting=backgroundMime]').show();
  139. },
  140. submit: function(e, response) {
  141. startLoading();
  142. $('label#upload-login-background').removeClass('icon-upload').addClass('icon-loading-small');
  143. },
  144. fail: function (e, response){
  145. $('label#upload-login-background').removeClass('icon-loading-small').addClass('icon-upload');
  146. OC.msg.finishedError('#theming_settings_msg', response._response.jqXHR.responseJSON.data.message);
  147. $('#theming_settings_loading').hide();
  148. }
  149. };
  150. $('#uploadlogo').fileupload(uploadParamsLogo);
  151. $('#upload-login-background').fileupload(uploadParamsLogin);
  152. // clicking preview should also trigger file upload dialog
  153. $('#theming-preview-logo').on('click', function(e) {
  154. e.stopPropagation();
  155. $('#uploadlogo').click();
  156. });
  157. $('#theming-preview').on('click', function() {
  158. $('#upload-login-background').click();
  159. });
  160. $('#theming-name').change(function(e) {
  161. var el = $(this);
  162. $.when(el.focusout()).then(function() {
  163. setThemingValue('name', $(this).val());
  164. });
  165. if (e.keyCode == 13) {
  166. setThemingValue('name', $(this).val());
  167. }
  168. });
  169. $('#theming-url').change(function(e) {
  170. var el = $(this);
  171. $.when(el.focusout()).then(function() {
  172. setThemingValue('url', $(this).val());
  173. });
  174. if (e.keyCode == 13) {
  175. setThemingValue('url', $(this).val());
  176. }
  177. });
  178. $('#theming-slogan').change(function(e) {
  179. var el = $(this);
  180. $.when(el.focusout()).then(function() {
  181. setThemingValue('slogan', $(this).val());
  182. });
  183. if (e.keyCode == 13) {
  184. setThemingValue('slogan', $(this).val());
  185. }
  186. });
  187. $('#theming-color').change(function (e) {
  188. setThemingValue('color', '#' + $(this).val());
  189. });
  190. $('.theme-undo').click(function (e) {
  191. var setting = $(this).data('setting');
  192. startLoading();
  193. $('.theme-undo[data-setting=' + setting + ']').hide();
  194. $.post(
  195. OC.generateUrl('/apps/theming/ajax/undoChanges'), {'setting' : setting}
  196. ).done(function(response) {
  197. if (setting === 'color') {
  198. var colorPicker = document.getElementById('theming-color');
  199. colorPicker.style.backgroundColor = response.data.value;
  200. colorPicker.value = response.data.value.slice(1).toUpperCase();
  201. } else if (setting !== 'logoMime' && setting !== 'backgroundMime') {
  202. var input = document.getElementById('theming-'+setting);
  203. input.value = response.data.value;
  204. }
  205. preview(setting, response.data.value, response.data.serverCssUrl);
  206. });
  207. });
  208. $('.theme-remove-bg').click(function() {
  209. startLoading();
  210. $.post(
  211. OC.generateUrl('/apps/theming/ajax/updateLogo'), {'backgroundColor' : true}
  212. ).done(function(response) {
  213. preview('backgroundMime', 'backgroundColor');
  214. }).fail(function(response) {
  215. OC.msg.finishedSaving('#theming_settings_msg', response);
  216. });
  217. });
  218. });