Issues (2756)

js/insert.js (1 issue)

1
// Init some stuff
2
$(document).ready(function(){
3
	$('#add-url, #add-keyword').keypress(function(e){
4
		if (e.which == 13) {add_link();}
5
	});
6
	add_link_reset();
7
	$('#new_url_form').attr('action', 'javascript:add_link();');
8
9
	$('input.text').focus(function(){
10
		$(this).select();
11
	});
12
13
	// this one actually has little impact, the .hasClass('disabled') in each edit_link_display(), remove() etc... fires faster
14
	$(document).on( 'click', 'a.button', function() {
15
		if( $(this).hasClass('disabled') ) {
16
			return false;
17
		}
18
	});
19
20
	// When Searching, explode search text in pieces -- see split_search_text_before_search()
21
	$('#filter_form').submit( function(){
22
		split_search_text_before_search();
23
		return true;
24
	});
25
});
26
27
// Create new link and add to table
28
function add_link() {
29
	if( $('#add-button').hasClass('disabled') ) {
30
		return false;
31
	}
32
	var newurl = $("#add-url").val();
33
	var nonce = $("#nonce-add").val();
34
	if ( !newurl || newurl == 'http://' || newurl == 'https://' ) {
35
		return;
36
	}
37
	var keyword = $("#add-keyword").val();
38
	add_loading("#add-button");
39
	$.getJSON(
40
		ajaxurl,
41
		{action:'add', url: newurl, keyword: keyword, nonce: nonce},
42
		function(data){
43
			if(data.status == 'success') {
44
				$('#main_table tbody').prepend( data.html ).trigger("update");
45
				$('#nourl_found').css('display', 'none');
46
				zebra_table();
47
				increment_counter();
48
				toggle_share_fill_boxes( data.url.url, data.shorturl, data.url.title );
49
			}
50
51
			add_link_reset();
52
			end_loading("#add-button");
53
			end_disable("#add-button");
54
55
			feedback(data.message, data.status);
56
		}
57
	);
58
}
59
60
function toggle_share_fill_boxes( url, shorturl, title ) {
61
	$('#copylink').val( shorturl );
62
	$('#titlelink').val( title );
63
	$('#origlink').attr( 'href', url ).html( url );
64
	$('#statlink').attr( 'href', shorturl+'+' ).html( shorturl+'+' );
65
	var tweet = ( title ? title + ' ' + shorturl : shorturl );
66
	$('#tweet_body').val( tweet ).keypress();
67
	$('#shareboxes').slideDown( '300', function(){ init_clipboard(); } ); // clipboard re-initialized after slidedown to make sure the invisible Flash element is correctly positionned
68
	$('#tweet_body').keypress();
69
}
70
71
// Display the edition interface
72
function edit_link_display(id) {
73
	if( $('#edit-button-'+id).hasClass('disabled') ) {
74
		return false;
75
	}
76
	add_loading('#actions-'+id+' .button');
77
	var keyword = $('#keyword_'+id).val();
78
	var nonce = get_var_from_query( $('#edit-button-'+id).attr('href'), 'nonce' );
79
	$.getJSON(
80
		ajaxurl,
81
		{ action: "edit_display", keyword: keyword, nonce: nonce, id: id },
82
		function(data){
83
			$("#id-" + id).after( data.html );
84
			$("#edit-url-"+ id).focus();
85
			end_loading('#actions-'+id+' .button');
86
		}
87
	);
88
}
89
90
// Delete a link
91
function remove_link(id) {
92
	if( $('#delete-button-'+id).hasClass('disabled') ) {
93
		return false;
94
	}
95
	if (!confirm('Really delete?')) {
96
		return;
97
	}
98
	var keyword = $('#keyword_'+id).val();
99
	var nonce = get_var_from_query( $('#delete-button-'+id).attr('href'), 'nonce' );
100
	$.getJSON(
101
		ajaxurl,
102
		{ action: "delete", keyword: keyword, nonce: nonce, id: id },
103
		function(data){
104
			if (data.success == 1) {
105
				$("#id-" + id).fadeOut(function(){
106
					$(this).remove();
107
					if( $('#main_table tbody tr').length  == 1 ) {
108
						$('#nourl_found').css('display', '');
109
					}
110
111
					zebra_table();
112
				});
113
				decrement_counter();
114
				decrease_total_clicks( id );
115
			} else {
116
				alert('something wrong happened while deleting :/');
117
			}
118
		}
119
	);
120
}
121
122
// Redirect to stat page
123
function go_stats(link) {
124
	window.location=link;
125
}
126
127
// Cancel edition of a link
128
function edit_link_hide(id) {
129
	$("#edit-" + id).fadeOut(200, function(){
130
        $("#edit-" + id).remove();
131
		end_disable('#actions-'+id+' .button');
132
	});
133
}
134
135
// Save edition of a link
136
function edit_link_save(id) {
137
	add_loading("#edit-close-" + id);
138
	var newurl = $("#edit-url-" + id).val();
139
	var newkeyword = $("#edit-keyword-" + id).val();
140
	var title = $("#edit-title-" + id).val();
141
	var keyword = $('#old_keyword_'+id).val();
142
	var nonce = $('#nonce_'+id).val();
143
	var www = $('#yourls-site').val();
144
	$.getJSON(
145
		ajaxurl,
146
		{action:'edit_save', url: newurl, id: id, keyword: keyword, newkeyword: newkeyword, title: title, nonce: nonce },
147
		function(data){
148
			if(data.status == 'success') {
149
150
				if( data.url.title != '' ) {
151
					var display_link = '<a href="' + data.url.url + '" title="' + data.url.title + '">' + data.url.display_title + '</a><br/><small><a href="' + data.url.url + '">' + data.url.display_url + '</a></small>';
152
				} else {
153
					var display_link = '<a href="' + data.url.url + '" title="' + data.url.url + '">' + data.url.display_url + '</a>';
154
				}
155
156
				$("#url-" + id).html(display_link);
157
				$("#keyword-" + id).html('<a href="' + data.url.shorturl + '" title="' + data.url.shorturl + '">' + data.url.keyword + '</a>');
158
				$("#edit-" + id).fadeOut(200, function(){
159
                    $("#edit-" + id).remove();
160
					$('#main_table tbody').trigger("update");
161
				});
162
				$('#keyword_'+id).val( newkeyword );
163
				$('#statlink-'+id).attr( 'href', data.url.shorturl+'+' );
164
			}
165
			feedback(data.message, data.status);
166
			end_loading("#edit-close-" + id);
167
			end_disable("#edit-close-" + id);
168
			if(data.status == 'success') {
0 ignored issues
show
Operator == prohibited; use === instead
Loading history...
169
				end_disable("#actions-" + id + ' .button');
170
			}
171
		}
172
	);
173
}
174
175
// Prettify table with odd & even rows
176
function zebra_table() {
177
	$("#main_table tbody tr:even").removeClass('odd').addClass('even');
178
	$("#main_table tbody tr:odd").removeClass('even').addClass('odd');
179
	$('#main_table tbody').trigger("update");
180
}
181
182
// Ready to add another URL
183
function add_link_reset() {
184
	$('#add-url').val('').focus();
185
	$('#add-keyword').val('');
186
}
187
188
// Increment URL counters
189
function increment_counter() {
190
	$('.increment').each(function(){
191
		$(this).html( parseInt($(this).html()) + 1);
192
	});
193
}
194
195
// Decrement URL counters
196
function decrement_counter() {
197
	$('.increment').each(function(){
198
		$(this).html( parseInt($(this).html()) - 1 );
199
	});
200
}
201
202
// Decrease number of total clicks
203
function decrease_total_clicks( id ) {
204
	var total_clicks = $("#overall_tracking strong:nth-child(2)");
205
	total_clicks.html( parseInt( total_clicks.html() ) - parseInt( $('#clicks-' + id).html() ) );
206
}
207
208
// Toggle Share box
209
function toggle_share(id) {
210
	if( $('#share-button-'+id).hasClass('disabled') ) {
211
		return false;
212
	}
213
	var link = $('#url-'+id+' a:first');
214
	var longurl = link.attr('href');
215
	var title = link.attr('title');
216
	var shorturl = $('#keyword-'+id+' a:first').attr('href');
217
218
	toggle_share_fill_boxes( longurl, shorturl, title );
219
}
220
221
// When "Search" is clicked, split search text to beat servers which don't like query string with "http://"
222
// See https://github.com/YOURLS/YOURLS/issues/1576
223
function split_search_text_before_search() {
224
	// Add 2 hidden fields and populate them with parts of search text
225
	$("<input type='hidden' name='search_protocol' />").appendTo('#filter_form');
226
	$("<input type='hidden' name='search_slashes' />").appendTo('#filter_form');
227
	var search = get_protocol_slashes_and_rest( $('#filter_form input[name=search]').val() );
228
	$('#filter_form input[name=search]').val( search.rest );
229
	$('#filter_form input[name=search_protocol]').val( search.protocol );
230
	$('#filter_form input[name=search_slashes]').val( search.slashes );
231
}
232
233