Commit 86a1d297 authored by Ryan Herbert's avatar Ryan Herbert

tokeniser.js file/form.html add min-tokens feature

allows us to set a minimum amount of tokens required within a
token_container, preventing deletion of any more tokens if the min
amount has been reached.

See #3045
parent b0bd73e4
......@@ -47,11 +47,13 @@ Tokeniser.prototype = Object.create(Closeable.prototype);
Tokeniser.prototype.tokenise = function(text) {
var token = this.createToken(text);
this.target.appendChild(token);
var tokens = this.getTokens();
tokens.children('.icon-cancel').removeClass('disabledClass');
this.form_input.value = this.readTokens();
}
Tokeniser.prototype.readTokens = function() {
var nodes = $(this.target).children('.set_token');
var nodes = this.getTokens();
return nodes.map(function callback() {
return $(this).data('set-id');
})
......@@ -81,9 +83,18 @@ Tokeniser.prototype.createToken = function(set_id) {
}
Tokeniser.prototype.removeToken = function(token) {
this.target.removeChild(token);
this.form_input.value = this.readTokens();
return token;
var tokens = this.getTokens();
var len = tokens.length;
var min = this.target.dataset.minTokens;
if (typeof min === "undefined" ||
(typeof this.target.dataset.minTokens !== "undefined" &&
this.target.dataset.minTokens < len)) {
this.target.removeChild(token);
this.form_input.value = this.readTokens();
return token;
} else {
tokens.children('.icon-cancel').addClass('disabledClass');
}
}
Tokeniser.prototype.createCloseButton = function() {
......@@ -94,3 +105,7 @@ Tokeniser.prototype.createCloseButton = function() {
}
return close;
}
Tokeniser.prototype.getTokens = function() {
return $(this.target).children('.set_token');
}
......@@ -34,7 +34,7 @@
name="set_ids"
type="text"
value="{{=",".join([set['id'] for set in sets])}}">
<div id="set_div" class="token_container">
<div id="set_div" class="token_container" data-min-tokens=1>
{{ for set in sets: }}
<span class="set_token {{=set['type']}}_token" data-set-id="{{=set['id']}}">
<i class="icon-cancel" onclick="new Tokeniser(document.getElementById('set_div'), document.getElementById('file_set_list')).removeToken(this.parentNode);"></i>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment