settings-admin.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 setThemingValue(setting, value) {
  22. OC.msg.startSaving('#theming_settings_msg');
  23. $.post(
  24. OC.generateUrl('/apps/theming/ajax/updateStylesheet'), {'setting' : setting, 'value' : value}
  25. ).done(function(response) {
  26. OC.msg.finishedSaving('#theming_settings_msg', response);
  27. }).fail(function(response) {
  28. OC.msg.finishedSaving('#theming_settings_msg', response);
  29. });
  30. preview(setting, value);
  31. }
  32. function calculateLuminance(rgb) {
  33. var hexValue = rgb.replace(/[^0-9A-Fa-f]/, '');
  34. var r,g,b;
  35. if (hexValue.length === 3) {
  36. hexValue = hexValue[0] + hexValue[0] + hexValue[1] + hexValue[1] + hexValue[2] + hexValue[2];
  37. }
  38. if (hexValue.length !== 6) {
  39. return 0;
  40. }
  41. r = parseInt(hexValue.substring(0,2), 16);
  42. g = parseInt(hexValue.substring(2,4), 16);
  43. b = parseInt(hexValue.substring(4,6), 16);
  44. return (0.299*r + 0.587*g + 0.114*b)/255;
  45. }
  46. function generateRadioButton(color) {
  47. var radioButton = '<svg xmlns="http://www.w3.org/2000/svg" height="16" width="16">' +
  48. '<path d="M8 1a7 7 0 0 0-7 7 7 7 0 0 0 7 7 7 7 0 0 0 7-7 7 7 0 0 0-7-7zm0 1a6 6 0 0 1 6 6 6 6 0 0 1-6 6 6 6 0 0 1-6-6 6 6 0 0 1 6-6zm0 2a4 4 0 1 0 0 8 4 4 0 0 0 0-8z" fill="' + color + '"/></svg>';
  49. return btoa(radioButton);
  50. }
  51. function preview(setting, value) {
  52. if (setting === 'color') {
  53. var headerClass = document.getElementById('header');
  54. var expandDisplayNameClass = document.getElementById('expandDisplayName');
  55. var headerAppName = headerClass.getElementsByClassName('header-appname')[0];
  56. var textColor, icon;
  57. var luminance = calculateLuminance(value);
  58. var elementColor = value;
  59. if (luminance > 0.5) {
  60. textColor = "#000000";
  61. icon = 'caret-dark';
  62. } else {
  63. textColor = "#ffffff";
  64. icon = 'caret';
  65. }
  66. if (luminance > 0.8) {
  67. elementColor = '#555555';
  68. }
  69. headerClass.style.background = value;
  70. headerClass.style.backgroundImage = '../img/logo-icon.svg';
  71. expandDisplayNameClass.style.color = textColor;
  72. headerAppName.style.color = textColor;
  73. $('#previewStyles').html(
  74. '#header .icon-caret { background-image: url(\'' + OC.getRootPath() + '/core/img/actions/' + icon + '.svg\') }' +
  75. 'input[type="checkbox"].checkbox:checked:enabled:not(.checkbox--white) + label:before {' +
  76. 'background-image:url(\'' + OC.getRootPath() + '/core/img/actions/checkmark-white.svg\');' +
  77. 'background-color: ' + elementColor + '; background-position: center center; background-size:contain;' +
  78. 'width:12px; height:12px; padding:0; margin:2px 6px 6px 2px; border-radius:1px;}' +
  79. 'input[type="radio"].radio:checked:not(.radio--white):not(:disabled) + label:before {' +
  80. 'background-image: url(\'data:image/svg+xml;base64,' + generateRadioButton(elementColor) + '\'); }'
  81. );
  82. }
  83. var timestamp = new Date().getTime();
  84. if (setting === 'logoMime') {
  85. var logos = document.getElementsByClassName('logo-icon');
  86. var previewImageLogo = document.getElementById('theming-preview-logo');
  87. if (value !== '') {
  88. logos[0].style.backgroundImage = "url('" + OC.generateUrl('/apps/theming/logo') + "?v" + timestamp + "')";
  89. logos[0].style.backgroundSize = "contain";
  90. previewImageLogo.src = OC.generateUrl('/apps/theming/logo') + "?v" + timestamp;
  91. } else {
  92. logos[0].style.backgroundImage = "url('" + OC.getRootPath() + '/core/img/logo-icon.svg?v' + timestamp + "')";
  93. logos[0].style.backgroundSize = "contain";
  94. previewImageLogo.src = OC.getRootPath() + '/core/img/logo-icon.svg?v' + timestamp;
  95. }
  96. }
  97. if (setting === 'backgroundMime') {
  98. var previewImage = document.getElementById('theming-preview');
  99. if (value !== '') {
  100. previewImage.style.backgroundImage = "url('" + OC.generateUrl('/apps/theming/loginbackground') + "?v" + timestamp + "')";
  101. } else {
  102. previewImage.style.backgroundImage = "url('" + OC.getRootPath() + '/core/img/background.jpg?v' + timestamp + "')";
  103. }
  104. }
  105. if (setting === 'name') {
  106. window.document.title = t('core', 'Admin') + " - " + value;
  107. }
  108. hideUndoButton(setting, value);
  109. }
  110. function hideUndoButton(setting, value) {
  111. var themingDefaults = {
  112. name: 'Nextcloud',
  113. slogan: t('lib', 'a safe home for all your data'),
  114. url: 'https://nextcloud.com',
  115. color: '#0082c9',
  116. logoMime: '',
  117. backgroundMime: ''
  118. };
  119. if (value === themingDefaults[setting] || value === '') {
  120. $('.theme-undo[data-setting=' + setting + ']').hide();
  121. } else {
  122. $('.theme-undo[data-setting=' + setting + ']').show();
  123. }
  124. }
  125. $(document).ready(function () {
  126. $('#theming [data-toggle="tooltip"]').tooltip();
  127. $('html > head').append($('<style type="text/css" id="previewStyles"></style>'));
  128. $('#theming .theme-undo').each(function() {
  129. var setting = $(this).data('setting');
  130. var value = $('#theming-'+setting).val();
  131. if(setting === 'logoMime' || setting === 'backgroundMime') {
  132. var value = $('#current-'+setting).val();
  133. }
  134. hideUndoButton(setting, value);
  135. });
  136. var uploadParamsLogo = {
  137. pasteZone: null,
  138. dropZone: null,
  139. done: function (e, response) {
  140. preview('logoMime', response.result.data.name);
  141. OC.msg.finishedSaving('#theming_settings_msg', response.result);
  142. },
  143. submit: function(e, response) {
  144. OC.msg.startSaving('#theming_settings_msg');
  145. },
  146. fail: function (e, response){
  147. OC.msg.finishedError('#theming_settings_msg', response._response.jqXHR.responseJSON.data.message);
  148. }
  149. };
  150. var uploadParamsLogin = {
  151. pasteZone: null,
  152. dropZone: null,
  153. done: function (e, response) {
  154. preview('backgroundMime', response.result.data.name);
  155. OC.msg.finishedSaving('#theming_settings_msg', response.result);
  156. },
  157. submit: function(e, response) {
  158. OC.msg.startSaving('#theming_settings_msg');
  159. },
  160. fail: function (e, response){
  161. OC.msg.finishedError('#theming_settings_msg', response._response.jqXHR.responseJSON.data.message);
  162. }
  163. };
  164. $('#uploadlogo').fileupload(uploadParamsLogo);
  165. $('#upload-login-background').fileupload(uploadParamsLogin);
  166. $('#theming-name').change(function(e) {
  167. var el = $(this);
  168. $.when(el.focusout()).then(function() {
  169. setThemingValue('name', $(this).val());
  170. });
  171. if (e.keyCode == 13) {
  172. setThemingValue('name', $(this).val());
  173. }
  174. });
  175. $('#theming-url').change(function(e) {
  176. var el = $(this);
  177. $.when(el.focusout()).then(function() {
  178. setThemingValue('url', $(this).val());
  179. });
  180. if (e.keyCode == 13) {
  181. setThemingValue('url', $(this).val());
  182. }
  183. });
  184. $('#theming-slogan').change(function(e) {
  185. var el = $(this);
  186. $.when(el.focusout()).then(function() {
  187. setThemingValue('slogan', $(this).val());
  188. });
  189. if (e.keyCode == 13) {
  190. setThemingValue('slogan', $(this).val());
  191. }
  192. });
  193. $('#theming-color').change(function (e) {
  194. setThemingValue('color', '#' + $(this).val());
  195. });
  196. $('.theme-undo').click(function (e) {
  197. var setting = $(this).data('setting');
  198. OC.msg.startSaving('#theming_settings_msg');
  199. $.post(
  200. OC.generateUrl('/apps/theming/ajax/undoChanges'), {'setting' : setting}
  201. ).done(function(response) {
  202. if (setting === 'color') {
  203. var colorPicker = document.getElementById('theming-color');
  204. colorPicker.style.backgroundColor = response.data.value;
  205. colorPicker.value = response.data.value.slice(1).toUpperCase();
  206. } else if (setting !== 'logoMime' && setting !== 'backgroundMime') {
  207. var input = document.getElementById('theming-'+setting);
  208. input.value = response.data.value;
  209. }
  210. preview(setting, response.data.value);
  211. OC.msg.finishedSaving('#theming_settings_msg', response);
  212. });
  213. });
  214. });