EntityPalette.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.editor.plugins.EntityPalette"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.editor.plugins.EntityPalette"] = true;
  8. dojo.provide("dojox.editor.plugins.EntityPalette");
  9. dojo.require("dijit._Widget");
  10. dojo.require("dijit._Templated");
  11. dojo.require("dijit._PaletteMixin");
  12. dojo.require("dojo.i18n");
  13. dojo.requireLocalization("dojox.editor.plugins", "latinEntities", null, "ROOT,ar,bg,ca,cs,da,de,el,es,fi,fr,he,hr,hu,it,ja,kk,ko,nb,nl,pl,pt,pt-pt,ro,ru,sk,sl,sv,th,tr,zh,zh-tw");
  14. dojo.experimental("dojox.editor.plugins.EntityPalette");
  15. dojo.declare("dojox.editor.plugins.EntityPalette",
  16. [dijit._Widget, dijit._Templated, dijit._PaletteMixin],
  17. {
  18. // summary:
  19. // A keyboard accessible HTML entity-picking widget (for inserting symbol characters)
  20. // description:
  21. // Grid showing various entities, so the user can pick a certain entity.
  22. // Can be used standalone, or as a popup.
  23. //
  24. // example:
  25. // | <div dojoType="dojox.editor.plugins.EntityPalette"></div>
  26. //
  27. // example:
  28. // | var picker = new dojox.editor.plugins.EntityPalette({ },srcNode);
  29. // | picker.startup();
  30. // templateString: [protected] String
  31. // The basic template used to render the palette.
  32. // Should generally be over-ridden to define different classes.
  33. templateString: '<div class="dojoxEntityPalette">\n' +
  34. ' <table>\n' +
  35. ' <tbody>\n' +
  36. ' <tr>\n' +
  37. ' <td>\n' +
  38. ' <table class="dijitPaletteTable">\n' +
  39. ' <tbody dojoAttachPoint="gridNode"></tbody>\n' +
  40. ' </table>\n' +
  41. ' </td>\n' +
  42. ' </tr>\n' +
  43. ' <tr>\n' +
  44. ' <td>\n'+
  45. ' <table dojoAttachPoint="previewPane" class="dojoxEntityPalettePreviewTable">\n' +
  46. ' <tbody>\n' +
  47. ' <tr>\n' +
  48. ' <th class="dojoxEntityPalettePreviewHeader">Preview</th>\n' +
  49. ' <th class="dojoxEntityPalettePreviewHeader" dojoAttachPoint="codeHeader">Code</th>\n' +
  50. ' <th class="dojoxEntityPalettePreviewHeader" dojoAttachPoint="entityHeader">Name</th>\n' +
  51. ' <th class="dojoxEntityPalettePreviewHeader">Description</th>\n' +
  52. ' </tr>\n' +
  53. ' <tr>\n' +
  54. ' <td class="dojoxEntityPalettePreviewDetailEntity" dojoAttachPoint="previewNode"></td>\n' +
  55. ' <td class="dojoxEntityPalettePreviewDetail" dojoAttachPoint="codeNode"></td>\n' +
  56. ' <td class="dojoxEntityPalettePreviewDetail" dojoAttachPoint="entityNode"></td>\n' +
  57. ' <td class="dojoxEntityPalettePreviewDetail" dojoAttachPoint="descNode"></td>\n' +
  58. ' </tr>\n' +
  59. ' </tbody>\n' +
  60. ' </table>\n' +
  61. ' </td>\n' +
  62. ' </tr>\n' +
  63. ' </tbody>\n' +
  64. ' </table>\n' +
  65. '</div>',
  66. baseClass: "dojoxEntityPalette",
  67. // showPreview: [public] Boolean
  68. // Whether the preview pane will be displayed, to show details about the selected entity.
  69. showPreview: true,
  70. // showCode: [public] boolean
  71. // Show the character code for the entity.
  72. showCode: false,
  73. // showentityName: [public] boolean
  74. // Show the entity name for the entity.
  75. showEntityName: false,
  76. // palette: [public] String
  77. // The symbol pallete to display. The only current one is 'latin'.
  78. palette: "latin",
  79. dyeClass: 'dojox.editor.plugins.LatinEntity',
  80. // domNodeClass [protected] String
  81. paletteClass: 'editorLatinEntityPalette',
  82. cellClass: "dojoxEntityPaletteCell",
  83. postMixInProperties: function(){
  84. // Convert hash of entities into two-dimensional rows/columns table (array of arrays)
  85. var choices = dojo.i18n.getLocalization("dojox.editor.plugins", "latinEntities");
  86. var numChoices = 0;
  87. var entityKey;
  88. for(entityKey in choices){numChoices++;}
  89. var choicesPerRow = Math.floor(Math.sqrt(numChoices));
  90. var numRows = choicesPerRow;
  91. var currChoiceIdx = 0;
  92. var rows = [];
  93. var row = [];
  94. for(entityKey in choices){
  95. currChoiceIdx++;
  96. row.push(entityKey);
  97. if(currChoiceIdx % numRows === 0){
  98. rows.push(row);
  99. row = [];
  100. }
  101. }
  102. if(row.length > 0){
  103. rows.push(row);
  104. }
  105. this._palette = rows;
  106. },
  107. buildRendering: function(){
  108. // Instantiate the template, which makes a skeleton table which we'll insert the entities
  109. this.inherited(arguments);
  110. var i18n = dojo.i18n.getLocalization("dojox.editor.plugins", "latinEntities");
  111. this._preparePalette(
  112. this._palette,
  113. i18n
  114. );
  115. var cells = dojo.query(".dojoxEntityPaletteCell", this.gridNode);
  116. dojo.forEach(cells, function(cellNode){
  117. this.connect(cellNode, "onmouseenter", "_onCellMouseEnter");
  118. }, this);
  119. },
  120. _onCellMouseEnter: function(e){
  121. // summary:
  122. // Simple function to handle updating the display at the bottom of
  123. // the palette.
  124. // e:
  125. // The event.
  126. // tags:
  127. // private
  128. this._displayDetails(e.target);
  129. },
  130. postCreate: function(){
  131. this.inherited(arguments);
  132. // Show the code and entity name (if enabled to do so.)
  133. dojo.style(this.codeHeader, "display", this.showCode?"":"none");
  134. dojo.style(this.codeNode, "display", this.showCode?"":"none");
  135. dojo.style(this.entityHeader, "display", this.showEntityName?"":"none");
  136. dojo.style(this.entityNode, "display", this.showEntityName?"":"none");
  137. if(!this.showPreview){
  138. dojo.style(this.previewNode,"display","none");
  139. }
  140. },
  141. _setCurrent: function(/*DOMNode*/ node){
  142. // summary:
  143. // Called when a entity is hovered or focused.
  144. // description:
  145. // Removes highlight of the old entity, and highlights
  146. // the new entity.
  147. // tags:
  148. // protected
  149. this.inherited(arguments);
  150. if(this.showPreview){
  151. this._displayDetails(node);
  152. }
  153. },
  154. _displayDetails: function(/*DOMNode*/ cell){
  155. // summary:
  156. // Display the details of the currently focused entity in the preview pane
  157. var dye = this._getDye(cell);
  158. if(dye){
  159. var ehtml = dye.getValue();
  160. var ename = dye._alias;
  161. this.previewNode.innerHTML=ehtml;
  162. this.codeNode.innerHTML="&amp;#"+parseInt(ehtml.charCodeAt(0), 10)+";";
  163. this.entityNode.innerHTML="&amp;"+ename+";";
  164. var i18n = dojo.i18n.getLocalization("dojox.editor.plugins", "latinEntities");
  165. this.descNode.innerHTML=i18n[ename].replace("\n", "<br>");
  166. }else{
  167. this.previewNode.innerHTML="";
  168. this.codeNode.innerHTML="";
  169. this.entityNode.innerHTML="";
  170. this.descNode.innerHTML="";
  171. }
  172. }
  173. });
  174. dojo.declare("dojox.editor.plugins.LatinEntity",
  175. null,
  176. {
  177. // summary:
  178. // Represents a character.
  179. // Initialized using an alias for the character (like cent) rather
  180. // than with the character itself.
  181. constructor: function(/*String*/ alias){
  182. // summary:
  183. // Construct JS object representing an entity (associated w/a cell
  184. // in the palette)
  185. // value: String
  186. // alias name: 'cent', 'pound' ..
  187. this._alias = alias;
  188. },
  189. getValue: function(){
  190. // summary:
  191. // Returns HTML representing the character, like &amp;
  192. //
  193. return "&" + this._alias + ";";
  194. },
  195. fillCell: function(/*DOMNode*/ cell){
  196. // Deal with entities that have keys which are reserved words.
  197. cell.innerHTML = this.getValue();
  198. }
  199. });
  200. }