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