-
Notifications
You must be signed in to change notification settings - Fork 0
/
slugify.js
140 lines (113 loc) · 5.47 KB
/
slugify.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
(function (root, factory) {
if ( typeof define === 'function' && define.amd ) {
define([], factory(root));
} else if ( typeof exports === 'object' ) {
module.exports = factory(root);
} else {
root.slugify = factory(root);
}
})(typeof global !== 'undefined' ? global : this.window || this.global, function (root) {
'use strict';
/*** Private Methods */
function extendDefaults(defaults, properties) {
Object.keys(properties).forEach(property => {
if(properties.hasOwnProperty(property)) {
defaults[property] = properties[property];
}
});
return defaults;
}
function toSlugify(text) {
return text.toString().toLowerCase().trim()
.normalize("NFD") /* separate accent from letter */
.replace(/[\u0300-\u036f]/g, "") /* remove all separated accents */
.replace(/\s+/g, "_") /* replace spaces with _ */
.replace(/&/g, "-") /* replace & with "-" */
.replace(/[^\w\-]+/g, "") /* remove all non-word chars */
.replace(/--+/g, "-") /* replace multiple "-" with single "-" */
}
function buildEnableMonitor(string) {
let settings = this.getSettings();
elMonitor = document.querySelectorAll(string);
[].forEach.call(elMonitor, function(el) {
if(el.tagName == "INPUT" && el.type == "checkbox")
settings.enable = el.checked;
});
}
function buildSourceElement(element) {
if(this.getSettings().enable == true) {
if(typeof element !== "object")
element = document.querySelectorAll(element);
let targetElement = this.getTargetElement();
let settings = this.getSettings();
[].forEach.call(element, function(elSource) {
elSource.addEventListener("focusout", (event) => {
if(elSource.tagName == "TEXTAREA" || (elSource.tagName == "INPUT" && elSource.type == "text")) {
let slugResult = toSlugify(elSource.value);
if(settings.mode == "replace")
elSource.value = slugResult;
if(targetElement !== false) {
if(typeof targetElement !== "object")
targetElement = document.querySelectorAll(targetElement);
[].forEach.call(targetElement, function(elTarget) {
if(elTarget.tagName == "TEXTAREA" || (elTarget.tagName == "INPUT" && elTarget.type == "text")) {
if(settings.mode == "replace" || settings.mode == "target" || (settings.mode == "fill" && elTarget.value.trim().replace(/[\u0300-\u036f]/g, "").replace(/\s+/g, "") == ""))
elTarget.value = slugResult;
}
else
elTarget.innerHTML = slugResult;
});
}
}
});
});
}
}
function buildTargetElement(element) {
if(this.getSettings().enable == true)
if(element !== false) {
let sourceElement = document.querySelectorAll(this.getSourceElement());
if(typeof element !== "object")
element = document.querySelectorAll(element);
[].forEach.call(element, function(el) {
el.addEventListener("focusout", (event) => {
if(el.tagName == "TEXTAREA" || (el.tagName == "INPUT" && el.type == "text"))
el.value = toSlugify(el.value);
if(el.value.trim().replace(/[\u0300-\u036f]/g, "").replace(/\s+/g, "") == "")
[].forEach.call(sourceElement, function(source) {
el.value = toSlugify(source.value);
});
});
});
}
}
/*** Register plugin in window object */
function slugify (element, params) {
let defaults = {
enable: true, /* boolean or Html Element(checkbox), to allow plugin running */
targetElement: false, /* boolean or HTML Element */
mode: "replace", /* "replace" slugify result to element and targetElement, "target" slugify result only to targetElement, "fill" slugify result only to targetElement if the target element is empty */
};
let settings = (arguments[1] && typeof arguments[1] === "object") ? extendDefaults(defaults, arguments[1]) : defaults;
let sourceElement = element;
let targetElement = settings.targetElement;
this.getSettings = function() {
return settings;
}
this.getSourceElement = function() {
return sourceElement;
}
this.getTargetElement = function() {
return targetElement;
}
this.init();
}
/*** Public Methods */
slugify.prototype.init = function() {
if(typeof this.getSettings().enable === "string")
buildEnableMonitor.call(this, this.getSettings().enable);
buildSourceElement.call(this, this.getSourceElement());
buildTargetElement.call(this, this.getTargetElement());
}
return slugify;
});