src/cm/media/js/lib/flexible-js-formatting/combo-box/combo-box.js
changeset 0 40c8f766c9b8
equal deleted inserted replaced
-1:000000000000 0:40c8f766c9b8
       
     1 /*
       
     2  * Copyright (C) 2004 Baron Schwartz <baron at sequent dot org>
       
     3  *
       
     4  * This program is free software; you can redistribute it and/or modify it
       
     5  * under the terms of the GNU Lesser General Public License as published by the
       
     6  * Free Software Foundation, version 2.1.
       
     7  *
       
     8  * This program is distributed in the hope that it will be useful, but WITHOUT
       
     9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    10  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    11  * details.
       
    12  */
       
    13 
       
    14 var key;
       
    15 var combo;
       
    16 
       
    17 document.onkeydown = function(e) {
       
    18     if (combo && combo.editing && window.event && window.event.keyCode == 8) {
       
    19         window.event.cancelBubble = true;
       
    20         window.event.returnValue = false;
       
    21         if (combo.insertSpace) {
       
    22             combo.insertSpace = false;
       
    23         }
       
    24         else {
       
    25             with (combo.options[combo.options.length - 1]) {
       
    26                 text = text.substring(0, text.length - 1);
       
    27             }
       
    28         }
       
    29     }
       
    30 }
       
    31 
       
    32 function edit(e) {
       
    33     if (window.event){
       
    34         key = window.event.keyCode;
       
    35         combo = window.event.srcElement;
       
    36         // Stop the browser from scrolling through <option>s
       
    37         window.event.cancelBubble = true;
       
    38         window.event.returnValue = false;
       
    39     }
       
    40     else if (e) {
       
    41         key = e.which;
       
    42         combo = e.target;
       
    43     }
       
    44     else {
       
    45         return true;
       
    46     }
       
    47 
       
    48     if (key == 13 || key == 8 || (key > 31 && key < 127)) {
       
    49         if (combo.editing && key == 13) {
       
    50             // Done editing
       
    51             combo.editing = false;
       
    52             combo = null;
       
    53             return false;
       
    54         }
       
    55         else if (!combo.editing) {
       
    56             combo.editing = true;
       
    57             combo.options[combo.options.length] = new Option("");
       
    58         }
       
    59 
       
    60         // Normal key
       
    61         if (key > 32 && key < 127) {
       
    62             with (combo.options[combo.options.length - 1]) {
       
    63                 if (combo.insertSpace) {
       
    64                     combo.insertSpace = false;
       
    65                     text = text + " " + String.fromCharCode(key);
       
    66                 }
       
    67                 else {
       
    68                     text = text + String.fromCharCode(key);
       
    69                 }
       
    70             }
       
    71         }
       
    72         // The backspace key
       
    73         else if (key == 8 && combo.options[combo.options.length - 1].text.length) {
       
    74             if (combo.insertSpace) {
       
    75                 combo.insertSpace = false;
       
    76             }
       
    77             else {
       
    78                 with (combo.options[combo.options.length - 1]) {
       
    79                     text = text.substring(0, text.length - 1);
       
    80                 }
       
    81             }
       
    82         }
       
    83         // Space key requires special treatment; some browsers will not append a space
       
    84         else if (key == 32) {
       
    85             combo.insertSpace = true;
       
    86         }
       
    87         combo.selectedIndex = combo.options.length - 1;
       
    88         return false;
       
    89     }
       
    90 }