📄 正在查看:twcms/plugin/editor_um/umeditor/dialogs/video/video.js
大小:10,884 字节 · 修改:2013-11-25 19:20:04 · 行数:275
1
2(function(){
3 var domUtils = UM.dom.domUtils;
4 var widgetName = 'insertvideo';
5
6 UM.registerWidget( widgetName,{
7
8 tpl: "<link rel=\"stylesheet\" type=\"text/css\" href=\"<%=video_url%>video.css\" />" +
9 "<div class=\"edui-video-wrapper\">" +
10 "<div id=\"eduiVideoTab\">" +
11 "<div id=\"eduiVideoTabHeads\" class=\"edui-video-tabhead\">" +
12 "<span tabSrc=\"video\" class=\"edui-video-focus\"><%=lang_tab_insertV%></span>" +
13 "</div>" +
14 "<div id=\"eduiVideoTabBodys\" class=\"edui-video-tabbody\">" +
15 "<div id=\"eduiVideoPanel\" class=\"edui-video-panel\">" +
16 "<table><tr><td><label for=\"eduiVideoUrl\" class=\"edui-video-url\"><%=lang_video_url%></label></td><td><input id=\"eduiVideoUrl\" type=\"text\"></td></tr></table>" +
17 "<div id=\"eduiVideoPreview\"></div>" +
18 "<div id=\"eduiVideoInfo\">" +
19 "<fieldset>" +
20 "<legend><%=lang_video_size%></legend>" +
21 "<table>" +
22 "<tr><td><label for=\"eduiVideoWidth\"><%=lang_videoW%></label></td><td><input class=\"edui-video-txt\" id=\"eduiVideoWidth\" type=\"text\"/></td></tr>" +
23 "<tr><td><label for=\"eduiVideoHeight\"><%=lang_videoH%></label></td><td><input class=\"edui-video-txt\" id=\"eduiVideoHeight\" type=\"text\"/></td></tr>" +
24 "</table>" +
25 "</fieldset>" +
26 "<fieldset>" +
27 "<legend><%=lang_alignment%></legend>" +
28 "<div id=\"eduiVideoFloat\"></div>" +
29 "</fieldset>" +
30 "</div>" +
31 "</div>" +
32 "</div>" +
33 "</div>" +
34 "</div>",
35 initContent:function( editor, $widget ){
36
37 var me = this,
38 lang = editor.getLang( widgetName),
39 video_url = UMEDITOR_CONFIG.UMEDITOR_HOME_URL + 'dialogs/video/';
40
41 me.lang = lang;
42 me.editor = editor;
43 me.root().html( $.parseTmpl( me.tpl, $.extend( { video_url: video_url }, lang['static'] ) ) );
44
45 me.initController( lang );
46
47 },
48 initEvent:function(){
49
50 var me = this,
51 url = $("#eduiVideoUrl")[0];
52
53 if( 'oninput' in url ) {
54 url.oninput = function(){
55 me.createPreviewVideo( this.value );
56 };
57 } else {
58 url.onpropertychange = function () {
59 me.createPreviewVideo( this.value );
60 }
61 }
62
63 },
64 initController: function( lang ){
65
66 var me = this,
67 img = me.editor.selection.getRange().getClosedNode(),
68 url;
69
70 me.createAlignButton( ["eduiVideoFloat"] );
71
72 //编辑视频时初始化相关信息
73 if(img && img.className == "edui-faked-video"){
74 $("#eduiVideoUrl")[0].value = url = img.getAttribute("_url");
75 $("#eduiVideoWidth")[0].value = img.width;
76 $("#eduiVideoHeight")[0].value = img.height;
77 var align = domUtils.getComputedStyle(img,"float"),
78 parentAlign = domUtils.getComputedStyle(img.parentNode,"text-align");
79 me.updateAlignButton(parentAlign==="center"?"center":align);
80 }
81 me.createPreviewVideo(url);
82
83 },
84 /**
85 * 根据url生成视频预览
86 */
87 createPreviewVideo: function(url){
88
89 if ( !url )return;
90 var matches = url.match(/youtu.be\/(\w+)$/) || url.match(/youtube\.com\/watch\?v=(\w+)/) || url.match(/youtube.com\/v\/(\w+)/),
91 youku = url.match(/youku\.com\/v_show\/id_(\w+)/),
92 youkuPlay = /player\.youku\.com/ig.test(url),
93 me = this,
94 lang = me.lang;
95
96 if(!youkuPlay){
97 if (matches){
98 url = "https://www.youtube.com/v/" + matches[1] + "?version=3&feature=player_embedded";
99 }else if(youku){
100 url = "http://player.youku.com/player.php/sid/"+youku[1]+"/v.swf"
101 }else if(!me.endWith(url,[".swf",".flv",".wmv"])){
102 $("#eduiVideoPreview").html( lang.urlError );
103 return;
104 }
105 }else{
106 url = url.replace(/\?f=.*/,"");
107 }
108 $("#eduiVideoPreview")[0].innerHTML = '<embed type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
109 ' src="' + url + '"' +
110 ' width="' + 420 + '"' +
111 ' height="' + 280 + '"' +
112 ' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" ></embed>';
113
114 },
115 /**
116 * 将单个视频信息插入编辑器中
117 */
118 insertSingle: function(){
119
120 var me = this,
121 width = $("#eduiVideoWidth")[0],
122 height = $("#eduiVideoHeight")[0],
123 url=$('#eduiVideoUrl')[0].value,
124 align = this.findFocus("eduiVideoFloat","name");
125
126 if(!url) return false;
127 if ( !me.checkNum( [width, height] ) ) return false;
128 this.editor.execCommand('insertvideo', {
129 url: me.convert_url(url),
130 width: width.value,
131 height: height.value,
132 align: align
133 });
134
135 },
136 /**
137 * URL转换
138 */
139 convert_url: function(s){
140 return s.replace(/http:\/\/www\.tudou\.com\/programs\/view\/([\w\-]+)\/?/i,"http://www.tudou.com/v/$1")
141 .replace(/http:\/\/www\.youtube\.com\/watch\?v=([\w\-]+)/i,"http://www.youtube.com/v/$1")
142 .replace(/http:\/\/v\.youku\.com\/v_show\/id_([\w\-=]+)\.html/i,"http://player.youku.com/player.php/sid/$1")
143 .replace(/http:\/\/www\.56\.com\/u\d+\/v_([\w\-]+)\.html/i, "http://player.56.com/v_$1.swf")
144 .replace(/http:\/\/www.56.com\/w\d+\/play_album\-aid\-\d+_vid\-([^.]+)\.html/i, "http://player.56.com/v_$1.swf")
145 .replace(/http:\/\/v\.ku6\.com\/.+\/([^.]+)\.html/i, "http://player.ku6.com/refer/$1/v.swf");
146 },
147 /**
148 * 检测传入的所有input框中输入的长宽是否是正数
149 */
150 checkNum: function checkNum( nodes ) {
151
152 var me = this;
153
154 for ( var i = 0, ci; ci = nodes[i++]; ) {
155 var value = ci.value;
156 if ( !me.isNumber( value ) && value) {
157 alert( me.lang.numError );
158 ci.value = "";
159 ci.focus();
160 return false;
161 }
162 }
163 return true;
164 },
165 /**
166 * 数字判断
167 * @param value
168 */
169 isNumber: function( value ) {
170 return /(0|^[1-9]\d*$)/.test( value );
171 },
172 updateAlignButton: function( align ) {
173 var aligns = $( "#eduiVideoFloat" )[0].children;
174
175 for ( var i = 0, ci; ci = aligns[i++]; ) {
176 if ( ci.getAttribute( "name" ) == align ) {
177 if ( ci.className !="edui-video-focus" ) {
178 ci.className = "edui-video-focus";
179 }
180 } else {
181 if ( ci.className =="edui-video-focus" ) {
182 ci.className = "";
183 }
184 }
185 }
186
187 },
188 /**
189 * 创建图片浮动选择按钮
190 * @param ids
191 */
192 createAlignButton: function( ids ) {
193
194 var lang = this.lang,
195 vidoe_home = UMEDITOR_CONFIG.UMEDITOR_HOME_URL + 'dialogs/video/';
196
197 for ( var i = 0, ci; ci = ids[i++]; ) {
198 var floatContainer = $( "#" + ci ) [0],
199 nameMaps = {"none":lang['default'], "left":lang.floatLeft, "right":lang.floatRight};
200 for ( var j in nameMaps ) {
201 var div = document.createElement( "div" );
202 div.setAttribute( "name", j );
203 if ( j == "none" ) div.className="edui-video-focus";
204 div.style.cssText = "background:url("+ vidoe_home +"images/" + j + "_focus.jpg);";
205 div.setAttribute( "title", nameMaps[j] );
206 floatContainer.appendChild( div );
207 }
208 this.switchSelect( ci );
209 }
210 },
211 /**
212 * 选择切换
213 */
214 switchSelect: function( selectParentId ) {
215 var selects = $( "#" + selectParentId )[0].children;
216 for ( var i = 0, ci; ci = selects[i++]; ) {
217 domUtils.on( ci, "click", function () {
218 for ( var j = 0, cj; cj = selects[j++]; ) {
219 cj.className = "";
220 cj.removeAttribute && cj.removeAttribute( "class" );
221 }
222 this.className = "edui-video-focus";
223 } )
224 }
225 },
226 /**
227 * 找到id下具有focus类的节点并返回该节点下的某个属性
228 * @param id
229 * @param returnProperty
230 */
231 findFocus: function( id, returnProperty ) {
232 var tabs = $( "#" + id )[0].children,
233 property;
234 for ( var i = 0, ci; ci = tabs[i++]; ) {
235 if ( ci.className=="edui-video-focus" ) {
236 property = ci.getAttribute( returnProperty );
237 break;
238 }
239 }
240 return property;
241 },
242 /**
243 * 末尾字符检测
244 */
245 endWith: function(str,endStrArr){
246 for(var i=0,len = endStrArr.length;i<len;i++){
247 var tmp = endStrArr[i];
248 if(str.length - tmp.length<0) return false;
249
250 if(str.substring(str.length-tmp.length)==tmp){
251 return true;
252 }
253 }
254 return false;
255 },
256 width:610,
257 height:498,
258 buttons: {
259 ok: {
260 exec: function( editor ){
261 $("#eduiVideoPreview").html("");
262 editor.getWidgetData(widgetName).insertSingle();
263 }
264 },
265 cancel: {
266 exec: function(){
267 //清除视频
268 $("#eduiVideoPreview").html("");
269 }
270 }
271 }
272 });
273
274})();
275