Skip to content

Commit

Permalink
Dev: Trying to figure out labels.js (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Mar 2, 2018
1 parent 9060f0f commit e85984f
Showing 1 changed file with 54 additions and 18 deletions.
72 changes: 54 additions & 18 deletions assets/scripts/admin/labels.js
Expand Up @@ -89,30 +89,55 @@ $(document).on('ready pjax:scriptcomplete', function(){
});

/**
* @todo
* @param {array} lsrows
* @return {string} ';' or '\t'
*/
function quickaddfunction() {
function get_separator(lsrows) {
var separatorchar = "\t";
if (lsrows[0].indexOf("\t") == -1) {
separatorchar = ';';
}
return separatorchar;
}

/**
* Returns true if replace button is used (instead of add).
* @param {object} that
* @return {boolean}
*/
function get_replace(that) {
var lsreplace = false;
if ($(this).attr('id') == 'btnqareplace') {
if ($(that).attr('id') == 'btnqareplace') {
lsreplace = true;
}
return lsreplace;
}

/**
* Use-cases to deal with:
* 1) User enters one part on each line, e.g. "label text one".
* 2) User enters two parts on each line, "L001; label text one".
* 3) User enters label code plus a part for each language, "L001; label text one; gesundheit".
* 4) User enters a language part too much (more parts than language tabs).
*/
function quickaddfunction() {
var lsreplace = get_replace(this);

if (lsreplace) {
$(".answertable tbody>tr").remove();
}

// NB: lsrows is all the lines from the quick add modal.
var lsrows = $('#quickaddarea').val().split("\n");

var separatorchar = "\t";
if (lsrows[0].indexOf("\t") == -1) {
separatorchar = ';';
}
var separatorchar = get_separator(lsrows);

console.log('lsrows', lsrows);
$(lsrows).each(function(index, row) {
var code = undefined;

// NB: params is the ';' or '\t' split parts of each line.
// E.g. line "a;s" will have params ["a", "s"].
// A line "L001;English text;Deutsch text" will put label code
// L001, and expect an English and a German language tab.
var params = row.split(separatorchar);
var k = 0;
if (params.length > $(".lslanguage").length){
Expand All @@ -123,7 +148,9 @@ function quickaddfunction() {
var event = {};
event.target = $(".btnaddanswer:last");
var retcode = add_label(event);
//var retcode = add_label();

/*
if (lsreplace) {
if (index!=0|| (!lsreplace && $("div[id^='newedit']:not(:last) tbody>tr").length > 0)) {
event = {};
Expand All @@ -132,23 +159,28 @@ function quickaddfunction() {
retcode = add_label();
}
}
*/

// seems always undefined
/*
// TODO: Seems always undefined?
if (typeof(code) != "undefined") {
$("#code_"+retcode).val(code);
}
*/

// TODO: What does this do?
$(".lslanguage").each(function(i) {
//console.log("input[name=title_"+$(this).val()+"_"+retcode+"]");
//console.log('i', i);
$("input[name=title_" + $(this).val() + "_" + retcode + "]").val(params[k]);
if (typeof(code) != "undefined" && i > 0) {
$("#row_" + $(this).val() + "_" + retcode + " td:first").text(code);
}
//if (typeof(code) != "undefined" && i > 0) {
//$("#row_" + $(this).val() + "_" + retcode + " td:first").text(code);
//}
k++;
});
});

// Clear and hide modal.
$("#quickaddarea").val('');
$('#quickadd').modal('hide');
}
Expand Down Expand Up @@ -215,6 +247,7 @@ function sync_label(event)
*/
function get_next_code(event) {
var next_code;

if (event) {
if ($(event.target).closest('tr').find('.codeval').size()>0) {
next_code = getNextCode($(event.target).closest('tr').find('.codeval').val());
Expand All @@ -237,13 +270,15 @@ function get_next_code(event) {
* @return {string} Random id.
*/
function add_label(event) {
console.trace();
var next_code = get_next_code(event);
var html = createNewLabelTR(true,true);
var row_id;

if (typeof(event) == "undefined") {
row_id = -1;
} else {
console.log('event.target', event.target);
row_id = $(event.target)
.parent()
.parent()
Expand All @@ -263,11 +298,11 @@ function add_label(event) {
html = str_replace("###next###",randomid,html);
html = str_replace("###lang###",$("#lslanguagemain").val(),html);

if (typeof(event) == "undefined") {
//if (typeof(event) == "undefined") {
$(".first tbody").append(html);
} else {
$(event.target).parent().parent().after(html);
}
//} else {
//$(event.target).parent().parent().after(html);
//}
html = createNewLabelTR(true,false);

html = str_replace("###assessmentval###",'0',html);
Expand All @@ -292,13 +327,14 @@ function add_label(event) {
}

/**
* Deletes a label row?
* @param {object} event
*/
function del_label(event) {
event.preventDefault();
var $sRowID = $(event.target).parent().parent().attr('id');

$aRowInfo=$sRowID.split('_');// first is row, second langage and last the row number
$aRowInfo = $sRowID.split('_');// first is row, second langage and last the row number
$(".tab-pane").each(function(divindex,divelement){
var div_language = $(".lslanguage",divelement).val();

Expand Down

0 comments on commit e85984f

Please sign in to comment.