|
1 |
|
2 (function() { |
|
3 tinymce.create('tinymce.plugins.wpGallery', { |
|
4 |
|
5 init : function(ed, url) { |
|
6 var t = this; |
|
7 |
|
8 t.url = url; |
|
9 t._createButtons(); |
|
10 |
|
11 // Register the command so that it can be invoked by using tinyMCE.activeEditor.execCommand('...'); |
|
12 ed.addCommand('WP_Gallery', function() { |
|
13 var el = ed.selection.getNode(), post_id, vp = tinymce.DOM.getViewPort(), |
|
14 H = vp.h - 80, W = ( 640 < vp.w ) ? 640 : vp.w; |
|
15 |
|
16 if ( el.nodeName != 'IMG' ) return; |
|
17 if ( ed.dom.getAttrib(el, 'class').indexOf('wpGallery') == -1 ) return; |
|
18 |
|
19 post_id = tinymce.DOM.get('post_ID').value; |
|
20 tb_show('', tinymce.documentBaseURL + '/media-upload.php?post_id='+post_id+'&tab=gallery&TB_iframe=true&width='+W+'&height='+H); |
|
21 |
|
22 tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' ); |
|
23 }); |
|
24 |
|
25 ed.onMouseDown.add(function(ed, e) { |
|
26 if ( e.target.nodeName == 'IMG' && ed.dom.hasClass(e.target, 'wpGallery') ) |
|
27 ed.plugins.wordpress._showButtons(e.target, 'wp_gallerybtns'); |
|
28 }); |
|
29 |
|
30 ed.onBeforeSetContent.add(function(ed, o) { |
|
31 o.content = t._do_gallery(o.content); |
|
32 }); |
|
33 |
|
34 ed.onPostProcess.add(function(ed, o) { |
|
35 if (o.get) |
|
36 o.content = t._get_gallery(o.content); |
|
37 }); |
|
38 }, |
|
39 |
|
40 _do_gallery : function(co) { |
|
41 return co.replace(/\[gallery([^\]]*)\]/g, function(a,b){ |
|
42 return '<img src="'+tinymce.baseURL+'/plugins/wpgallery/img/t.gif" class="wpGallery mceItem" title="gallery'+tinymce.DOM.encode(b)+'" />'; |
|
43 }); |
|
44 }, |
|
45 |
|
46 _get_gallery : function(co) { |
|
47 |
|
48 function getAttr(s, n) { |
|
49 n = new RegExp(n + '=\"([^\"]+)\"', 'g').exec(s); |
|
50 return n ? tinymce.DOM.decode(n[1]) : ''; |
|
51 }; |
|
52 |
|
53 return co.replace(/(?:<p[^>]*>)*(<img[^>]+>)(?:<\/p>)*/g, function(a,im) { |
|
54 var cls = getAttr(im, 'class'); |
|
55 |
|
56 if ( cls.indexOf('wpGallery') != -1 ) |
|
57 return '<p>['+tinymce.trim(getAttr(im, 'title'))+']</p>'; |
|
58 |
|
59 return a; |
|
60 }); |
|
61 }, |
|
62 |
|
63 _createButtons : function() { |
|
64 var t = this, ed = tinyMCE.activeEditor, DOM = tinymce.DOM, editButton, dellButton; |
|
65 |
|
66 DOM.remove('wp_gallerybtns'); |
|
67 |
|
68 DOM.add(document.body, 'div', { |
|
69 id : 'wp_gallerybtns', |
|
70 style : 'display:none;' |
|
71 }); |
|
72 |
|
73 editButton = DOM.add('wp_gallerybtns', 'img', { |
|
74 src : t.url+'/img/edit.png', |
|
75 id : 'wp_editgallery', |
|
76 width : '24', |
|
77 height : '24', |
|
78 title : ed.getLang('wordpress.editgallery') |
|
79 }); |
|
80 |
|
81 tinymce.dom.Event.add(editButton, 'mousedown', function(e) { |
|
82 var ed = tinyMCE.activeEditor; |
|
83 ed.windowManager.bookmark = ed.selection.getBookmark('simple'); |
|
84 ed.execCommand("WP_Gallery"); |
|
85 }); |
|
86 |
|
87 dellButton = DOM.add('wp_gallerybtns', 'img', { |
|
88 src : t.url+'/img/delete.png', |
|
89 id : 'wp_delgallery', |
|
90 width : '24', |
|
91 height : '24', |
|
92 title : ed.getLang('wordpress.delgallery') |
|
93 }); |
|
94 |
|
95 tinymce.dom.Event.add(dellButton, 'mousedown', function(e) { |
|
96 var ed = tinyMCE.activeEditor, el = ed.selection.getNode(); |
|
97 |
|
98 if ( el.nodeName == 'IMG' && ed.dom.hasClass(el, 'wpGallery') ) { |
|
99 ed.dom.remove(el); |
|
100 |
|
101 ed.execCommand('mceRepaint'); |
|
102 return false; |
|
103 } |
|
104 }); |
|
105 }, |
|
106 |
|
107 getInfo : function() { |
|
108 return { |
|
109 longname : 'Gallery Settings', |
|
110 author : 'WordPress', |
|
111 authorurl : 'http://wordpress.org', |
|
112 infourl : '', |
|
113 version : "1.0" |
|
114 }; |
|
115 } |
|
116 }); |
|
117 |
|
118 tinymce.PluginManager.add('wpgallery', tinymce.plugins.wpGallery); |
|
119 })(); |