wordimage.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * Created by JetBrains PhpStorm.
  3. * User: taoqili
  4. * Date: 12-1-30
  5. * Time: 下午12:50
  6. * To change this template use File | Settings | File Templates.
  7. */
  8. var wordImage = {};
  9. //(function(){
  10. var g = baidu.g,
  11. flashObj,flashContainer;
  12. wordImage.init = function(opt, callbacks) {
  13. showLocalPath("localPath");
  14. //createCopyButton("clipboard","localPath");
  15. createFlashUploader(opt, callbacks);
  16. addUploadListener();
  17. addOkListener();
  18. };
  19. function hideFlash(){
  20. flashObj = null;
  21. flashContainer.innerHTML = "";
  22. }
  23. function addOkListener() {
  24. dialog.onok = function() {
  25. if (!imageUrls.length) return;
  26. var images = domUtils.getElementsByTagName(editor.document,"img");
  27. editor.fireEvent('saveScene');
  28. for (var i = 0,img; img = images[i++];) {
  29. var src = img.getAttribute("word_img");
  30. if (!src) continue;
  31. for (var j = 0,url; url = imageUrls[j++];) {
  32. if (src.indexOf(url.original.replace(" ","")) != -1) {
  33. img.src = editor.options.wordImagePath + url.url;
  34. img.setAttribute("_src", editor.options.wordImagePath + url.url); //同时修改"_src"属性
  35. img.setAttribute("title",url.title);
  36. domUtils.removeAttributes(img, ["word_img","style","width","height"]);
  37. editor.fireEvent("selectionchange");
  38. break;
  39. }
  40. }
  41. }
  42. editor.fireEvent('saveScene');
  43. hideFlash();
  44. };
  45. dialog.oncancel = function(){
  46. hideFlash();
  47. }
  48. }
  49. /**
  50. * 绑定开始上传事件
  51. */
  52. function addUploadListener() {
  53. g("upload").onclick = function () {
  54. flashObj.upload();
  55. this.style.display = "none";
  56. };
  57. }
  58. function showLocalPath(id) {
  59. //单张编辑
  60. var img = editor.selection.getRange().getClosedNode();
  61. var images = editor.execCommand('wordimage');
  62. if(images.length==1 || img && img.tagName == 'IMG'){
  63. g(id).value = images[0];
  64. return;
  65. }
  66. var path = images[0];
  67. var leftSlashIndex = path.lastIndexOf("/")||0, //不同版本的doc和浏览器都可能影响到这个符号,故直接判断两种
  68. rightSlashIndex = path.lastIndexOf("\\")||0,
  69. separater = leftSlashIndex > rightSlashIndex ? "/":"\\" ;
  70. path = path.substring(0, path.lastIndexOf(separater)+1);
  71. g(id).value = path;
  72. }
  73. function createFlashUploader(opt, callbacks) {
  74. //由于lang.flashI18n是静态属性,不可以直接进行修改,否则会影响到后续内容
  75. var i18n = utils.extend({},lang.flashI18n);
  76. //处理图片资源地址的编码,补全等问题
  77. for(var i in i18n){
  78. if(!(i in {"lang":1,"uploadingTF":1,"imageTF":1,"textEncoding":1}) && i18n[i]){
  79. i18n[i] = encodeURIComponent(editor.options.langPath + editor.options.lang + "/images/" + i18n[i]);
  80. }
  81. }
  82. opt = utils.extend(opt,i18n,false);
  83. var option = {
  84. createOptions:{
  85. id:'flash',
  86. url:opt.flashUrl,
  87. width:opt.width,
  88. height:opt.height,
  89. errorMessage:lang.flashError,
  90. wmode:browser.safari ? 'transparent' : 'window',
  91. ver:'10.0.0',
  92. vars:opt,
  93. container:opt.container
  94. }
  95. };
  96. option = extendProperty(callbacks, option);
  97. flashObj = new baidu.flash.imageUploader(option);
  98. flashContainer = $G(opt.container);
  99. }
  100. function extendProperty(fromObj, toObj) {
  101. for (var i in fromObj) {
  102. if (!toObj[i]) {
  103. toObj[i] = fromObj[i];
  104. }
  105. }
  106. return toObj;
  107. }
  108. //})();
  109. function getPasteData(id) {
  110. baidu.g("msg").innerHTML = lang.copySuccess + "</br>";
  111. setTimeout(function() {
  112. baidu.g("msg").innerHTML = "";
  113. }, 5000);
  114. return baidu.g(id).value;
  115. }
  116. function createCopyButton(id, dataFrom) {
  117. baidu.swf.create({
  118. id:"copyFlash",
  119. url:"fClipboard_ueditor.swf",
  120. width:"58",
  121. height:"25",
  122. errorMessage:"",
  123. bgColor:"#CBCBCB",
  124. wmode:"transparent",
  125. ver:"10.0.0",
  126. vars:{
  127. tid:dataFrom
  128. }
  129. }, id
  130. );
  131. var clipboard = baidu.swf.getMovie("copyFlash");
  132. var clipinterval = setInterval(function() {
  133. if (clipboard && clipboard.flashInit) {
  134. clearInterval(clipinterval);
  135. clipboard.setHandCursor(true);
  136. clipboard.setContentFuncName("getPasteData");
  137. //clipboard.setMEFuncName("mouseEventHandler");
  138. }
  139. }, 500);
  140. }
  141. createCopyButton("clipboard", "localPath");