view-tabs.md 14.9 KB
Newer Older
C
Catouse 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
# Tabs

<div class="alert alert-warning">
  <h4>Compatibility</h4>
  <ul>
    <li>There may be compatibility issues in the browser of your mobile device.</li>
  </ul>
</div>

## Examples

<div class="example">
  <div class="tabs" id="tabsExample"></div>
</div>

```html
<div class="tabs" id="tabsExample"></div>
```

```js
// Definition tab
var tabs = [{
    title: 'iframe example',
    url: 'docs/partial/iframe-tab.html',
    type: 'iframe',
    forbidClose: true
}, {
    title: 'Ajax example',
    url: 'docs/partial/remote-tab.html',
    type: 'ajax'
}, {
    title: 'Custom example',
    icon: 'icon-star',
    type: 'custom',
    content: function() {
        return '<div class="alert alert-block alert-success"><p>The content here is dynamically generated by functions. Update when the following time is refreshed.</p><p>' + (new Date().format('yyyy-MM-dd hh:mm:ss')) +   '</p></div>';
    }
}, {
    title: 'MZUI',
    url: 'http://openzui.com/m',
    type: 'iframe'
}, {
    defaultTitle: 'Unable to load the tab.',
    url: 'http://zui1.sexy'
}];

// Initialize tabs
$('#tabsExample').tabs({tabs: tabs});
```

## Options

<table class="table table-bordered">
  <thead>
    <tr>
      <th style="width: 140px">Option</th>
      <th style="width: 150px">Name</th>
      <th style="width: 150px">Value</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>tabs</code></td>
      <td>Initial tab list</td>
      <td>Default: `[]`</td>
      <td>Specify an array of tab objects to define the tabs that are displayed after the tabs is initialized. Tab page object definitions are as follows.</td>
    </tr>
    <tr>
      <td><code>defaultTab</code></td>
      <td>Default activated tab ID</td>
      <td>Default: `null`</td>
      <td>If set as null(`null`), the first tab in the initial tab list will be activated after the tab is initialized.</td>
    </tr>
    <tr>
      <td><code>contextMenu</code></td>
      <td>Whether to enable the context menu</td>
      <td>Default:`true`</td>
