video.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. (function () {
  2. var domUtils = UM.dom.domUtils;
  3. var widgetName = 'video';
  4. UM.registerWidget(widgetName, {
  5. tpl: "<link rel=\"stylesheet\" type=\"text/css\" href=\"<%=video_url%>video.css\" />" +
  6. "<div class=\"edui-video-wrapper\">" +
  7. "<div id=\"eduiVideoTab\">" +
  8. "<div id=\"eduiVideoTabBodys\" class=\"edui-video-tabbody\">" +
  9. "<div id=\"eduiVideoPanel\" class=\"edui-video-panel\">" +
  10. "<table id='edui-video-input'><tr><td><label for=\"eduiVideoUrl\" class=\"edui-video-url\"><%=lang_video_url%></label></td><td><input id=\"eduiVideoUrl\" type=\"text\"></td><td><div class='video-btn-container'><input type='file' name='videofile' accept='video/mp4, video/webm, application/octet-stream, .mp4, .webm, .flv' id='uploadvideo'><p class='edui-btn edui-btn-primary'>上传视频</p></div></td><td><a href='javascript:' id='choosevideo' class='edui-btn edui-btn-primary'>选择视频</a></td></tr></table>" +
  11. "<div id=\"eduiVideoPreview\"></div>" +
  12. "<div id=\"eduiVideoInfo\">" +
  13. "<fieldset>" +
  14. "<legend style='font-size:14px;width:auto;border-bottom:none;padding:0 5px;margin-bottom:5px;'><%=lang_video_size%></legend>" +
  15. "<table>" +
  16. "<tr><td><label for=\"eduiVideoWidth\"><%=lang_videoW%></label></td><td><input class=\"edui-video-txt\" id=\"eduiVideoWidth\" type=\"text\"/></td></tr>" +
  17. "<tr><td><label for=\"eduiVideoHeight\"><%=lang_videoH%></label></td><td><input class=\"edui-video-txt\" id=\"eduiVideoHeight\" type=\"text\"/></td></tr>" +
  18. "</table>" +
  19. "</fieldset>" +
  20. "<fieldset>" +
  21. "<legend style='font-size:14px;width:auto;border-bottom:none;padding:0 5px;margin-bottom:5px;'><%=lang_alignment%></legend>" +
  22. "<div id=\"eduiVideoFloat\"></div>" +
  23. "</fieldset>" +
  24. "</div>" +
  25. "</div>" +
  26. "</div>" +
  27. "</div>" +
  28. "</div>",
  29. initContent: function (editor, $widget) {
  30. var me = this,
  31. lang = editor.getLang(widgetName),
  32. video_url = UMEDITOR_CONFIG.UMEDITOR_HOME_URL + 'dialogs/video/';
  33. me.lang = lang;
  34. me.editor = editor;
  35. me.$widget = $widget;
  36. me.root().html($.parseTmpl(me.tpl, $.extend({video_url: video_url}, lang['static'])));
  37. me.initController(lang);
  38. },
  39. initEvent: function (editor, $w) {
  40. var me = this,
  41. url = $("#eduiVideoUrl", me.$widget)[0];
  42. if ('oninput' in url) {
  43. url.oninput = function () {
  44. me.createPreviewVideo(this.value);
  45. };
  46. } else {
  47. url.onpropertychange = function () {
  48. me.createPreviewVideo(this.value);
  49. }
  50. }
  51. me.editor = editor;
  52. me.dialog = $w;
  53. $(me.dialog).delegate("#choosevideo", "click", function (e) {
  54. var selectUrl = typeof Config !== 'undefined' && Config.modulename === 'index' ? 'user/attachment' : 'general/attachment/select';
  55. parent.Fast.api.open(selectUrl + "?element_id=&multiple=false&mimetype=video/*", "选择", {
  56. callback: function (data) {
  57. var fullurl = Fast.api.cdnurl(data.url, true);
  58. $("#eduiVideoUrl").val(fullurl);
  59. me.createPreviewVideo(fullurl);
  60. }
  61. });
  62. return false;
  63. });
  64. $(me.dialog).delegate("#uploadvideo", "change", function (e) {
  65. var files = $(this).prop('files');
  66. if (files.length > 0) {
  67. var uploadCallback = me.editor.getOpt('imageUploadCallback');
  68. uploadCallback.call(me, files[0], function (url, data) {
  69. $("#eduiVideoUrl").val(data.fullurl);
  70. me.createPreviewVideo(data.fullurl);
  71. });
  72. }
  73. return;
  74. });
  75. },
  76. initController: function (lang) {
  77. var me = this,
  78. img = me.editor.selection.getRange().getClosedNode(),
  79. url;
  80. me.createAlignButton(["eduiVideoFloat"]);
  81. //编辑视频时初始化相关信息
  82. if (img && img.className == "edui-faked-video") {
  83. $("#eduiVideoUrl", me.$widget)[0].value = url = img.getAttribute("_url");
  84. $("#eduiVideoWidth", me.$widget)[0].value = img.width;
  85. $("#eduiVideoHeight", me.$widget)[0].value = img.height;
  86. var align = domUtils.getComputedStyle(img, "float"),
  87. parentAlign = domUtils.getComputedStyle(img.parentNode, "text-align");
  88. me.updateAlignButton(parentAlign === "center" ? "center" : align);
  89. }
  90. me.createPreviewVideo(url);
  91. },
  92. /**
  93. * 根据url生成视频预览
  94. */
  95. createPreviewVideo: function (url) {
  96. if (!url) return;
  97. var me = this,
  98. lang = me.lang,
  99. conUrl = me.convert_url(url);
  100. $("#eduiVideoPreview", me.$widget)[0].innerHTML = '<video ' +
  101. ' src="' + url + '"' +
  102. ' width="' + 420 + '"' +
  103. ' height="' + 280 + '"' +
  104. ' controls autoplay preload="auto"></video><br>';
  105. },
  106. /**
  107. * 将单个视频信息插入编辑器中
  108. */
  109. insertSingle: function () {
  110. var me = this,
  111. width = $("#eduiVideoWidth", me.$widget)[0],
  112. height = $("#eduiVideoHeight", me.$widget)[0],
  113. url = $('#eduiVideoUrl', me.$widget)[0].value,
  114. align = this.findFocus("eduiVideoFloat", "name");
  115. if (!url) return false;
  116. if (!me.checkNum([width, height])) return false;
  117. this.editor.execCommand('insertvideo', {
  118. url: me.convert_url(url),
  119. width: width.value,
  120. height: height.value,
  121. align: align
  122. });
  123. },
  124. /**
  125. * URL转换
  126. */
  127. convert_url: function (url) {
  128. if (!url) return '';
  129. return url;
  130. },
  131. /**
  132. * 检测传入的所有input框中输入的长宽是否是正数
  133. */
  134. checkNum: function checkNum(nodes) {
  135. var me = this;
  136. for (var i = 0, ci; ci = nodes[i++];) {
  137. var value = ci.value;
  138. if (!me.isNumber(value) && value) {
  139. alert(me.lang.numError);
  140. ci.value = "";
  141. ci.focus();
  142. return false;
  143. }
  144. }
  145. return true;
  146. },
  147. /**
  148. * 数字判断
  149. * @param value
  150. */
  151. isNumber: function (value) {
  152. return /(0|^[1-9]\d*$)/.test(value);
  153. },
  154. updateAlignButton: function (align) {
  155. var aligns = $("#eduiVideoFloat", this.$widget)[0].children;
  156. for (var i = 0, ci; ci = aligns[i++];) {
  157. if (ci.getAttribute("name") == align) {
  158. if (ci.className != "edui-video-focus") {
  159. ci.className = "edui-video-focus";
  160. }
  161. } else {
  162. if (ci.className == "edui-video-focus") {
  163. ci.className = "";
  164. }
  165. }
  166. }
  167. },
  168. /**
  169. * 创建图片浮动选择按钮
  170. * @param ids
  171. */
  172. createAlignButton: function (ids) {
  173. var lang = this.lang,
  174. vidoe_home = UMEDITOR_CONFIG.UMEDITOR_HOME_URL + 'dialogs/video/';
  175. for (var i = 0, ci; ci = ids[i++];) {
  176. var floatContainer = $("#" + ci, this.$widget) [0],
  177. nameMaps = {"none": lang['default'], "left": lang.floatLeft, "right": lang.floatRight};
  178. for (var j in nameMaps) {
  179. var div = document.createElement("div");
  180. div.setAttribute("name", j);
  181. if (j == "none") div.className = "edui-video-focus";
  182. div.style.cssText = "background:url(" + vidoe_home + "images/" + j + "_focus.jpg);";
  183. div.setAttribute("title", nameMaps[j]);
  184. floatContainer.appendChild(div);
  185. }
  186. this.switchSelect(ci);
  187. }
  188. },
  189. /**
  190. * 选择切换
  191. */
  192. switchSelect: function (selectParentId) {
  193. var selects = $("#" + selectParentId, this.$widget)[0].children;
  194. for (var i = 0, ci; ci = selects[i++];) {
  195. $(ci).on("click", function () {
  196. for (var j = 0, cj; cj = selects[j++];) {
  197. cj.className = "";
  198. cj.removeAttribute && cj.removeAttribute("class");
  199. }
  200. this.className = "edui-video-focus";
  201. })
  202. }
  203. },
  204. /**
  205. * 找到id下具有focus类的节点并返回该节点下的某个属性
  206. * @param id
  207. * @param returnProperty
  208. */
  209. findFocus: function (id, returnProperty) {
  210. var tabs = $("#" + id, this.$widget)[0].children,
  211. property;
  212. for (var i = 0, ci; ci = tabs[i++];) {
  213. if (ci.className == "edui-video-focus") {
  214. property = ci.getAttribute(returnProperty);
  215. break;
  216. }
  217. }
  218. return property;
  219. },
  220. /**
  221. * 末尾字符检测
  222. */
  223. endWith: function (str, endStrArr) {
  224. for (var i = 0, len = endStrArr.length; i < len; i++) {
  225. var tmp = endStrArr[i];
  226. if (str.length - tmp.length < 0) return false;
  227. if (str.substring(str.length - tmp.length) == tmp) {
  228. return true;
  229. }
  230. }
  231. return false;
  232. },
  233. width: 610,
  234. height: 358,
  235. buttons: {
  236. ok: {
  237. exec: function (editor, $w) {
  238. $("#eduiVideoPreview", $w).html("");
  239. editor.getWidgetData(widgetName).insertSingle();
  240. }
  241. },
  242. cancel: {
  243. exec: function () {
  244. //清除视频
  245. $("#eduiVideoPreview").html("");
  246. }
  247. }
  248. }
  249. });
  250. })();