Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div style="margin-bottom: 30px; height: 28px; text-align: center;">
  <script src="https://buttons.github.io/buttons.js"></script>
  <a class="github-button" href="https://github.com/bookchiq" data-size="large" data-show-count="true" aria-label="Follow @bookchiq on GitHub" target="_blank">Follow @bookchiq</a>
</div>
  
  <div class="container">
    <form action="" id="form">
      <div class="panel panel-default">
        <div class="panel-heading">
          <a href="https://habitica.com/" target="_blank">Habitica</a> - Batch create sub-tasks
        </div>
        <div class="panel-body">
          <!-- api user -->
          <div class="form-group">
            <label class="control-label">
              User ID
            </label>
            <input type="text" id="api-user" class="form-control">
          </div>
          <!-- api token -->
          <div class="form-group">
            <label class="control-label">
              API Token
            </label>
            <input type="text" id="api-token" class="form-control">
          </div>
          <p>
            <i class="glyphicon glyphicon-question-sign text-danger"></i>
            <a href="https://habitica.com/user/settings/api" class="text-warning" target="_blank">
              How to find out my User ID and API Token? - Settings / API
            </a>
          </p>
          
          <hr>

          <!-- task type -->
          <div class="form-group">
            <label class="control-label">Task Type</label>
            <div>
              <label class="radio-inline">
                <input type="radio" name="task-type" id="inlineRadio2" value="todo" checked> To-Do
              </label>
              <label class="radio-inline">
                <input type="radio" name="task-type" id="inlineRadio1" value="daily"> Daily
              </label>
            </div>
          </div>

          <!-- task title -->
          <div class="form-group">
            <label class="control-label">Task Title</label>
            <input type="text" id="task-title" class="form-control">
          </div>

          <!-- task items -->
          <div class="form-group">
            <label class="control-label">Checklist</label>
            <textarea class="form-control" id="checklist" rows="5" placeholder="first item
second item
third item
...
                                                                 "></textarea>
            <p class="help-inline">Sub-tasks, one item per row.</p>
          </div>
        </div>
        <div class="panel-footer">
          <button class="btn btn-primary">Create</button>
          <span id="message"></span>
        </div>
    </form>
  </div>
              
            
!

CSS

              
                body {
  padding: 30px;
}

#message {
  margin-left: 10px;
}
              
            
!

JS

              
                // https://stackoverflow.com/a/105074/260793
var createTask, getTaskData, getUserData, guid, handleFormSubmit, initialize, isBlank, makeChecklist, makeItem, restoreUserData, s4, showMessage, validateTaskData;

guid = function() {
  return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
};

s4 = function() {
  return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
};

showMessage = function(msg, level) {
  if (msg) {
    return $('#message').html(`<span class="text-${level}">${msg}</span>`);
  } else {
    return $('#message').html('Creating...', 'info');
  }
};

getUserData = function() {
  var token, user, userData;
  userData = {};
  token = _.trim($('#api-token').val());
  if (token) {
    userData['x-api-key'] = localStorage['habitica-api-token'] = token;
  } else {
    throw new Error('Api token is required');
  }
  user = _.trim($('#api-user').val());
  if (user) {
    userData['x-api-user'] = localStorage['habitica-api-user'] = user;
  } else {
    throw new Error('User ID is required');
  }
  return userData;
};

restoreUserData = function() {
  $('#api-user').val(localStorage['habitica-api-user']);
  return $('#api-token').val(localStorage['habitica-api-token']);
};

makeItem = function(text) {
  return {
    id: guid(),
    text: text,
    completed: false
  };
};

isBlank = function(text) {
  return !_.trim(text);
};

makeChecklist = function() {
  var text;
  text = $('#checklist').val();
  return _.chain(text).split('\n').reject(isBlank).map(makeItem).value();
};

getTaskData = function() {
  var task;
  return task = {
    type: $('[name=task-type]:checked').val(),
    text: _.trim($('#task-title').val()),
    checklist: makeChecklist()
  };
};

validateTaskData = function(taskData) {
  if (!taskData.text) {
    throw new Error('task title is required');
  }
  if (taskData.checklist.length === 0) {
    throw new Error('checklist is required');
  }
};

resetTaskForm = function() {
  $('#task-title').val('')
  $('#checklist').val('')
};

createTask = function(userData, taskData) {
  return $.ajax({
    url: 'https://habitica.com/api/v4/tasks/user',
    method: 'POST',
    headers: userData,
    data: taskData,
    success: function() {
      resetTaskForm()
      return showMessage('Task created successfully! Now refresh your task list.', 'success');
    },
    error: function(xhr, status, err) {
      showMessage('Task failed to create.', 'danger');
      return console.error(err);
    }
  });
};

handleFormSubmit = function(evt) {
  var err, taskData, userData;
  evt.preventDefault();
  try {
    userData = getUserData();
    taskData = getTaskData();
    validateTaskData(taskData);
    createTask(userData, taskData);
    showMessage();
  } catch (error) {
    err = error;
    showMessage(err.message, 'danger');
  }
  return false;
};

initialize = function() {
  restoreUserData();
  return $('#form').submit(handleFormSubmit);
};

$(document).ready(initialize);

              
            
!
999px

Console