Browse Source

Update asset manager APIs

pull/36/head
Artur Arseniev 10 years ago
parent
commit
1fe359b4d6
  1. 45
      src/asset_manager/config/config.js
  2. 65
      src/asset_manager/main.js
  3. 47
      src/asset_manager/view/AssetsView.js
  4. 4
      src/asset_manager/view/FileUploader.js
  5. 4
      src/demo.js
  6. 19
      src/editor/model/Editor.js

45
src/asset_manager/config/config.js

@ -1,56 +1,17 @@
define(function () {
return {
stylePrefix: 'am-',
/*
// Indicates if try to load data from the selected storage
autoload: 1,
// Indicates which storage to use. Available: local | remote
storageType: 'local',
// Default assets
assets: [],
// Style prefix
stylePrefix: 'am-',
// Where store remote assets
urlStore : 'http://localhost/assets/store',
// Where fetch remote assets
urlLoad : 'http://localhost/assets/load',
// The name that will be used to identify assets inside storage.
// If empty will be used: prefix + 'assets'
storageName: 'assets',
// Custom parameters to pass with set request
paramsStore : {},
// Custom parameters to pass with get request
paramsLoad : {},
// Callback before request
beforeSend : function(jqXHR,settings){},
// Callback after request
onComplete : function(jqXHR,status){},
*/
// Default assets
assets: [],
// Url where uploads will be send
urlUpload: 'http://localhost/assets/upload',
upload: 'http://localhost/assets/upload',
// Text on upload input
uploadText: 'Drop files here or click to upload',
// Disable upload input
disableUpload: false,
// Store assets data where the new one is added or deleted
storeOnChange: true,
// It could be useful to avoid send other requests for saving assets,
// as after upload the script may have already done it
storeAfterUpload: false,
};
});

65
src/asset_manager/main.js

