JAVASCRIPT   68

BlockArg

Guest on 9th July 2022 06:19:54 PM

  1. /*
  2. Argument types
  3.  
  4. n: numbers
  5. t: text
  6. m: regular menu with icons
  7. o: image menu and input type
  8.  
  9. */
  10. var BlockArg = function(block) {
  11.         this.daddy = block;
  12.   this.type = 'blockarg';
  13.   this.argType = ((Number(block.value).toString() != 'NaN') && (block.value != '')) ?  'n' : 't';
  14.   switch (this.argType){
  15.         case 'n':
  16.                 this.argValue =  block.value;
  17.                 this.div = this.addNumArg();   
  18.                 break;
  19.         case 't':
  20.                 this.argValue =  block.value;
  21.                 this.div = this.addTextArg();  
  22.                 break;
  23.         default:
  24.                         console.log ("missing blockarg for:", this.argType, this.daddy.blocktype);
  25.                         break;
  26.   }
  27. }
  28.  
  29. BlockArg.prototype.addNumArg = function() {
  30.         var str = this.argValue.toString();
  31.         return this.addArgument(str, false);
  32. }
  33.  
  34. BlockArg.prototype.addTextArg = function() {
  35.         var str = this.argValue.toString();
  36.         return this.addArgument(str, true);
  37. }
  38.  
  39. BlockArg.prototype.addArgument = function(str, isText) {
  40.   var sform = newHTML("form", "argform", this.daddy.div);
  41.         var ti = newHTML("input", getType(this.daddy, isText), sform);  
  42.   ti.type = this.getFormInputType();
  43.   ti.value = str;
  44.   if (isText) this.setTextEvents(ti);
  45.   else this.setNumberEvents(ti);
  46.   ti.onkeypress =function(evt){handleKeyPress(evt)}
  47.   sform.onsubmit= function(evt){submitOverride(evt)}
  48.   this.arg = sform;
  49.         function handleKeyPress(e){
  50.         var key=e.keyCode || e.which;
  51.         if (key==13) submitOverride(e)
  52.         }
  53.         function submitOverride(e){
  54.         e.preventDefault();
  55.         var input =e.target;
  56.         input.blur();
  57.                 }
  58.         return sform;
  59.         function getType(b, isText){
  60.                 if (!isText) return "numinput";
  61.                 if (b.blocktype.indexOf("box") < 0) return "textinput";
  62.                 if (b.isReporter) return "textinput rbox";
  63.                 return  "textinput box";
  64.         }
  65. }
  66.  
  67. BlockArg.prototype.setTextEvents = function (ti){
  68.   var sform = ti.parentNode;
  69.   var me = this;
  70.   ti.maxLength =  40;
  71.   ti.onfocus = handleTextFieldFocus;
  72.   ti.onblur = handleTextFieldBlur;
  73.   var me = this;
  74.   function handleTextFieldBlur (e){
  75.                 var sform = ti.parentNode;
  76.                 me.argValue = ti.value;
  77.         TA.activeFocus = undefined;  
  78.         }
  79.  
  80.         function handleTextFieldFocus(e){
  81.                 var sform = ti.parentNode;
  82.         if (Events.dragged) ti.blur();
  83.         else {
  84.                 var p = me.daddy.div.parentNode;
  85.                         p.appendChild(me.daddy.div);
  86.                 TA.activeFocus = ti;
  87.                 }
  88.         }
  89. }
  90.  
  91. BlockArg.prototype.setNumberEvents = function (ti){
  92.   var me = this;
  93.         ti.maxLength =  3;
  94.   ti.onfocus = handleNumFieldFocus;
  95.   function handleNumFieldFocus(e){
  96.         if (Events.dragged) { ti.blur(); return;}
  97.     TA.activeFocus = ti
  98.     if (!isTablet) ti.onmouseup =  function(evt){ti.select();}
  99.    }
  100.   ti.onkeydown = handleNumKey;
  101.   ti.onkeyup = function(evt){validateNumber(ti);}
  102.   ti.onblur =handleNumFieldBlur;
  103.  
  104.   function handleNumFieldBlur(e){
  105.     TA.activeFocus = undefined;
  106.     validateNumber(ti);  
  107.     me.argValue = ti.value;
  108.     ti.onmouseup = undefined;
  109.     }
  110.         function validateNumber(ti) {
  111.     var p = ti.parentNode.parentNode.owner;
  112.     var currentval =ti.value - 0;
  113.     if ((p.min!=null) &&(p.min > currentval)) ti.value = p.min;
  114.     if ((p.max != null)  && (p.max < currentval)) ti.value = p.max;
  115.     if (ti.value.length > 1)  ti.value =(String(Number (ti.value)) == 'NaN') ?  ti.value[0] : Number (ti.value);
  116.    
  117.     }
  118.         function handleNumKey(e){
  119.         var resvedkeys= [8,13, 37,38,39,40];
  120.         var key=e.keyCode || e.which;
  121.         if(key==189) key=45;
  122.         if (resvedkeys.indexOf(key) > -1) return;
  123.         var c = String.fromCharCode(key);
  124.         if ('01234567890-.'.indexOf(c)== -1) e.preventDefault();
  125.   }
  126. }
  127.  
  128.  BlockArg.prototype.getFormInputType = function (){
  129.   if (!isTablet) return "text";
  130.   if (this.argType == 'n') return  "number";
  131.   return "text";
  132. }
  133.  
  134. BlockArg.prototype.isText = function () { return (this.argType != 'n');}

Raw Paste


Login or Register to edit or fork this paste. It's free.