MediaWiki:Common.js

From John's wiki
Revision as of 01:18, 27 January 2022 by Sixsigma (talk | contribs)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */

//console.log( 'Hi from Common.js' );

for ( var i in document.jj5_totals ) {

  var spec = document.jj5_totals[ i ];

  sum_column( spec.id, spec.col );

}

for ( var i in document.jj5_options ) {

  var spec = document.jj5_options[ i ];

  var parts_total = 0;

  for ( var j in spec.parts ) {

    var part = spec.parts[ j ];
    var sum_id = part + '_sum';
    var sum = read_float( document.getElementById( sum_id ).innerHTML );
    if ( isNaN( sum ) ) { continue; }
    parts_total += sum;

  }

  var options_total = parts_total;

  for ( var j in spec.options ) {

    var option = spec.options[ j ];
    var sum_id = option + '_sum';
    var sum = read_float( document.getElementById( sum_id ).innerHTML );
    if ( isNaN( sum ) ) { continue; }
    options_total += sum;

  }

  var ul = document.getElementById( 'jj5_options' );

  var parts_label = parts_total.toLocaleString('en-AU',{currency:'AUD',minimumFractionDigits:2});
  var options_label = options_total.toLocaleString('en-AU',{currency:'AUD',minimumFractionDigits:2});

  var line = spec.label + ': $' + parts_label;

  if ( spec.options ) { line += ' (with options: $' + options_label + ')'; }

  var li = document.createElement('li');
  li.appendChild(document.createTextNode(line));
  ul.appendChild(li);

}

function sum_column( id, col ) {
  //console.log( [ id, col ] );
  var total = 0;
  var mytable = document.getElementById( id );
  for ( var v=1; v < mytable.rows.length - 1; v++ ) {
    var val = read_cell( mytable, v, col );
    var ordered = read_cell( mytable, v, col + 2 );
    if ( ordered ) { continue; }
    //console.log( val );
    val = val.replace( '$', '' );
    val = val.replace( ',', '' );
    val = parseFloat( val );
    if ( isNaN( val ) ) { continue; }
    //console.log( val );
    total += val;
  }
  //console.log( total );
  total = total.toLocaleString('en-AU',{currency:'AUD',minimumFractionDigits:2});
  document.getElementById( id + '_sum' ).innerHTML = total;
}

function read_cell( table, row, col ) {

  try {
    return table.rows[row].cells[col].innerHTML.trim();
  }
  catch ( ex ) {
    return null;
  }
}

function read_float( input ) {
  var val = input.replace( '$', '' );
  val = val.replace( ',', '' );
  return parseFloat( val );
}