## AssetManager You can customize the initial state of the module from the editor initialization, by passing the following [Configuration Object][1] ```js const editor = grapesjs.init({ assetManager: { // options } }) ``` Once the editor is instantiated you can use its API. Before using these methods you should get the module from the instance ```js const assetManager = editor.AssetManager; ``` ## Available Events * `asset:open` - Asset Manager opened. * `asset:close` - Asset Manager closed. * `asset:add` - Asset added. The [Asset] is passed as an argument to the callback. * `asset:remove` - Asset removed. The [Asset] is passed as an argument to the callback. * `asset:update` - Asset updated. The updated [Asset] and the object containing changes are passed as arguments to the callback. * `asset:upload:start` - Before the upload is started. * `asset:upload:end` - After the upload is ended. * `asset:upload:error` - On any error in upload, passes the error as an argument. * `asset:upload:response` - On upload response, passes the result as an argument. * `asset` - Catch-all event for all the events mentioned above. An object containing all the available data about the triggered event is passed as an argument to the callback. * `asset:custom` - Event for handling custom Asset Manager UI. ## Methods * [open][2] * [close][3] * [isOpen][4] * [add][5] * [get][6] * [getAll][7] * [getAllVisible][8] * [remove][9] * [store][10] * [load][11] * [getContainer][12] [Asset]: asset.html ## open Open the asset manager. ### Parameters * `options` **[Object][13]?** Options for the asset manager. (optional, default `{}`) * `options.types` **[Array][14]<[String][15]>** Types of assets to show. (optional, default `['image']`) * `options.select` **[Function][16]?** Type of operation to perform on asset selection. If not specified, nothing will happen. ### Examples ```javascript assetManager.open({ select(asset, complete) { const selected = editor.getSelected(); if (selected && selected.is('image')) { selected.addAttributes({ src: asset.getSrc() }); // The default AssetManager UI will trigger `select(asset, false)` on asset click // and `select(asset, true)` on double-click complete && assetManager.close(); } } }); // with your custom types (you should have assets with those types declared) assetManager.open({ types: ['doc'], ... }); ``` ## close Close the asset manager. ### Examples ```javascript assetManager.close(); ``` ## isOpen Checks if the asset manager is open ### Examples ```javascript assetManager.isOpen(); // true | false ``` Returns **[Boolean][17]** ## add Add new asset/s to the collection. URLs are supposed to be unique ### Parameters * `asset` **([String][15] | [Object][13] | [Array][14]<[String][15]> | [Array][14]<[Object][13]>)** URL strings or an objects representing the resource. * `opts` **[Object][13]?** Options (optional, default `{}`) ### Examples ```javascript // As strings assetManager.add('http://img.jpg'); assetManager.add(['http://img.jpg', './path/to/img.png']); // Using objects you can indicate the type and other meta informations assetManager.add({ // type: 'image', // image is default src: 'http://img.jpg', height: 300, width: 200, }); assetManager.add([{ src: 'img2.jpg' }, { src: 'img2.png' }]); ``` Returns **[Asset]** ## get Return asset by URL ### Parameters * `src` **[String][15]** URL of the asset ### Examples ```javascript const asset = assetManager.get('http://img.jpg'); ``` Returns **([Asset] | null)** ## getAll Return the global collection, containing all the assets Returns **Collection<[Asset]>** ## getAllVisible Return the visible collection, which contains assets actually rendered Returns **Collection<[Asset]>** ## remove Remove asset ### Parameters * `asset` **([String][15] | [Asset])** Asset or asset URL * `opts` ### Examples ```javascript const removed = assetManager.remove('http://img.jpg'); // or by passing the Asset const asset = assetManager.get('http://img.jpg'); assetManager.remove(asset); ``` Returns **[Asset]** Removed asset ## store Store assets data to the selected storage ### Parameters * `noStore` **[Boolean][17]** If true, won't store ### Examples ```javascript var assets = assetManager.store(); ``` Returns **[Object][13]** Data to store ## load Load data from the passed object. The fetched data will be added to the collection. ### Parameters * `data` **[Object][13]** Object of data to load (optional, default `{}`) ### Examples ```javascript var assets = assetManager.load({ assets: [...] }) ``` Returns **[Object][13]** Loaded assets ## getContainer Return the Asset Manager Container Returns **[HTMLElement][18]** ## render Render assets ### Parameters * `assts` * `assets` **[array][14]** Assets to render, without the argument will render all global assets ### Examples ```javascript // Render all assets assetManager.render(); // Render some of the assets const assets = assetManager.getAll(); assetManager.render(assets.filter( asset => asset.get('category') == 'cats' )); ``` Returns **[HTMLElement][18]** [1]: https://github.com/artf/grapesjs/blob/master/src/asset_manager/config/config.js [2]: #open [3]: #close [4]: #isopen [5]: #add [6]: #get [7]: #getall [8]: #getallvisible [9]: #remove [10]: #store [11]: #load [12]: #getcontainer [13]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object [14]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array [15]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String [16]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function [17]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean [18]: https://developer.mozilla.org/docs/Web/HTML/Element