Alien-GvaScript

 view release on metacpan or  search on metacpan

test/functional/keyMap/basic.html  view on Meta::CPAN


function Up() {
  var cell = $('current_cell');
  var colIndex = cell.cellIndex;
  var row = cell.parentNode;
  if (row.rowIndex > 0) {
    cell.id = null;
    row.parentNode.rows[row.rowIndex - 1].cells[colIndex].id = "current_cell";
  }
}

function Down() {
  var cell = $('current_cell');
  var colIndex = cell.cellIndex;
  var row = cell.parentNode;
  if (row.rowIndex + 1 < row.parentNode.rows.length) {
    cell.id = null;
    row.parentNode.rows[row.rowIndex + 1].cells[colIndex].id = "current_cell";
  }
}

function Left() {
  var cell = $('current_cell');
  if (cell.cellIndex > 0) {
    cell.id = null;
    cell.parentNode.cells[cell.cellIndex - 1].id = "current_cell";
  }
}

function Right() {
  var cell = $('current_cell');
  if (cell.cellIndex + 1 < cell.parentNode.cells.length) {
    cell.id = null;
    cell.parentNode.cells[cell.cellIndex + 1].id = "current_cell";
  }
}

function fill_cell_from_key(event) {
  $('current_cell').innerHTML = event.keyName; 
  Event.stop(event);
}


function say(msg) {
  return function() {alert(msg)};
}


var keymap;
document.observe('dom:loaded', function() {
  var C_X_map = {A: say('A'),
                 B: say('B'),
                 R: GvaScript.KeyMap.Prefix({K: say('Ctrl-X R K'),
                                          O: say('Ctrl-X R O')})};

  var rules   = {UP:     Up, 
                 DOWN:   Down, 
                 LEFT:   Left, 
                 RIGHT:  Right, 
                 RETURN: Red,
                 27:     say('no escape from here'),

                 C_X: GvaScript.KeyMap.Prefix(C_X_map),

                 REGEX: [ ["",   /[0-9]/,             fill_cell_from_key],
                          ["C_", /^[aeiou]$/i,        fill_cell_from_key],
                          [null, "RETURN|TAB|ESCAPE", add_msg           ] ]};

  keymap = new GvaScript.KeyMap(rules);

  keymap.observe("keydown", 'table');

  $('table').focus();
});


function push_grab_all() {
  keymap.rules.push(GvaScript.KeyMap.MapAllKeys(add_msg));
}

function push_ignore_all() {
  keymap.rules.push(GvaScript.KeyMap.MapAllKeys(function () {}));
}

function push_navigate_divs() {
  var cur_div = 3;

  var tmp_rules = {
    UP : function () {
      if (cur_div > 1) {
        document.getElementById("d" + cur_div).style.background = "";
        cur_div -= 1;
        document.getElementById("d" + cur_div).style.background = "yellow";
      }
    },
    DOWN : function () {
      if (cur_div < 4) {
        document.getElementById("d" + cur_div).style.background = "";
        cur_div += 1;
        document.getElementById("d" + cur_div).style.background = "yellow";
      }
    },
    REGEX: [[null, '.*', function () {}]]
  }

  keymap.rules.push(tmp_rules);
}

function log_rules() {
  $A(keymap.rules).each(function(r, i) {
    r = $H(r);
    var msg = i + ': ';
    r.keys().each(function(k) {
      msg += ('<br/>\t<strong>'+k+ ':</strong> '+r.get(k)+'<br />');
    });
    log(msg);
  });
}

function log(msg) {
  $('logs').innerHTML += "<div class='msg'> >> "+msg+" </div>" ;



( run in 0.427 second using v1.01-cache-2.11-cpan-f5b5a18a01a )