@@ -47,7 +47,7 @@ if (typeof jQuery === "undefined") {
47
47
*/
48
48
trimText = function ( text , maxLength ) {
49
49
maxLength = maxLength || $ . fn . multitabs . defaults . navTab . maxTitleLength ;
50
- var words = String ( text ) . split ( ' ' ) ;
50
+ var words = ( text + "" ) . split ( ' ' ) ;
51
51
var t = '' ;
52
52
for ( var i = 0 ; i < words . length ; i ++ ) {
53
53
var w = $ . trim ( words [ i ] ) ;
@@ -238,64 +238,122 @@ if (typeof jQuery === "undefined") {
238
238
* @returns self Chain structure.
239
239
*/
240
240
create : function ( obj , active ) {
241
- var self = this ,
242
- options = self . options ,
243
- $el = self . $element ,
244
- $editor = $el . tabContent . find ( '.tab-pane[data-content="editor"]' ) ;
245
- var param , navTabHtml , closeBtnHtml , display , tabPaneHtml , index , id , $navTab , $tabPane ;
241
+ var self = this , options = self . options ;
242
+ var param , index , id , $navTab ;
246
243
if ( ! ( param = self . _getParam ( obj ) ) ) {
247
244
return self ; //return multitabs obj when is invaid obj
248
245
}
249
246
if ( $navTab = self . _exist ( param ) ) {
250
247
self . active ( $navTab ) ;
248
+ if ( param . content === 'editor' ) { //Prohibited open more than 1 editor tab
249
+ self . _getTabPane ( $navTab ) . prepend ( '<div class="help-block alert alert-warning">' + options . language . editorUnsave . cover + '</div>' ) ;
250
+ }
251
251
return self ;
252
252
}
253
- index = getTabIndex ( param . content , options . navBar . maxTabs ) ;
254
253
param . active = param . active === undefined ? active : param . active ;
255
- id = 'multitabs_' + param . content + '_' + index ;
254
+ index = getTabIndex ( param . content , options . navBar . maxTabs ) ;
255
+ id = self . _generateId ( param , index ) ;
256
256
257
- //Prohibited open more than 1 editor tab
258
- if ( param . content === 'editor' && $editor . length && $editor . hasClass ( 'unsave' ) ) {
259
- $navTab = $el . navPanelList . find ( 'a[data-content="editor"]' ) ;
257
+ $navTab = self . _createNavTab ( param , index ) ;
258
+ //tab-pane create
259
+ self . _createTabPane ( param , index ) ;
260
+ //add tab to storage
261
+ self . _storage ( id , param ) ;
262
+ if ( param . active ) {
260
263
self . active ( $navTab ) ;
261
- $tabPane = self . _getTabPane ( $navTab ) ;
262
- $tabPane . before ( '<div class="help-block alert alert-warning">' + options . language . editorUnsave . cover + '</div>' ) ;
263
- return self ;
264
264
}
265
- //get layoutTemplates
266
- display = options . showClose ? 'display:inline;' : '' ;
267
- closeBtnHtml = ( param . content === 'main' ) ? '' : defaultLayoutTemplates . closeBtn . replace ( '{style}' , display ) ; //main content can not colse.
268
- navTabHtml = defaultLayoutTemplates . navTab
269
- . replace ( '{navTabId}' , id )
270
- . replace ( '{content}' , param . content )
271
- . replace ( '{index}' , index )
272
- . replace ( '{url}' , param . url )
273
- . replace ( '{title}' , param . title )
274
- + closeBtnHtml ;
275
- //tab create
276
- var $navTabLi = $el . navPanelList . find ( 'a[data-content="' + param . content + '"][data-index="' + index + '"]' ) . parent ( 'li' ) ;
277
- if ( $navTabLi . length ) {
278
- $navTabLi . html ( navTabHtml ) ;
279
- self . _getTabPane ( $navTabLi . find ( 'a:first' ) ) . remove ( ) ; //remove old content pane directly
280
- } else $el . navPanelList . append ( '<li>' + navTabHtml + '</li>' ) ;
281
- $navTab = $el . navPanelList . find ( 'a[data-id="' + id + '"]' ) ;
282
- //tab-pane create
265
+ return self ;
266
+ } ,
267
+
268
+ /**
269
+ * Create tab pane
270
+ * @param param
271
+ * @param index
272
+ * @returns {*|{} }
273
+ * @private
274
+ */
275
+ _createTabPane : function ( param , index ) {
276
+ var self = this , $el = self . $element , id = self . _generateId ( param , index ) ;
277
+ $el . tabContent . append ( self . _getTabPaneHtml ( param , index ) ) ;
278
+ return $el . tabContent . find ( '#' + id ) ;
279
+ } ,
280
+
281
+ /**
282
+ * get tab pane html
283
+ * @param param
284
+ * @param index
285
+ * @returns {string }
286
+ * @private
287
+ */
288
+ _getTabPaneHtml : function ( param , index ) {
289
+ var self = this , options = self . options ,
290
+ id = self . _generateId ( param , index ) ;
283
291
if ( param . iframe ) {
284
- tabPaneHtml = defaultLayoutTemplates . iframeTabPane
292
+ return defaultLayoutTemplates . iframeTabPane
285
293
. replace ( '{class}' , options . iframeTabPane . class )
286
294
. replace ( '{tabPaneId}' , id ) ;
287
295
} else {
288
- tabPaneHtml = defaultLayoutTemplates . ajaxTabPane
296
+ return defaultLayoutTemplates . ajaxTabPane
289
297
. replace ( '{class}' , options . ajaxTabPane . class )
290
298
. replace ( '{tabPaneId}' , id ) ;
291
299
}
292
- $el . tabContent . append ( tabPaneHtml ) ;
293
- //add tab to storage
294
- self . _storage ( id , param ) ;
295
- if ( param . active ) {
296
- self . active ( $navTab ) ;
300
+ } ,
301
+
302
+ /**
303
+ * create nav tab
304
+ * @param param
305
+ * @param index
306
+ * @returns {*|{} }
307
+ * @private
308
+ */
309
+ _createNavTab : function ( param , index ) {
310
+ var self = this , $el = self . $element ;
311
+ var navTabHtml = self . _getNavTabHtml ( param , index ) ;
312
+
313
+ var $navTabLi = $el . navPanelList . find ( 'a[data-content="' + param . content + '"][data-index="' + index + '"]' ) . parent ( 'li' ) ;
314
+ if ( $navTabLi . length ) {
315
+ $navTabLi . html ( navTabHtml ) ;
316
+ self . _getTabPane ( $navTabLi . find ( 'a:first' ) ) . remove ( ) ; //remove old content pane directly
317
+ } else {
318
+ $el . navPanelList . append ( '<li>' + navTabHtml + '</li>' ) ;
297
319
}
298
- return self ;
320
+ return $el . navPanelList . find ( 'a[data-content="' + param . content + '"][data-index="' + index + '"]' ) ;
321
+
322
+ } ,
323
+
324
+ /**
325
+ * get nav tab html
326
+ * @param param
327
+ * @param index
328
+ * @returns {string }
329
+ * @private
330
+ */
331
+ _getNavTabHtml : function ( param , index ) {
332
+ var self = this ,
333
+ options = self . options ;
334
+ var closeBtnHtml , display , id ;
335
+
336
+ id = self . _generateId ( param , index ) ;
337
+ display = options . showClose ? 'display:inline;' : '' ;
338
+ closeBtnHtml = ( param . content === 'main' ) ? '' : defaultLayoutTemplates . closeBtn . replace ( '{style}' , display ) ; //main content can not colse.
339
+ return defaultLayoutTemplates . navTab
340
+ . replace ( '{index}' , index )
341
+ . replace ( '{navTabId}' , id )
342
+ . replace ( '{url}' , param . url )
343
+ . replace ( '{title}' , param . title )
344
+ . replace ( '{content}' , param . content )
345
+ + closeBtnHtml ;
346
+ } ,
347
+
348
+ /**
349
+ * generate tab pane's id
350
+ * @param param
351
+ * @param index
352
+ * @returns {string }
353
+ * @private
354
+ */
355
+ _generateId : function ( param , index ) {
356
+ return 'multitabs_' + param . content + '_' + index ;
299
357
} ,
300
358
301
359
/**
0 commit comments