-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathurl2surt.jQuery.js
More file actions
88 lines (85 loc) · 3.45 KB
/
url2surt.jQuery.js
File metadata and controls
88 lines (85 loc) · 3.45 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
/*!
* jQuery plugin url2surt - a URL to SURT/SURT-prefix converter
* Original author: Robert Kolatzek
* Licensed under the MIT license
*/
(function($) {
var settings = {
/* id of form/div to append surt input element to */
destitationId: 'surts',
/* id of input element with url to convert into a surt */
sourceId: 'url',
/* class name of a new span with the surt */
surtClass: 'surts',
/* convert to SURT prefix? */
asSurtPrefix: false,
/* string with message: "on click this will be removed */
clickToDelteStr: 'Click to delte',
/* value of name attribute for hidden input elements with created surt (will be an array! -> name="surt[]") */
inputName: 'surt'
};
var methods = {
init: function(options) {
return this.each(function() {
var opt = $.extend({}, settings, options);
$(this).bind('click.url2surt', opt, methods.convert);
});
},
destroy: function( ) {
return this.each(function() {
$(window).unbind('.url2surt');
});
},
convert: function(event) {
var settings = $.extend({}, event.data);
var urlStr = $('#' + settings.sourceId).val();
urlStr = urlStr.replace(/\/\//g, '/');
var parts = urlStr.split('/');
var surtParts = new Array();
if (parts.length > 1) {
if (parts[0].match(/(http|https|ftp):/gi)) {
surtParts[0] = parts[0] + '/';
}
var domainParts = parts[1].split(".");
if (domainParts.length > 1) {
domainParts.reverse();
surtParts[1] = ('(' + domainParts.join(',') + ',)').replace(/\*\./gi,'');
}
else {
surtParts[1] = '(' + parts[1] + ',';
}
//console.log(parts);
if (parts.length > 2) {
var pathParts = new Array();
var i = 2;
while (i < parts.length) {
pathParts[pathParts.length] = parts[i];
i++;
}
surtParts[2] = pathParts.join('/');
}
}
var surt = surtParts.join('/');
if(urlStr.match(/\/\/([^\/]+)\/$/gi) !== null){
surt = surt+'/';
}
if(settings.asSurtPrefix && !surt.match(/[^\/]\/$/g) && !surt.match(/\)$/g)){
surt = surt.replace(/\/([^\/]+)$/, '/');
}
else if(settings.asSurtPrefix && surt.match(/\)$/g)){
surt = surt.replace(/\)$/, '');
}
$('#'+settings.destitationId).append('<br/><span class="'+settings.surtClass+'" title="'+settings.clickToDelteStr+'"><input name="'+settings.inputName+'[]" type="hidden" value="'+surt+'">'+surt+'</span>');
$('.'+settings.surtClass).click(function(){$(this).remove();});
}
};
$.fn.url2surt = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.url2surt');
}
}
})(jQuery);