EntityPalette.js 6.7 KB

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