fontPicker.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * Copyright (C) 2012 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 define,require,document */
  25. define("webodf/editor/widgets/fontPicker", [
  26. "dijit/form/Select"],
  27. function (Select) {
  28. "use strict";
  29. /**
  30. * @constructor
  31. */
  32. var FontPicker = function (callback) {
  33. var self = this,
  34. editorSession,
  35. select,
  36. documentFonts = [];
  37. select = new Select({
  38. name: 'FontPicker',
  39. disabled: true,
  40. maxHeight: 200,
  41. style: {
  42. width: '150px'
  43. }
  44. });
  45. this.widget = function () {
  46. return select;
  47. };
  48. this.value = function () {
  49. return select.get('value');
  50. };
  51. this.setValue = function (value) {
  52. select.set('value', value);
  53. };
  54. /**
  55. * Returns the font family for a given font name. If unavailable,
  56. * return the name itself (e.g. editor fonts won't have a name-family separation
  57. * @param {!string} name
  58. * @return {!string}
  59. */
  60. this.getFamily = function (name) {
  61. var i;
  62. for (i = 0; i < documentFonts.length; i += 1) {
  63. if ((documentFonts[i].name === name) && documentFonts[i].family) {
  64. return documentFonts[i].family;
  65. }
  66. }
  67. return name;
  68. };
  69. // events
  70. this.onAdd = null;
  71. this.onRemove = null;
  72. function populateFonts() {
  73. var i,
  74. name,
  75. family,
  76. editorFonts = editorSession ? editorSession.availableFonts : [],
  77. selectionList = [];
  78. documentFonts = editorSession ? editorSession.getDeclaredFonts() : [];
  79. // First populate the fonts used in the document
  80. for (i = 0; i < documentFonts.length; i += 1) {
  81. name = documentFonts[i].name;
  82. family = documentFonts[i].family || name;
  83. selectionList.push({
  84. label: '<span style="font-family: ' + family + ';">' + name + '</span>',
  85. value: name
  86. });
  87. }
  88. if (editorFonts.length) {
  89. // Then add a separator
  90. selectionList.push({
  91. type: 'separator'
  92. });
  93. }
  94. // Lastly populate the fonts provided by the editor
  95. for (i = 0; i < editorFonts.length; i += 1) {
  96. selectionList.push({
  97. label: '<span style="font-family: ' + editorFonts[i] + ';">' + editorFonts[i] + '</span>',
  98. value: editorFonts[i]
  99. });
  100. }
  101. select.removeOption(select.getOptions());
  102. select.addOption(selectionList);
  103. }
  104. this.setEditorSession = function(session) {
  105. editorSession = session;
  106. populateFonts();
  107. select.setAttribute('disabled', !editorSession);
  108. };
  109. populateFonts();
  110. callback(self);
  111. };
  112. return FontPicker;
  113. });