pizhu-assist.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. console.info("assist");
  2. // 用于记录那个录音图标正在播放
  3. var audioId = null;
  4. // 获取焦点
  5. function getRange() {
  6. var selection = window.getSelection ? window.getSelection() :
  7. document.selection;
  8. var range = selection.createRange ? selection.createRange() : selection
  9. .getRangeAt(0);
  10. return range;
  11. }
  12. function saveHtml(id) {
  13. var html = $('#displayArea').html();
  14. $
  15. .ajax({
  16. type: "POST",
  17. url: "/service?wdService=savePiZhuNrHtml&wdtest=false",
  18. data: {
  19. 'id': id,
  20. 'html': html
  21. },
  22. async: false,
  23. dataType: "json",
  24. error: function () {
  25. // alert('fail');
  26. }
  27. });
  28. }
  29. function saveEditor(divId) {
  30. var element = $("#" + divId).find('#displayArea')[0];
  31. var html = $("#" + divId).find('#displayArea').html();
  32. var id = element.getAttribute('nrid');
  33. $.ajax({
  34. type: "POST",
  35. url: "/service?wdService=savePiZhuNrHtml&wdtest=false",
  36. data: {
  37. 'id': id,
  38. 'html': html
  39. },
  40. async: false,
  41. dataType: "json",
  42. error: function () {
  43. // alert('fail');
  44. }
  45. });
  46. }
  47. function addToDisplayArea(item) {
  48. $('#displayArea').focus();
  49. var range = getRange();
  50. range.insertNode(item);
  51. range.setStartAfter(item);
  52. // 为图片添加click事件
  53. // imageClick();
  54. // 为div添加ondragstart事件
  55. addDivDragStart();
  56. // 重新fouse
  57. cursorManager.setEndOfRange(range);
  58. }
  59. // 根据src创建一个图片
  60. function createImage(src) {
  61. var img = new Image();
  62. $(img).addClass('item');
  63. img.src = src;
  64. return img;
  65. }
  66. // 1、为书写状态添加img
  67. function addWritingImage(draw) {
  68. var canvas = draw.canvas;
  69. var img = createImage(canvas.toDataURL());
  70. draw.erase();
  71. draw.isDraw = false;
  72. addToDisplayArea(img);
  73. }
  74. function addRecorderImage(index) {
  75. var img = createImage('/nr/image/py/mobileIcon-voiceMsg.png');
  76. $(img).addClass('audio');
  77. $(img).data('index', index);
  78. // 新增音频内容到服务器中
  79. addToService(index, img, id);
  80. // 为div添加图标
  81. addToDisplayArea(img);
  82. // 为录音图标添加事件
  83. addAudioEvent('.audio');
  84. }
  85. // 将数据上传到服务器,index用于命名,img用于为该元素添加属性
  86. function addToService(index, img, id) {
  87. console.info("进入");
  88. var fd = new FormData();
  89. fd.append("audioData", recorders[index].getBlob());
  90. fd.append('parentId', id);
  91. fd.append('index', index);
  92. var xhr = new XMLHttpRequest();
  93. xhr.onreadystatechange = function () {
  94. if (xhr.readyState == 4 && xhr.status == 200) {
  95. console.info(xhr.responseText);
  96. var result = JSON.parse(xhr.responseText);
  97. if (result['result']) {
  98. $(img).attr('data-id', result['id']);
  99. }
  100. }
  101. }
  102. xhr
  103. .open(
  104. "POST",
  105. "/service?wdService=uploadWAV&wdtest=false",
  106. false);
  107. xhr.send(fd);
  108. console.info("离开");
  109. }
  110. // 为音频图标添加点击事件
  111. function addAudioEvent(selector) {
  112. $(selector).on('click', function (event) {
  113. var target = event.target;
  114. var playFlag = $(event.target).data('palyFlag');
  115. //播放器
  116. var audio = document.getElementById('player');
  117. if (playFlag == null || '' == playFlag || 'stoping' == playFlag) {
  118. //播放内容的index
  119. var index = $(target).data('index');
  120. if (index != audioId) {
  121. audio = null;
  122. }
  123. $(event.target).data('palyFlag', 'playing');
  124. event.target.src = 'nr/image/py/mobileIcon-playVoice.gif';
  125. //audio == null证明需要重新播放录音
  126. if (audio == null) {
  127. audio = document.getElementById('player');
  128. /* 设置audio的url */
  129. playRecording(index);
  130. } else { //继续播放audio中的内容
  131. audio.play();
  132. }
  133. /* 为audio添加结束事件 */
  134. $(audio).on('ended', function (e) {
  135. $(target).data('palyFlag', 'stoping');
  136. target.src = 'nr/image/py/mobileIcon-voiceMsg.png';
  137. audio = null;
  138. audioId = null;
  139. });
  140. } else {
  141. event.target.src = 'nr/image/py/mobileIcon-voiceMsg.png';
  142. audio.pause();
  143. $(target).data('palyFlag', 'stoping');
  144. audioId = $(target).data('index');
  145. }
  146. })
  147. }
  148. // 2、为canvas添加mousedown和mouseup事件
  149. // 如果鼠标离开该canvas且该该画板有书写内容,调用createImage方法
  150. function canvasAddEvent(draw) {
  151. $(draw.canvas).on('mousedown', function (event) {
  152. if (draw.timeoutObject) {
  153. clearTimeout(draw.timeoutObject);
  154. }
  155. });
  156. $(draw.canvas).on('mouseup', function (event) {
  157. if (draw.isDraw) {
  158. var timeoutObject = setTimeout(_addWritingImage(draw), 1000);
  159. draw.timeoutObject = timeoutObject;
  160. }
  161. });
  162. $(draw.canvas).bind('touchstart', function (event) {
  163. if (draw.timeoutObject) {
  164. clearTimeout(draw.timeoutObject);
  165. }
  166. });
  167. $(draw.canvas).bind('touchend', function (event) {
  168. if (draw.isDraw) {
  169. var timeoutObject = setTimeout(_addWritingImage(draw), 1000);
  170. draw.timeoutObject = timeoutObject;
  171. }
  172. });
  173. }
  174. // setTimeout使用的闭包
  175. function _addWritingImage(draw) {
  176. return function () {
  177. addWritingImage(draw);
  178. }
  179. }
  180. function GetUrlRelativePath(url) {
  181. var url = url;
  182. var arrUrl = url.split("//");
  183. var start = arrUrl[1].indexOf("/");
  184. var relUrl = arrUrl[1].substring(start);//stop省略,截取从start开始到结尾的所有字符
  185. // if (relUrl.indexOf("?") != -1) {
  186. // relUrl = relUrl.split("?")[0];
  187. // }
  188. return relUrl;
  189. }
  190. function pizhuInit(id,resDesign) {
  191. // 1、加载displayArea
  192. $('#displayArea')
  193. .load("/service?wdService=getCommonNrDataFile&wdtest=false&id=" + id,
  194. null,
  195. function () {
  196. console.log("pizhuInit");
  197. console.log(resDesign);
  198. //显示音频数据
  199. if(resDesign.privateData&&resDesign.privateData.children){
  200. for(var i=0;i<resDesign.privateData.children.length;i++){
  201. var childData=resDesign.privateData.children[i];
  202. $('#displayArea').append("<div class=\"audio\" data-id=\""+childData.ID+"\"></div>");
  203. }
  204. }
  205. var audios = document.querySelectorAll('.audio');
  206. //初始化样式
  207. $('#displayArea > .audio').addClass('vLine-dashed');
  208. $('#displayArea > .audio').removeClass("mobileIcon-voiceMsg").addClass("icon-voiceMsg");
  209. $('#displayArea > .audio').removeClass("mobileIcon-playVoice").addClass("icon-voiceMsg");
  210. $('#displayArea > .audio').removeClass("icon-playVoice").addClass("icon-voiceMsg");
  211. $('#displayArea > .audio').removeClass("icon-pauseVoice").addClass("icon-voiceMsg");
  212. if (audios.length > 0) {
  213. $(".audio-box").show();
  214. var nowTime = $(".nowTime");
  215. var audioE = $("#audioE");
  216. var drag = false;
  217. var startX = 0;
  218. var startmoveOffsetX = 0;
  219. var moveingOffsetX = 0;
  220. var endmoveOoffsetX = 0;
  221. var doneW = 0;
  222. var doneLeft = 0;
  223. var progressW = 0;
  224. var nowTimeLeft = 0;
  225. var nowTimeW = parseFloat($(".nowTime").width());
  226. console.log("nowTimeW---" + nowTimeW);
  227. var duration = 0;
  228. var doneWP = 0;
  229. var imgItemIndex = 0;
  230. var initAudio = function () {
  231. var url = null;
  232. var id = $('#displayArea > .audio').eq(0).attr('data-id');
  233. /* 获取url */
  234. $.ajax({
  235. type: "POST",
  236. url: "/service?wdService=getCommonNrDataFilePath&wdtest=false",
  237. data: {
  238. 'id': id
  239. },
  240. async: false,
  241. success: function (data) {
  242. url = data;
  243. },
  244. error: function () {
  245. console.info('fail');
  246. }
  247. });
  248. audioE.get(0).src = '/service?wdService=getData&wdtest=false&path=' + url;
  249. }
  250. initAudio();
  251. //设置时间
  252. var setTime = function (timelong, domElement) {
  253. var m = Math.floor(timelong / 60);
  254. if (m < 10) {
  255. m = "0" + m;
  256. }
  257. var s = Math.floor(timelong % 60);
  258. if (s < 10) {
  259. s = "0" + s;
  260. }
  261. var timeStr = m + ":" + s;
  262. $("." + domElement).html(timeStr);
  263. }
  264. //设置nowTime位置
  265. var setnowTimeLeft = function (setClassName, left) {
  266. var audioBoxLeft = $(".audio-box").offset().left;
  267. left = parseFloat(left - audioBoxLeft);
  268. $("." + setClassName).css({
  269. "left": left + "px"
  270. })
  271. }
  272. // $('#displayArea').css("display","block");
  273. // $('#displayArea').css("background","#fff");
  274. // $('#displayArea > .item').css("height","50px");
  275. // $('#displayArea > .item').css("width","50px");
  276. // $('#displayArea > .item').css("margin","0px");
  277. // $('#displayArea > .item').css("border","solid 0px #CCCCCC");
  278. // 2、为音频图标添加事件
  279. $('#displayArea > .audio').on('click', function (event) {
  280. imgItemIndex = $(this).index();
  281. var target = event.target;
  282. /* 变换图片 */
  283. // var playFlag = $(event.target).data('palyFlag');
  284. // console.log("playFlag:"+playFlag);
  285. // 播放器
  286. // var audio = document.getElementById('player');
  287. // if (playFlag == null || '' == playFlag || 'stoping' == playFlag) {
  288. if ($(this).hasClass('icon-voiceMsg')||$(this).hasClass('icon-pauseVoice')) {
  289. var id = $(event.target).attr('data-id');
  290. if (id != audioId) {
  291. audio = null;
  292. }
  293. // $(event.target).data('palyFlag', 'playing');
  294. $('#displayArea > .audio').each(function () {
  295. if ($(this).hasClass('icon-playVoice')||$(this).hasClass('icon-pauseVoice')) {
  296. $(this).addClass('icon-voiceMsg').removeClass('icon-pauseVoice').removeClass('icon-playVoice');
  297. }
  298. });
  299. $(event.target).removeClass('icon-pauseVoice').removeClass("icon-voiceMsg").addClass("icon-playVoice");
  300. // event.target.src = 'nr/image/py/mobileIcon-playVoice.gif';
  301. var url = null;
  302. /* 获取url */
  303. $.ajax({
  304. type: "POST",
  305. url: "/service?wdService=getCommonNrDataFilePath&wdtest=false",
  306. data: {
  307. 'id': id
  308. },
  309. async: false,
  310. success: function (data) {
  311. url = data;
  312. },
  313. error: function () {
  314. console.info('fail');
  315. }
  316. });
  317. if (GetUrlRelativePath(audioE.get(0).src) != '/service?wdService=getData&wdtest=false&path=' + url) {
  318. audioE.get(0).src = '/service?wdService=getData&wdtest=false&path=' + url;
  319. }
  320. if ($(".control-button").hasClass('icon-play')) {
  321. $(".control-button").removeClass('icon-play').addClass('icon-pause');
  322. }
  323. audioE.get(0).play();
  324. // if (audioE == null) {
  325. // audio = document.getElementById('player');
  326. // /* 设置audio的url */
  327. // audio.src = '/service?wdService=getData&wdtest=false&path=' + url;
  328. // audio.onloadedmetadata = function (e) {
  329. // audio.play();
  330. // };
  331. // } else {
  332. // audio.play();
  333. // }
  334. /* 为audio添加结束事件 */
  335. // audioE.on('ended', function (e) {
  336. // // target.src = 'nr/image/py/mobileIcon-voiceMsg.png';
  337. // audio = null;
  338. // audioId = null;
  339. // });
  340. } else {
  341. if ($(".control-button").hasClass('icon-pause')) {
  342. $(".control-button").removeClass('icon-pause').addClass('icon-play');
  343. }
  344. //event.target.src = 'nr/image/py/mobileIcon-voiceMsg.png';
  345. $(event.target).removeClass("icon-playVoice").removeClass('icon-voiceMsg').addClass("icon-pauseVoice");
  346. audioE.get(0).pause();
  347. // $(target).data('palyFlag', 'stoping');
  348. audioId = $(target).attr('data-id');
  349. }
  350. // event.cancelBubble = true;
  351. // if (event.stopPropagation) {
  352. // event.stopPropagation();
  353. // }
  354. });
  355. //播放暂停功能
  356. $(".control-button").on("click", function () {
  357. if ($(".control-button").hasClass('icon-play')) {
  358. $(".control-button").removeClass('icon-play').addClass('icon-pause');
  359. audioE.get(0).play();
  360. $('#displayArea > .audio').eq(imgItemIndex).removeClass('icon-pauseVoice').removeClass('icon-voiceMsg').addClass('icon-playVoice');
  361. } else {
  362. $(".control-button").removeClass('icon-pause').addClass('icon-play');
  363. audioE.get(0).pause();
  364. $('#displayArea > .audio').each(function () {
  365. if ($(this).hasClass('icon-playVoice')) {
  366. $(this).addClass('icon-pauseVoice').removeClass('icon-voiceMsg').removeClass('icon-playVoice');
  367. }
  368. });
  369. }
  370. })
  371. //元数据加载完,设置总时长和当前播放时间
  372. audioE.on("loadedmetadata", function () {
  373. duration = audioE.get(0).duration
  374. setTime(duration, "time");
  375. setTime(audioE.get(0).currentTime, "nowTime");
  376. doneW = parseFloat($(".progress-done").width());
  377. doneLeft = parseFloat($(".progress-done").offset().left);
  378. nowTimeLeft = parseFloat(doneW + doneLeft - nowTimeW / 2);
  379. setnowTimeLeft("nowTime", nowTimeLeft);
  380. })
  381. //当前播放时间改变时
  382. audioE.on("timeupdate", function () {
  383. if (!drag) {
  384. var currentTime = audioE.get(0).currentTime;
  385. var percent = parseFloat(currentTime / duration) * 100;
  386. $(".progress-done").css({
  387. "width": percent + "%"
  388. });
  389. setTime(audioE.get(0).currentTime, "nowTime");
  390. doneW = parseFloat($(".progress-done").width());
  391. nowTimeLeft = parseFloat(doneW + doneLeft - nowTimeW / 2);
  392. setnowTimeLeft("nowTime", nowTimeLeft);
  393. }
  394. })
  395. audioE.on("ended", function () {
  396. audioE.get(0).currentTime = 0;
  397. $('#displayArea > .audio').each(function () {
  398. if ($(this).hasClass('icon-playVoice')) {
  399. // $(this).data('palyFlag', 'stoping');
  400. $(this).addClass('icon-voiceMsg').removeClass('icon-pauseVoice').removeClass('icon-playVoice');
  401. }
  402. });
  403. $(".control-button").removeClass('icon-pause').addClass('icon-play');
  404. })
  405. //鼠标按下nowTime时
  406. $(".nowTime").on("mousedown", function (e) {
  407. startmoveOffsetX = e.pageX;
  408. drag = true;
  409. doneW = parseFloat($(".progress-done").width());
  410. progressW = parseFloat($(".audioprogress-div").width());
  411. doneW = parseFloat($(".progress-done").width());
  412. })
  413. //鼠标在页面滑动时
  414. $("html").on("mousemove", function (e) {
  415. if (drag) {
  416. moveingOffsetX = e.pageX - startmoveOffsetX;
  417. if (parseFloat(doneW + moveingOffsetX) > progressW) {
  418. $(".progress-done").css({
  419. "width": "100%"
  420. });
  421. doneW = parseFloat($(".progress-done").width());
  422. nowTimeLeft = parseFloat(doneW + doneLeft - nowTimeW / 2);
  423. setnowTimeLeft("nowTime", nowTimeLeft);
  424. } else if (parseFloat(doneW + moveingOffsetX) < 0) {
  425. $(".progress-done").css({
  426. "width": "0%"
  427. });
  428. nowTimeLeft = parseFloat(doneW + doneLeft - nowTimeW / 2);
  429. setnowTimeLeft("nowTime", nowTimeLeft);
  430. } else {
  431. doneWP = parseFloat(parseFloat(doneW + moveingOffsetX) / progressW) * 100;
  432. $(".progress-done").css({
  433. "width": doneWP + "%"
  434. });
  435. var doneNW = parseFloat($(".progress-done").width());
  436. nowTimeLeft = parseFloat(doneNW + doneLeft - nowTimeW / 2);
  437. setnowTimeLeft("nowTime", nowTimeLeft);
  438. }
  439. }
  440. });
  441. //松开鼠标时
  442. $("html").on("mouseup", function (e) {
  443. if (drag) {
  444. audioE.get(0).currentTime = parseFloat(doneWP / 100 * duration);
  445. }
  446. drag = false;
  447. })
  448. }
  449. });
  450. $('#ssEditor').load("/service?wdService=getCommonNrDataFile&wdtest=false&id=" + id,
  451. null,
  452. function () {
  453. var audios = document.querySelectorAll('.audio');
  454. // $('#ssEditor').css("display","block");
  455. // $('#ssEditor').css("background","#fff");
  456. $('#ssEditor > .item').css("height", "50px");
  457. $('#ssEditor > .item').css("width", "50px");
  458. $('#ssEditor > .item').css("margin", "0px");
  459. $('#ssEditor > .item').css("border", "solid 0px #CCCCCC");
  460. $("#ss").click();
  461. // 2、为音频图标添加事件
  462. $('#ssEditor > .audio')
  463. .on(
  464. 'click',
  465. function (event) {
  466. var target = event.target;
  467. /* 变换图片 */
  468. var playFlag = $(event.target)
  469. .data('palyFlag');
  470. // 播放器
  471. var audio = document
  472. .getElementById('player');
  473. if (playFlag == null ||
  474. '' == playFlag ||
  475. 'stoping' == playFlag) {
  476. var id = $(event.target).attr(
  477. 'data-id');
  478. if (id != audioId) {
  479. audio = null;
  480. }
  481. $(event.target).data(
  482. 'palyFlag', 'playing');
  483. event.target.src = 'nr/image/py/mobileIcon-playVoice.gif';
  484. var url = null;
  485. /* 获取url */
  486. $.ajax({
  487. type: "POST",
  488. url: "/service?wdService=getCommonNrDataFilePath&wdtest=false",
  489. data: {
  490. 'id': id
  491. },
  492. async: false,
  493. success: function (
  494. data) {
  495. url = data;
  496. },
  497. error: function () {
  498. console
  499. .info('fail');
  500. }
  501. });
  502. if (audio == null) {
  503. audio = document
  504. .getElementById('player');
  505. /* 设置audio的url */
  506. audio.src = '/service?wdService=getData&wdtest=false&path=' +
  507. url;
  508. audio.onloadedmetadata = function (
  509. e) {
  510. audio.play();
  511. };
  512. } else {
  513. audio.play();
  514. }
  515. /* 为audio添加结束事件 */
  516. $(audio).on('ended', function (e) {
  517. $(target)
  518. .data(
  519. 'palyFlag',
  520. 'stoping');
  521. target.src = 'nr/image/py/mobileIcon-voiceMsg.png';
  522. audio = null;
  523. audioId = null;
  524. });
  525. } else {
  526. event.target.src = 'nr/image/py/mobileIcon-voiceMsg.png';
  527. audio.pause();
  528. $(target).data('palyFlag',
  529. 'stoping');
  530. audioId = $(target).attr(
  531. 'data-id');
  532. }
  533. event.cancelBubble = true;
  534. if (event.stopPropagation) {
  535. event.stopPropagation();
  536. }
  537. });
  538. });
  539. }
  540. /*打开录音页面*/
  541. function openRecordWindow(id) {
  542. var url = window.location.protocol + '//' + window.location.host + '/main/editor/record.jsp' + '?id=' + id;
  543. window.open(url, "_blank", "toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes, width=400, height=400");
  544. }
  545. function openWriteWindow(id) {
  546. var url = window.location.protocol + '//' + window.location.host + '/main/editor/handwritten.jsp' + '?id=' + id;
  547. window.open(url, "_blank", "toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes, width=700, height=600");
  548. }