forked from inc2734/smart-custom-fields
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.js
208 lines (189 loc) · 6.1 KB
/
editor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* editor.js
* Version : 2.0.0
* Author : inc2734
* Created : September 23, 2014
* Modified : July 11, 2018
* License : GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
jQuery( function( $ ) {
$( '.smart-cf-meta-box' ).each( function( i, e ) {
var wrapper = $( e );
var btn_add_repeat_group = wrapper.find( '.btn-add-repeat-group' );
var btn_remove_repeat_group = wrapper.find( '.btn-remove-repeat-group' );
var table_class = '.smart-cf-meta-box-table';
var cnt = wrapper.find( table_class ).length;
/**
* グループ追加ボタン
*/
btn_add_repeat_group.click( function( e ) {
cnt ++;
var parent = $( this ).parents( '.smart-cf-meta-box-repeat-tables' );
add_repeat_group( $( this ) );
} );
/**
* グループ削除ボタン
*/
btn_remove_repeat_group.click( function() {
var table = $( this ).parents( table_class );
table.fadeOut( 'fast', function() {
$( this ).remove();
} );
var tables = $( this ).parents( '.smart-cf-meta-box-repeat-tables' );
if ( tables.find( table_class ).length === 2 ) {
cnt ++;
add_repeat_group( $( this ) );
}
} );
function add_repeat_group( button ) {
var tables = button.parents( '.smart-cf-meta-box-repeat-tables' );
var table = tables.find( table_class ).first();
var clone = table.clone( true, true ).hide();
clone.find( 'input, select, textarea' ).each( function( i, e ) {
var name = $( this ).attr( 'name' );
if ( name ) {
$( this ).attr( 'name',
name.replace(
/^(smart-custom-fields\[.+?\])\[\]/,
'$1[' + cnt + ']'
)
);
$( this ).removeAttr( 'disabled' );
}
} );
clone.find( '.smart-cf-datetime_picker' ).addClass( 'add' );
button.parent().after( clone.fadeIn( 'fast' ) );
button.trigger( 'smart-cf-after-add-group', { button: button, clone: clone} );
}
/**
* 画像アップローダー
*/
wrapper.find( '.btn-add-image' ).click( function( e ) {
e.preventDefault();
var custom_uploader_image;
var upload_button = $( this );
if ( custom_uploader_image ) {
custom_uploader_image.open();
return;
}
wp.media.view.Modal.prototype.on( 'ready', function(){
$( 'select.attachment-filters' )
.find( '[value="uploaded"]' )
.attr( 'selected', true )
.parent()
.trigger( 'change' );
} );
custom_uploader_image = wp.media( {
button : {
text: smart_cf_uploader.image_uploader_title
},
states: [
new wp.media.controller.Library({
title : smart_cf_uploader.image_uploader_title,
library : wp.media.query( { type: 'image' } ),
multiple : false,
filterable: 'uploaded'
})
]
} );
custom_uploader_image.on( 'select', function() {
var images = custom_uploader_image.state().get( 'selection' );
images.each( function( file ){
var sizes = file.get('sizes');
var image_area = upload_button.parent().find( '.smart-cf-upload-image' );
var sizename = image_area.data('size');
var img = sizes[ sizename ] || sizes.full;
var alt_attr = file.get('title');
image_area.find( 'img' ).remove();
image_area.prepend(
'<img src="' + img.url + '" alt="' + alt_attr + '" />'
);
image_area.removeClass( 'hide' );
upload_button.parent().find( 'input[type="hidden"]' ).val( file.toJSON().id );
} );
} );
custom_uploader_image.open();
} );
/**
* 画像削除ボタン
*/
wrapper.find( '.smart-cf-upload-image' ).hover( function() {
$( this ).find( '.btn-remove-image' ).fadeIn( 'fast', function() {
$( this ).removeClass( 'hide' );
} );
}, function() {
$( this ).find( '.btn-remove-image' ).fadeOut( 'fast', function() {
$( this ).addClass( 'hide' );
} );
} );
wrapper.find( '.btn-remove-image' ).click( function() {
$( this ).parent().find( 'img' ).remove();
$( this ).parent().siblings( 'input[type="hidden"]' ).val( '' );
$( this ).parent().addClass( 'hide' );
} );
/**
* ファイルアップローダー
*/
wrapper.find( '.btn-add-file' ).click( function( e ) {
e.preventDefault();
var custom_uploader_file;
var upload_button = $( this );
if ( custom_uploader_file ) {
custom_uploader_file.open();
return;
}
custom_uploader_file = wp.media( {
title : smart_cf_uploader.file_uploader_title,
button: {
text: smart_cf_uploader.file_uploader_title
},
multiple: false
} );
custom_uploader_file.on( 'select', function() {
var images = custom_uploader_file.state().get( 'selection' );
images.each( function( file ){
var image_area = upload_button.parent().find( '.smart-cf-upload-file' );
var alt_attr = file.get('title');
image_area.find( 'a' ).remove();
image_area.prepend(
'<a href="' + file.toJSON().url + '" target="_blank"><img src="' + file.toJSON().icon + '" alt="' + alt_attr + '" /><span>' + file.toJSON().filename + '</span></a>'
);
image_area.removeClass( 'hide' );
upload_button.parent().find( 'input[type="hidden"]' ).val( file.toJSON().id );
} );
} );
custom_uploader_file.open();
} );
/**
* ファイル削除ボタン
*/
wrapper.find( '.smart-cf-upload-file' ).hover( function() {
$( this ).find( '.btn-remove-file' ).fadeIn( 'fast', function() {
$( this ).removeClass( 'hide' );
} );
}, function() {
$( this ).find( '.btn-remove-file' ).fadeOut( 'fast', function() {
$( this ).addClass( 'hide' );
} );
} );
wrapper.find( '.btn-remove-file' ).click( function() {
$( this ).parent().find( 'img' ).remove();
$( this ).parent().siblings( 'input[type="hidden"]' ).val( '' );
$( this ).parent().addClass( 'hide' );
} );
/**
* sortable
*/
wrapper.find( '.smart-cf-meta-box-repeat-tables' ).sortable( {
handle: '.smart-cf-icon-handle',
items : '> .smart-cf-meta-box-table:not( :first-child )',
start : function( e, ui ) {
$( this ).trigger( 'smart-cf-repeat-table-sortable-start', ui.item );
},
stop : function( e, ui ) {
$( this ).trigger( 'smart-cf-repeat-table-sortable-stop', ui.item );
},
} );
} );
} );