fontEffectsPane.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * Copyright (C) 2013 KO GmbH <copyright@kogmbh.com>
  3. *
  4. * @licstart
  5. * This file is part of WebODF.
  6. *
  7. * WebODF is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU Affero General Public License (GNU AGPL)
  9. * as published by the Free Software Foundation, either version 3 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * WebODF is distributed in the hope that it will be useful, but
  13. * 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 WebODF. If not, see <http://www.gnu.org/licenses/>.
  19. * @licend
  20. *
  21. * @source: http://www.webodf.org/
  22. * @source: https://github.com/kogmbh/WebODF/
  23. */
  24. /*global runtime,define,require,document,dijit */
  25. define("webodf/editor/widgets/dialogWidgets/fontEffectsPane", [
  26. "webodf/editor/widgets/dialogWidgets/idMangler"],
  27. function (IdMangler) {
  28. "use strict";
  29. var FontEffectsPane = function (callback) {
  30. var self = this,
  31. idMangler = new IdMangler(),
  32. editorSession,
  33. contentPane,
  34. form,
  35. preview,
  36. textColorPicker,
  37. backgroundColorPicker,
  38. fontPicker;
  39. this.widget = function () {
  40. return contentPane;
  41. };
  42. this.value = function () {
  43. var textProperties = form.get('value'),
  44. textStyle = textProperties.textStyle;
  45. textProperties.fontWeight = (textStyle.indexOf('bold') !== -1)
  46. ? 'bold'
  47. : 'normal';
  48. textProperties.fontStyle = (textStyle.indexOf('italic') !== -1)
  49. ? 'italic'
  50. : 'normal';
  51. textProperties.underline = (textStyle.indexOf('underline') !== -1)
  52. ? 'solid'
  53. : 'none';
  54. delete textProperties.textStyle;
  55. return textProperties;
  56. };
  57. this.setStyle = function (styleName) {
  58. var style = editorSession.getParagraphStyleAttributes(styleName)['style:text-properties'],
  59. s_bold,
  60. s_italic,
  61. s_underline,
  62. s_fontSize,
  63. s_fontName,
  64. s_color,
  65. s_backgroundColor;
  66. if (style !== undefined) {
  67. s_bold = style['fo:font-weight'];
  68. s_italic = style['fo:font-style'];
  69. s_underline = style['style:text-underline-style'];
  70. s_fontSize = parseFloat(style['fo:font-size']);
  71. s_fontName = style['style:font-name'];
  72. s_color = style['fo:color'];
  73. s_backgroundColor = style['fo:background-color'];
  74. form.attr('value', {
  75. fontName: s_fontName && s_fontName.length ? s_fontName : 'Arial',
  76. fontSize: isNaN(s_fontSize) ? 12 : s_fontSize,
  77. textStyle: [
  78. s_bold,
  79. s_italic,
  80. s_underline === 'solid' ? 'underline' : undefined
  81. ]
  82. });
  83. textColorPicker.set('value', s_color && s_color.length ? s_color : '#000000');
  84. backgroundColorPicker.set('value', s_backgroundColor && s_backgroundColor.length ? s_backgroundColor : '#ffffff');
  85. } else {
  86. // TODO: Use default style here
  87. form.attr('value', {
  88. fontFamily: 'sans-serif',
  89. fontSize: 12,
  90. textStyle: []
  91. });
  92. textColorPicker.set('value', '#000000');
  93. backgroundColorPicker.set('value', '#ffffff');
  94. }
  95. };
  96. /*jslint unparam: true*/
  97. function init(cb) {
  98. require([
  99. "dojo",
  100. "dojo/ready",
  101. "dijit/layout/ContentPane",
  102. "dojox/widget/ColorPicker", // referenced in fontEffectsPane.html
  103. "webodf/editor/widgets/fontPicker"
  104. ], function (dojo, ready, ContentPane, ColorPicker, FontPicker) {
  105. var editorBase = dojo.config && dojo.config.paths &&
  106. dojo.config.paths['webodf/editor'];
  107. runtime.assert(editorBase, "webodf/editor path not defined in dojoConfig");
  108. ready(function () {
  109. contentPane = new ContentPane({
  110. title: runtime.tr("Font Effects"),
  111. href: editorBase + "/widgets/dialogWidgets/fontEffectsPane.html",
  112. preload: true,
  113. ioMethod: idMangler.ioMethod
  114. });
  115. contentPane.onLoad = function () {
  116. var textColorTB = idMangler.byId('textColorTB'),
  117. backgroundColorTB = idMangler.byId('backgroundColorTB');
  118. form = idMangler.byId('fontEffectsPaneForm');
  119. runtime.translateContent(form.domNode);
  120. preview = idMangler.getElementById('previewText');
  121. textColorPicker = idMangler.byId('textColorPicker');
  122. backgroundColorPicker = idMangler.byId('backgroundColorPicker');
  123. // Bind dojox widgets' values to invisible form elements, for easy parsing
  124. textColorPicker.onChange = function (value) {
  125. textColorTB.set('value', value);
  126. };
  127. backgroundColorPicker.onChange = function (value) {
  128. backgroundColorTB.set('value', value);
  129. };
  130. fontPicker = new FontPicker(function (picker) {
  131. picker.widget().startup();
  132. idMangler.getElementById('fontPicker').appendChild(picker.widget().domNode);
  133. picker.widget().name = 'fontName';
  134. picker.setEditorSession(editorSession);
  135. });
  136. // Automatically update preview when selections change
  137. form.watch('value', function () {
  138. if (form.value.textStyle.indexOf('bold') !== -1) {
  139. preview.style.fontWeight = 'bold';
  140. } else {
  141. preview.style.fontWeight = 'normal';
  142. }
  143. if (form.value.textStyle.indexOf('italic') !== -1) {
  144. preview.style.fontStyle = 'italic';
  145. } else {
  146. preview.style.fontStyle = 'normal';
  147. }
  148. if (form.value.textStyle.indexOf('underline') !== -1) {
  149. preview.style.textDecoration = 'underline';
  150. } else {
  151. preview.style.textDecoration = 'none';
  152. }
  153. preview.style.fontSize = form.value.fontSize + 'pt';
  154. preview.style.fontFamily = fontPicker.getFamily(form.value.fontName);
  155. preview.style.color = form.value.color;
  156. preview.style.backgroundColor = form.value.backgroundColor;
  157. });
  158. };
  159. return cb();
  160. });
  161. });
  162. }
  163. /*jslint unparam: false*/
  164. this.setEditorSession = function(session) {
  165. editorSession = session;
  166. if (fontPicker) {
  167. fontPicker.setEditorSession(editorSession);
  168. }
  169. };
  170. init(function () {
  171. return callback(self);
  172. });
  173. };
  174. return FontEffectsPane;
  175. });