forked from yairEO/tagify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text-input.html
142 lines (121 loc) · 6.72 KB
/
text-input.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Tagify - pupetteer tests</title>
<meta name="description" content="Converts HTML input/textarea into Tags component">
<meta name="author" content="Yair Even-Or">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="../dist/tagify.css">
<script src="../dist/tagify.min.js"></script>
<style>
body{ font: 16px Arial; }
form{
max-width:600px;
}
form > section{ margin-bottom: 2em; }
.tagify+input, .tagify+textarea {
position: initial !important;
left: 0 !important;
transform: none !important;
width: 100%;
margin-top: .2em;
min-height: 11ch;
background: powderblue;
font-family: "Fira Code";
}
.tagify{
margin: .2em;
min-width: 400px;
}
</style>
</head>
<body>
<section>
<input name='input' class='some_class_name' placeholder='Type an English letter' value='css, html, javascript, css, css, css, css' data-blacklist='.NET,PHP'>
<br/>
<button class='tags--removeAllBtn' type='button'>Remove all these tags ⬆</button>
</section>
<script>
var inputElm = document.querySelector('input[name=input]'),
whitelist = [
"A# .NET", "A# (Axiom)", "A-0 System", "A+", "A++", "ABAP", "ABC", "ABC ALGOL", "ABSET", "ABSYS", "ACC", "Accent", "Ace DASL", "ACL2", "Avicsoft", "ACT-III", "Action!", "ActionScript", "Ada", "Adenine", "Agda", "Agilent VEE", "Agora", "AIMMS", "Alef", "ALF", "ALGOL 58", "ALGOL 60", "ALGOL 68", "ALGOL W", "Alice", "Alma-0", "AmbientTalk", "Amiga E", "AMOS", "AMPL", "Apex (Salesforce.com)", "APL", "AppleScript", "Arc", "ARexx", "Argus", "AspectJ", "Assembly language", "ATS", "Ateji PX", "AutoHotkey", "Autocoder", "AutoIt", "AutoLISP / Visual LISP", "Averest", "AWK", "Axum", "Active Server Pages", "ASP.NET",
"B", "Babbage", "Bash", "BASIC", "bc", "BCPL", "BeanShell", "Batch (Windows/Dos)", "Bertrand", "BETA", "Bigwig", "Bistro", "BitC", "BLISS", "Blockly", "BlooP", "Blue", "Boo", "Boomerang", "Bourne shell (including bash and ksh)", "BREW", "BPEL", "B",
"C--", "C++ – ISO/IEC 14882", "C# – ISO/IEC 23270", "C/AL", "Caché ObjectScript", "C Shell", "Caml", "Cayenne", "CDuce", "Cecil", "Cesil", "Céu", "Ceylon", "CFEngine", "CFML", "Cg", "Ch", "Chapel", "Charity", "Charm", "Chef", "CHILL", "CHIP-8", "chomski", "ChucK", "CICS", "Cilk", "Citrine (programming language)", "CL (IBM)", "Claire", "Clarion", "Clean", "Clipper", "CLIPS", "CLIST", "Clojure", "CLU", "CMS-2", "COBOL – ISO/IEC 1989", "CobolScript – COBOL Scripting language", "Cobra", "CODE", "CoffeeScript", "ColdFusion", "COMAL", "Combined Programming Language (CPL)", "COMIT", "Common Intermediate Language (CIL)", "Common Lisp (also known as CL)", "COMPASS", "Component Pascal", "Constraint Handling Rules (CHR)", "COMTRAN", "Converge", "Cool", "Coq", "Coral 66", "Corn", "CorVision", "COWSEL", "CPL", "CPL", "Cryptol", "csh", "Csound", "CSP", "CUDA", "Curl", "Curry", "Cybil", "Cyclone", "Cython",
"Java", "Javascript",
"M2001", "M4", "M#", "Machine code", "MAD (Michigan Algorithm Decoder)", "MAD/I", "Magik", "Magma", "make", "Maple", "MAPPER now part of BIS", "MARK-IV now VISION:BUILDER", "Mary", "MASM Microsoft Assembly x86", "MATH-MATIC", "Mathematica", "MATLAB", "Maxima (see also Macsyma)", "Max (Max Msp – Graphical Programming Environment)", "Maya (MEL)", "MDL", "Mercury", "Mesa", "Metafont", "Microcode", "MicroScript", "MIIS", "Milk (programming language)", "MIMIC", "Mirah", "Miranda", "MIVA Script", "ML", "Model 204", "Modelica", "Modula", "Modula-2", "Modula-3", "Mohol", "MOO", "Mortran", "Mouse", "MPD", "Mathcad", "MSIL – deprecated name for CIL", "MSL", "MUMPS", "Mystic Programming L"
];
// initialize Tagify on the above input node reference
var tagify = new Tagify(inputElm, {
enforceWhitelist: true,
whitelist: [...new Set(inputElm.value.trim().split(/\s*,\s*/))] // Array of values. stackoverflow.com/a/43375571/104380
});
document.querySelector('.tags--removeAllBtn')
.addEventListener('click', tagify.removeAllTags.bind(tagify))
// Chainable event listeners
tagify.on('add', onAddTag)
.on('remove', onRemoveTag)
.on('input', onInput)
.on('edit', onTagEdit)
.on('invalid', onInvalidTag)
.on('click', onTagClick)
.on('focus', onTagifyFocusBlur)
.on('blur', onTagifyFocusBlur)
.on('dropdown:hide dropdown:show', e => console.log(e.type))
.on('dropdown:select', onDropdownSelect)
var mockAjax = (function mockAjax(){
var timeout;
return function(duration){
clearTimeout(timeout); // abort last request
return new Promise(function(resolve, reject){
timeout = setTimeout(resolve, duration || 700, whitelist)
})
}
})()
// tag added callback
function onAddTag(e){
console.log("onAddTag: ", e.detail);
console.log("original input value: ", inputElm.value)
tagify.off('add', onAddTag) // exmaple of removing a custom Tagify event
}
// tag remvoed callback
function onRemoveTag(e){
console.log("onRemoveTag:", e.detail, "tagify instance value:", tagify.value)
}
// on character(s) added/removed (user is typing/deleting)
function onInput(e){
console.log("onInput: ", e.detail);
tagify.whitelist = null; // reset current whitelist
tagify.loading(true) // show the loader animation
// get new whitelist from a delayed mocked request (Promise)
mockAjax()
.then(function(result){
tagify.settings.whitelist = result.concat(tagify.value) // add already-existing tags to the new whitelist array
tagify
.loading(false)
// render the suggestions dropdown.
.dropdown.show(e.detail.value);
})
.catch(err => tagify.dropdown.hide())
}
function onTagEdit(e){
console.log("onTagEdit: ", e.detail);
}
// invalid tag added callback
function onInvalidTag(e){
console.log("onInvalidTag: ", e.detail);
}
// invalid tag added callback
function onTagClick(e){
console.log(e.detail);
console.log("onTagClick: ", e.detail);
}
function onTagifyFocusBlur(e){
console.log(e.type, "event fired")
}
function onDropdownSelect(e){
console.log("onDropdownSelect: ", e.detail)
}
</script>
</body>
</html>