fontEffectsPane.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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", [], function () {
  26. "use strict";
  27. var FontEffectsPane = function (callback) {
  28. var self = this,
  29. editorSession,
  30. contentPane,
  31. form,
  32. preview,
  33. textColorPicker,
  34. backgroundColorPicker,
  35. fontPicker;
  36. this.widget = function () {
  37. return contentPane;
  38. };
  39. this.value = function () {
  40. var textProperties = form.get('value'),
  41. textStyle = textProperties.textStyle;
  42. textProperties.fontWeight = (textStyle.indexOf('bold') !== -1)
  43. ? 'bold'
  44. : 'normal';
  45. textProperties.fontStyle = (textStyle.indexOf('italic') !== -1)
  46. ? 'italic'
  47. : 'normal';
  48. textProperties.underline = (textStyle.indexOf('underline') !== -1)
  49. ? 'solid'
  50. : 'none';
  51. delete textProperties.textStyle;
  52. return textProperties;
  53. };
  54. this.setStyle = function (styleName) {
  55. var style = editorSession.getParagraphStyleAttributes(styleName)['style:text-properties'],
  56. s_bold,
  57. s_italic,
  58. s_underline,
  59. s_fontSize,
  60. s_fontName,
  61. s_color,
  62. s_backgroundColor;
  63. if (style !== undefined) {
  64. s_bold = style['fo:font-weight'];
  65. s_italic = style['fo:font-style'];
  66. s_underline = style['style:text-underline-style'];
  67. s_fontSize = parseFloat(style['fo:font-size']);
  68. s_fontName = style['style:font-name'];
  69. s_color = style['fo:color'];
  70. s_backgroundColor = style['fo:background-color'];
  71. form.attr('value', {
  72. fontName: s_fontName && s_fontName.length ? s_fontName : 'Arial',
  73. fontSize: isNaN(s_fontSize) ? 12 : s_fontSize,
  74. textStyle: [
  75. s_bold,
  76. s_italic,
  77. s_underline === 'solid' ? 'underline' : undefined
  78. ]
  79. });
  80. textColorPicker.set('value', s_color && s_color.length ? s_color : '#000000');
  81. backgroundColorPicker.set('value', s_backgroundColor && s_backgroundColor.length ? s_backgroundColor : '#ffffff');
  82. } else {
  83. // TODO: Use default style here
  84. form.attr('value', {
  85. fontFamily: 'sans-serif',
  86. fontSize: 12,
  87. textStyle: []
  88. });
  89. textColorPicker.set('value', '#000000');
  90. backgroundColorPicker.set('value', '#ffffff');
  91. }
  92. };
  93. function init(cb) {
  94. require([
  95. "dojo",
  96. "dojo/ready",
  97. "dojo/dom-construct",
  98. "dijit/layout/ContentPane",
  99. "dojox/widget/ColorPicker",
  100. "webodf/editor/widgets/fontPicker"
  101. ], function (dojo, ready, domConstruct, ContentPane, ColorPicker, FontPicker) {
  102. var editorBase = dojo.config && dojo.config.paths &&
  103. dojo.config.paths['webodf/editor'];
  104. runtime.assert(editorBase, "webodf/editor path not defined in dojoConfig");
  105. ready(function () {
  106. contentPane = new ContentPane({
  107. title: runtime.tr("Font Effects"),
  108. href: editorBase+"/widgets/dialogWidgets/fontEffectsPane.html",
  109. preload: true
  110. });
  111. contentPane.onLoad = function () {
  112. var textColorTB = dijit.byId('textColorTB'),
  113. backgroundColorTB = dijit.byId('backgroundColorTB');
  114. form = dijit.byId('fontEffectsPaneForm');
  115. runtime.translateContent(form.domNode);
  116. preview = document.getElementById('previewText');
  117. textColorPicker = dijit.byId('textColorPicker');
  118. backgroundColorPicker = dijit.byId('backgroundColorPicker');
  119. // Bind dojox widgets' values to invisible form elements, for easy parsing
  120. textColorPicker.onChange = function (value) {
  121. textColorTB.set('value', value);
  122. };
  123. backgroundColorPicker.onChange = function (value) {
  124. backgroundColorTB.set('value', value);
  125. };
  126. fontPicker = new FontPicker(function (picker) {
  127. picker.widget().startup();
  128. document.getElementById('fontPicker').appendChild(picker.widget().domNode);
  129. picker.widget().name = 'fontName';
  130. picker.setEditorSession(editorSession);
  131. });
  132. // Automatically update preview when selections change
  133. form.watch('value', function () {
  134. if (form.value.textStyle.indexOf('bold') !== -1) {
  135. preview.style.fontWeight = 'bold';
  136. } else {
  137. preview.style.fontWeight = 'normal';
  138. }
  139. if (form.value.textStyle.indexOf('italic') !== -1) {
  140. preview.style.fontStyle = 'italic';
  141. } else {
  142. preview.style.fontStyle = 'normal';
  143. }
  144. if (form.value.textStyle.indexOf('underline') !== -1) {
  145. preview.style.textDecoration = 'underline';
  146. } else {
  147. preview.style.textDecoration = 'none';
  148. }
  149. preview.style.fontSize = form.value.fontSize + 'pt';
  150. preview.style.fontFamily = fontPicker.getFamily(form.value.fontName);
  151. preview.style.color = form.value.color;
  152. preview.style.backgroundColor = form.value.backgroundColor;
  153. });
  154. };
  155. return cb();
  156. });
  157. });
  158. }
  159. this.setEditorSession = function(session) {
  160. editorSession = session;
  161. if (fontPicker) {
  162. fontPicker.setEditorSession(editorSession);
  163. }
  164. };
  165. init(function () {
  166. return callback(self);
  167. });
  168. };
  169. return FontEffectsPane;
  170. });