This repository has been archived by the owner on Mar 25, 2019. It is now read-only.
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
翰文
committed
Apr 1, 2015
1 parent
89dd085
commit 770dda5
Showing
12 changed files
with
256 additions
and
70 deletions.
There are no files selected for viewing
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
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
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
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
This file was deleted.
Oops, something went wrong.
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,77 @@ | ||
/**! | ||
* desc | ||
* Copyright(c) Alibaba Group Holding Limited. | ||
* MIT Licensed | ||
* | ||
* Authors: | ||
* 翰文 <hanwen.sah@taobao.com> (http://shepherdwind.com) | ||
*/ | ||
'use strict'; | ||
|
||
var DzDispatcher = require('../dispatcher/DzDispatcher'); | ||
var DzConstants = require('../constants/DzConstants'); | ||
var createThumbnail = require('../utils/createThumbnail'); | ||
var request = require('../utils/request'); | ||
|
||
var FileActions = { | ||
|
||
upload: function(file, accept, config) { | ||
|
||
createThumbnail(file) | ||
.then(function(thumbnail) { | ||
file.thumbnail = thumbnail; | ||
return file; | ||
}) | ||
.then(accept) | ||
.then(function() { | ||
DzDispatcher.dispatch({ | ||
actionType: DzConstants.FILE_ADD, | ||
file: file | ||
}); | ||
|
||
upload(file, config); | ||
}) | ||
.catch(function(msg) { | ||
DzDispatcher.dispatch({ | ||
actionType: DzConstants.FILE_REJECTED, | ||
file: file, | ||
msg: msg | ||
}); | ||
}); | ||
} | ||
|
||
}; | ||
|
||
function upload(file, config) { | ||
request | ||
.upload(file, config) | ||
.on('progress', function(e) { | ||
DzDispatcher.dispatch({ | ||
actionType: DzConstants.FILE_UPLOAD_PROGRESS, | ||
uid: file.uid, | ||
percent: e.percent | ||
}); | ||
}) | ||
.end(function(err, ret) { | ||
if (err || ret.status !== 200) { | ||
var message = err ? err.message : ret.text; | ||
DzDispatcher.dispatch({ | ||
actionType: DzConstants.FILE_UPLOAD_FAIL, | ||
uid: file.uid, | ||
error: message | ||
}); | ||
|
||
config.error(message); | ||
return; | ||
} | ||
|
||
DzDispatcher.dispatch({ | ||
actionType: DzConstants.FILE_UPLOAD_SUCCESS, | ||
uid: file.uid, | ||
body: ret.body | ||
}); | ||
|
||
config.success(ret.body); | ||
}); | ||
} | ||
module.exports = FileActions; |
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,8 @@ | ||
var keymirror = require('keymirror'); | ||
module.exports = keymirror({ | ||
FILE_ADD: null, | ||
FILE_REJECTED: null, | ||
FILE_UPLOAD_PROGRESS: null, | ||
FILE_UPLOAD_SUCCESS: null, | ||
FILE_UPLOAD_FAIL: null | ||
}); |
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,6 @@ | ||
var keymirror = require('keymirror'); | ||
module.exports = keymirror({ | ||
ADD: null, | ||
REJECTED: null, | ||
UPLOADING: null | ||
}); |
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,3 @@ | ||
var Dispatcher = require('flux').Dispatcher; | ||
|
||
module.exports = new Dispatcher(); |
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,104 @@ | ||
/**! | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
* desc | ||
* Copyright(c) Alibaba Group Holding Limited. | ||
* MIT Licensed | ||
* | ||
* Authors: | ||
* 翰文 <hanwen.sah@taobao.com> (http://shepherdwind.com) | ||
*/ | ||
'use strict'; | ||
|
||
var DzDispatcher = require('../dispatcher/DzDispatcher'); | ||
var DzConstants = require('../constants/DzConstants'); | ||
var StatusConstants = require('../constants/StatusConstants'); | ||
var EventEmitter = require('events').EventEmitter; | ||
var assign = require('object-assign'); | ||
var uid = require('../utils/uid'); | ||
|
||
var CHANGE_EVENT = 'change'; | ||
var _files = {}; | ||
|
||
var FileStore = assign({}, EventEmitter.prototype, { | ||
|
||
getAllFiles: function() { | ||
return _files; | ||
}, | ||
|
||
getAddedFiles: function() { | ||
return Object.keys(_files).filter(function(uid) { | ||
var file = _files[uid]; | ||
if (file.status === StatusConstants.ADD) { | ||
return file; | ||
} | ||
}); | ||
}, | ||
|
||
addChangeListener: function(callback) { | ||
this.on(CHANGE_EVENT, callback); | ||
}, | ||
|
||
removeChangeListener: function(callback) { | ||
this.removeListener(CHANGE_EVENT, callback); | ||
}, | ||
|
||
emitChange: function() { | ||
this.emit(CHANGE_EVENT); | ||
} | ||
|
||
}); | ||
|
||
function add(file) { | ||
var _uid = uid(); | ||
file.uid = _uid; | ||
file.status = StatusConstants.ADD; | ||
_files[_uid] = file; | ||
} | ||
|
||
function reject(file, msg) { | ||
add(file); | ||
file.status = StatusConstants.REJECTED; | ||
file.done = msg; | ||
} | ||
|
||
function success(uid) { | ||
_files[uid].done = true; | ||
} | ||
|
||
function uploading(uid, percent) { | ||
_files[uid].status = StatusConstants.UPLOADING; | ||
_files[uid].percent = percent; | ||
} | ||
|
||
function fail(uid, msg) { | ||
_files[uid].done = msg; | ||
} | ||
|
||
DzDispatcher.register(function(action) { | ||
switch (action.actionType) { | ||
case DzConstants.FILE_ADD: | ||
add(action.file); | ||
break; | ||
|
||
case DzConstants.FILE_REJECTED: | ||
reject(action.file, action.msg); | ||
break; | ||
|
||
case DzConstants.FILE_UPLOAD_PROGRESS: | ||
uploading(action.uid, action.percent); | ||
break; | ||
|
||
case DzConstants.FILE_UPLOAD_SUCCESS: | ||
success(action.uid); | ||
break; | ||
|
||
case DzConstants.FILE_UPLOAD_FAIL: | ||
fail(action.uid, action.error); | ||
break; | ||
|
||
default: | ||
} | ||
|
||
FileStore.emitChange(); | ||
}); | ||
|
||
module.exports = FileStore; |
Oops, something went wrong.
flux 是否适合组件?不同组件实例的数据由同一个 store 存储是否合适