This topic describes how to use PageWidget.
1. Configure the constructor
Set the mount point and style for the iframe of the widget.
By default, the iframe is mounted to the body element. Set the mount point for the iframe as needed.
<div id="container"></div>import { PageWidget } from 'aliyun-pds-js-widget';
const domain_id = 'bj1'; // Your domain ID.
const token_info = {
access_token: 'xxxxx',
refresh_token: 'xxxxx',
// ...
};
const widgetOpts = {
widget_endpoint: `https://${domain_id}.apps.aliyunpds.com`,
// The mount point.
container: document.querySelector('#container'),
// The custom style.
iframe_style: {
width: '100%',
height: '100%',
},
// The embedded page.
module: 'drive/user',
token_info,
// Configure the function that obtains refreshed tokens.
refresh_token_fun: async () => {
// The business processing logic. Call the refreshToken operation that is encapsulated on the server.
// new_token_info Obtain the new token from the server.
// let new_token_info = await fetch(.....)
return new_token_info;
},
};
const ins = new PageWidget(widgetOpts);
PageWidget constructor parameters
Parameter | Type | Required | Description |
module | string | Yes | The component name. For more information, see 4. Details about the module parameter. |
widget_endpoint | string | Yes | The URL that is used to access the component. In most cases, the value of this parameter is the domain name that hosts the application. |
container | string or HTMLElement | Yes | The element that is used to mount the component. |
lang | string | The language. Valid values: zh_CN and en_US. For the first time, the language specified by the navigator.language property in the browser is used by default. | |
token_info | Object | Yes | The JSON object for the access token. |
share_token | string | The access token for sharing, which is required when you share resources. | |
refresh_token_fun | Function | Yes | The method that is used to refresh the access token when it expires. After the method is called, a new access token is returned. |
refresh_share_token_fun | Function | The method that is used to refresh the access token for sharing when it expires. After the method is called, a new access token for sharing is returned. | |
iframe_style | Object | The custom iframe style. | |
iframe_attr | Object | The custom iframe attribute. | |
menus | {name:string, hide:boolean}[] | The menu items related to file operations. You can hide specific menu items. Example: [{name: "sharelink", hide: true}]. name indicates the item name. For more information, see 5. Details about the menus parameter. |
2. Configure the instance
Sample code:
const ins = new PageWidget(widgetOpts)
// Call a method.
ins.setLang('zh-CN')Methods supported by PageWidget
Method | Description | Parameter description |
setToken | Sets the logon state of the component. | A JSON object that contains the token information. |
removeToken | Clears the logon state of the component. | N/A |
setLang | Sets the language of the component. | A string that indicates the language. Replace the hyphen (-) with an underscore (_). For example, if you want to use simplified Chinese, set the value to zh_CN, which corresponds to zh-CN in browsers. |
setShareToken | Sets the sharing token for the component. | string |
on | Listens to events. | See below for details. |
destroy | Destroys the component. | N/A |
changeModule | Changes the component. | A string that indicates the component name, such as "drive/user". For more information, see 4. Details about the module parameter. |
setMenus | Sets the menu items. | The menu items related to file operations. You can hide specific menu items. Example: [{name: "sharelink", hide: true}]. name indicates the item name. For more information, see 5. Details about the menus parameter. |
3. Listen to events
Enable event listening for the component. When a specific event occurs on the component, the corresponding callback is returned.
Sample code:
const ins = new PageWidget(widgetOpts);
ins.on('Event name', function(data) {
// do something...
})Supported events
Event | Description | Payload parameter |
load | The component is rendered and triggered for the first time. | N/A |
error | A component error occurs. | An object that is triggered when a component error occurs. Example: {code: "TokenExpired", mesage:"xxx"}. |
tokenChanged | The token for the component is changed. | An object, which is |
shareTokenChanged | The sharing token for the component is changed. | An object, which is |
langChanged | The language of the component is changed. | A string that indicates the language, such as |
routerChanged | The route of the component is changed. | A string, which is a URL path that starts with a slash, such as |
fileChanged | A file change occurs. | A string in the format of {"type": "copy", "fileList": []}. type indicates the type of the file operation and |
preview | Preview starts or stops. | An object that contains the file information. The value is |
popup | The status of the pop-up window is changed. | A string in the format of {"visible": false}. |
Error codes for specific error events
TokenInvalid: The token is invalid or not specified.
TokenExpired: The token has expired.
For information about other error codes, see Error handling.
Callback parameters of the fileChanged event
ins.on('fileChanged', ({type, fileList})=>{
// Handle the callback.
})Parameter | Required | Type | |
type | Yes | string |
|
fileList | Yes | Object | Required attributes:
Optional attributes:
|
4. Details about the module parameter
Use the constructor parameter module to configure the component, or call the changeModule method to change the component.
Components supported by PageWidget | User interface (UI):
Admin console:
|
Components supported by PreviewWidget |
|
Sample code:
const widgetOpts = {
module: 'drive/user',
// ...
}
const ins = new PageWidget(widgetOpts);
ins.changeModule('drive/group')5. Details about the menus parameter
Use the constructor parameter menus or call the setMenus method to hide or show specific menu items that are related to file operations. Set the value of hide to true to hide a menu item and false to show a menu item.
downloadsharesharelinkmovecopystar: add to favorites/remove from favoritesrenameremarkdetailshare_ownerdelete
Sample code:
const widgetOpts = {
menus: [
{ name: 'download', hide: false },
{ name: 'copy', hide: false },
{ name: 'move', hide: false },
{ name: 'rename', hide: false },
{ name: 'delete', hide: false },
{ name: 'remark', hide: false },
// ...
],
// ...
}
const ins = new PageWidget(widgetOpts);
ins.setMenus([
{ name: 'download', hide: false },
{ name: 'copy', hide: false },
{ name: 'move', hide: false },
{ name: 'rename', hide: false },
{ name: 'delete', hide: false },
{ name: 'remark', hide: false },
// ...
])