PageWidget
This topic describes how to use PageWidget. PageWidget provides methods that allow developers to quickly embed PDS BasicUI pages into their own Web applications by using an iframe.
1. Constructor
Custom iframe mount point and styles
By default, the iframe is mounted to the body element. You can specify a custom mount point for the iframe as needed. For example:
<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'),
// Custom styles.
iframe_style: {
width: '100%',
height: '100%',
},
// The page to embed.
module: 'drive/user',
token_info,
// Configure a function to obtain a token when the current token expires.
refresh_token_fun: async () => {
// Your business logic to call the server-side refreshToken API.
// Obtain new_token_info 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. The module parameter. |
widget_endpoint | string | Yes | The entry URL for the component UI. This is typically your domain's |
container | string or HTMLElement | Yes | The element where the component is mounted. |
lang | string | The language. Valid values: | |
token_info | Object | Yes | The |
share_token | string | The | |
refresh_token_fun | Function | Yes | A function to refresh the |
refresh_share_token_fun | Function | A function to refresh the | |
iframe_style | Object | Custom styles for the iframe. | |
iframe_attr | Object | Custom attributes for the iframe. | |
menus | {name:string, hide:boolean}[] | A list of menu items for file operations. You can hide specific menu buttons. For example: | |
show_avatar | boolean | Specifies whether to display the user avatar in the upper-right corner. Default value: | |
show_sidebar | boolean | Specifies whether to display the sidebar. Default value: |
2. Instance methods
Sample call:
const ins = new PageWidget(widgetOpts)
// Call a method.
ins.setLang('zh-CN')PageWidget supports the following methods:
Method | Description | Parameters |
setToken | Sets the | A JSON object that contains the token information. |
removeToken | Clears the | None |
setLang | Sets the component language. | The language code. Replace the hyphen (-) with an underscore (_). For example, use |
setShareToken | Sets the | string |
on | Listens for an event. | See details below. |
destroy | Destroys the component. | None |
changeModule | Switches the displayed page module. | A string that specifies the component name, such as "drive/user". For more information, see 4. The module parameter. |
setMenus | Configures the visibility of menu items. | A list of menu items for file operations. You can hide specific menu buttons. For example: |
3. Event listening
You can listen for component events and execute a callback function when an event is triggered.
Example:
const ins = new PageWidget(widgetOpts);
ins.on('event_name', function(data) {
// do something...
})Supported events
Event | Description | Payload |
load | Fired when the component is rendered for the first time. | None |
error | Fired when an error occurs in the component. | An object that contains error details, such as |
tokenChanged | Fired when the | An object that contains the |
shareTokenChanged | Fired when the | An object that contains the |
langChanged | Fired when the component language changes. | A string that specifies the language name, such as |
routerChanged | Fired when the component route changes. | A string that specifies the URL path, which starts with a forward slash ( |
fileChanged | Fired when a file operation occurs. | {"type": "copy", "fileList": []}, where |
preview | Fired when a file preview starts or stops. | An object that contains the file information. The value is |
popup | Fired when a pop-up window's visibility changes. | {"visible": false}, where |
error event codes
TokenInvalid: The token is invalid or not set.TokenExpired: The token has expired.
For other error codes, see Error handling.
fileChanged callback parameters
ins.on('fileChanged', ({type, fileList})=>{
// Handle the callback.
})Parameter | Required | Type | Description |
type | Yes | string |
|
fileList | Yes | Object | For details on file item fields, see File. Required properties:
Optional properties:
|
4. module parameter
You can set the initial module with the module constructor parameter or switch modules at runtime with the changeModule method.
Component names supported by PageWidget | User interface
Admin console
|
Example:
const widgetOpts = {
module: 'drive/user',
// ...
}
const ins = new PageWidget(widgetOpts);
ins.changeModule('drive/group')5. menus parameter
You can use the menus constructor parameter or the setMenus method to show or hide the file operation buttons on the page. The value true shows the buttons, and false hides them.
download: Downloadshare: Sharesharelink: Share linkmove: Movecopy: Copystar: Add to/Remove from favoritesrename: Renameremark: Remarkdetail: Detailsshare_owner: Manage Collaboratorsdelete: Delete
Example:
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 },
// ...
])