toolbar.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * Created by JetBrains PhpStorm.
  3. * User: dongyancen
  4. * Date: 12-4-12
  5. * Time: 下午4:46
  6. * To change this template use File | Settings | File Templates.
  7. */
  8. module( 'ui.toolbar' );
  9. test( 'toolbar显示', function() {
  10. var editor = new baidu.editor.ui.Editor();
  11. var toolbar =[ 'link','unlink','|'];
  12. baidu.editor.ui['link'] = function (cmd){
  13. return function (editor, title){
  14. var ui = new baidu.editor.ui.Button({
  15. className: 'edui-for-' + cmd,
  16. title: title || editor.options.labelMap || editor.getLang('labelMap') || '',
  17. onclick: function (){
  18. editor.execCommand(cmd);
  19. },
  20. showText: false,
  21. editor:editor
  22. });
  23. editor.addListener('selectionchange', function (type, causeByUi, uiReady){
  24. var state = editor.queryCommandState(cmd);
  25. if (state == -1) {
  26. ui.setDisabled(true);
  27. ui.setChecked(false);
  28. } else {
  29. if(!uiReady){
  30. ui.setDisabled(false);
  31. ui.setChecked(state);
  32. }
  33. }
  34. });
  35. return ui;
  36. };
  37. }('link');
  38. baidu.editor.ui['unlink'] = function (cmd){
  39. return function (editor, title){
  40. var ui = new baidu.editor.ui.Button({
  41. className: 'edui-for-' + cmd,
  42. title: title || editor.options.labelMap || editor.getLang('labelMap') || '',
  43. onclick: function (){
  44. editor.execCommand(cmd);
  45. },
  46. showText: false,
  47. editor:editor
  48. });
  49. editor.addListener('selectionchange', function (type, causeByUi, uiReady){
  50. var state = editor.queryCommandState(cmd);
  51. if (state == -1) {
  52. ui.setDisabled(true);
  53. ui.setChecked(false);
  54. } else {
  55. if(!uiReady){
  56. ui.setDisabled(false);
  57. ui.setChecked(state);
  58. }
  59. }
  60. });
  61. return ui;
  62. };
  63. }('unlink');
  64. var toolbarUi = new baidu.editor.ui.Toolbar({theme:editor.options.theme});
  65. for ( var j = 0; j < toolbar.length; j++ ) {
  66. var toolbarItem = toolbar[j].toLowerCase();
  67. var toolbarItemUi = null;
  68. if ( typeof toolbarItem == 'string' ) {
  69. if ( toolbarItem == '|' ) {
  70. toolbarItem = 'Separator';
  71. }
  72. if ( baidu.editor.ui[toolbarItem] ) {
  73. toolbarItemUi = new baidu.editor.ui[toolbarItem]( editor );
  74. }
  75. } else {
  76. toolbarItemUi = toolbarItem;
  77. }
  78. if ( toolbarItemUi ) {
  79. toolbarUi.add( toolbarItemUi );
  80. }
  81. }
  82. var theme = ' edui-'+editor.options.theme;
  83. toolbarUi.render(te.dom[0]);
  84. toolbarUi.postRender();
  85. var toolbarShow = document.getElementById('editor').firstChild;
  86. equal(toolbarShow.className,'edui-toolbar '+theme,'检查toolbar的显示');
  87. equal(toolbarShow.childNodes.length ,toolbar.length,'检查toolbar的显示');
  88. for(var i=0;i<toolbar.length;i++){
  89. if(toolbar[i] =='|'){
  90. equal(toolbarShow.childNodes[i].className,'edui-box edui-separator '+theme,'检查toolbar的显示');
  91. }
  92. else{
  93. equal(toolbarShow.childNodes[i].className,'edui-box edui-button edui-for-' + toolbar[i] + theme,'检查toolbar的显示');
  94. }
  95. }
  96. var link = toolbarShow.childNodes[0].firstChild;
  97. equal(link.className,'edui-'+editor.options.theme,'mouseover效果');
  98. if(ua.browser.ie){
  99. ua.mouseenter(link);
  100. }
  101. else{
  102. ua.mouseover(link);
  103. }
  104. setTimeout(function (){
  105. link = document.getElementById('editor').firstChild.childNodes[0].firstChild;
  106. equal(link.className,'edui-'+editor.options.theme+' edui-state-hover','mouseover效果');
  107. if(ua.browser.ie){
  108. ua.mouseleave(link);
  109. }
  110. else{
  111. ua.mouseout(link);
  112. }
  113. setTimeout(function (){
  114. link = document.getElementById('editor').firstChild.childNodes[0].firstChild;
  115. equal(link.className,'edui-'+editor.options.theme ,'mouseout效果');
  116. toolbarUi.dispose();
  117. equal(document.getElementById('editor').childNodes.length,0,'成功销毁');
  118. start();
  119. }, 400);
  120. }, 300);
  121. stop();
  122. } );
  123. test( 'toolbar点击', function() {
  124. var toolbar =[ 'link','unlink'];
  125. baidu.editor.ui['link'] = function (cmd){
  126. return function (editor, title){
  127. var ui = new baidu.editor.ui.Button({
  128. className: 'edui-for-' + cmd,
  129. title: title || editor.options.labelMap || editor.getLang('labelMap') || '',
  130. onclick: function (){
  131. editor.execCommand(cmd, {href:'http://www.google.com/'});
  132. },
  133. showText: false,
  134. editor:editor
  135. });
  136. editor.addListener('selectionchange', function (type, causeByUi, uiReady){
  137. var state = editor.queryCommandState(cmd);
  138. if (state == -1) {
  139. ui.setDisabled(true);
  140. ui.setChecked(false);
  141. } else {
  142. if(!uiReady){
  143. ui.setDisabled(false);
  144. ui.setChecked(state);
  145. }
  146. }
  147. });
  148. return ui;
  149. };
  150. }('link');
  151. baidu.editor.ui['unlink'] = function (cmd){
  152. return function (editor, title){
  153. var ui = new baidu.editor.ui.Button({
  154. className: 'edui-for-' + cmd,
  155. title: title ||editor.options.labelMap || editor.getLang('labelMap') || '',
  156. onclick: function (){
  157. editor.execCommand(cmd);
  158. },
  159. showText: false,
  160. editor:editor
  161. });
  162. editor.addListener('selectionchange', function (type, causeByUi, uiReady){
  163. var state = editor.queryCommandState(cmd);
  164. if (state == -1) {
  165. ui.setDisabled(true);
  166. ui.setChecked(false);
  167. } else {
  168. if(!uiReady){
  169. ui.setDisabled(false);
  170. ui.setChecked(state);
  171. }
  172. }
  173. });
  174. return ui;
  175. };
  176. }('unlink');
  177. var editor = new baidu.editor.ui.Editor();
  178. // editor.render('editor');
  179. var toolbarUi = new baidu.editor.ui.Toolbar({theme:editor.options.theme});
  180. for ( var j = 0; j < toolbar.length; j++ ) {
  181. var toolbarItem = toolbar[j].toLowerCase();
  182. var toolbarItemUi = null;
  183. if ( typeof toolbarItem == 'string' ) {
  184. if ( toolbarItem == '|' ) {
  185. toolbarItem = 'Separator';
  186. }
  187. if ( baidu.editor.ui[toolbarItem] ) {
  188. toolbarItemUi = new baidu.editor.ui[toolbarItem]( editor );
  189. }
  190. } else {
  191. toolbarItemUi = toolbarItem;
  192. }
  193. if ( toolbarItemUi ) {
  194. toolbarUi.add( toolbarItemUi );
  195. }
  196. }
  197. toolbarUi.render(te.dom[0]);
  198. toolbarUi.postRender();
  199. var toolbarShow = document.getElementById('editor').firstChild;
  200. var linkButton = toolbarShow.childNodes[0].firstChild.firstChild.firstChild;
  201. var content = document.createElement('div');
  202. content.innerHTML = '<p>hello</p>';
  203. document.body.appendChild(content);
  204. //要使用link,先要设置range和editor.body等内容
  205. editor.selection = new dom.Selection( document );
  206. var range = new baidu.editor.dom.Range( document );
  207. editor.body = document.getElementById('editor').nextSibling;
  208. range.selectNode(document.body.lastChild).select();
  209. editor.document = editor.body;
  210. ua.click(linkButton);
  211. setTimeout(function (){
  212. equal(document.body.lastChild.firstChild.firstChild.attributes['href'].nodeValue,'http://www.google.com/','点击link按钮后,生成连接');
  213. equal(document.body.lastChild.firstChild.firstChild.innerHTML,'hello','点击link按钮后,生成连接');
  214. toolbarUi.dispose();
  215. content.parentNode.removeChild(content);
  216. start();
  217. }, 300);
  218. stop();
  219. } );