Difference between revisions of "MediaWiki:Common.js"

(Fix link)
Line 15: Line 15:
 
/* END */
 
/* END */
  
 +
/* Below here was copied from Grammar Wiki so may need to be changed to support any headers, etc. that are unique for the Vocab Wiki */
 +
 +
jQuery('#pinyin_sort').click();
 +
var rows, len, kids, settingsMenuLoc;
 +
 +
        //set cefr/hsk totals
 +
      if($('#cefr-total').length !== 0) {
 +
          //Set CEFR total
 +
          var total = $('.cefr-value').map(function(){
 +
              return Number($.trim($(this).text()));
 +
            }).get().reduce(function(a, b) { return a + b; }, 0);
 +
          $('#cefr-total').html(total);
 +
          //Set HSK total
 +
          total = $('.hsk-value').map(function(){
 +
              return Number($.trim($(this).text()));
 +
            }).get().reduce(function(a, b) { return a + b; }, 0);
 +
          $('#hsk-total').html(total);
 +
        }
 +
//convert keyword table to json
 +
if($('#json').length !== 0){
 +
$('#json').html("");
 +
$('#json').append("{");
 +
 +
//keywords
 +
rows = $('#keyword_table tbody tr');
 +
len = rows.length;
 +
$.each(rows,function(index,element){
 +
kids = $(this).children();
 +
$('#json').append('"'+kids[1].innerHTML+'": {' + '"pinyin": "' + kids[0].innerHTML +'", "count":'+kids[2].innerHTML.replace("Count: ","")+'}');
 +
if (index == len - 1)
 +
$('#json').append("<br/>");
 +
else
 +
$('#json').append(",<br/>");
 +
});
 +
 +
//translations
 +
rows = $('#translation_table tbody tr');
 +
len = rows.length;
 +
$.each(rows,function(index,element){
 +
kids = $(this).children();
 +
$('#json').append('"'+kids[0].innerHTML+'": {' + '"count":'+kids[1].innerHTML.replace("Count: ","")+'}');
 +
if (index == len - 1)
 +
$('#json').append("<br/>");
 +
else
 +
$('#json').append(",<br/>");
 +
});
 +
 +
//full grammar list
 +
$('.sortable tbody:first').append($('.sortable tbody')[1].innerHTML);
 +
$('.sortable tbody:first').append($('.sortable tbody')[2].innerHTML);
 +
rows = $('.sortable:first tbody tr');
 +
len = rows.length;
 +
if(len !== 0)
 +
$('#json').html("[");
 +
$.each(rows,function(index,element){
 +
kids = $(this).children();
 +
//Check "valid" Hash
 +
if(kids[3].innerHTML[0] === 'A') {
 +
var pos = kids[5].innerHTML.replace(/<br>/g, ",").replace(/"/g, "'");
 +
if(kids.length > 6) {
 +
if(kids[6].innerHTML)
 +
pos += "-"+$('.sortable thead:first th:nth-child(7)').text();
 +
if(kids[7].innerHTML)
 +
pos += "-"+$('.sortable thead:first th:nth-child(8)').text();
 +
if(kids[8].innerHTML)
 +
pos += "-"+$('.sortable thead:first th:nth-child(9)').text();
 +
if(kids[9].innerHTML)
 +
pos += "-"+$('.sortable thead:first th:nth-child(10)').text();
 +
if(kids[10].innerHTML)
 +
pos += "-"+$('.sortable thead:first th:nth-child(11)').text();
 +
 +
}
 +
if(kids[5].className === "Structure")
 +
kids[5].className = "Grammatical Structure"; //So that the alphabetical order in Caine stays the same as on the wiki
 +
$('#json').append(
 +
'{"hash":"'+kids[3].innerHTML+'","title": "'+kids[0].innerHTML.replace(/"/g, "'")+
 +
'","pattern": "'+kids[1].innerHTML.replace(/"/g, "'")+
 +
'","example":"'+kids[2].innerHTML.replace(/"/g, "'")+
 +
'","pos":"'+kids[5].className+"-"+pos+
 +
'","level":"'+kids[4].innerHTML+'" }'
 +
);
 +
if (index == len - 1)
 +
$('#json').append("<br/>");
 +
else
 +
$('#json').append(",<br/>");
 +
}
 +
});
 +
if(len !== 0)
 +
$('#json').append("]");
 +
else
 +
$('#json').append("}");
 +
 +
$('#keyword_table').hide();
 +
$('#translation_table').hide();
 +
$('.sortable').hide();
 +
if(!$('#pt-logout').length) {
 +
$('#json').hide();
 +
$('#bodyContent').append('<pre>Please log in</pre>');
 +
$('.sortable').remove();
 +
}
 +
}
 +
 +
//convert keyword table to tsv
 +
if($('#tsv_data').length !== 0){
 +
$('#tsv_data').html("");
 +
 +
//For Keywords
 +
rows = $('#keyword_table tbody tr');
 +
len = rows.length;
 +
$.each(rows,function(index,element){
 +
kids = $(this).children();
 +
$('#tsv_data').append(kids[1].innerHTML+' ' + kids[0].innerHTML +' '+kids[2].innerHTML.replace("Count: ","")+'<br/>');
 +
});
 +
$('#keyword_table').hide();
 +
 +
//For Translations
 +
rows = $('#translation_table tbody tr');
 +
len = rows.length;
 +
$.each(rows,function(index,element){
 +
kids = $(this).children();
 +
$('#tsv_data').append(kids[0].innerHTML+' ' +kids[1].innerHTML.replace("Count: ","")+'<br/>');
 +
});
 +
$('#translation_table').hide();
 +
 +
}
 
});
 
});

Revision as of 00:36, 17 October 2021

