-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcondor.js
158 lines (138 loc) · 5.34 KB
/
condor.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
// CONDOR 1.5 Made by Chen Ye, MIT License
(function ($) {
$.fn.condor = function (command) {
var settings,
$target = $(this),
numInputs = 0,
publicMethods = {
getValues: function () {
var values = [],
i = 0;
$target.find(".condor-active input").each(function () {
if (this.value.length !== 0) {
values[i] = this.value;
i += 1;
}
});
return values;
}
};
function refreshNumInputs() {
numInputs = $target.children(".condor-active").length;
return numInputs;
}
function addField(hint, parentclass, childclass) {
$target.append("<div class='field " + parentclass + " '><div class='ui left icon input " + childclass + "'><input type='" + settings.inputType + "' placeholder='" + settings.inactiveHint + "'><i class='plus icon'></i></div></div>");
}
function addActiveField(id) {
addField('', 'condor-active', '');
return $target.children(".condor-active").filter(function () {
return $(this).find("input").val().length === 0;
}).first();
}
function addInactiveField(id) {
var $field = $target.children(".condor-add");
addField('add another link', 'condor-add', settings.inactiveInputClass);
settings.addCallback.call();
return $field;
}
function conditionalAddInactiveField() {
if ($target.hasClass("no-new")) {
$target.removeClass("no-new");
refreshNumInputs();
if (numInputs < settings.maxInputs) {
addInactiveField(numInputs);
}
}
}
function removeInactiveFields() {
$target.children(".condor-add").remove();
}
function bindInactive() {
$target.on("click focusin", ".condor-add", function () {
makeActive($(this));
});
}
function bindActive() {
//This instantaneously detects if an active input gets filled
$target.on("propertychange keyup input paste", ".condor-active", function (event) {
var $this = $(this),
$input = $this.find("input");
// If no longer an empty string
if ($input.val() === '') {
$target.addClass("no-new");
removeInactiveFields();
} else {
conditionalAddInactiveField();
}
});
$target.on("blur", ".condor-active input", function (event) {
if ((this.value === '') && (numInputs > settings.minInputs)) {
$(this).parent().parent().remove();
numInputs -= 1;
conditionalAddInactiveField();
}
});
}
function makeActive($field) {
var index,
name,
$input = $field.find('input'),
$icon = $field.find('i.icon');
numInputs += 1;
$field.find('.input').removeClass(settings.inactiveInputClass);
$icon.removeClass('plus');
$icon.addClass(settings.activeIcon);
$input.attr('placeholder', settings.activeHint);
if (settings.uniqueNames) {
index = refreshNumInputs();
name = settings.namePrefix + '-' + index;
$input.attr('name', name);
} else {
$input.attr('name', settings.namePrefix);
}
$field.addClass('condor-active');
$field.removeClass('condor-add');
$target.addClass('no-new');
settings.activateCallback.call();
}
if (publicMethods[command]) {
return publicMethods[command].apply(this, []);
} else {
settings = $.extend({
minInputs: 1,
maxInputs: 10,
namePrefix: 'inputs',
uniqueNames: true,
inactiveHint: 'add input',
inactiveInputClass: 'inverted',
activeHint: '',
activeIcon: 'linkify',
addCallback: function () {},
activateCallback: function () {},
inputType: 'text',
prepopulate: []
}, command);
}
function prepopulate() {
settings.prepopulate.forEach(function (value, index, ar) {
var $field = addActiveField(refreshNumInputs()),
$input = $field.find("input");
$input.val(value);
makeActive($field);
});
}
function initialize() {
$target.addClass('no-new');
bindActive();
bindInactive();
prepopulate();
for (i = numInputs; i < settings.minInputs; i++) {
addInactiveField(numInputs);
}
makeActive($target.children(".condor-add"));
}
initialize();
return this;
};
}(jQuery));