Breadcrumb.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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.Breadcrumb"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.editor.plugins.Breadcrumb"] = true;
  8. dojo.provide("dojox.editor.plugins.Breadcrumb");
  9. dojo.require("dojo.string");
  10. dojo.require("dijit.Toolbar");
  11. dojo.require("dijit.Menu");
  12. dojo.require("dijit.MenuItem");
  13. dojo.require("dijit.MenuSeparator");
  14. dojo.require("dijit._editor.range");
  15. dojo.require("dijit._editor.selection");
  16. dojo.require("dijit._editor._Plugin");
  17. dojo.require("dijit.form.Button");
  18. dojo.require("dojo.i18n");
  19. dojo.requireLocalization("dojox.editor.plugins", "Breadcrumb", 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");
  20. dojo.experimental("dojox.editor.plugins.Breadcrumb");
  21. dojo.declare("dojox.editor.plugins._BreadcrumbMenuTitle",[dijit._Widget, dijit._Templated, dijit._Contained],{
  22. // summary:
  23. // SImple internal, non-clickable, menu entry to act as a menu title bar.
  24. templateString: "<tr><td dojoAttachPoint=\"title\" colspan=\"4\" class=\"dijitToolbar\" style=\"font-weight: bold; padding: 3px;\"></td></tr>",
  25. menuTitle: "",
  26. postCreate: function(){
  27. dojo.setSelectable(this.domNode, false);
  28. var label = this.id+"_text";
  29. dijit.setWaiState(this.domNode, "labelledby", label);
  30. },
  31. _setMenuTitleAttr: function(str){
  32. this.title.innerHTML = str;
  33. },
  34. _getMenuTitleAttr: function(str){
  35. return this.title.innerHTML;
  36. }
  37. });
  38. dojo.declare("dojox.editor.plugins.Breadcrumb",dijit._editor._Plugin,{
  39. // summary:
  40. // This plugin provides Breadcrumb cabability to the editor. When
  41. // As you move around the editor, it updates with your current indention
  42. // depth.
  43. // _menu: [private]
  44. // The popup menu that is displayed.
  45. _menu: null,
  46. // breadcrumbBar: [protected]
  47. // The toolbar containing the breadcrumb.
  48. breadcrumbBar: null,
  49. setEditor: function(editor){
  50. // summary:
  51. // Over-ride for the setting of the editor.
  52. // editor: Object
  53. // The editor to configure for this plugin to use.
  54. this.editor = editor;
  55. this._buttons = [];
  56. this.breadcrumbBar = new dijit.Toolbar();
  57. var strings = dojo.i18n.getLocalization("dojox.editor.plugins", "Breadcrumb");
  58. this._titleTemplate = strings.nodeActions;
  59. dojo.place(this.breadcrumbBar.domNode, editor.footer);
  60. this.editor.onLoadDeferred.addCallback(dojo.hitch(this, function(){
  61. this._menu = new dijit.Menu({});
  62. dojo.addClass(this.breadcrumbBar.domNode, "dojoxEditorBreadcrumbArrow");
  63. var self = this;
  64. var body = new dijit.form.ComboButton({
  65. showLabel: true,
  66. label: "body",
  67. _selNode: editor.editNode,
  68. dropDown: this._menu,
  69. onClick: dojo.hitch(this, function(){
  70. this._menuTarget = editor.editNode;
  71. this._selectContents();
  72. })
  73. });
  74. // Build the menu
  75. this._menuTitle = new dojox.editor.plugins._BreadcrumbMenuTitle({menuTitle: strings.nodeActions});
  76. this._selCMenu = new dijit.MenuItem({label: strings.selectContents, onClick: dojo.hitch(this, this._selectContents)});
  77. this._delCMenu = new dijit.MenuItem({label: strings.deleteContents, onClick: dojo.hitch(this, this._deleteContents)});
  78. this._selEMenu = new dijit.MenuItem({label: strings.selectElement, onClick: dojo.hitch(this, this._selectElement)});
  79. this._delEMenu = new dijit.MenuItem({label: strings.deleteElement, onClick: dojo.hitch(this, this._deleteElement)});
  80. this._moveSMenu = new dijit.MenuItem({label: strings.moveStart, onClick: dojo.hitch(this, this._moveCToStart)});
  81. this._moveEMenu = new dijit.MenuItem({label: strings.moveEnd, onClick: dojo.hitch(this, this._moveCToEnd)});
  82. this._menu.addChild(this._menuTitle);
  83. this._menu.addChild(this._selCMenu);
  84. this._menu.addChild(this._delCMenu);
  85. this._menu.addChild(new dijit.MenuSeparator({}));
  86. this._menu.addChild(this._selEMenu);
  87. this._menu.addChild(this._delEMenu);
  88. this._menu.addChild(new dijit.MenuSeparator({}));
  89. this._menu.addChild(this._moveSMenu);
  90. this._menu.addChild(this._moveEMenu);
  91. body._ddConnect = dojo.connect(body, "openDropDown", dojo.hitch(this, function(){
  92. this._menuTarget = body._selNode;
  93. this._menuTitle.set("menuTitle", dojo.string.substitute(this._titleTemplate,{
  94. "nodeName": "&lt;body&gt;"
  95. }));
  96. this._selEMenu.set("disabled", true);
  97. this._delEMenu.set("disabled", true);
  98. this._selCMenu.set("disabled", false);
  99. this._delCMenu.set("disabled", false);
  100. this._moveSMenu.set("disabled", false);
  101. this._moveEMenu.set("disabled", false);
  102. }));
  103. this.breadcrumbBar.addChild(body);
  104. this.connect(this.editor, "onNormalizedDisplayChanged", "updateState");
  105. }));
  106. this.breadcrumbBar.startup();
  107. if(dojo.isIE){
  108. // Sometimes IE will mess up layout and needs to be poked.
  109. setTimeout(dojo.hitch(this, function(){this.breadcrumbBar.domNode.className = this.breadcrumbBar.domNode.className;}), 100);
  110. }
  111. },
  112. _selectContents: function(){
  113. // summary:
  114. // Internal function for selecting the contents of a node.
  115. this.editor.focus();
  116. if(this._menuTarget){
  117. var nodeName = this._menuTarget.tagName.toLowerCase();
  118. switch(nodeName){
  119. case 'br':
  120. case 'hr':
  121. case 'img':
  122. case 'input':
  123. case 'base':
  124. case 'meta':
  125. case 'area':
  126. case 'basefont':
  127. break;
  128. default:
  129. try{
  130. dojo.withGlobal(this.editor.window,
  131. "collapse", dijit._editor.selection, [null]);
  132. dojo.withGlobal(this.editor.window,
  133. "selectElementChildren", dijit._editor.selection, [this._menuTarget]);
  134. this.editor.onDisplayChanged();
  135. }catch(e){/*squelch*/}
  136. }
  137. }
  138. },
  139. _deleteContents: function(){
  140. // summary:
  141. // Internal function for selecting the contents of a node.
  142. if(this._menuTarget){
  143. this.editor.beginEditing();
  144. this._selectContents();
  145. dojo.withGlobal(this.editor.window,
  146. "remove", dijit._editor.selection, [this._menuTarget]);
  147. this.editor.endEditing();
  148. this._updateBreadcrumb();
  149. this.editor.onDisplayChanged();
  150. }
  151. },
  152. _selectElement: function(){
  153. // summary:
  154. // Internal function for selecting the contents of a node.
  155. this.editor.focus();
  156. if(this._menuTarget){
  157. dojo.withGlobal(this.editor.window,
  158. "collapse", dijit._editor.selection, [null]);
  159. dojo.withGlobal(this.editor.window,
  160. "selectElement", dijit._editor.selection, [this._menuTarget]);
  161. this.editor.onDisplayChanged();
  162. }
  163. },
  164. _deleteElement: function(){
  165. // summary:
  166. // Internal function for selecting the contents of a node.
  167. if(this._menuTarget){
  168. this.editor.beginEditing();
  169. this._selectElement();
  170. dojo.withGlobal(this.editor.window,
  171. "remove", dijit._editor.selection, [this._menuTarget]);
  172. this.editor.endEditing();
  173. this._updateBreadcrumb();
  174. this.editor.onDisplayChanged();
  175. }
  176. },
  177. _moveCToStart: function(){
  178. // summary:
  179. // Internal function for selecting the contents of a node.
  180. this.editor.focus();
  181. if(this._menuTarget){
  182. this._selectContents();
  183. dojo.withGlobal(this.editor.window,
  184. "collapse", dijit._editor.selection, [true]);
  185. }
  186. },
  187. _moveCToEnd: function(){
  188. // summary:
  189. // Internal function for selecting the contents of a node.
  190. this.editor.focus();
  191. if(this._menuTarget){
  192. this._selectContents();
  193. dojo.withGlobal(this.editor.window,
  194. "collapse", dijit._editor.selection, [false]);
  195. }
  196. },
  197. _updateBreadcrumb: function(){
  198. // summary:
  199. // Function to trigger updating of the breadcrumb
  200. // tags:
  201. // private
  202. var ed = this.editor;
  203. if(ed.window){
  204. var sel = dijit.range.getSelection(ed.window);
  205. if(sel && sel.rangeCount > 0){
  206. var range = sel.getRangeAt(0);
  207. // Check the getSelectedElement call. Needed when dealing with img tags.
  208. var node = dojo.withGlobal(ed.window,
  209. "getSelectedElement", dijit._editor.selection) || range.startContainer;
  210. //var node = range.startContainer;
  211. var bcList = [];
  212. // Make sure we get a selection within the editor document,
  213. // have seen cases on IE where this wasn't true.
  214. if(node && node.ownerDocument === ed.document){
  215. while(node && node !== ed.editNode && node != ed.document.body && node != ed.document){
  216. if(node.nodeType === 1){
  217. bcList.push({type: node.tagName.toLowerCase(), node: node});
  218. }
  219. node = node.parentNode;
  220. }
  221. bcList = bcList.reverse();
  222. while(this._buttons.length){
  223. var db = this._buttons.pop();
  224. dojo.disconnect(db._ddConnect);
  225. this.breadcrumbBar.removeChild(db);
  226. }
  227. this._buttons = [];
  228. var i;
  229. var self = this;
  230. for(i = 0; i < bcList.length; i++){
  231. var bc = bcList[i];
  232. var b = new dijit.form.ComboButton({
  233. showLabel: true,
  234. label: bc.type,
  235. _selNode: bc.node,
  236. dropDown: this._menu,
  237. onClick: function(){
  238. self._menuTarget = this._selNode;
  239. self._selectContents();
  240. }
  241. });
  242. b._ddConnect = dojo.connect(b, "openDropDown", dojo.hitch(b, function(){
  243. self._menuTarget = this._selNode;
  244. var nodeName = self._menuTarget.tagName.toLowerCase();
  245. var title = dojo.string.substitute(self._titleTemplate,{
  246. "nodeName": "&lt;" + nodeName + "&gt;"
  247. });
  248. self._menuTitle.set("menuTitle", title);
  249. switch(nodeName){
  250. case 'br':
  251. case 'hr':
  252. case 'img':
  253. case 'input':
  254. case 'base':
  255. case 'meta':
  256. case 'area':
  257. case 'basefont':
  258. self._selCMenu.set("disabled", true);
  259. self._delCMenu.set("disabled", true);
  260. self._moveSMenu.set("disabled", true);
  261. self._moveEMenu.set("disabled", true);
  262. self._selEMenu.set("disabled", false);
  263. self._delEMenu.set("disabled", false);
  264. break;
  265. default:
  266. self._selCMenu.set("disabled", false);
  267. self._delCMenu.set("disabled", false);
  268. self._selEMenu.set("disabled", false);
  269. self._delEMenu.set("disabled", false);
  270. self._moveSMenu.set("disabled", false);
  271. self._moveEMenu.set("disabled", false);
  272. }
  273. }));
  274. this._buttons.push(b);
  275. this.breadcrumbBar.addChild(b);
  276. }
  277. if(dojo.isIE){
  278. // Prod it to fix layout.
  279. this.breadcrumbBar.domNode.className = this.breadcrumbBar.domNode.className;
  280. }
  281. }
  282. }
  283. }
  284. },
  285. updateState: function(){
  286. // summary:
  287. // Over-ride of updateState to hide the toolbar when the iframe is not visible.
  288. // Also triggers the breadcrumb update.
  289. if(dojo.style(this.editor.iframe, "display") === "none" || this.get("disabled")){
  290. dojo.style(this.breadcrumbBar.domNode, "display", "none");
  291. }else{
  292. if(dojo.style(this.breadcrumbBar.domNode, "display") === "none"){
  293. dojo.style(this.breadcrumbBar.domNode, "display", "block");
  294. }
  295. this._updateBreadcrumb();
  296. // Some themes do padding, so we have to resize again after display.
  297. var size = dojo.marginBox(this.editor.domNode);
  298. this.editor.resize({h: size.h});
  299. }
  300. },
  301. destroy: function(){
  302. // summary:
  303. // Over-ride to clean up the breadcrumb toolbar.
  304. if(this.breadcrumbBar){
  305. this.breadcrumbBar.destroyRecursive();
  306. this.breadcrumbBar = null;
  307. }
  308. if(this._menu){
  309. this._menu.destroyRecursive();
  310. delete this._menu;
  311. }
  312. this._buttons = null;
  313. delete this.editor.breadcrumbBar;
  314. this.inherited(arguments);
  315. }
  316. });
  317. // Register this plugin.
  318. dojo.subscribe(dijit._scopeName + ".Editor.getPlugin",null,function(o){
  319. if(o.plugin){ return; }
  320. var name = o.args.name.toLowerCase();
  321. if(name === "breadcrumb"){
  322. o.plugin = new dojox.editor.plugins.Breadcrumb({});
  323. }
  324. });
  325. }