PageWidget

Updated at:

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 apps subdomain.

container

string or HTMLElement

Yes

The element where the component is mounted.

lang

string

The language. Valid values: zh_CN and en_US. If this parameter is not specified, it defaults to the browser's navigator.language setting.

token_info

Object

Yes

The access token, passed as a JSON object. To learn how to obtain a token, see JWT-based application integration.

share_token

string

The share token. This token is useful for performing operations on shared resources.

refresh_token_fun

Function

Yes

A function to refresh the access token when it expires. This function must return a new token_info object.

refresh_share_token_fun

Function

A function to refresh the share token when it expires. This function must return a new share_token.

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: [{name: "sharelink", hide: true}]. The name parameter specifies the menu item name. For more information, see 5. The menus parameter.

show_avatar

boolean

Specifies whether to display the user avatar in the upper-right corner. Default value: false. (Supported in v1.5.1 and later)

show_sidebar

boolean

Specifies whether to display the sidebar. Default value: false. (Supported in v1.5.1 and later)

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 logon state for the component.

A JSON object that contains the token information.

removeToken

Clears the logon state.

None

setLang

Sets the component language.

The language code. Replace the hyphen (-) with an underscore (_). For example, use zh_CN instead of zh-CN.

setShareToken

Sets the share_token.

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: [{name: "sharelink", hide: true}]. The name parameter specifies the menu item name. For more information, see 5. The menus parameter.

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 {code: "TokenExpired", message:"xxx"}.

tokenChanged

Fired when the access token changes.

An object that contains the token information, or null.

shareTokenChanged

Fired when the share_token changes.

An object that contains the share_token information, or null.

langChanged

Fired when the component language changes.

A string that specifies the language name, such as zh_CN.

routerChanged

Fired when the component route changes.

A string that specifies the URL path, which starts with a forward slash (/), such as /disk/widget/drive/user.

fileChanged

Fired when a file operation occurs.

{"type": "copy", "fileList": []}, where type indicates the file operation type and fileList is a list of objects that contain file information. For specific values, see the supplementary content below.

preview

Fired when a file preview starts or stops.

An object that contains the file information. The value is null when the preview stops.

popup

Fired when a pop-up window's visibility changes.

{"visible": false}, where visible is a boolean value that specifies whether the dialog box is visible (true) or hidden (false).

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

  • star: Add to or remove from favorites.

  • copy: Copy.

  • move: Move.

  • delete: Move to the recycle bin.

  • remove: Permanently delete.

  • restore: Restore from the recycle bin.

  • remark: Modify the remark.

  • rename: Rename.

  • create: Create.

  • upload: Upload.

fileList

Yes

Object

For details on file item fields, see File.

Required properties:

  • drive_id: The drive ID. Type: string.

  • file_id: The file ID.

  • parent_file_id: The parent file ID.

  • type: The type. Valid values: file and folder.

Optional properties:

  • name: The file name.

  • created_at: The time when the file was created, in ISO 8601 format.

  • size: The file size in bytes. This property applies only when type is file.

  • starred: Specifies whether the file is added to favorites. Type: boolean.

  • updated_at: The time when the file was last updated, in ISO 8601 format.

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

  • drive/group: team space

  • drive/enterprise: enterprise space

  • drive/user: personal space

  • mycategory/image: My pictures (search results page)

  • mycategory/video: My videos (search results page)

  • mycategory/audio: My audio (search results page)

  • mycategory/doc: My documents (search results page)

  • ishare: my shares

  • share: received shares

  • star: my favorites

  • handover: Received drives

  • mysharelinks: my shared links

  • trash: recycle bin

Admin console

  • groups: team management

  • udrives: user drives

  • tdrives: team drives

  • shareConfig: share authorization settings

  • shareLinkManagement: shared link management

  • logAudit: log audit

  • standardCustom: Standard customization

  • seniorCustom: Advanced customization

  • fileAccess: File access

  • fileSettings: File settings

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: Download

  • share: Share

  • sharelink: Share link

  • move: Move

  • copy: Copy

  • star: Add to/Remove from favorites

  • rename: Rename

  • remark: Remark

  • detail: Details

  • share_owner: Manage Collaborators

  • delete: 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 },
  // ...
])