create.js 1.12 KB
Newer Older
1 2 3 4 5 6
'use strict'

import $ from 'jquery'
import _ from 'lodash'
import { setInputSelection } from '../helpers/form'
import { makeSafePath } from '../helpers/pages'
NGPixel's avatar
NGPixel committed
7

8
// -> Create New Document
NGPixel's avatar
NGPixel committed
9

10 11
module.exports = (currentBasePath) => {
  let suggestedCreatePath = currentBasePath + '/new-page'
NGPixel's avatar
NGPixel committed
12

13 14 15 16 17 18
  $('.btn-create-prompt').on('click', (ev) => {
    $('#txt-create-prompt').val(suggestedCreatePath)
    $('#modal-create-prompt').toggleClass('is-active')
    setInputSelection($('#txt-create-prompt').get(0), currentBasePath.length + 1, suggestedCreatePath.length)
    $('#txt-create-prompt').removeClass('is-danger').next().addClass('is-hidden')
  })
NGPixel's avatar
NGPixel committed
19

20 21 22 23 24
  $('#txt-create-prompt').on('keypress', (ev) => {
    if (ev.which === 13) {
      $('.btn-create-go').trigger('click')
    }
  })
NGPixel's avatar
NGPixel committed
25

26 27 28 29 30 31 32 33 34 35
  $('.btn-create-go').on('click', (ev) => {
    let newDocPath = makeSafePath($('#txt-create-prompt').val())
    if (_.isEmpty(newDocPath)) {
      $('#txt-create-prompt').addClass('is-danger').next().removeClass('is-hidden')
    } else {
      $('#txt-create-prompt').parent().addClass('is-loading')
      window.location.assign('/create/' + newDocPath)
    }
  })
}