-
Notifications
You must be signed in to change notification settings - Fork 0
/
Photo auto alt.js
184 lines (159 loc) · 4.61 KB
/
Photo auto alt.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
/*
Photo Auto Alt v1.0 derived from Gallery Auto Alt v1.3
*/
var titleCase = confirm("OK for Title Case, Cancel for Lower Case");
var parent = $(".modal-body");
var srcUrl = $("input[type=text]:eq(0)", parent);
var altTag = $("input[type=text]:eq(2)", parent);
var url = srcUrl.val();
var parser = document.createElement("a");
parser.href = url;
var path = decodeURIComponent(parser.pathname);
var lastSlash = path.lastIndexOf("/")+1;
var lastDot = path.lastIndexOf(".");
var fileName = path.substr(lastSlash, lastDot-lastSlash);
var cleanedFileName = cleanFileName(fileName);
altTag.val(cleanedFileName);
function cleanFileName(fileName)
{
// replaces hyphens and underscores to spaces
// http://stackoverflow.com/questions/7005784/regex-to-match-a-string-not-surrounded-by-brackets
fileName = liquidize(fileName);
fileName = fileName.replace(/-|_(?![^{]*\})/g, ' ');
// remove default postfix of a number and prefix of gallery
fileName = removePostFixAndSuffix(fileName);
// Title case or Captalize first letter
fileName = titleCase ? toTitleCase(fileName) : capitalizeFirstLetter(fileName);
return fileName;
}
// defaully removes a number prefix and "gallery" postfix
function removePostFixAndSuffix(fileName, prefix, suffix)
{
// defaults for optional parameters
prefix = prefix == undefined ? /^\d+/g : prefix;
suffix = suffix == undefined ? "gallery" : suffix;
if (prefix instanceof RegExp)
{
fileName = fileName.replace(prefix, "");
}
else if (fileName.indexOf(prefix) === 0)
{
fileName = fileName.substr(prefix.length);
}
if (fileName.endsWith(suffix))
{
fileName = fileName.substr(0, fileName.length - suffix.length)
}
// general cleanup rather than worring about spaces
return fileName.trim();
}
function defaultPrefix(fileName)
{
}
// http://stackoverflow.com/questions/4878756/javascript-how-to-capitalize-first-letter-of-each-word-like-a-2-word-city
function toTitleCase(str)
{
return str.replace(/(?![^{]*\})\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
// http://stackoverflow.com/questions/1026069/capitalize-the-first-letter-of-string-in-javascript
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
String.prototype.capitalizeFirstLetter = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}
function liquidize(string) {
var liquids = [
"page_name",
"page_slug",
"page_created",
"page_updated",
"website_urn",
"website_slug",
"location_uid",
"location_urn",
"location_domain",
"location_corporate",
"location_name",
"location_name_slug",
"location_city",
"location_city_slug",
"location_state",
"location_state_slug",
"location_state_name",
"location_state_name_slug",
"location_street_address",
"location_neighborhood",
"location_neighborhood_slug",
"location_neighborhood_2",
"location_postal_code",
"location_phone_number",
"location_office_hours",
"location_access_hours",
"location_floor_plans",
"location_primary_amenity",
"location_qualifier",
"location_primary_landmark",
"location_office_hours",
"location_access_hours",
"location_google_plus_id",
"location_go_squared_id",
"location_go_squared_tag",
"location_ga_tracking_id",
"location_ga_profile_id",
"location_facebook_id",
"location_twitter_id",
"location_yelp_id",
"location_pinterest_id",
"location_instagram_id",
"location_youtube_id",
"location_foursquare_id",
"location_tumblr_id",
"location_vimeo_id",
"location_nearby_schools",
"location_nearby_employers",
"location_apartment_amenity_1",
"location_apartment_amenity_2",
"location_community_amenity_1",
"location_community_amenity_2",
"location_landmark_1_type",
"location_landmark_1_name",
"location_landmark_2_type",
"location_landmark_2_name",
"location_property_feature_1",
"location_property_feature_2",
"location_property_feature_3",
"client_name",
"client_name_slug",
"client_domain",
"client_vertical",
"client_vertical_slug",
"client_urn",
"client_uid",
"client_type",
"client_single_domain",
"client_go_squared_id",
"client_go_squared_tag",
"client_cls_url",
"client_cxm_url",
"client_dsh_url",
"client_cpas_url",
"client_cpns_url",
"client_nae_url",
"client_vls_url",
"theme_name",
"theme_slug",
"theme_primary_color",
"theme_secondary_color",
"theme_tertiary_color",
"theme_primary_font",
"theme_secondary_font"
];
liquids.forEach(function(liquid){
if (string.indexOf(liquid) > -1)
{
string = string.replace(liquid, "{{"+liquid+"}}");
}
});
return string;
}