79
      <td>If set as `true`, enable context menus on the navigation to facilitate user actions on tabs. When using a mouse, right click to display a menu. The option can be see to an object to override the default options of [contextmenu](#javascript/contextmenu).</td>
C
Catouse 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
    </tr>
    <tr>
      <td><code>defaultTabIcon</code></td>
      <td>Default tab icon</td>
      <td>Default: `'icon-window'`</td>
      <td>Use icon class name in [Control->icon]('#control/icon') to set the tab page. Use this option as the default icon if there is no tab icon property specified in the tab object.</td>
    </tr>
    <tr>
      <td><code>showMessage</code></td>
      <td>Whether to display an error message</td>
      <td>Default: `true`</td>
      <td></td>
    </tr>
    <tr>
      <td><code>errorTemplate</code></td>
      <td>Error message template string</td>
      <td>Default: `'<div class="alert alert-block alert-danger with-icon"><i class="icon-warning-sign"></i><div class="content">{0}</div></div>'`</td>
      <td></td>
    </tr>
    <tr>
      <td><code>messagerOptions</code></td>
      <td>Popout message option</td>
      <td>`null`</td>
      <td>When a popout message is displayed, use this option to set an object for [Messager](javascript/messager) parameter.</td>
    </tr>
    <tr>
      <td><code>lang</code></td>
      <td>Current interface language</td>
      <td>Default: `'zh_cn'`</td>
      <td>
        <p>So the available options are:</p>
        <ul>
          <li>`zh_cn`: Use simplified Chinese interface;</li>
          <li>`zh_tw`: Use traditional Chinese interface;</li>
          <li>`en`: Use English interface;</li>
          <li>
            <p>Or specify an object to customize the language text, e.g.:</p>
            <pre><code>{
    reload: 'Reload',
    close: 'Close',
    closeOthers: 'Close other tabs',
    closeRight: 'Close the right tab',
    reopenLast: 'Restore last closed tab',
    errorCannotFetchFromRemote: 'Unable to get contents from remote server ({0}).'
}</code></pre>
          </li>
        </ul>
      </td>
    </tr>
  </tbody>
</table>

## Tab page objects

Each tab uses an object to store related information. Define the default label in the tabs by defining an array of tab objects to be passed to the initialization function at the initialization. You also need to define a tab object when you need to dynamically open a tab. Attributes supported by tab objects include:

<table class="table table-bordered">
  <thead>
    <tr>
      <th style="width: 140px">Option</th>
      <th style="width: 150px">Name</th>
      <th style="width: 150px">Value</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>id</code></td>
      <td>Unique identifier of the tab</td>
      <td>Automatically generated by default</td>
      <td>Generally it does not need to be specified. If you need to manage the label object, you need to set a globally unique string for getting the tab object next time.</td>
    </tr>
    <tr>
      <td><code>type</code></td>
      <td>Tab content type</td>
      <td>Default:`'auto'`</td>
      <td>
        <p>All available values include:</p>
        <ul>
          <li>`'iframe'`: use iframe to load a remote page;</li>
          <li>`'ajax'`: use ajax to load remote content;</li>
          <li>`'custom'`: use `content` value as the content. If `content` is a function, the function returns the value as the content;</li>
          <li>`'auto'`: Automatically set based on other attributes. If set `url` attribute and `ajax` attribute is set as `ajax`, it is `iframe`. If not, set `url` attribute as `custom`.</li>
        </ul>
      </td>
    </tr>
    <tr>
      <td><code>url</code></td>
      <td>Remote content address</td>
      <td>Default: `''`</td>
      <td>If `type` attribute is `iframe`, it is the remote page address. If `type` is `ajax`, it is used as the remote content request address.</td>
    </tr>
    <tr>
      <td><code>ajax</code></td>
      <td>ajax Request parameter</td>
      <td>Default: `{type: 'GET'}`</td>
      <td>An object can be used to define jQuery Ajax Request parameter.</td>
    </tr>
    <tr>
      <td><code>icon</code></td>
      <td>Tab icon</td>
      <td>Default: `'icon-window'`</td>
      <td>Use icon class names in [Control->icon]('#control/icon') to set the tab page. The default is `'icon-window'` which can be set in `defaultTabIcon`.</td>
    </tr>
    <tr>
      <td><code>title</code></td>
      <td>Tab title</td>
      <td>Default: `''`</td>
      <td>When the tab title is set as empty `''` and tab type(`type`) is `'iframe'`, get iframe page title as the tab title.</td>
    </tr>
    <tr>
      <td><code>defaultTitle</code></td>
      <td>Tab page default title</td>
      <td>Default: `''`</td>
      <td>When the tab title is set as empty `''` and tab type(`type`) is `'iframe'`, get iframe page title as the tab title. Default title is used if the tab is not loaded.</td>
    </tr>
    <tr>
      <td><code>forbidClose</code></td>
      <td>Whether to prohibit tabs from being manually closed</td>
      <td>Default: `false`</td>
      <td>If set as `true`, user is not allowed to close this tab.</td>
    </tr>
    <tr>
      <td><code>onCreate</code></td>
      <td>Callback function when the tab is created</td>
      <td>Default: `null`</td>
      <td>This callback function contains a parameter for the tab page object.</td>
    </tr>
    <tr>
      <td><code>onOpen</code></td>
      <td>Callback function when the tab opens</td>
      <td>Default: `null`</td>
      <td>This callback function contains a parameter for the tab page object.</td>
    </tr>
    <tr>
      <td><code>onClose</code></td>
      <td>Callback function when the tab is closed</td>
      <td>Default: `null`</td>
      <td>This callback function contains a parameter for the tab page object.</td>
    </tr>
    <tr>
      <td><code>createTime</code>(Read only)</td>
      <td>Timestamp created by the tab page object</td>
      <td>Default: `0`</td>
      <td>This tab can be used to get the timestamp created by the tab object when the tab is created in the tabs.</td>
    </tr>
    <tr>
      <td><code>openTime</code>(Read only)</td>
      <td>The timestamp when the tab was last opened</td>
      <td>Default: `0`</td>
      <td>This tab can be used to get the timestamp of the last time the tab was opened when the tab was created in the tabs.</td>
    </tr>
    <tr>
      <td><code>loaded</code>(Read only)</td>
      <td>The timestamp of the last content loading of the tab page</td>
      <td>Default: `0`</td>
      <td>When the tab is created, this property can be used to get the timestamp of the content loaded by the tab last time.</td>
    </tr>
  </tbody>
</table>

## Methods

### `open(tab)`

Open and activate a tab. If this tab is opened for the first time, load the tab content. The parameters are defined as follows:

* `tab`: Tab page object, or automatically create a tab object using a remote server address string as a parameter;
* `forceReload`: Optional parameter. If set as `true`, force the tab to reload the content even if the tab content has been loaded. Equivalent to reloading(Refresh).

```js
// Get the tab object instance
var myTabs = $('#myTabs').data('zui.tabs');

// Tab object to be opened
var myTab = {
    url: 'http://openzui.com',
    type: 'iframe',
    defaultTitle: 'ZUI Official website'
};
// Open the tab
myTabs.open(myTab);
```

```js
// When the tab type is iframe, iframe address can also be used as a parameter.
myTabs.open('http://openzui.com');
```

### `close(tabId, forceClose)`

Close the tab. Its parameters are defined as:

* `tabId`: Id of the tab object to be closed;
* `forceClose`: Optional. If set as `true`, force to close the tab even when `forbidClose` is set as `true`.

```js
// Get the tab object instance
var myTabs = $('#myTabs').data('zui.tabs');

// Close the tab
myTabs.close('myCloseTabId');
```

### `getTab(tabId)`

According to the tab page ID to get the tab object. Its parameters are defined as follows:

* `tabId`: ID of the tag object to get.

```js
// Get the tab object instance
var myTabs = $('#myTabs').data('zui.tabs');

// Get the currently active tab object
var mySpecialTab = myTabs.getTab('mySpecialTabId');
console.log('The title of the page obtained is', mySpecialTab.title);
```

### `getActiveTab()`

Get the currently active tab object.

```js
// Get the tab object instance
var myTabs = $('#myTabs').data('zui.tabs');

// Get the currently active tab object
var myActiveTab = myTabs.getActiveTab();
console.log('The currently active tab title is', myActiveTab.title);
```

### `reload(tab)`

Reload tab content. Its parameters are defined as follows:

* `tab`: The tab object to be reloaded. Reload the currently active tab if this parameter is not specified.

```js
// Get the tab object instance
var myTabs = $('#myTabs').data('zui.tabs');

// Reload the currently active tab
myTabs.reload();

// Get the tab object to load
var myTab = myTabs.getTab('reloadTabId');

// Reload the tab
myTabs.reload(myTab);
```

### `closeOthers(tabId)`

Close tabs other than the current tab. Its parameters are defined as follows:

`tabId`:Current tab ID.

```js
// Get the tab object instance
var myTabs = $('#myTabs').data('zui.tabs');

// Close tabs other than the current tab
myTabs.closeOthers('tabID');
```

### `closeRight(tabId)`

CLose the tabs to the right of the current tab on the navigation. Its parameters are defined as follows:

`tabId`: Current tab ID.

```js
// Get the tab object instance
var myTabs = $('#myTabs').data('zui.tabs');

// Close the tabs to the right of the current tab on the navigation
myTabs.closeRight('tabID');
```

### `closeAll()`

Close navigation to all tabs.

```js
// Get the tab manager object instance
var myTabs = $('#myTabs').data('zui.tabs');

// Close all tabs
myTabs.closeAll();
```

### `reopen()`

Reopen the last closed tab.

```js
// Get the tab manager object instance
var myTabs = $('#myTabs').data('zui.tabs');

// Reopen the last closed tab
myTabs.reopen();
```

## Events

### `onOpen`

Triggered when the tab is open. Event callback function `this` is the instance of the current tab manager instance. The parameters are defined as follows:

* `tab`: Currently open tab object.

```js
// Set the event callback function at the initialization
$('#myTabs').tabs({
    onOpen: function(tab) {
        console.log('Bookmark page ' + tab.title + ' is open.');
    }
});
```

```js
// Use jQuery $().on() listen events
$('#myTabs').on('onOpen', function(event, tab) {
    console.log('Bookmark page ' + tab.title + ' is open.');
});
```

### `onLoad`

Triggered when the tab is loaded. Event callback function `this` is the instance of the current tab manager. The parameters are defined as follows:

* `tab`: Currently loaded tab object.

```js
// Set the event callback function at the initialization
$('#myTabs').tabs({
    onLoad: function(tab) {
        console.log('Bookmark page ' + tab.title + ' loading is complete.');
    }
});
```

```js
// Use jQuery $().on() listen events
$('#myTabs').on('onLoad', function(event, tab) {
    console.log('Bookmark page ' + tab.title + ' loading is complete.');
});
```

### `onClose`

Triggered when the tab is closed. Event callback function `this` is the instance of the current tab manager. The parameters are defined as follows:

* `tab`: Tab object that is closed.

```js
// Set the event callback function at the initialization
$('#myTabs').tabs({
    onClose: function(tab) {
        console.log('Bookmark page ' + tab.title + ' is closed.');
    }
});
```

```js
// Use jQuery $().on() listen events
$('#myTabs').on('onClose', function(event, tab) {
    console.log('Bookmark page ' + tab.title + ' is closed.');
});
```

## Customize

Use `.tabs-navbar` element and `.tabs-container` element in tab initialization elements to define the navigation and tab content in the parent element.

``` html
<div id="myTabs" class="tabs">
  <nav class="tabs-navbar"></nav>
  <nav class="tabs-container"></nav>
</div>
```

C
Catouse 已提交
463 464
<script src="../../dist/lib/tabs/zui.tabs.js"></script>
<link href="../../dist/lib/tabs/zui.tabs.css" rel="stylesheet">
C
Catouse 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
<script>
function afterPageLoad() {
    $('#tabsExample').tabs({
        tabs: [{
            title: 'iframe example',
            url: 'docs/partial/iframe-tab.html',
            type: 'iframe',
            forbidClose: true
        }, {
            title: 'Ajax example',
            url: 'docs/partial/remote-tab.html',
            type: 'ajax'
        }, {
            title: 'Custom example',
            icon: 'icon-star',
            type: 'custom',
            content: function() {
                return '<div class="alert alert-block alert-success"><p>The content here is dynamically generated by functions. Update when the following time is refreshed.</p><p>' + (new Date().format('yyyy-MM-dd hh:mm:ss')) +   '</p></div>';
            }
        }, {
            title: 'MZUI',
            url: 'http://openzui.com/m',
            type: 'iframe'
        }, {
            defaultTitle: 'Unable to load tab',
            url: 'http://zui1.sexy'
        }]
    });
}
</script>