keyword-to-url.js
7.2 KB
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
209
210
211
212
jQuery(document).ready( function($) {
//---------------------------------------------------------------------
//Global constants (used as defines)
var mcnkeywordtourlSource = {
MANUAL: 0,
IMPORT: 1
};
//---------------------------------------------------------------------
//Initial CSS changes
mcnkeywordtourlSetCSS();
//---------------------------------------------------------------------
//MANUAL -- add a keyword-URL pair to the table from the textboxes on the page.
mcnkeywordtourlTurnOffAllValidationMessages();
$("#mcnkeywordtourlAdd").click( function() {
mcnkeywordtourlTurnOffAllValidationMessages();
insertKeywordUrlIntoTable(
mcnkeywordtourlSource.MANUAL, $("#mcnkeywordtourlKeyword").val(), $("#mcnkeywordtourlURL").val()
);
});
//Remove a row in the table when its Remove link is clicked.
//I had to use this way since the regular way, $(".linkRemove").click(function(), did not work.
//This is because I am assigning events to a class rather than an id.
$(document).on( 'click', "span.mcnkeywordtourlRemoveClass", function() {
mcnkeywordtourlTurnOffAllValidationMessages();
$(this).parents('tr').first().remove();
mcnkeywordtourlSaveHiddenInput();
});
//---------------------------------------------------------------------
//IMPORT -- add keyword-URL pairs to the table from a file import.
$("#mcnkeywordtourlImportFileChooser").click( function() {
mcnkeywordtourlTurnOffAllValidationMessages();
});
$("#mcnkeywordtourlImportLoad").click( function() {
mcnkeywordtourlTurnOffAllValidationMessages();
//Check for the HTML 5 File API support.
if ( !window.File || !window.FileReader || !window.FileList || !window.Blob ) {
$("#mcnkeywordtourlValid5").show();
return;
}
var fileExtension = $("#mcnkeywordtourlImportFileChooser").val().substr(-4);
if ( fileExtension !== ".csv" && fileExtension !== ".txt" ) {
$("#mcnkeywordtourlValid4").show();
return;
}
var file = $("#mcnkeywordtourlImportFileChooser")[0].files[0];
if (file) {
if ( !$("#mcnkeywordtourlImportAppend").prop('checked') ) {
$("#mcnkeywordtourlTable tbody").remove();
}
var reader = new FileReader();
reader.onload = function() {
//Replace the linefeeds so we can read UNIX and MS-DOS file types
var temp = this.result.replace("\r\n", "\n");
var keywordurlpairAll = temp.split("\n");
keywordurlpairAll.sort();
keywordurlpairAll.forEach( function( value, index, ar ) {
var keywordurlpairSingle = value.split(",");
insertKeywordUrlIntoTable(
mcnkeywordtourlSource.IMPORT, keywordurlpairSingle[0], keywordurlpairSingle[1]
);
});
}
reader.readAsText(file);
} else {
$("#mcnkeywordtourlValid4").show();
}
});
//---------------------------------------------------------------------
//Export
$("#mcnkeywordtourlExport").click( function() {
mcnkeywordtourlTurnOffAllValidationMessages();
});
//---------------------------------------------------------------------
//Shared functions
function insertKeywordUrlIntoTable( source, keyword, url ) {
//Trim Keywords and URL
keyword = $.trim(keyword);
url = $.trim(url);
//Remove prefix (we calculate and add a prefix during renderings)
url = url.replace( "http://", "" );
url = url.replace( "mailto:", "" );
//See if either field is empty
if ( ( keyword.length == 0 ) || ( url.length == 0 ) ) {
if ( source == mcnkeywordtourlSource.MANUAL ) {
$("#mcnkeywordtourlValid1a").show();
}
return;
}
//See if keyword or url contains illegal characters: quotes
if (
( keyword.indexOf("'") != -1 ) || ( keyword.indexOf("\"") != -1 ) ||
( url.indexOf("'") != -1 ) || ( url.indexOf("\"") != -1 )
) {
( source == mcnkeywordtourlSource.MANUAL ) ?
$("#mcnkeywordtourlValid2a").show() : $("#mcnkeywordtourlValid2b").show();
return;
}
//See if keyword contains illegal characters: ampersands
//This is because we won't know it the user used &, & or &
if ( keyword.indexOf("&") != -1 ) {
( source == mcnkeywordtourlSource.MANUAL ) ?
$("#mcnkeywordtourlValid2c").show() : $("#mcnkeywordtourlValid2b").show();
return;
}
//See if keyword is already present in table
var flag = false;
$("#mcnkeywordtourlTable").find("tr").each( function () {
var td1 = $(this).find("td:eq(1)").text();
if ( td1.toLowerCase() === keyword.toLowerCase() ) {
flag = true;
return false; //this is the way you do a break on these loops (you return false to the callback)
}
});
if ( flag ) {
( source == mcnkeywordtourlSource.MANUAL ) ?
$("#mcnkeywordtourlValid3a").show() : $("#mcnkeywordtourlValid2b").show();
return;
}
var urlPrefix = ( url.indexOf('@') > 0 ) ? 'mailto:' : 'http://';
$('#mcnkeywordtourlTable').prepend(
'<tr>' +
'<td><span class="mcnkeywordtourlRemoveClass">删除</span></td>' +
'<td class="mcnkeywordtourlKeywordClass">' + keyword + '</td>' +
'<td><a href="' + urlPrefix + url + '">' + url + '</a></td>' +
'</tr>\n'
);
//We have to update the CSS here otherwise the newly inserted
//row elements don't get the original CSS for their class.
mcnkeywordtourlSetCSS();
mcnkeywordtourlSaveHiddenInput();
}
function mcnkeywordtourlSaveHiddenInput() {
$("#mcn_keyword_to_url_serialized").val("");
$("#mcnkeywordtourlTable").find("tr").each( function () {
var $tds = $(this).find('td');
$("#mcn_keyword_to_url_serialized").val(
$("#mcn_keyword_to_url_serialized").val() +
$tds.eq(1).text() + "mcn0" +
$tds.eq(2).text() + "mcn1"
);
});
}
function mcnkeywordtourlTurnOffAllValidationMessages() {
$("#mcnkeywordtourlValid1a").hide();
$("#mcnkeywordtourlValid2a").hide();
$("#mcnkeywordtourlValid3a").hide();
$("#mcnkeywordtourlValid2b").hide();
$("#mcnkeywordtourlValid2c").hide();
$("#mcnkeywordtourlValid4").hide();
$("#mcnkeywordtourlValid5").hide();
}
function mcnkeywordtourlSetCSS() {
//We have a hidden anchor tag on the page. We do that so we can get some
//of its CSS properties and then set our span links to match whatever WordPress
//styles regular anchor tag (links) use on that page.
$("#mcnkeywordtourlBaseAnchor").css( "display", "none" );
$(".mcnkeywordtourlRemoveClass").css( "color", $("#mcnkeywordtourlBaseAnchor").css("color") );
$(".mcnkeywordtourlRemoveClass").css( "textDecoration", $("#mcnkeywordtourlBaseAnchor").css("textDecoration") );
$(".mcnkeywordtourlRemoveClass").css( "cursor", $("#mcnkeywordtourlBaseAnchor").css("cursor") );
$(".mcnkeywordtourlKeywordClass").css( "padding-left", '8px' );
$(".mcnkeywordtourlKeywordClass").css( "padding-right", '8px' );
//We want to hide the message that says the setting were saved because it could persist
//even after the user makes changes (Remove) to the table. We want to keep this message area
//available for other errors that WordPress may report so We just hide it if it contains
//the 'Settings saved.' message
$("#setting-error-settings_updated:contains('Settings saved.')").css( "display", "none" );
}
});