js/core/code.js
// Open popup to name and create a new file
Open popup to name and create a new file
Open popup to name and create a new file
function addNewFilePopup(newFile) {
    var fileType;
    switch(newFile.extension){
      case 'js':
        fileType = "JavaScript"
        break
      case 'xml':
        fileType = "Blocky"
        break
    }
    html =
        '<div class="addNewFilePopUp">' +
        '<input id="name' + fileType + 'File" type="text" placeholder="File name" autofocus/>' +
        '<button id="add' + fileType + 'File" class="popupBtn">Add ' + fileType + ' file</button>' +
        '<p id="errMsg"></p>' +
        '</div>';
    popup = Espruino.Core.App.openPopup({
      title: "Create new " + fileType + " file",
      contents: html,
      position: "center"
    });
    document.getElementById("add" + fileType + "File").addEventListener('click', function() {
      var newName = document.getElementById("name" + fileType + "File").value.trim();
      var errMsgHolder = document.getElementById("errMsg");
      if (newName == "" || newName.length == 0) {
        errMsgHolder.innerHTML = "Name is required";
        return false;
      } else if (!(/^\S{1,}$/.test(newName))) {
        errMsgHolder.innerHTML = 'Name cannot contain whitespace';
        return false;
      } else if(!(/^[a-zA-Z]+$/.test(newName))){
        errMsgHolder.innerHTML = 'Only alphabets allowed';
        return false;
      } else {
        errMsgHolder.innerHTML = '';
      }
      newFile.name = newName;
      Espruino.Core.File.setFilesArray(newFile);
      popup.close();  
    });
  }
function addNewFilePopup ( newFile ) { var fileType ; switch ( newFile . extension ) { case 'js' : fileType = "JavaScript" break case 'xml' : fileType = "Blocky" break } html = '<div class="addNewFilePopUp">' + '<input id="name' + fileType + 'File" type="text" placeholder="File name" autofocus/>' + '<button id="add' + fileType + 'File" class="popupBtn">Add ' + fileType + ' file</button>' + '<p id="errMsg"></p>' + '</div>' ; popup = Espruino . Core . App . openPopup ( { title : "Create new " + fileType + " file" , contents : html , position : "center" } ) ; document . getElementById ( "add" + fileType + "File" ) . addEventListener ( 'click' , function ( ) { var newName = document . getElementById ( "name" + fileType + "File" ) . value . trim ( ) ; var errMsgHolder = document . getElementById ( "errMsg" ) ; if ( newName == "" || newName . length == 0 ) { errMsgHolder . innerHTML = "Name is required" ; return false ; } else if ( ! ( / ^\S{1,}$ / . test ( newName ) ) ) { errMsgHolder . innerHTML = 'Name cannot contain whitespace' ; return false ; } else if ( ! ( / ^[a-zA-Z]+$ / . test ( newName ) ) ) { errMsgHolder . innerHTML = 'Only alphabets allowed' ; return false ; } else { errMsgHolder . innerHTML = '' ; } newFile . name = newName ; Espruino . Core . File . setFilesArray ( newFile ) ; popup . close ( ) ; } ) ; }
Open popup to name and create a new file
Open popup to name and create a new file
// Initializing popup // Add onClick logic to add file and close popup // Get input // Validate input // Set file name // file.js function // Close the popup