$(function() {

/* START Change DisplayTitle */
var cefr = $('#info-cefr').text();
var hsk = $('#info-hsk').text();
var link = $('#info-link').text();
var headerText = $('.allset-page-heading h1').text();
var header = $('.allset-page-heading h1');
if(cefr !== "") {
   header.html(headerText + "<span id='header-levs' style='float:right'><span id='header-cefr'><a href='https://resources.allsetlearning.com/chinese/vocabulary/" + cefr + "'>" + cefr + "</a></span>/<span id='header-hsk'><a href='https://resources.allsetlearning.com/chinese/vocabulary/" + hsk + "'>" + hsk + "</a></span><a href='/chinese/vocabulary/" + link + "' title='" + link + "'><img src='https://resources.allsetlearning.com/gramwiki/images/silk/link.png'></a></span>"); 
   // modify settings cog location so it aligns with the right-floated header items
   $('#settings-button').css('margin', '-0.5em 0 0 0.5em');
}

/* END */

/* Below here was copied from Grammar Wiki so may need to be changed to support any headers, etc. that are unique for the Vocab Wiki */

jQuery('#pinyin_sort').click();
	var rows, len, kids, settingsMenuLoc;

        //set cefr/hsk totals
       if($('#cefr-total').length !== 0) {
          //Set CEFR total
           var total = $('.cefr-value').map(function(){
               return Number($.trim($(this).text()));
            }).get().reduce(function(a, b) { return a + b; }, 0);
           $('#cefr-total').html(total);
           //Set HSK total
           total = $('.hsk-value').map(function(){
               return Number($.trim($(this).text()));
            }).get().reduce(function(a, b) { return a + b; }, 0);
           $('#hsk-total').html(total);
        }
	//convert keyword table to json
	if($('#json').length !== 0){
 		$('#json').html("");
		$('#json').append("{");

		//keywords
		rows = $('#keyword_table tbody tr');
		len = rows.length;
		$.each(rows,function(index,element){
			kids = $(this).children();
			$('#json').append('"'+kids[1].innerHTML+'": {' + '"pinyin": "' + kids[0].innerHTML +'", "count":'+kids[2].innerHTML.replace("Count: ","")+'}');
			if (index == len - 1)
				$('#json').append("<br/>");
			else
				$('#json').append(",<br/>");
		});

		//translations
		rows = $('#translation_table tbody tr');
		len = rows.length;
		$.each(rows,function(index,element){
			kids = $(this).children();
			$('#json').append('"'+kids[0].innerHTML+'": {' + '"count":'+kids[1].innerHTML.replace("Count: ","")+'}');
			if (index == len - 1)
				$('#json').append("<br/>");
			else
				$('#json').append(",<br/>");
		});

		//full grammar list
		$('.sortable tbody:first').append($('.sortable tbody')[1].innerHTML);
		$('.sortable tbody:first').append($('.sortable tbody')[2].innerHTML);
		rows = $('.sortable:first tbody tr');
		len = rows.length;
		if(len !== 0)
			$('#json').html("[");
		$.each(rows,function(index,element){
			kids = $(this).children();
			//Check "valid" Hash
			if(kids[3].innerHTML[0] === 'A') {
				var pos = kids[5].innerHTML.replace(/<br>/g, ",").replace(/"/g, "'");
				if(kids.length > 6) {
					if(kids[6].innerHTML)
						pos += "-"+$('.sortable thead:first th:nth-child(7)').text();
					if(kids[7].innerHTML)
						pos += "-"+$('.sortable thead:first th:nth-child(8)').text();
					if(kids[8].innerHTML)
						pos += "-"+$('.sortable thead:first th:nth-child(9)').text();
					if(kids[9].innerHTML)
						pos += "-"+$('.sortable thead:first th:nth-child(10)').text();
					if(kids[10].innerHTML)
						pos += "-"+$('.sortable thead:first th:nth-child(11)').text();

				}
				if(kids[5].className === "Structure")
					kids[5].className = "Grammatical Structure"; //So that the alphabetical order in Caine stays the same as on the wiki
				$('#json').append(
					'{"hash":"'+kids[3].innerHTML+'","title": "'+kids[0].innerHTML.replace(/"/g, "'")+
					'","pattern": "'+kids[1].innerHTML.replace(/"/g, "'")+
					'","example":"'+kids[2].innerHTML.replace(/"/g, "'")+
					'","pos":"'+kids[5].className+"-"+pos+
					'","level":"'+kids[4].innerHTML+'" }'
				);
				if (index == len - 1)
					$('#json').append("<br/>");
				else
					$('#json').append(",<br/>");
			}
		});
		if(len !== 0)
			$('#json').append("]");
		else
			$('#json').append("}");

		$('#keyword_table').hide();
		$('#translation_table').hide();
		$('.sortable').hide();
		if(!$('#pt-logout').length) {
			$('#json').hide();
			$('#bodyContent').append('<pre>Please log in</pre>');
			$('.sortable').remove();
		}
	}

	//convert keyword table to tsv
	if($('#tsv_data').length !== 0){
		$('#tsv_data').html("");

		//For Keywords
		rows = $('#keyword_table tbody tr');
		len = rows.length;
		$.each(rows,function(index,element){
			kids = $(this).children();
			$('#tsv_data').append(kids[1].innerHTML+'	' + kids[0].innerHTML +'	'+kids[2].innerHTML.replace("Count: ","")+'<br/>');
		});
		$('#keyword_table').hide();

		//For Translations
		rows = $('#translation_table tbody tr');
		len = rows.length;
		$.each(rows,function(index,element){
			kids = $(this).children();
			$('#tsv_data').append(kids[0].innerHTML+'	' +kids[1].innerHTML.replace("Count: ","")+'<br/>');
		});
		$('#translation_table').hide();

	}
});