-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin-notes.js
154 lines (128 loc) · 4.37 KB
/
plugin-notes.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
/**
* Automagically make all links in plugin notes have target="_blank"
*/
jQuery(document).ready(function($) {
jQuery('span.wp-plugin_note a').each(function(){
jQuery(this).attr( 'target', '_blank' );
});
});
function add_plugin_note( plugin_slug, plugin_name ) {
edit_plugin_note(plugin_slug, plugin_name);
}
function edit_plugin_note( plugin_name ) {
var note_elements = get_plugin_note_elements(plugin_name);
// Hide note, show form and focus on textarea
note_elements.box.hide('normal');
note_elements.form.show('normal');
note_elements.form.input.focus();
}
function delete_plugin_note( plugin_name ) {
if(confirm(i18n_plugin_notes.confirm_delete)) {
var note_elements = get_plugin_note_elements(plugin_name);
note_elements.box.find('.waiting').show();
note_elements.form.input.val('');
save_plugin_note( plugin_name );
}
}
function templatesave_plugin_note( plugin_name ) {
if(confirm(i18n_plugin_notes.confirm_new_template)) {
var template_switch = document.getElementById('wp-plugin_note_new_template_'+plugin_name);
template_switch.value = 'y';
save_plugin_note( plugin_name );
}
}
function cancel_plugin_note( plugin_name ) {
var note_elements = get_plugin_note_elements(plugin_name);
note_elements.box.show('normal');
note_elements.form.hide('normal');
}
function save_plugin_note( plugin_name ) {
var note_elements = get_plugin_note_elements(plugin_name);
// Get form values
var _nonce = jQuery('input[name=wp-plugin_notes_nonce]').val();
var plugin_slug = jQuery('input[name=wp-plugin_note_slug_'+plugin_name+']').val();
var plugin_note_color = jQuery('#wp-plugin_note_color_'+plugin_name).val();
var plugin_new_template = jQuery('input[name=wp-plugin_note_new_template_'+plugin_name+']').val();
var plugin_note = note_elements.form.input.val();
// Show waiting container
note_elements.form.find('.waiting').show();
// Prepare data
var post = {};
post.action = 'plugin_notes_edit_comment';
post.plugin_name = plugin_name;
post.plugin_note = plugin_note;
post.plugin_slug = plugin_slug;
post.plugin_note_color = plugin_note_color;
post.plugin_new_template = plugin_new_template;
post._nonce = _nonce;
// Send the request
jQuery.ajax({
type : 'POST',
url : (ajaxurl) ? ajaxurl : 'admin-ajax.php',
data : post,
success : function(xml) { plugin_note_saved(xml, note_elements); },
error : function(xml) { plugin_note_error(xml, note_elements); }
});
return false;
}
function plugin_note_saved ( xml, note_elements ) {
var response;
// Uh oh, we have an error
if ( typeof(xml) == 'string' ) {
plugin_note_error({'responseText': xml}, note_elements);
return false;
}
// Parse the response
response = wpAjax.parseAjaxResponse(xml);
if ( response.errors ) {
// Uh oh, errors found
plugin_note_error({'responseText': wpAjax.broken}, note_elements);
return false;
}
response = response.responses[0];
// Add/Delete new content
note_elements.form.find('.waiting').hide();
/**
* Update the plugin note after edit/delete action
*/
if(response.action.indexOf('save_template') == -1 ) {
note_elements.box.parent().after(response.data);
note_elements.box.parent().remove();
note_elements.form.hide('normal');
jQuery('#wp-plugin_note_'+note_elements.name+' span.wp-plugin_note a').each(function(){
jQuery(this).attr( 'target', '_blank' );
});
}
/**
* Update *all* empty notes with the new template after save_template action
* Reset the save_template switch
* Display success message
*/
else {
jQuery('textarea.new_note').each(function(){
jQuery(this).val( note_elements.form.input.val() );
});
var template_switch = document.getElementById('wp-plugin_note_new_template_'+note_elements.name);
template_switch.value = 'n';
note_elements.form.find('span.success').html(i18n_plugin_notes.success_save_template).show().parent().show();
}
}
function plugin_note_error ( xml, note_elements ) {
note_elements.form.find('.waiting').hide();
if ( xml.responseText ) {
error = xml.responseText.replace( /<.[^<>]*?>/g, '' );
} else {
error = xml;
}
if ( error ) {
note_elements.form.find('span.error').html(error).show().parent().show();
}
}
function get_plugin_note_elements(name) {
var elements = {};
elements.name = name;
elements.box = jQuery('#wp-plugin_note_'+name);
elements.form = jQuery('#wp-plugin_note_form_'+name);
elements.form.input = elements.form.children('textarea');
return elements;
}