fontPicker.js 4.6 KB

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