-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
assets/javascripts/discourse/components/json-file-uploader.js.es6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
export default Ember.Component.extend({ | ||
fileInput: null, | ||
loading: false, | ||
expectedRootObjectName: null, | ||
hover: 0, | ||
|
||
classNames: ['json-uploader'], | ||
|
||
_initialize: function() { | ||
const $this = this.$(); | ||
const self = this; | ||
|
||
const $fileInput = $this.find('#js-file-input'); | ||
this.set('fileInput', $fileInput[0]); | ||
|
||
$fileInput.on('change', function() { | ||
self.fileSelected(this.files); | ||
}); | ||
|
||
$this.on('dragover', function(e) { | ||
if (e.preventDefault) e.preventDefault(); | ||
return false; | ||
}); | ||
$this.on('dragenter', function(e) { | ||
if (e.preventDefault) e.preventDefault(); | ||
self.set('hover', self.get('hover') + 1); | ||
return false; | ||
}); | ||
$this.on('dragleave', function(e) { | ||
if (e.preventDefault) e.preventDefault(); | ||
self.set('hover', self.get('hover') - 1); | ||
return false; | ||
}); | ||
$this.on('drop', function(e) { | ||
if (e.preventDefault) e.preventDefault(); | ||
|
||
self.set('hover', 0); | ||
self.fileSelected(e.dataTransfer.files); | ||
return false; | ||
}); | ||
|
||
}.on('didInsertElement'), | ||
|
||
accept: function() { | ||
return ".json,application/json,application/x-javascript,text/json" + (this.get('extension') ? "," + this.get('extension') : ""); | ||
}.property('extension'), | ||
|
||
setReady: function() { | ||
let parsed; | ||
try { | ||
parsed = JSON.parse(this.get('value')); | ||
} catch (e) { | ||
this.set('ready', false); | ||
return; | ||
} | ||
|
||
const rootObject = parsed[this.get('expectedRootObjectName')]; | ||
|
||
if (rootObject !== null && rootObject !== undefined) { | ||
this.set('ready', true); | ||
} else { | ||
this.set('ready', false); | ||
} | ||
}.observes('destination', 'expectedRootObjectName'), | ||
|
||
actions: { | ||
selectFile: function() { | ||
const $fileInput = $(this.get('fileInput')); | ||
$fileInput.click(); | ||
} | ||
}, | ||
|
||
fileSelected(fileList) { | ||
const self = this; | ||
let files = []; | ||
for (let i = 0; i < fileList.length; i++) { | ||
files[i] = fileList[i]; | ||
} | ||
const fileNameRegex = /\.(json|txt)$/; | ||
files = files.filter(function(file) { | ||
if (fileNameRegex.test(file.name)) { | ||
return true; | ||
} | ||
if (file.type === "text/plain") { | ||
return true; | ||
} | ||
return false; | ||
}); | ||
const firstFile = fileList[0]; | ||
|
||
this.set('loading', true); | ||
|
||
let reader = new FileReader(); | ||
reader.onload = function(evt) { | ||
self.set('value', evt.target.result); | ||
self.set('loading', false); | ||
}; | ||
|
||
reader.readAsText(firstFile); | ||
} | ||
|
||
}); |
12 changes: 12 additions & 0 deletions
12
assets/javascripts/discourse/templates/components/json-file-uploader.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<div class="jsfu-shade-container"> | ||
<div class="jsfu-file"> | ||
<input id="js-file-input" type="file" style="display:none;" accept={{accept}}> | ||
{{d-button class="fileSelect" action="selectFile" class="" icon="upload" label="upload_selector.select_file"}} | ||
{{conditional-loading-spinner condition=loading size="small"}} | ||
</div> | ||
<div class="jsfu-separator">{{i18n "alternation"}}</div> | ||
<div class="jsfu-paste"> | ||
{{textarea value=value}} | ||
</div> | ||
<div class="jsfu-shade {{if hover '' 'hidden'}}"><span class="text">{{fa-icon "upload"}}</span></div> | ||
</div> |