@@ -1019,6 +1019,9 @@ function SimpleMDE(options) {
1019
1019
if ( ! options . hasOwnProperty ( "status" ) ) {
1020
1020
options . status = [ "autosave" , "lines" , "words" , "cursor" ] ;
1021
1021
}
1022
+ if ( ! options . hasOwnProperty ( "statusCustom" ) ) {
1023
+ options . statusCustom = { } ;
1024
+ }
1022
1025
1023
1026
1024
1027
// Add default preview rendering function
@@ -1172,7 +1175,7 @@ SimpleMDE.prototype.render = function(el) {
1172
1175
if ( options . toolbar !== false ) {
1173
1176
this . createToolbar ( ) ;
1174
1177
}
1175
- if ( options . status !== false ) {
1178
+ if ( options . status !== false || options . statusCustom ) {
1176
1179
this . createStatusbar ( ) ;
1177
1180
}
1178
1181
if ( options . autosave != undefined && options . autosave . enabled === true ) {
@@ -1372,47 +1375,95 @@ SimpleMDE.prototype.createToolbar = function(items) {
1372
1375
} ;
1373
1376
1374
1377
SimpleMDE . prototype . createStatusbar = function ( status ) {
1378
+ var customOptions = this . options . statusCustom ;
1375
1379
status = status || this . options . status ;
1376
1380
var options = this . options ;
1377
1381
1378
1382
if ( ! status || status . length === 0 ) return ;
1379
1383
1380
- var bar = document . createElement ( "div" ) ;
1381
- bar . className = "editor-statusbar" ;
1384
+ // Copy the defaults if the status is a boolean true
1385
+ if ( status === true ) status = [ "autosave" , "lines" , "words" , "cursor" ] ;
1382
1386
1383
- var pos , cm = this . codemirror ;
1384
- for ( var i = 0 ; i < status . length ; i ++ ) {
1385
- ( function ( name ) {
1386
- var el = document . createElement ( "span" ) ;
1387
- el . className = name ;
1388
- if ( name === "words" ) {
1389
- el . innerHTML = "0" ;
1390
- cm . on ( "update" , function ( ) {
1391
- el . innerHTML = wordCount ( cm . getValue ( ) ) ;
1392
- } ) ;
1393
- } else if ( name == "characters" ) {
1387
+ // Set up the in-built actions: autosave, lines, words, cursor
1388
+ var actions = { } ;
1389
+ var i , name , onUpdate , defaultValue ;
1390
+ for ( i = 0 ; i < status . length ; i ++ ) {
1391
+ name = status [ i ] ;
1392
+
1393
+ if ( name === "words" ) {
1394
+ defaultValue = function ( el ) {
1394
1395
el . innerHTML = "0" ;
1395
- cm . on ( "update" , function ( ) {
1396
- el . innerHTML = cm . getValue ( ) . length ;
1397
- } ) ;
1398
- } else if ( name === "lines" ) {
1396
+ } ;
1397
+ onUpdate = function ( el ) {
1398
+ el . innerHTML = wordCount ( cm . getValue ( ) ) ;
1399
+ } ;
1400
+ } else if ( name === "lines" ) {
1401
+ defaultValue = function ( el ) {
1399
1402
el . innerHTML = "0" ;
1400
- cm . on ( "update" , function ( ) {
1401
- el . innerHTML = cm . lineCount ( ) ;
1402
- } ) ;
1403
- } else if ( name === "cursor" ) {
1403
+ } ;
1404
+ onUpdate = function ( el ) {
1405
+ el . innerHTML = cm . lineCount ( ) ;
1406
+ } ;
1407
+ } else if ( name === "cursor" ) {
1408
+ defaultValue = function ( el ) {
1404
1409
el . innerHTML = "0:0" ;
1405
- cm . on ( "cursorActivity" , function ( ) {
1406
- pos = cm . getCursor ( ) ;
1407
- el . innerHTML = pos . line + ":" + pos . ch ;
1408
- } ) ;
1409
- } else if ( name === "autosave" ) {
1410
+ } ;
1411
+ onUpdate = function ( el ) {
1412
+ pos = cm . getCursor ( ) ;
1413
+ el . innerHTML = pos . line + ":" + pos . ch ;
1414
+ } ;
1415
+ } else if ( name === "autosave" ) {
1416
+ defaultValue = function ( el ) {
1410
1417
if ( options . autosave != undefined && options . autosave . enabled === true ) {
1411
1418
el . setAttribute ( "id" , "autosaved" ) ;
1412
1419
}
1420
+ } ;
1421
+ }
1422
+ actions [ name ] = {
1423
+ onUpdate : onUpdate ,
1424
+ defaultValue : defaultValue
1425
+ } ;
1426
+ }
1427
+
1428
+ // Iterate any user-provided options
1429
+ for ( var key in customOptions ) {
1430
+ if ( customOptions . hasOwnProperty ( key ) ) {
1431
+ var thisOption = customOptions [ key ] ;
1432
+
1433
+ // Copy the option into the combined actions
1434
+ // This will allow the user to override the defaults
1435
+ actions [ key ] = {
1436
+ defaultValue : thisOption . defaultValue ,
1437
+ onUpdate : thisOption . onUpdate
1438
+ } ;
1439
+ }
1440
+ }
1441
+
1442
+ var bar = document . createElement ( "div" ) ;
1443
+ bar . className = "editor-statusbar" ;
1444
+
1445
+ var pos , cm = this . codemirror ;
1446
+ // Create a new span for each action
1447
+ for ( name in actions ) {
1448
+ if ( actions . hasOwnProperty ( name ) ) {
1449
+ var el = document . createElement ( "span" ) ;
1450
+ el . className = name ;
1451
+ // Ensure the defaultValue is a function
1452
+ if ( typeof actions [ name ] . defaultValue === "function" ) {
1453
+ actions [ name ] . defaultValue ( el ) ;
1454
+ }
1455
+ // Ensure the onUpdate is a function
1456
+ if ( typeof actions [ name ] . onUpdate === "function" ) {
1457
+ // Create a closure around the span and the name
1458
+ // of the current action, then execute the onUpdate handler
1459
+ cm . on ( "update" , ( function ( el , name ) {
1460
+ return function ( ) {
1461
+ actions [ name ] . onUpdate ( el ) ;
1462
+ } ;
1463
+ } ( el , name ) ) ) ;
1413
1464
}
1414
1465
bar . appendChild ( el ) ;
1415
- } ) ( status [ i ] ) ;
1466
+ }
1416
1467
}
1417
1468
1418
1469
var cmWrapper = this . codemirror . getWrapperElement ( ) ;
0 commit comments