Tue Dec 27 2016
Copied to clipboard! Copy reply
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
<template>
  <div>
    <div class="ui modal" data-modal-mastheadimages>
      <i class="close icon"></i>
      <div class="header">
        <i class="image large icon"></i>
        Upload Masthead Images 
      </div>    
      <div class="content">        
        <h3>
          <i class="warning circle icon"></i>
          Your files <b>must</b> be named in the following format: 
          <span style="color: cornflowerblue">template name</span>&mdash;
          <span style="color: cadetblue">device</span>&mdash;
          <span style="color: salmon">filename</span>
        </h3>
        <div class="ui basic segment">          
          <ul class="ui bulleted list">
            <li>
              Example:&nbsp;&nbsp;&nbsp;
              <span style="color: cornflowerblue">sbsr</span>&mdash;
              <span style="color: cadetblue">desktop</span>&mdash;
              <span style="color: salmon">xmas199.jpg</span>
            </li>
          </ul>
        </div>
        
          <div class="field">
            <p class="ui basic segment">
              <input type="file" name="masthead-files" multiple @change='handleFilesChange'>
            </p>
          </div>             
        <div class="ui warning message" v-if='exists.length'>          
          <div>
            One or more files exist with the same name. Would you like to overwrite?
          </div>
          <table class="ui fixed table">
            <thead>
              <tr>
                <th>Name</th>
                <th>Overwrite?</th>
                <th>Modified Date</th>              
              </tr>
            </thead>
            <tbody>
              <tr v-for="file in exists">
                <td>{{ file.name }}</td>
                <td>
                  <div class="ui slider checkbox" name='overwrite-masthead-toggle'>
                    <input type="checkbox" tabindex="0" class="hidden">
                    <label></label>
                  </div>
                </td>
                <td>{{ file.modified }}</td>              
              </tr>
            </tbody>
          </table>
          
        </div>
      </div>
      <div class="actions">      
        <div class="ui positive right labeled icon button" @click="handleSubmit">
          Upload
          <i class="checkmark icon"></i>
        </div>
      </div>      
    </div>  
  </div>
</template>

<script>

var $mastheadModal

export default {
  name: 'MastheadModal',
  data() {
    return {
      files: [],
      exists: []
    }
  },
  mounted() {
    const store = this.$store
    $mastheadModal = $('div[data-modal-mastheadimages]')
    $mastheadModal.modal({
      onApprove() {        
        return false
      },
      onHidden() {
        store.dispatch('toggleMastheadModal')
        return false
      }
    })
  },
  computed: {
    mastheadModal() {
      return this.$store.state.modal_masthead.show
    }
  },
  watch: {
    mastheadModal(on) {
      on = on ? 'show' : 'hide'      
      $mastheadModal.modal(on)      
    }
  },
  updated() {
    console.log('updated!')
    $('div[name=overwrite-masthead-toggle]').checkbox()
  },
  methods: {    
    handleFilesChange(e) {
      var files = [...document.querySelector('input[name=masthead-files]').files]
      .map(f => {        
        var modifiedDate = f.lastModifiedDate.toString().split(' ').splice(0,4).join(' ')        
        return {name:f.name, modified: modifiedDate}
      })
      this.exists = files.filter( f => this.$store.state.mastheadImages.indexOf(f.name))
    },
    handleSubmit(e) {
      const formdata = new FormData()
      const files = [...document.querySelector('input[name=masthead-files]').files]
      const request = new XMLHttpRequest()
      
      files.forEach(file => formdata.append('files[]', file, file.name))
      
      request.open('POST', `${window.server}/upload-masthteads`, true)
      request.send(formdata)
      request.onload = function() {
        if (request.status == 404) {
          console.log("Can't reach server")
          return
        }
        var data = JSON.parse(request.responseText)
        if (data.result == 0) {            
          console.log('error')
        } else {          
          console.log('success')
        }
      }
    }
  } 
}

</script>