0

I am writing an extension for a text-editor (Brackets) that can generate HTML and append libraries automatically in the HTML.

I have an Object called 'choice'.

This modal requests the users input: Imgur

choice grabs the user's input by defining methods on choice

partial JS here:

var choice = new Object();

    choice.language = function () {
        //Buid HTML top 'head'
        var htmlTop = "<!DOCTYPE html>" + "<html>" + "<head lang='";
        //Grab Selected Language Type
        var languageChoice = document.getElementById("languages").value;
        //Determine what Selected Language type is and complete HTML 'head'
        if (languageChoice === "english") {
          languageChoice = "en";
          return htmlTop + languageChoice + "'>";
        } else if (languageChoice === "german") {
          languageChoice = "de";
          return htmlTop + languageChoice + "'>";
        } else if (languageChoice === "spanish") {
          languageChoice = "es";
          return htmlTop + languageChoice + "'>";
        } else if (languageChoice === "french") {
          languageChoice = "fr";
          return htmlTop + languageChoice + "'>";
        } else if (languageChoice === "italian") {
          languageChoice = "it";
          return htmlTop + languageChoice + "'>";
        } else if (languageChoice === "chinese") {
          languageChoice = "zh-cn";
          return htmlTop + languageChoice + "'>";
        }
      }; //end choice.language

    choice.charset = function () {
        //Build meta and the rest of the 'head tag'
        var htmlCharset_Beginning = "<meta charset='";
        var htmlCharset_End = "'>" + "<title> -Insert Title- </title>" + "<!-- Insert CSS links below -->" + "</head>" + "<body>";
        var charsetChoice = document.getElementById("charset").value;
        if (charsetChoice === "utf8") {
          charsetChoice = "UTF-8";
          return htmlCharset_Beginning + charsetChoice + htmlCharset_End;
        } else {
          charsetChoice = "UTF-16";
          return htmlCharset_Beginning + charsetChoice + htmlCharset_End;
        }
      }; // end choice.charset

    choice.doctype = function () {
      var doctypeChoice = document.getElementById("doctype").value;
      return doctypeChoice;
    }; // end doctype

    choice.libraries = function () {
      var checkedBoxes = getCheckedBoxes("lib_checkboxes");
      checkedBoxes.forEach(function(item){
      var scripts =+ $(item).data('script');

   });//End forEach
      var bottomHTML = scripts + "</body>" + "</html>";
      return bottomHTML;
    }; //End choice.libraries


    var chosenTemplate = function(){
    var template = choice.language() + choice.charset() + choice.libraries();

      // insert html into file, this will overwrite whatever content happens to be there already
      EditorManager.getCurrentFullEditor()._codeMirror.setValue(template);

      // automatically close the modal window
      $('#templates_modalBtn').click();
    };

    //Get checkedBoxes function
    // Pass the checkbox name to the function
    function getCheckedBoxes(chkboxName) {
      var checkboxes = document.getElementsByName(chkboxName);
      var checkboxesChecked = [];
      // loop over them all
      for (var i = 0; i < checkboxes.length; i++) {
        // And stick the checked ones onto an array...
        if (checkboxes[i].checked) {
          checkboxesChecked.push(checkboxes[i]);
        }
      }
      // Return the array if it is non-empty, or null
      return checkboxesChecked.length > 0 ? checkboxesChecked : null;
    }


  } // End action();

  //JEFF STOP CODING HERE

  // Register the commands and insert in the File menu
  CommandManager.register(Strings.MENU_COMMAND, 'templates', action);
  var menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
  menu.addMenuDivider();
  menu.addMenuItem('templates');

}); //end define;

QUESTION:

Can I save multiple methods (each method returns a string) as a variable?

Example here:

  var chosenTemplate = function(){
    var template = choice.language() + choice.charset() + choice.libraries();

      // insert html into file, this will overwrite whatever content happens to be there already
      EditorManager.getCurrentFullEditor()._codeMirror.setValue(template);

      // automatically close the modal window
      $('#templates_modalBtn').click();
    };

My code is loading with no errors, but its not executing at all so I am trying to debug and figure out what is going wrong...

14
  • choice.libraries() isn't actually returning anything, by the way. Commented Sep 6, 2015 at 1:24
  • How can I make it return something? In my code I have it as 'return bottomHTML' Commented Sep 6, 2015 at 1:25
  • I fixed it I think, you can look at it now again please @Purag Commented Sep 6, 2015 at 1:27
  • 1
    You should really learn to work with objects. A lot of your code can be made a lot cleaner and less prone to errors. I would submit it for codereview.stackexchange.com if I were you. Fixing your frail logic would probably fix whatever doubts you're having as well. Commented Sep 6, 2015 at 2:09
  • 1
    Just to show you an example of what you could do instead, here's the top part of your code pretty much reduced by half: jsbin.com/rutawewiza/edit?js Just to give you an idea. Commented Sep 6, 2015 at 2:45

1 Answer 1

2

Before you realize the function 'chosenTemplate', you should check whether the document stream of the page has already downloaded. If it not, you may not be able to get the value of the widget (empty).

Sign up to request clarification or add additional context in comments.

3 Comments

How would one do that? Theoretically? Thanks for the input!
Would wrapping my code in a $(document).ready(function(){}); do the same thing?
Yes, wrapping code in a $(document).ready(function(){}) may help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.