Skip to content

Commit

Permalink
* Bug fix: avoid temporal_widget.js error in case jQuery.tokeninput i…
Browse files Browse the repository at this point in the history
…sn't

  available
  [ichim-david refs #92877]
  • Loading branch information
ichim-david committed Feb 13, 2018
1 parent da4fc49 commit e119faa
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 107 deletions.
3 changes: 3 additions & 0 deletions docs/HISTORY.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Changelog

7.4.dev0 - (unreleased)
-----------------------
* Bug fix: avoid temporal_widget.js error in case jQuery.tokeninput isn't
available
[ichim-david refs #92877]

7.3 - (2017-12-12)
------------------
Expand Down
215 changes: 108 additions & 107 deletions eea/forms/skins/eea_forms/temporal_widget.js
Original file line number Diff line number Diff line change
@@ -1,123 +1,124 @@
(function($){
$(document).ready(function() {
$(document).ready(function() {

var field = $("#temporalCoverage");
var field = $("#temporalCoverage");

var numeric_keypad = [96, 97, 98, 99, 100, 101, 102, 103, 104, 105];
var skip_keys = [8];
var numeric_keypad = [96, 97, 98, 99, 100, 101, 102, 103, 104, 105];
var skip_keys = [8];

var dynamic = $("#temporal_btn");
dynamic.parent().css({"font-weight": "normal"});
var dynamic = $("#temporal_btn");
dynamic.parent().css({"font-weight": "normal"});


var old_value = "";
var old_value = "";


function get_tokenInput_values(){
var field_value = field.val();
if(field_value){
var value = field_value.split("\n");
return $.map(value, function(val, idx){
return {name: val, id: val};
});
}
}

function check_is_dynamic(elem){
var tokenInput = $("ul.token-input-list-facebook");
var field_value = field.val();
if(elem.is(":checked")){
tokenInput.hide();
old_value = field_value !== "-1" ? field_value : "";
field.val("-1");
} else {
tokenInput.show();
if(old_value || field.val() === "-1"){
field.tokenInput("clear");
field.val(old_value);
var token_values = get_tokenInput_values();
if(token_values){
$.each(token_values, function(i, o){
field.tokenInput("add", o);
});
}
function get_tokenInput_values(){
var field_value = field.val();
if(field_value){
var value = field_value.split("\n");
return $.map(value, function(val, idx){
return {name: val, id: val};
});
}
}
}
}

dynamic.on("change", function(evt){
check_is_dynamic($(this));
});


field.tokenInput([], {
theme: "facebook",
tokenValue: "name",
hintText: "Type in a year or a range of years (e.g. 1999-2005).",
searchingText: "",
noResultsText: "",
tokenDelimiter: "\n",
onReady: function(){
check_is_dynamic(dynamic);
},
onAdd: function(item) {
var context = field[0];
var current_tags = context.value.split('\n');
var new_tag_name = item.name;
if (current_tags.indexOf(new_tag_name) === -1) {
context.value += "\n" + new_tag_name;
}
},
onDelete: function(item) {
var context = field[0];
var tokens = context.value.split('\n');
var item_name = item.name;
var i, length, token, output = [];
for (i = 0, length = tokens.length; i < length; i += 1) {
token = tokens[i];
if (token !== item_name) {
output.push(token);
}
}
context.value = output.join('\n');
},
prePopulate: get_tokenInput_values(),
allowNewTokens: true
});

function check_isNaN(value, keycode){
if(numeric_keypad.indexOf(keycode) != -1){
return false;
}
if(value === " "){
return true;
} else {
return isNaN(value);
}
}

$("#token-input-temporalCoverage").on("keydown", function(evt){
var pressed = String.fromCharCode(evt.keyCode);
var field_value = evt.srcElement.value.trim();


if(skip_keys.indexOf(evt.keyCode) === -1){
if(field_value.length === 4){
if(check_isNaN(pressed, evt.keyCode)){
return false;
} else {
evt.srcElement.value = field_value + "-";
}
function check_is_dynamic(elem){
var tokenInput = $("ul.token-input-list-facebook");
var field_value = field.val();
if(elem.is(":checked")){
tokenInput.hide();
old_value = field_value !== "-1" ? field_value : "";
field.val("-1");
} else {
tokenInput.show();
if(old_value || field.val() === "-1"){
field.tokenInput("clear");
field.val(old_value);
var token_values = get_tokenInput_values();
if(token_values){
$.each(token_values, function(i, o){
field.tokenInput("add", o);
});
}
}
}
}
else if(check_isNaN(pressed, evt.keyCode)) {
return false;

dynamic.on("change", function(evt){
check_is_dynamic($(this));
});
// 92877 check if tokenInput is available
if (field.tokenInput) {
field.tokenInput([], {
theme: "facebook",
tokenValue: "name",
hintText: "Type in a year or a range of years (e.g. 1999-2005).",
searchingText: "",
noResultsText: "",
tokenDelimiter: "\n",
onReady: function(){
check_is_dynamic(dynamic);
},
onAdd: function(item) {
var context = field[0];
var current_tags = context.value.split('\n');
var new_tag_name = item.name;
if (current_tags.indexOf(new_tag_name) === -1) {
context.value += "\n" + new_tag_name;
}
},
onDelete: function(item) {
var context = field[0];
var tokens = context.value.split('\n');
var item_name = item.name;
var i, length, token, output = [];
for (i = 0, length = tokens.length; i < length; i += 1) {
token = tokens[i];
if (token !== item_name) {
output.push(token);
}
}
context.value = output.join('\n');
},
prePopulate: get_tokenInput_values(),
allowNewTokens: true
});
}

if(field_value.length === 9){
return false;
function check_isNaN(value, keycode){
if(numeric_keypad.indexOf(keycode) != -1){
return false;
}
if(value === " "){
return true;
} else {
return isNaN(value);
}
}
}
});

});
$("#token-input-temporalCoverage").on("keydown", function(evt){
var pressed = String.fromCharCode(evt.keyCode);
var field_value = evt.srcElement.value.trim();


if(skip_keys.indexOf(evt.keyCode) === -1){
if(field_value.length === 4){
if(check_isNaN(pressed, evt.keyCode)){
return false;
} else {
evt.srcElement.value = field_value + "-";
}
}
else if(check_isNaN(pressed, evt.keyCode)) {
return false;
}

if(field_value.length === 9){
return false;
}
}
});

});
})(jQuery);

0 comments on commit e119faa

Please sign in to comment.