@ -47,16 +47,26 @@ define(function(require) {
name: 'AssetManager',
/**
* If public module
* Indicates if module is public
* @type {Boolean}
* @private
*/
public: true,
/**
* Manda
* @type {String}
* @private
*/
storageKey: 'assets',
/**
* Initialize module
* @param {Object} config Configurations
* @param {Array<Object>} [config.assets=[]] Default assets
* @param {String} [config.uploadText='Drop files here or click to upload'] Upload text
* @param {String} [config.upload=''] Where to send upload data. Expects as return a JSON with asset/s object
* as: {data: [{src:'...'}, {src:'...'}]}
* @example
* ...
* {
@ -64,7 +74,7 @@ define(function(require) {
* {src:'path/to/image.png'},
* ...
* ],
* upload: 'http://dropbox/path', // set to false to disable it
* upload: 'http://dropbox/path', // Set to false to disable it
* uploadText: 'Drop files here or click to upload',
* }
*/
@ -90,8 +100,6 @@ define(function(require) {
fu = new FileUpload(obj);
},
stm: c.stm,
/**
* Add new asset/s to the collection. URLs are supposed to be unique
* @param {string|Object|Array<string>|Array<Object>} asset URL strings or an objects representing the resource.
@ -152,27 +160,44 @@ define(function(require) {
/**
* Store assets data to the selected storage
* @return {this}
* @param {Boolean} noStore If true, won't store
* @return {Object} Data to store
* @example
* var assets = assetManager.store();
*/
store: function(){
if(!this.stm)
return;
this.stm.store({
assets: JSON.stringify(this.getAll().toJSON())
});
return this;
store: function(noStore){
var obj = {};
var assets = JSON.stringify(this.getAll().toJSON());
obj[this.storageKey] = assets;
if(!noStore && c.stm)
c.stm.store(obj);
return obj;
},
/**
* Load data from the selected storage. The fetched data will be added to the collection
* @return {Object} Stored assets
* Load data from the passed object, if the object is empty will try to fetch them
* autonomously from the storage manager.
* The fetched data will be added to the collection
* @param {Object} data Object of data to load
* @return {Object} Loaded assets
* @example
* var assets = assetManager.load();
* // The format below will be used by the editor model
* // to load automatically all the stuff
* var assets = assetManager.load({
* assets: [...]
* });
*
*/
load: function(){
var name = 'assets';
if(!this.stm)
return;
var data = this.stm.load([name]);
var assets = (JSON.parse(data[name]) || []).reverse();
load: function(data){
var d = data || '';
var name = this.storageKey;
if(!d && c.stm)
d = c.stm.load(name);
var assets = [];
try{
assets = JSON.parse(d[name]);
}catch(err){}
this.getAll().add(assets);
return assets;
},

47
src/asset_manager/view/AssetsView.js

@ -15,24 +15,6 @@ define(['backbone', './AssetView', './AssetImageView', './FileUploader', 'text!.
this.listenTo( this.collection, 'add', this.addToAsset );
this.listenTo( this.collection, 'deselectAll', this.deselectAll );
this.className = this.pfx + 'assets';
var c = this.config;
// Check if storage is required and if Storage Manager is available
if(this.config.stm && this.config.storageType !== ''){
var type = this.config.storageType;
this.provider = this.config.stm.get(type);
this.storeName = this.config.storageName ? this.config.storageName : this.className;
if(this.provider){
// Create new instance of provider
this.storagePrv = this.provider.clone().set(this.config);
this.collection.reset();
this.collection.add(this.load());
if(this.config.storeOnChange){
var ev = 'remove' + (this.config.storeAfterUpload ? ' add' : '');
this.listenTo(this.collection, ev, this.store);
}
}
}
this.events = {};
this.events.submit = 'addFromStr';
@ -83,35 +65,6 @@ define(['backbone', './AssetView', './AssetImageView', './FileUploader', 'text!.
return this.inputUrl;
},
/**
* Store collection
*
* @return void
* */
store: function(){
if(this.storagePrv)
this.storagePrv.store(this.storeName, JSON.stringify(this.collection.toJSON()) );
},
/**
* Load collection
*
* @return {Object}
* */
load: function(){
var result = null;
if(this.storagePrv)
result = this.storagePrv.load(this.storeName);
if(typeof result !== 'object'){
try{
result = JSON.parse(result);
}catch(err){
console.warn(err);
}
}
return result;
},
/**
* Add asset to collection
* */

4
src/asset_manager/view/FileUploader.js

@ -16,7 +16,7 @@ define(['backbone', 'text!./../template/fileUploader.html'],
this.pfx = this.config.stylePrefix || '';
this.target = this.collection || {};
this.uploadId = this.pfx + 'uploadFile';
this.disabled = this.config.disableUpload;
this.disabled = !this.config.upload;
this.events['change #' + this.uploadId] = 'uploadFile';
this.delegateEvents();
},
@ -35,7 +35,7 @@ define(['backbone', 'text!./../template/fileUploader.html'],
}
var target = this.target;
$.ajax({
url : this.config.urlUpload, //this.config.urlUpload
url : this.config.upload,
type : 'POST',
data : formData,
beforeSend : this.config.beforeSend,

4
src/demo.js

@ -41,7 +41,7 @@ require(['config/require-config'], function() {
}
}],
},
/*
assetManager: {
storageType : '',
storeOnChange : true,
@ -59,7 +59,7 @@ require(['config/require-config'], function() {
{ type: 'image', src : './img/bg-gr-v.png', date: '2015-02-01',height:1, width:1728},
]
},
*/
styleManager : {
sectors: [{

19
src/editor/model/Editor.js

@ -96,9 +96,11 @@ define([
// Check if module is storable
var sm = this.get('StorageManager');
if(M.store && M.load && sm){
if(M.storageKey && M.store && M.load && sm){
cfg.stm = sm;
this.set('storables', this.get('storables').push(module));
var storables = this.get('storables');
storables.push(M);
this.set('storables', storables);
}
M.init(cfg);
@ -726,6 +728,12 @@ define([
if(smc.storeStyles)
store.styles = JSON.stringify(this.getStyle());
this.get('storables').forEach(function(m){
var obj = m.store(1);
for(var el in obj)
store[el] = obj[el];
});
sm.store(store);
return store;
},
@ -737,6 +745,9 @@ define([
*/
load: function(){
var result = this.getCacheLoad(1);
this.get('storables').forEach(function(m){
m.load(result);
});
//this.setComponents(result.components || result.html);
//this.setStyle(result.styles || result.css);
return result;
@ -774,6 +785,10 @@ define([
if(smc.storeStyles)
load.push('styles');
this.get('storables').forEach(function(m){
load.push(m.storageKey);
});
this.cacheLoad = sm.load(load);
return this.cacheLoad;

Loading…
Cancel
Save