2024-12-03 04:54:17 +00:00
'use strict' ;
2022-12-09 06:19:33 +00:00
/ * *
PAGE . js handles ANSItex and ViewData page frames
It also handles windows , horizontal / vertical scrolling and content paging .
This is inspired by frame . js provided by Synchronet
Objects :
+ Page - our display page , build with Windows
- Line 1 - Title ( Fixed )
- Line 2. . 23 - Content ( Scrollable )
- Line 24 - Status / Command Input ( Fixed )
2023-12-21 23:25:32 +00:00
= @ todo When scrolling is disabled , and the canvas is greater than the window , then "nextpage" returns the next frame
= Pageable windows cannot have children [ b - z frames ] , only "CONTENT" is paged
= @ todo Pageable windows are pagable when scrolling is false and window canvas . height > window . height and canvas . width = window . width
2022-12-09 06:19:33 +00:00
+ Window - size W x H , where W / H can be larger than the Screen
- Window holds all the content to be shown
- x , y - attributes define the position of the window in it ' s parent [ 1. . ]
- z - determines which layer the window is on , higher z is shown [ 0. . ]
- width / height - determines the physical size of the window ( cannot be larger than it ' s parent )
- canvas width / height - determines the logical size of the window , which if larger than physical enables scrolling
- ox / oy - determines the start of the canvas that is shown , relative to canvas width / height
- service - Current supported are ANSItex ( 80 x24 ) and ViewData ( 40 x24 )
- content - array of Chars height / width order
- visible - determines if the window ( and it ' s children ) are renderable
= Windows can be have children , and the z determines the layer shown relative to its parent
= Swapping z values determines which windows are hidden by others
+ Char - object holding each character , and it ' s color when rendered
= Rendering
- ANSItex
+ Each attribute can have it ' s own color ( colors take up no positional space )
+ We only change render control colors to change attributes when it actually changes , otherwise we render just the character
- ViewData
+ An attribute Foreground or Background or Special Function takes up a character
+ Character must be set to NULL when it ' s a control character
= EXAMPLE :
2023-12-24 09:11:40 +00:00
a = new Page ( ) // root frame 80 x 24 for ANSItex
2022-12-09 06:19:33 +00:00
b = new Window ( 1 , 1 , 40 , 22 , a . content ) // b frame 40 x 22 - starting at 1,1
c = new Window ( 41 , 1 , 40 , 22 , a . content ) // c frame 40 x 22 - starting at 41,1 (child of a)
d = new Window ( 1 , 1 , 21 , 10 , c ) // d frame 20 x 11 - starting at 1,1 of c
e = new Window ( 25 , 12 , 10 , 5 , c ) // e frame 10 x 5 - starting at 25,12 of c
f = new Window ( 15 , 8 , 13 , 7 , c ) // f frame 13 x 7 - starting at 15,8 of c
-- : _ _ _ _ . _ _ _ _ | _ _ _ _ . _ _ _ _ | _ _ _ _ . _ _ _ _ | _ _ _ _ . _ _ _ _ | _ _ _ _ . _ _ _ _ | _ _ _ _ . _ _ _ _ | _ _ _ _ . _ _ _ _ | _ _ _ _ . _ _ _ _ |
01 : TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
02 : 22222222222222222222222222222222222222224444444444444444444443333333333333333333
03 : 2 24 43 3
04 : 2 24 43 3
05 : 2 24 43 3
06 : 2 24 43 3
07 : 2 24 43 3
08 : 2 24 444444443333333 3
09 : 2 24 466666666666663 3
10 : 2 24 46 63 3
11 : 2 2444444444444446 63 3
12 : 2 2333333333333336 633333333 3
13 : 2 23 36 665555553 3
14 : 2 23 36 65 53 3
15 : 2 23 366666666666665 53 3
16 : 2 23 333333333335555 53 3
17 : 2 23 355555555553 3
18 : 2 23 333333333333 3
19 : 2 23 3
20 : 2 23 3
21 : 2 23 3
22 : 2 23 3
23 : 22222222222222222222222222222222222222223333333333333333333333333333333333333333
24 : PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
-- : _ _ _ _ . _ _ _ _ | _ _ _ _ . _ _ _ _ | _ _ _ _ . _ _ _ _ | _ _ _ _ . _ _ _ _ | _ _ _ _ . _ _ _ _ | _ _ _ _ . _ _ _ _ | _ _ _ _ . _ _ _ _ | _ _ _ _ . _ _ _ _ |
* /
2023-12-21 23:25:32 +00:00
load ( 'ansitex/load/windows.js' ) ; // Our supporting window class
2024-12-03 23:13:28 +00:00
load ( 'ansitex/load/msgbases.js' ) ; // To read/write to message bases
2023-12-24 06:44:02 +00:00
require ( 'sbbsdefs.js' , 'SS_USERON' ) ; // Need for our ANSI colors eg: BG_*
2022-12-09 06:19:33 +00:00
/ * *
* This object represents a full page that interacts with the user
*
* @ param service - Type of page ( tex = ANSI , vtx = VIEWDATA )
* @ param debug - Whether to dump some debug information . This is an int , and will start debugging on line debug
* @ constructor
*
* Pages have the following attributes :
* - dimensions - ( string ) representation of their width x height
* - dynamic _fields - ( array ) Location of fields that are dynamically filled
* - height - ( Number ) page height
* - input _fields - ( array ) Location of fields that take input
* - page - ( string ) Full page number ( frame + index )
* - width - ( Number ) page width
*
* Pages have the following settings :
* - cost - ( int ) Cost to view the page
* - page - ( object ) Frame / index of the page
* - provider - ( string ) Name of the frame provider
* - showHeader - ( boolean ) Whether to show the header when rendering the frame
* - type - ( TEX / VTX ) Type of frame
*
* Pages have the following public functions
* - build - Compile the frame for rendering
* - display - Display the compiled frame
2023-12-21 23:25:32 +00:00
* - import - Load a frame from a file source
* - save - Save the frame to the msgbase
* - load - Load the frame from the msgbase
2022-12-09 06:19:33 +00:00
* /
2023-12-24 09:11:40 +00:00
function Page ( debug ) {
2023-12-21 23:25:32 +00:00
this . _ _window _ _ = {
2022-12-09 06:19:33 +00:00
layout : undefined , // Window - Full page content
header : undefined , // Window - Page Title
provider : undefined , // Page provider (*)
pagenum : undefined , // Our page number (*)
cost : undefined , // Page cost (*)
2023-12-21 23:25:32 +00:00
body : undefined , // Window - Page body
} ;
this . _ _properties _ _ = {
name : new PageObject ,
2024-12-03 10:36:03 +00:00
//type: undefined, // Frame type (deprecated, see attrs)
attrs : 0 , // (Type 0, CUG 0 (public), Hidden=false)
2022-12-09 06:19:33 +00:00
input _fields : [ ] , // Array of our input fields
dynamic _fields : [ ] , // Array of our dynamic fields
2024-12-03 10:36:03 +00:00
//isAccessible: undefined, // Is this page visible to all users (deprecated, see attrs)
2023-12-27 11:24:20 +00:00
// If FALSE, only the SP can view/edit the frame
2024-12-03 10:36:03 +00:00
//isPublic: undefined, // Is this page visible to public (not CUG) users (deprecated, see attrs)
2023-12-27 11:24:20 +00:00
// If FALSE user must be a member of the CUG to view the frame
// All users, including unauthenticated, are members of 'system' (owner = 0)
// Frame's owned by the system where:
// isPublic is FALSE - the user must be logged in to view it
// isPublic is TRUE - can be viewed by non-logged in users
// Frame's owned by Service Providers where:
// isPublic is FALSE - can only be viewed if a user is
// a member of the Service Providers CUG
// isPublic is TRUE - can be viewed by users (if logged in)
2023-12-24 06:44:02 +00:00
key : [ ] , // Key actions
2024-12-03 23:13:28 +00:00
raw : { } , // Page raw content for each session type
2024-12-17 05:12:36 +00:00
date : undefined , // Date of frame
2022-12-09 06:19:33 +00:00
} ;
this . _ _defaults _ _ = {
attr : BG _BLACK | LIGHTGRAY ,
} ;
this . _ _compiled _ _ = {
2023-12-21 23:25:32 +00:00
build : undefined , // Our page compiled content
2022-12-09 06:19:33 +00:00
} ;
/ *
this . _ _settings _ _ = {
pageable : false , // If the virtual window is larger that height (and width is the same) next page is the next content
contenttitle : undefined , // Template (window) for 1st page (a)
contentsubtitle : undefined , // Template (window) for subsequent pages (b..z)
}
* /
/ * *
* @ todo borders for each window
* @ param service
* @ param debug
* /
2023-12-24 09:11:40 +00:00
function init ( debug ) {
log ( LOG _DEBUG , '- PAGE::init(): type [' + SESSION _EXT + ']' ) ;
this . _ _window _ _ . layout = new Window ( 1 , 1 , FRAME _WIDTH , FRAME _HEIGHT + 1 , 'LAYOUT' , this , debug ) ;
this . _ _window _ _ . body = new Window ( 1 , 2 , FRAME _WIDTH , FRAME _HEIGHT , 'CONTENT' , this . _ _window _ _ . layout , debug ) ;
this . _ _window _ _ . header = new Window ( 1 , 1 , FRAME _WIDTH , 1 , 'HEADER' , this . _ _window _ _ . layout , debug ) ;
this . _ _window _ _ . provider = new Window ( 1 , 1 , FRAME _PROVIDER _LENGTH , 1 , 'PROVIDER' , this . _ _window _ _ . header , debug ) ;
2022-12-09 06:19:33 +00:00
2023-12-24 06:44:02 +00:00
switch ( SESSION _EXT ) {
2022-12-09 06:19:33 +00:00
case 'tex' :
2023-12-24 06:44:02 +00:00
require ( 'ansitex/load/session/ansitex.js' , 'SESSION_ANSITEX' ) ;
2022-12-09 06:19:33 +00:00
2023-12-24 09:11:40 +00:00
this . _ _window _ _ . pagenum = new Window ( 57 , 1 , FRAME _PAGE _LENGTH , 1 , '#' , this . _ _window _ _ . header , debug ) ;
this . _ _window _ _ . cost = new Window ( 71 , 1 , FRAME _COST _LENGTH , 1 , '$' , this . _ _window _ _ . header , debug ) ;
2022-12-09 06:19:33 +00:00
break ;
case 'vtx' :
2023-12-24 06:44:02 +00:00
require ( 'ansitex/load/session/viewdata.js' , 'SESSION_VIEWDATA' ) ;
2022-12-09 06:19:33 +00:00
2023-12-24 09:11:40 +00:00
this . _ _window _ _ . pagenum = new Window ( 24 , 1 , FRAME _PAGE _LENGTH , 1 , '#' , this . _ _window _ _ . header , debug ) ;
this . _ _window _ _ . cost = new Window ( 35 , 1 , FRAME _COST _LENGTH , 1 , '$' , this . _ _window _ _ . header , debug ) ;
2022-12-09 06:19:33 +00:00
break ;
default :
2023-12-24 06:44:02 +00:00
throw new Error ( 'INVALID Page Service: ' + SESSION _EXT ) ;
2022-12-09 06:19:33 +00:00
}
}
/ * *
* Determine if this frame is accessible to the current user
* /
2024-12-03 10:36:03 +00:00
Object . defineProperty ( this , 'accessible' , {
get : function ( ) {
log ( LOG _DEBUG , '- Checking if user [' + user . number + '] can access frame: ' + this . name . toString ( ) ) ;
log ( LOG _DEBUG , '|* Frame Owner: ' + this . owner + ', System Frame: ' + this . isSystemPage ) ;
log ( LOG _DEBUG , '|* Accessible: ' + this . isAccessible ) ;
log ( LOG _DEBUG , '|* Public: ' + this . isPublic ) ;
log ( LOG _DEBUG , '|* Member: ' + this . isMember ) ;
// user.number 0 is unidentified user.
if ( user . number ) {
return (
( this . isAccessible && this . isSystemPage && ! this . isPublic ) ||
( this . isAccessible && this . isPublic ) ||
( this . isAccessible && ! this . isPublic && this . isMember ) ||
( pageEditor ( this . name . frame ) )
) ;
2022-12-09 06:19:33 +00:00
2024-12-03 10:36:03 +00:00
} else {
return this . isAccessible && this . isSystemPage && this . isPublic ;
}
2022-12-09 06:19:33 +00:00
}
} ) ;
2024-12-03 10:36:03 +00:00
// The page attributes, which determine accessible, public, cug and frame type
Object . defineProperty ( this , 'attrs' , {
get : function ( ) {
return this . _ _properties _ _ . attrs ;
} ,
/ * F r a m e a t t r i b u t e s a r e b i t i t e m s , b i t s :
* 1 - 4 TYPE
* 5 - 7 CUG
* 8 accessible
* /
set : function ( int ) {
if ( int === undefined )
int = 0 ;
this . _ _properties _ _ . attrs = int ;
}
2023-12-24 09:11:40 +00:00
} ) ;
2022-12-09 06:19:33 +00:00
2024-12-03 10:36:03 +00:00
Object . defineProperty ( this , 'cost' , {
get : function ( ) {
return Number ( this . _ _properties _ _ . cost ) ;
} ,
2023-12-24 06:44:02 +00:00
2024-12-03 10:36:03 +00:00
set : function ( int ) {
if ( int === undefined )
int = 0 ;
2023-12-24 09:11:40 +00:00
2024-12-03 10:36:03 +00:00
if ( typeof int !== 'number' )
throw new Error ( 'Cost must be a number' ) ;
2022-12-09 06:19:33 +00:00
2024-12-03 10:36:03 +00:00
this . _ _properties _ _ . cost = int ;
2022-12-09 06:19:33 +00:00
2024-12-03 10:36:03 +00:00
if ( ( '' + int ) . length > FRAME _COST _LENGTH - 1 - FRAME _ATTR _LENGTH )
throw new Error ( 'Cost too large' ) ;
2022-12-09 06:19:33 +00:00
2024-12-03 10:36:03 +00:00
// Populate the cost window
switch ( SESSION _EXT ) {
case 'tex' :
this . _ _window _ _ . cost . _ _properties _ _ . content = rawtoattrs ( ESC + '[1;32m' + padright ( int , FRAME _COST _LENGTH - 1 - FRAME _ATTR _LENGTH , ' ' ) + FRAME _COSTUNIT ) . content ;
2022-12-09 06:19:33 +00:00
2024-12-03 10:36:03 +00:00
break ;
case 'vtx' :
this . _ _window _ _ . cost . _ _properties _ _ . content = rawtoattrs ( VIEWDATA _BIN _GREEN + padright ( int , FRAME _COST _LENGTH - 1 - FRAME _ATTR _LENGTH , ' ' ) + FRAME _COSTUNIT ) . content ;
break ;
default :
throw new Error ( SESSION _EXT + ' type not implemented' ) ;
}
}
} ) ;
Object . defineProperty ( this , 'cug' , {
get : function ( ) {
return ( ( this . _ _properties _ _ . attrs >> 4 ) & 0x7 ) ;
2022-12-09 06:19:33 +00:00
}
} ) ;
2024-12-17 05:12:36 +00:00
Object . defineProperty ( this , 'date' , {
get : function ( ) {
if ( this . _ _properties _ _ . date !== undefined )
2024-12-19 10:52:35 +00:00
return strftime ( "%a, %d %b %Y %H:%M:%S %z" , this . _ _properties _ _ . date ) ;
2024-12-17 05:12:36 +00:00
} ,
set : function ( int ) {
if ( typeof int !== 'number' )
throw new Error ( 'DATE must be a epoch' ) ;
return this . _ _properties _ _ . date = int ;
}
} ) ;
2022-12-09 06:19:33 +00:00
Page . prototype . _ _defineGetter _ _ ( 'dimensions' , function ( ) {
2023-12-21 23:25:32 +00:00
return this . _ _properties _ _ . width + ' X ' + this . _ _properties _ _ . height ;
2022-12-09 06:19:33 +00:00
} ) ;
Page . prototype . _ _defineGetter _ _ ( 'dynamic_fields' , function ( ) {
2023-12-24 06:44:02 +00:00
return this . _ _properties _ _ . dynamic _fields === undefined ? [ ] : this . _ _properties _ _ . dynamic _fields ;
2022-12-09 06:19:33 +00:00
} ) ;
2023-12-21 23:25:32 +00:00
Page . prototype . _ _defineSetter _ _ ( 'dynamic_fields' , function ( array ) {
this . _ _properties _ _ . dynamic _fields = array ;
} ) ;
2022-12-09 06:19:33 +00:00
Page . prototype . _ _defineGetter _ _ ( 'height' , function ( ) {
2023-12-21 23:25:32 +00:00
return Number ( this . _ _window _ _ . layout . height ) ;
2022-12-09 06:19:33 +00:00
} ) ;
Page . prototype . _ _defineGetter _ _ ( 'input_fields' , function ( ) {
return this . _ _properties _ _ . input _fields ;
} ) ;
2023-12-21 23:25:32 +00:00
Page . prototype . _ _defineSetter _ _ ( 'input_fields' , function ( array ) {
this . _ _properties _ _ . input _fields = array ;
} ) ;
2022-12-09 06:19:33 +00:00
2024-12-03 10:36:03 +00:00
Object . defineProperty ( this , 'isAccessible' , {
get : function ( ) {
return ( this . _ _properties _ _ . attrs >> 7 !== 1 ) ;
}
2022-12-09 06:19:33 +00:00
} ) ;
2023-12-27 11:24:20 +00:00
/ * *
* Check if the user is already a member of the CUG
* /
2024-12-03 10:36:03 +00:00
Object . defineProperty ( this , 'isMember' , {
get : function ( ) {
// @todo Implement CUGs, this would be "=== SERVICE_PROVIDER" and user is a member of SERVICE_PROVIDER
return ( user . number > 0 ) && this . isSystemPage ;
}
2023-12-27 11:24:20 +00:00
} ) ;
2024-12-03 10:36:03 +00:00
// Is the page public or CUG
Object . defineProperty ( this , 'isPublic' , {
get : function ( ) {
return ( this . cug === 0 ) ;
}
} ) ;
2022-12-09 06:19:33 +00:00
2024-12-03 10:36:03 +00:00
// Is this a system page
Object . defineProperty ( this , 'isSystemPage' , {
get : function ( ) {
return ( this . owner === SYSTEM _OWNER ) ;
}
2023-12-24 06:44:02 +00:00
} ) ;
2024-12-03 04:54:17 +00:00
// Key Array
2023-12-24 06:44:02 +00:00
Page . prototype . _ _defineGetter _ _ ( 'key' , function ( ) {
return this . _ _properties _ _ . key ;
} ) ;
Page . prototype . _ _defineSetter _ _ ( 'key' , function ( array ) {
if ( typeof array !== 'object' )
throw new Error ( 'key must be an array :' + typeof array ) ;
2024-12-03 04:54:17 +00:00
if ( array . length !== 10 )
throw new Error ( 'key must contain 10 items :' + array ) ;
2023-12-24 06:44:02 +00:00
return this . _ _properties _ _ . key = array ;
2022-12-09 06:19:33 +00:00
} ) ;
Page . prototype . _ _defineGetter _ _ ( 'name' , function ( ) {
return this . _ _properties _ _ . name ;
} ) ;
Page . prototype . _ _defineSetter _ _ ( 'name' , function ( object ) {
if ( ! ( object instanceof PageObject ) )
throw new Error ( 'Page must be PageObject' ) ;
this . _ _properties _ _ . name = object ;
2023-12-24 09:11:40 +00:00
if ( ( '' + this . _ _properties _ _ . name . frame ) . length > FRAME _PAGE _LENGTH - 1 - FRAME _ATTR _LENGTH )
2024-12-19 10:52:35 +00:00
throw new Error ( 'Pagenum too large - ' + ( '' + this . _ _properties _ _ . name . frame ) . length + '|' + ( FRAME _PAGE _LENGTH - 1 - FRAME _ATTR _LENGTH ) ) ;
2023-12-24 09:11:40 +00:00
2023-12-24 06:44:02 +00:00
switch ( SESSION _EXT ) {
2022-12-09 06:19:33 +00:00
case 'tex' :
2023-12-24 09:11:40 +00:00
this . _ _window _ _ . pagenum . _ _properties _ _ . content = rawtoattrs ( ESC + '[1;37m' + this . _ _properties _ _ . name . toString ( ) ) . content ;
2022-12-09 06:19:33 +00:00
break ;
case 'vtx' :
2023-12-24 09:11:40 +00:00
this . _ _window _ _ . pagenum . _ _properties _ _ . content = rawtoattrs ( VIEWDATA _BIN _WHITE + this . _ _properties _ _ . name . toString ( ) ) . content ;
2022-12-09 06:19:33 +00:00
break ;
default :
2023-12-24 06:44:02 +00:00
throw new Error ( SESSION _EXT + ' type not implemented' ) ;
2022-12-09 06:19:33 +00:00
}
} ) ;
2024-12-03 10:36:03 +00:00
// Display who owns the page
Object . defineProperty ( this , 'owner' , {
get : function ( ) {
return pageOwner ( this . _ _properties _ _ . name . frame ) . prefix ;
} ,
} ) ;
2022-12-09 06:19:33 +00:00
Page . prototype . _ _defineGetter _ _ ( 'pagenext' , function ( ) {
return this . _ _properties _ _ . name . next ;
} ) ;
/ * *
* Determine who the owner of a page is
2024-12-03 10:36:03 +00:00
* @ deprecated use owner
2022-12-09 06:19:33 +00:00
* /
Page . prototype . _ _defineGetter _ _ ( 'pageowner' , function ( ) {
2023-12-21 23:25:32 +00:00
return pageOwner ( this . _ _properties _ _ . name . frame ) . prefix ;
2022-12-09 06:19:33 +00:00
} ) ;
Page . prototype . _ _defineSetter _ _ ( 'provider' , function ( ansi ) {
var provider ;
2023-12-24 06:44:02 +00:00
switch ( SESSION _EXT ) {
2022-12-09 06:19:33 +00:00
case 'tex' :
2023-12-24 09:11:40 +00:00
provider = rawtoattrs ( ansi + ESC + '[0m' ) . content ;
2022-12-09 06:19:33 +00:00
2023-12-24 09:11:40 +00:00
if ( provider [ 1 ] . filter ( function ( child ) { return child . ch ; } ) . length - 1 > FRAME _PROVIDER _LENGTH )
2022-12-09 06:19:33 +00:00
throw new Error ( 'Provider too large' ) ;
break ;
case 'vtx' :
2023-12-24 09:11:40 +00:00
provider = rawtoattrs ( ansi ) . content ;
2022-12-09 06:19:33 +00:00
2023-12-24 09:11:40 +00:00
if ( provider [ 1 ] . length - 1 > FRAME _PROVIDER _LENGTH )
2022-12-09 06:19:33 +00:00
throw new Error ( 'Provider too large' ) ;
break ;
default :
2023-12-24 06:44:02 +00:00
throw new Error ( SESSION _EXT + ' not implemented' ) ;
2022-12-09 06:19:33 +00:00
}
2023-12-21 23:25:32 +00:00
this . _ _window _ _ . provider . _ _properties _ _ . content = provider ;
2022-12-09 06:19:33 +00:00
} ) ;
2024-12-03 23:13:28 +00:00
Object . defineProperty ( this , 'raw' , {
get : function ( ) {
return this . _ _properties _ _ . raw
} ,
set : function ( value ) {
this . _ _properties _ _ . raw [ SESSION _EXT ] = value ;
}
2023-12-27 11:24:20 +00:00
} ) ;
2022-12-09 06:19:33 +00:00
Page . prototype . _ _defineSetter _ _ ( 'showHeader' , function ( bool ) {
if ( typeof bool !== 'boolean' )
throw new Error ( 'showHeader expected a true/false' ) ;
2023-12-21 23:25:32 +00:00
this . _ _window _ _ . header . visible = bool ;
2022-12-09 06:19:33 +00:00
} ) ;
2024-12-03 10:36:03 +00:00
Object . defineProperty ( this , 'type' , {
get : function ( ) {
return this . _ _properties _ _ . attrs & 0xf ;
} ,
} ) ;
2023-12-24 06:44:02 +00:00
Page . prototype . _ _defineGetter _ _ ( 'type' , function ( ) {
return this . _ _properties _ _ . type ;
} ) ;
Page . prototype . _ _defineSetter _ _ ( 'type' , function ( string ) {
if ( typeof string !== 'string' )
throw new Error ( 'type must be an string :' + typeof string ) ;
return this . _ _properties _ _ . type = string ;
} ) ;
2022-12-09 06:19:33 +00:00
Page . prototype . _ _defineGetter _ _ ( 'width' , function ( ) {
2023-12-21 23:25:32 +00:00
return Number ( this . _ _window _ _ . layout . width ) ;
2022-12-09 06:19:33 +00:00
} ) ;
/ * *
* Build the screen layout
*
* @ returns { * }
* /
this . build = function ( force ) {
2023-12-27 11:24:20 +00:00
log ( LOG _DEBUG , '* Building frame...' ) ;
2022-12-09 06:19:33 +00:00
if ( this . _ _compiled _ _ . build && ! force )
throw new Error ( 'Refusing to build without force.' ) ;
this . build _system _fields ( ) ;
2023-12-21 23:25:32 +00:00
this . _ _compiled _ _ . build = this . _ _window _ _ . layout . build ( 1 , 1 , false ) ;
2022-12-09 06:19:33 +00:00
// Add our dynamic values
var fields = this . dynamic _fields . filter ( function ( item ) { return item . value !== undefined ; } ) ;
2023-12-27 11:24:20 +00:00
log ( LOG _DEBUG , '|* We have DF fields:' + fields . length ) ;
2022-12-09 06:19:33 +00:00
if ( fields . length )
insert _fields ( fields , this . _ _compiled _ _ . build ) ;
// Add our dynamic values
fields = this . input _fields . filter ( function ( item ) { return item . value !== undefined ; } ) ;
2023-12-27 11:24:20 +00:00
log ( LOG _DEBUG , '|* We have INPUT fields:' + fields . length ) ;
2022-12-09 06:19:33 +00:00
if ( fields . length )
insert _fields ( fields , this . _ _compiled _ _ . build ) ;
// Insert our *_field data (if it is set)
function insert _fields ( fields , build ) {
for ( var i in fields ) {
2024-12-23 03:52:45 +00:00
log ( LOG _DEBUG , '|-- Adding:' + fields [ i ] . name + ', with value:' + fields [ i ] . value ) ;
2022-12-09 06:19:33 +00:00
var content = fields [ i ] . value . split ( '' ) ;
2024-12-03 04:54:17 +00:00
for ( var x = fields [ i ] . x ; x < fields [ i ] . x + Math . abs ( fields [ i ] . length ) ; x ++ ) {
2022-12-09 06:19:33 +00:00
var index = x - fields [ i ] . x ;
2024-12-08 07:00:24 +00:00
//log(LOG_DEBUG,'|-- i:'+i+', x:'+x+', y:'+fields[i].y);
2022-12-09 06:19:33 +00:00
if ( content [ index ] )
2024-12-08 07:00:24 +00:00
build [ fields [ i ] . y ] [ x ] . ch = ( ( fields [ i ] . type !== FIELD _PASSWORD ) ? content [ index ] : FIELD _PASSWORD _MASK ) ;
else
build [ fields [ i ] . y ] [ x ] . ch = ( fields [ i ] . pad ? fields [ i ] . pad : '.' ) ;
2022-12-09 06:19:33 +00:00
}
}
}
}
/ * *
2024-01-01 06:03:25 +00:00
* Build in any input _fields with values
2022-12-09 06:19:33 +00:00
* /
2024-01-01 11:46:53 +00:00
this . build _input _fields = function ( ) {
2024-01-01 06:03:25 +00:00
var that = this ;
2022-12-09 06:19:33 +00:00
2024-01-01 06:03:25 +00:00
var f = this . input _fields . filter ( function ( item ) { return item . value . length ; } ) ;
log ( LOG _DEBUG , '* INPUT_FIELDS WITH VALUES:' + f . length ) ;
if ( f . length ) {
f . forEach ( function ( field ) {
that . input _field ( field . name , field . value ) ;
} ) ;
this . _ _compiled _ _ . build = null ;
}
}
2022-12-09 06:19:33 +00:00
2024-01-01 06:03:25 +00:00
/ * *
* Build in our dynamic _fields that can be populated automatically
* /
this . build _system _fields = function ( context ) {
2024-12-23 03:52:45 +00:00
log ( LOG _DEBUG , 'Building system fields with context:' + context ) ;
2022-12-09 06:19:33 +00:00
var that = this ;
2024-01-01 06:03:25 +00:00
var f = this . dynamic _fields . filter ( function ( item ) { return item . value === undefined ; } ) ;
if ( f . length ) {
2024-12-23 03:52:45 +00:00
f . forEach ( function ( field , key ) {
log ( LOG _DEBUG , 'Key is:' + key ) ;
var ac = atcode ( field . name , field . length , field . pad , context ) ;
that . dynamic _field ( field . name , ac . value ) ;
if ( ac . key ) {
log ( LOG _DEBUG , 'ATCODE sets key for [' + field . name + '] using [' + ac . key + ']' ) ;
that . _ _properties _ _ . key [ ac . value ] = ac . key ;
}
2024-01-01 06:03:25 +00:00
} ) ;
}
2022-12-09 06:19:33 +00:00
}
/ * *
* Return the compiled screen as an array of lines
*
* @ param last - the last attribute sent to the screen
* @ param color - whether to render the color attributes
* /
this . display = function ( last , color ) {
var debug = false ;
if ( ! this . _ _compiled _ _ . build )
this . build ( ) ;
// Our built display
var display = this . _ _compiled _ _ . build ;
// Default attribute when the screen is cleared
var new _screen ;
// Default attribute when a new line is started
var new _line ;
var result = [ ] ;
var attr ;
2023-12-24 09:11:40 +00:00
new _screen = BG _BLACK | LIGHTGRAY ;
2023-12-24 06:44:02 +00:00
switch ( SESSION _EXT ) {
2022-12-09 06:19:33 +00:00
case 'tex' :
break ;
case 'vtx' :
new _line = BG _BLACK | LIGHTGRAY ;
break ;
default :
throw new Error ( SESSION _EXT + ' dump processing not implemented' ) ;
}
if ( last === undefined )
last = new _screen ;
// Check all our dynamic fields have been placed
2024-12-03 04:54:17 +00:00
var df = this . dynamic _fields . filter ( function ( item ) { return item . value === undefined ; } ) ;
2022-12-09 06:19:33 +00:00
// If our dynamic fields havent been filled in
if ( df . length > 0 )
2023-12-27 11:24:20 +00:00
throw new Error ( 'Dynamic fields [' + df . length + '] without values:' + ( df . map ( function ( item ) { return item . name ; } ) . join ( '|' ) ) ) ;
2022-12-09 06:19:33 +00:00
// Render the display
2024-12-03 04:54:17 +00:00
for ( var y = 1 ; y <= this . height ; y ++ ) {
2022-12-09 06:19:33 +00:00
var line = '' ;
if ( new _line )
last = new _line ;
if ( debug )
writeln ( '============== [' + y + '] ===============' ) ;
2024-12-03 04:54:17 +00:00
for ( var x = 1 ; x <= this . width ; x ++ ) {
2022-12-09 06:19:33 +00:00
if ( debug )
log ( LOG _DEBUG , '* CELL : y:' + y + ', x:' + x ) ;
// The current char value
var char = ( display [ y ] !== undefined && display [ y ] [ x ] !== undefined ) ? display [ y ] [ x ] : undefined ;
if ( debug )
2023-12-21 23:25:32 +00:00
log ( LOG _DEBUG , ' - CHAR : ' + ( char !== undefined ? char . ch : 'undefined' ) + ', ATTR:' + ( char !== undefined ? char . attr : 'undefined' ) + ', LAST:' + last ) ;
2022-12-09 06:19:33 +00:00
if ( debug ) {
writeln ( ) ;
writeln ( '-------- [' + x + '] ------' ) ;
2023-12-21 23:25:32 +00:00
writeln ( 'y:' + y + ',x:' + x + ', attr:' + ( char !== undefined ? char . attr : 'undefined' ) ) ;
2022-12-09 06:19:33 +00:00
}
if ( ( color === undefined ) || color ) {
// Only write a new attribute if it has changed (and not Videotex)
2023-12-24 06:44:02 +00:00
if ( ( SESSION _EXT === 'vtx' ) || ( last === undefined ) || ( last !== char . attr ) ) {
2022-12-09 06:19:33 +00:00
// The current attribute for this character
2023-12-24 06:44:02 +00:00
attr = ( char === undefined ) ? undefined : char . attribute ( last , SESSION _EXT , debug ) ;
2022-12-09 06:19:33 +00:00
2023-12-24 06:44:02 +00:00
switch ( SESSION _EXT ) {
2022-12-09 06:19:33 +00:00
case 'tex' :
// If the attribute is null, we'll write our default attribute
if ( attr === null )
2023-12-21 23:25:32 +00:00
line += this . attr
2022-12-09 06:19:33 +00:00
else
line += ( attr !== undefined ) ? attr : '' ;
break ;
case 'vtx' :
// If the attribute is null, we'll ignore it since we are drawing a character
if ( ( attr !== undefined ) && ( attr !== null ) ) {
if ( debug )
log ( LOG _DEBUG , ' = SEND ATTR :' + attr + ', attr length:' + attr . length + ', last:' + last ) ;
line += attr ;
}
break ;
default :
2023-12-24 06:44:02 +00:00
throw new Error ( 'service type:' + SESSION _EXT + ' hasnt been implemented.' ) ;
2022-12-09 06:19:33 +00:00
}
}
// For no-color output and ViewData, we'll render a character
} else {
2023-12-24 06:44:02 +00:00
if ( ( SESSION _EXT === 'vtx' ) && char . attr )
2022-12-09 06:19:33 +00:00
line += '^' ;
}
if ( char . ch !== undefined ) {
if ( debug )
2023-12-21 23:25:32 +00:00
log ( LOG _DEBUG , ' = SEND CHAR :' + char . ch + ', attr:' + char . attr + ', last:' + last ) ;
2022-12-09 06:19:33 +00:00
line += ( char . ch !== null ) ? char . ch : '' ;
} else {
if ( debug )
log ( LOG _DEBUG , ' = CHAR UNDEFINED' ) ;
line += ' ' ;
}
2023-12-21 23:25:32 +00:00
last = ( char . attr === undefined ) ? undefined : char . attr ;
2022-12-09 06:19:33 +00:00
}
result . push ( line ) ;
if ( debug && ( y > debug ) )
exit ( 1 ) ;
}
return result ;
}
/ * *
* Dump a page in an axis grid to view that it renders correctly
*
* @ param last - ( int ) The current cursor color
* @ param color - ( bool ) Optionally show color
* @ param debug - ( int ) Debugging mode starting at line
*
* @ note When drawing a Char :
*
* | CH | ATTR | RESULT |
* | -- -- -- -- -- -- | -- -- -- -- -- -- | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
* | undefined | undefined | no output ( cursor advances 1 ) | NOOP
* | null | undefined | invalid |
* | defined | undefined | invalid |
* | undefined | null | invalid |
* | null | null | invalid |
* | defined | null | render ch only ( cursor advances 1 ) | Viewdata
* | undefined | defined | render attr only ( no cursor move ) | ANSItex ( used to close the edge of a window )
* | null | defined | render attr only ( cursor advances 1 ) | Viewdata
* | defined | defined | render attr + ch ( cursor advances 1 ) | ANSItex
* | -- -- -- -- -- -- | -- -- -- -- -- -- | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- |
*
* + for ANSItex , attribute ( s ) dont advance the cursor , clear screen sets the default to BG _BLACK | LIGHTGRAY
* + for ViewData , an attribute does advance the cursor , and each attribute advances the cursor , also each new line starts with a default BG _BLACK | WHITE
* /
this . dump = function ( last , color , debug ) {
if ( ! this . _ _compiled _ _ . build )
this . build ( ) ;
// Our built display
var display = this . _ _compiled _ _ . build ;
color = ( color === undefined ) || ( color === '1' ) || ( color === true ) ;
2023-12-24 06:44:02 +00:00
writeln ( 'Dumping Page:' + this . name . toString ( ) ) ;
2022-12-09 06:19:33 +00:00
writeln ( '= Size :' + this . dimensions ) ;
writeln ( '- Last :' + last ) ;
writeln ( '- Color:' + color ) ;
writeln ( '- Debug:' + debug ) ;
if ( last === undefined )
last = new _screen ;
if ( debug ) {
writeln ( '==== content dump ====' ) ;
var yy = 1 ;
for ( var y in display ) {
write ( padright ( yy , 2 , 0 ) + ':' ) ;
var xx = 1 ;
for ( var x in display [ y ] ) {
if ( debug && ( y === debug ) ) {
writeln ( JSON . stringify ( display [ y ] [ x ] ) ) ;
writeln ( )
}
write ( '[' ) ;
2023-12-21 23:25:32 +00:00
if ( display [ y ] [ x ] . attr === undefined ) {
2022-12-09 06:19:33 +00:00
// NOOP
2023-12-21 23:25:32 +00:00
} else if ( display [ y ] [ x ] . attr === null ) {
2022-12-09 06:19:33 +00:00
// NOOP
} else {
try {
2023-12-21 23:25:32 +00:00
write ( ( last === display [ y ] [ x ] . attr ) ? '' : display [ y ] [ x ] . attr ) ;
2022-12-09 06:19:33 +00:00
} catch ( e ) {
writeln ( ) ;
writeln ( 'error:' + e ) ;
writeln ( ' y:' + y ) ;
writeln ( ' x:' + x ) ;
2023-12-21 23:25:32 +00:00
writeln ( JSON . stringify ( display [ y ] [ x ] . attr ) ) ;
2022-12-09 06:19:33 +00:00
exit ( 1 ) ;
}
}
write ( ':' ) ;
2023-12-21 23:25:32 +00:00
if ( display [ y ] [ x ] . ch === undefined ) {
2022-12-09 06:19:33 +00:00
// NOOP - No window filled a character at this location
2023-12-21 23:25:32 +00:00
write ( ( display [ y ] [ x ] . attr === undefined ) ? '--' : '' ) ;
} else if ( display [ y ] [ x ] . ch === null ) {
2022-12-09 06:19:33 +00:00
// NOOP
} else {
2023-12-21 23:25:32 +00:00
write ( '_' + display [ y ] [ x ] . ch ) ;
2022-12-09 06:19:33 +00:00
}
write ( ']' ) ;
2023-12-21 23:25:32 +00:00
last = display [ y ] [ x ] . attr ;
2022-12-09 06:19:33 +00:00
xx ++ ;
}
writeln ( '|' + padright ( xx - 1 , 2 , 0 ) ) ;
xx = 0 ;
yy ++ ;
}
// Detail dump when debug is a line number
if ( debug && ( y > debug ) ) {
writeln ( '==========================' ) ;
for ( var y in display ) {
writeln ( '------ [' + y + '] -------' ) ;
var xx = 1 ;
for ( var x in display [ y ] ) {
2023-12-21 23:25:32 +00:00
var attr = display [ y ] [ x ] . attr ;
2022-12-09 06:19:33 +00:00
2023-12-24 06:44:02 +00:00
writeln ( 'X:' + ( xx ++ ) + '|' + attr + ':' + display [ y ] [ x ] . ch + '|' + display [ y ] [ x ] . attribute ( last , SESSION _EXT , debug ) ) ;
2022-12-09 06:19:33 +00:00
// Only write a new attribute if it has changed
if ( ( this . last === undefined ) || ( this . last !== attr ) ) {
this . last = attr ;
}
}
}
}
writeln ( '==== END content dump ====' ) ;
}
// Dump Header
write ( '--:' ) ;
2024-12-03 04:54:17 +00:00
for ( var x = 0 ; x < this . width ; x += 10 ) {
2022-12-09 06:19:33 +00:00
write ( '_' . repeat ( 4 ) + '.' + '_' . repeat ( 4 ) + '|' ) ;
}
writeln ( ) ;
var result = this . display ( last , color ) ;
// We draw line by line.
for ( var y = 1 ; y <= this . height ; y ++ ) {
// Line intro
if ( color )
write ( '\x1b[0m' ) ;
write ( padright ( y , 2 , 0 ) + ':' ) ;
writeln ( result [ y - 1 ] ) ;
}
// Dump Header
write ( '--:' ) ;
2024-12-03 04:54:17 +00:00
for ( var x = 0 ; x < this . width ; x += 10 ) {
2022-12-09 06:19:33 +00:00
write ( '_' . repeat ( 4 ) + '.' + '_' . repeat ( 4 ) + '|' ) ;
}
writeln ( ) ;
if ( this . input _fields . length ) {
writeln ( '= Input Fields:' )
this . input _fields . forEach ( function ( x ) {
writeln ( ' - ' + x . name + ', type:' + x . type + ', length:' + x . length + ', value:' + x . value ) ;
} )
}
if ( this . dynamic _fields . length ) {
writeln ( '= Dynamic Fields:' )
this . dynamic _fields . forEach ( function ( x ) {
writeln ( ' - ' + x . name + ', length:' + Math . abs ( x . length ) + ', pad:' + x . pad + ', value:' + x . value ) ;
} )
}
// Reset our color
if ( color )
write ( '\x1b[0m' ) ;
}
/ * *
* Set the value for a dynamic field
*
* @ param field
* @ param value
* /
this . dynamic _field = function ( field , value ) {
var fields = this . dynamic _fields . filter ( function ( item ) { return item . name === field ; } ) ;
if ( fields . length !== 1 )
throw new Error ( 'Dynamic field: ' + field + ', doesnt exist?' ) ;
// Store our value
this . dynamic _fields [ this . dynamic _fields . indexOf ( fields [ 0 ] ) ] . value = value ;
}
2023-12-24 06:44:02 +00:00
/ * *
* Save the frame for later retrieval
* @ todo Inject back all input _fields and dynamic _fields
* @ todo this is not complete ?
* /
this . export = function ( ) {
var line ;
// If we have any input fields, we need to insert them back inside ESC .... ESC \ control codes
// @todo avoid the ending ESC \ with a control code.
this . input _fields . filter ( function ( child ) {
if ( child . y === y ) {
line . content = line . content . substring ( 0 , child . x - 1 )
+ 'FIELD:' + child . name
+ line . content . substring ( child . x + child . length , 80 )
+ 'END' ;
}
} )
// We draw line by line.
for ( var y = 1 ; y <= this . height ; y ++ ) {
// Line intro
write ( '\x1b[0m' ) ;
line = this . _ _window _ _ . layout . drawline ( 1 , this . width , y , false ) ;
write ( line . content ) ;
write ( '\x1b[0m' ) ;
writeln ( ) ;
}
}
2024-12-03 23:13:28 +00:00
Page . prototype . file _content = function ( filename ) {
log ( LOG _DEBUG , '|-- Importing frame content: [' + filename + ']' ) ;
var f = new File ( filename ) ;
if ( ! f . exists || ! f . open ( 'rb' , true ) ) {
log ( LOG _ERROR , '|? File doesnt exist: [' + filename + ']' ) ;
2024-12-09 05:12:49 +00:00
return undefined ;
2024-12-03 23:13:28 +00:00
}
var content = f . read ( ) ;
f . close ( ) ;
return content ;
} ;
2022-12-09 06:19:33 +00:00
/ * *
* Load a specific page
*
* @ param page
* /
2023-12-24 06:44:02 +00:00
this . get = function ( page ) {
2022-12-09 06:19:33 +00:00
if ( ! ( page instanceof PageObject ) )
throw new Error ( 'page must be a PageObject' ) ;
2024-12-03 10:36:03 +00:00
this . name = page ;
2024-01-01 06:03:25 +00:00
// Load a page from disk first if it exists
2024-12-03 10:36:03 +00:00
if ( FRAMES _MSG _FILES && this . import ( FRAMES _HOME + page . toString ( ) , SESSION _EXT ) )
2023-12-24 06:44:02 +00:00
return true ;
2024-01-01 06:03:25 +00:00
// Fall back to loading from msgbase
2024-12-03 23:13:28 +00:00
return FRAMES _MSG _BASE && this . load ( page ) ;
2022-12-09 06:19:33 +00:00
}
/ * *
* Set the value for an input field
*
* @ param field
* @ param value
* /
this . input _field = function ( field , value ) {
var fields = this . input _fields . filter ( function ( item ) { return item . name === field ; } ) ;
if ( fields . length !== 1 )
throw new Error ( 'Input field: ' + field + ', doesnt exist?' ) ;
// Store our value
this . input _fields [ this . input _fields . indexOf ( fields [ 0 ] ) ] . value = value ;
}
/ * *
* Load a frame from a file
*
2023-12-21 23:25:32 +00:00
* @ param filename - Name of file to load page from ( SBBS default dir is CTRL , so relative to it )
2022-12-09 06:19:33 +00:00
* @ returns { boolean }
* /
2024-12-03 23:13:28 +00:00
Page . prototype . import = function ( filename ) {
2023-12-27 11:24:20 +00:00
log ( LOG _DEBUG , '|- Importing frame: [' + filename + ']' ) ;
2022-12-09 06:19:33 +00:00
var f = new File ( filename ) ;
2023-12-21 23:25:32 +00:00
if ( ! f . exists || ! f . open ( 'rb' , true ) ) {
2024-01-01 06:03:25 +00:00
log ( LOG _ERROR , '|? File doesnt exist: [' + filename + ']' ) ;
2024-12-03 23:13:28 +00:00
return false ;
2023-12-21 23:25:32 +00:00
}
2024-12-03 23:13:28 +00:00
var contents = JSON . parse ( f . read ( ) ) ;
2024-12-07 08:32:42 +00:00
contents . date = f . date ;
2022-12-09 06:19:33 +00:00
f . close ( ) ;
2024-12-03 23:13:28 +00:00
// Load the page content
if ( contents . version === 2 )
contents . content = this . file _content ( FRAMES _HOME + SESSION _EXT + '/' + this . name . toString ( ) + '.' + CONTENT _EXT ) ;
2022-12-09 06:19:33 +00:00
2024-12-09 05:12:49 +00:00
// The content file doesnt exist
if ( contents . content === undefined )
return false ;
2024-12-03 23:13:28 +00:00
var valid _sauce = false ;
2024-01-01 06:03:25 +00:00
2024-12-03 23:13:28 +00:00
if ( contents . content . substr ( - 128 , 7 ) === 'SAUCE00' ) {
var sauceless _size = ascii ( contents . content . substr ( - 35 , 1 ) ) ;
2022-12-09 06:19:33 +00:00
sauceless _size <<= 8 ;
2024-12-03 23:13:28 +00:00
sauceless _size |= ascii ( contents . content . substr ( - 36 , 1 ) ) ;
2022-12-09 06:19:33 +00:00
sauceless _size <<= 8 ;
2024-12-03 23:13:28 +00:00
sauceless _size |= ascii ( contents . content . substr ( - 37 , 1 ) ) ;
2022-12-09 06:19:33 +00:00
sauceless _size <<= 8 ;
2024-12-03 23:13:28 +00:00
sauceless _size |= ascii ( contents . content . substr ( - 38 , 1 ) ) ;
2022-12-09 06:19:33 +00:00
2024-12-03 23:13:28 +00:00
var data _type = ascii ( contents . content . substr ( - 34 , 1 ) ) ;
var file _type = ascii ( contents . content . substr ( - 33 , 1 ) ) ;
2022-12-09 06:19:33 +00:00
2024-12-03 23:13:28 +00:00
var tinfo1 = ascii ( contents . content . substr ( - 31 , 1 ) ) ;
2022-12-09 06:19:33 +00:00
tinfo1 <<= 8 ;
2024-12-03 23:13:28 +00:00
tinfo1 |= ascii ( contents . content . substr ( - 32 , 1 ) ) ;
2022-12-09 06:19:33 +00:00
2024-12-03 23:13:28 +00:00
var tinfo2 = ascii ( contents . content . substr ( - 29 , 1 ) ) ;
2022-12-09 06:19:33 +00:00
tinfo2 <<= 8 ;
2024-12-03 23:13:28 +00:00
tinfo2 |= ascii ( contents . content . substr ( - 30 , 1 ) ) ;
2022-12-09 06:19:33 +00:00
2024-12-09 10:21:14 +00:00
var width ;
var height ;
2022-12-09 06:19:33 +00:00
switch ( data _type ) {
case 1 :
switch ( file _type ) {
// Plain ASCII
case 0 :
2024-12-03 23:13:28 +00:00
var ext = 'TXT' ;
2022-12-09 06:19:33 +00:00
if ( tinfo1 )
width = tinfo1 ;
if ( tinfo2 )
height = tinfo2 ;
break ;
// ANSI
case 1 :
2024-12-03 23:13:28 +00:00
var ext = 'ANS' ;
2022-12-09 06:19:33 +00:00
if ( tinfo1 )
width = tinfo1 ;
if ( tinfo2 )
height = tinfo2 ;
break ;
// Source
case 7 :
2024-12-03 23:13:28 +00:00
var ext = 'TXT' ;
2022-12-09 06:19:33 +00:00
break ;
}
valid _sauce = true ;
break ;
case 5 :
2024-12-03 23:13:28 +00:00
var ext = 'BIN' ;
2022-12-09 06:19:33 +00:00
width = file _type * 2 ;
height = ( sauceless _size / 2 ) / width ;
valid _sauce = true ;
break ;
}
if ( valid _sauce )
2024-12-03 23:13:28 +00:00
contents . content = contents . content . substr ( 0 , sauceless _size ) ;
2022-12-09 06:19:33 +00:00
}
2024-12-03 23:13:28 +00:00
return this . preload ( contents , SESSION _EXT ) ;
2023-12-24 06:44:02 +00:00
}
2024-12-03 23:13:28 +00:00
// Load a frame from a message base
2024-01-01 06:03:25 +00:00
this . load = function ( page ) {
2024-12-03 23:13:28 +00:00
var page = this . name . toString ( ) ;
var mb = new MsgAreas ( ) . getArea ( FRAMES _MSG _BASE )
var msg = mb . frames
. sort ( function ( a , b ) {
if ( a . when _imported _time === b . when _imported _time )
2024-12-09 10:46:39 +00:00
return a . number > b . number
2024-12-03 23:13:28 +00:00
else
2024-12-09 10:46:39 +00:00
return ( a . when _imported _time > b . when _imported _time ) ; } )
2024-12-07 09:19:42 +00:00
. filter ( function ( item ) { return ( item . from === 'SYSTEM' ) && ( item . to === page ) ; } ) . pop ( ) ;
2024-01-01 06:03:25 +00:00
if ( msg === undefined ) {
log ( LOG _DEBUG , '|- Frame not found: [' + page . toString ( ) + '] in [' + FRAMES _MSG _BASE + ']' ) ;
return false ;
} else {
2024-12-03 23:13:28 +00:00
log ( LOG _DEBUG , '|- Loading frame: [' + page . toString ( ) + '] from msgbase [' + msg . number + '] [' + SESSION _EXT + ']' ) ;
2024-01-02 12:56:47 +00:00
2024-12-03 23:13:28 +00:00
var contents = mb . getContent ( msg . number ) ;
2024-12-07 08:32:42 +00:00
2024-12-09 05:12:49 +00:00
if ( ( contents === undefined ) || ( contents . content [ SESSION _EXT ] === undefined ) )
2024-12-07 08:32:42 +00:00
return false ;
2024-12-03 23:13:28 +00:00
contents . content = contents . content [ SESSION _EXT ] ;
2024-12-07 09:19:42 +00:00
log ( LOG _DEBUG , '|/ Content:' + contents . content ) ;
2024-01-02 12:56:47 +00:00
2024-12-03 23:13:28 +00:00
return this . preload ( contents , SESSION _EXT ) ;
2024-01-01 06:03:25 +00:00
}
return false ;
}
/ * *
* After page load routines
* /
this . loadcomplete = function ( ) {
var po = pageOwner ( this . name . frame ) ;
switch ( SESSION _EXT ) {
case 'tex' :
this . _ _window _ _ . pagenum . _ _properties _ _ . content = rawtoattrs ( ESC + '[1;37m' + this . name . toString ( ) ) . content ;
this . provider = base64 _decode ( po . logoans ) ;
break ;
case 'vtx' :
this . _ _window _ _ . pagenum . _ _properties _ _ . content = rawtoattrs ( VIEWDATA _BIN _WHITE + this . name . toString ( ) ) . content ;
this . provider = base64 _decode ( po . logovtx ) ;
break ;
default :
throw new Error ( SESSION _EXT + ' hasnt been implemented' ) ;
}
// Dont show header on un-authed login frames
if ( ! user . number )
this . showHeader = false ;
}
2023-12-24 06:44:02 +00:00
/ * *
* Process a loaded frame from either a file or message base
*
* @ param contents
* @ param ext
* @ param width
* @ param height
* @ returns { boolean | null }
* /
2024-12-03 23:13:28 +00:00
this . preload = function ( contents , ext ) {
2022-12-09 06:19:33 +00:00
switch ( ext ) {
2023-12-27 11:24:20 +00:00
// Messages
case 'txt' :
log ( LOG _DEBUG , 'Processing txt' ) ;
var page = rawtoattrs ( contents , this . width , this . _ _window _ _ . body . y , this . _ _window _ _ . body . x , debug ) ;
this . _ _window _ _ . body . _ _properties _ _ . content = page . content ;
2024-12-03 23:13:28 +00:00
return contents ;
2023-12-27 11:24:20 +00:00
2022-12-09 06:19:33 +00:00
// ANSI files
case 'ans' :
2023-12-24 09:11:40 +00:00
// ViewData files
case 'bin' :
log ( LOG _DEBUG , 'Processing ANSI/VIEWDATA file' ) ;
var page = rawtoattrs ( contents , this . width , this . _ _window _ _ . body . y , this . _ _window _ _ . body . x , debug ) ;
2022-12-09 06:19:33 +00:00
2023-12-21 23:25:32 +00:00
this . _ _window _ _ . body . _ _properties _ _ . content = page . content ;
this . dynamic _fields = page . dynamic _fields ;
2022-12-09 06:19:33 +00:00
// Our fields are sorted in x descending order
2023-12-21 23:25:32 +00:00
this . input _fields = page . input _fields . sort ( function ( a , b ) { return a . x < b . x ? 1 : - 1 ; } ) ;
2022-12-09 06:19:33 +00:00
2024-12-03 23:13:28 +00:00
this . raw = contents ;
2023-12-27 11:24:20 +00:00
2022-12-09 06:19:33 +00:00
break ;
// ANSItex files
case 'tex' :
case 'vtx' :
2024-12-03 04:54:17 +00:00
log ( LOG _DEBUG , '|-- Processing FRAME file. V:' + contents . version ) ;
2022-12-09 06:19:33 +00:00
2024-12-03 04:54:17 +00:00
switch ( contents . version ) {
case 1 :
try {
for ( var index in contents ) {
if ( FRAME _SAVE _ATTRS . indexOf ( index ) === - 1 ) {
log ( LOG _ERROR , '|-! Unknown index [' + index + '] in input.' ) ;
continue ;
}
2022-12-09 06:19:33 +00:00
2024-12-03 04:54:17 +00:00
log ( LOG _DEBUG , '|-* Processing [' + index + '] with value [' + JSON . stringify ( contents [ index ] ) + '].' ) ;
switch ( index ) {
case 'content' :
//if (ext === 'tex')
// var page = rawtoattrs(base64_decode(contents[index]).replace("\x0a\x0d\x0a\x0d","\x0a\x0d"),this.width,this.__window__.body.y,this.__window__.body.x);
//else if (ext === 'vtx')
var page = rawtoattrs ( base64 _decode ( contents [ index ] ) , this . width , this . _ _window _ _ . body . y , this . _ _window _ _ . body . x ) ;
2022-12-09 06:19:33 +00:00
2024-12-03 04:54:17 +00:00
this . _ _window _ _ . body . _ _properties _ _ . content = page . content ;
this . dynamic _fields = page . dynamic _fields ;
2023-12-24 06:44:02 +00:00
2024-12-03 04:54:17 +00:00
// Our fields are sorted in x descending order
if ( page . input _fields . length )
this . input _fields = page . input _fields . sort ( function ( a , b ) { return a . x < b . x ? 1 : - 1 ; } ) ;
2023-12-24 06:44:02 +00:00
2024-12-03 23:13:28 +00:00
this . raw = base64 _decode ( contents [ index ] ) ;
2022-12-09 06:19:33 +00:00
2024-12-03 04:54:17 +00:00
break ;
2022-12-09 06:19:33 +00:00
2024-12-03 04:54:17 +00:00
case 'cost' :
this . cost = contents [ index ] ;
break ;
2023-12-24 06:44:02 +00:00
2024-12-03 04:54:17 +00:00
case 'date' :
log ( LOG _INFO , '|-/ Frame date : ' + contents [ index ] ) ;
break ;
2023-12-24 06:44:02 +00:00
2024-12-03 04:54:17 +00:00
case 'dynamic_fields' :
this . dynamic _fields = contents [ index ] ;
break ;
2022-12-09 06:19:33 +00:00
2024-12-03 04:54:17 +00:00
case 'frame' :
this . name . frame = '' + contents [ index ] ;
break ;
2023-12-24 06:44:02 +00:00
2024-12-03 04:54:17 +00:00
case 'index' :
this . name . index = contents [ index ] ;
break ;
2022-12-09 06:19:33 +00:00
2024-12-03 04:54:17 +00:00
case 'input_fields' :
this . input _fields = contents [ index ] ;
break ;
2022-12-09 06:19:33 +00:00
2024-12-03 04:54:17 +00:00
case 'isAccessible' :
this . isAccessible = ( ( contents [ index ] === 1 ) || ( contents [ index ] === true ) ) ;
break ;
2023-12-24 06:44:02 +00:00
2024-12-03 04:54:17 +00:00
case 'isPublic' :
this . isPublic = ( ( contents [ index ] === 1 ) || ( contents [ index ] === true ) ) ;
break ;
2023-12-24 06:44:02 +00:00
2024-12-03 04:54:17 +00:00
case 'key' :
this . key = contents [ index ] ;
break ;
2023-12-24 06:44:02 +00:00
2024-12-03 04:54:17 +00:00
case 'type' :
this . type = contents [ index ] ;
break ;
2022-12-09 06:19:33 +00:00
2024-12-03 04:54:17 +00:00
case 'version' :
log ( LOG _INFO , '|-/ Frame version : ' + contents [ index ] ) ;
break ;
2023-12-24 06:44:02 +00:00
2024-12-03 04:54:17 +00:00
case 'window' :
for ( var y in contents [ index ] ) {
//log(LOG_DEBUG,' - Y: '+y+', '+JSON.stringify(contents[index][y]));
2023-12-24 06:44:02 +00:00
2024-12-03 04:54:17 +00:00
if ( contents [ index ] [ y ] === null )
continue ;
2023-12-24 06:44:02 +00:00
2024-12-03 04:54:17 +00:00
for ( var x in contents [ index ] [ y ] ) {
//log(LOG_DEBUG,' - X: '+x+', '+JSON.stringify(contents[index][y][x]));
if ( contents [ index ] [ y ] [ x ] === null )
continue ;
2023-12-24 06:44:02 +00:00
2024-12-03 04:54:17 +00:00
this . _ _window _ _ . body . _ _properties _ _ . content [ y ] [ x ] = new Char (
contents [ index ] [ y ] [ x ] . _ _properties _ _ . ch ,
contents [ index ] [ y ] [ x ] . _ _properties _ _ . attr
) ;
}
}
break ;
default :
log ( LOG _ERROR , '|-! Frame property not handled: ' + index + ', value:' + contents [ index ] ) ;
2023-12-24 06:44:02 +00:00
}
2024-12-03 04:54:17 +00:00
}
2023-12-24 09:11:40 +00:00
2024-12-03 04:54:17 +00:00
} catch ( error ) {
log ( LOG _ERROR , '|-! Frame error : ' + error ) ;
2022-12-09 06:19:33 +00:00
2024-12-03 04:54:17 +00:00
// Load our system error frame.
// @todo If our system error page errors, then we go into a loop
this . get ( new PageObject ( FRAME _SYSTEM _ERROR ) ) ;
2022-12-09 06:19:33 +00:00
}
2024-12-03 04:54:17 +00:00
break ;
2022-12-09 06:19:33 +00:00
2024-12-03 04:54:17 +00:00
case 2 :
2024-12-08 07:00:24 +00:00
log ( LOG _DEBUG , '|-- Type: ' + SESSION _EXT ) ;
if ( ( SESSION _EXT === 'vtx' ) && contents . dynamic _fields ) {
log ( LOG _DEBUG , '|--- Dynamic Fields: ' + contents . dynamic _fields . length ) ;
this . dynamic _fields = contents . dynamic _fields ;
}
2024-12-03 23:13:28 +00:00
var page = rawtoattrs ( contents . content , this . width , this . _ _window _ _ . body . y , this . _ _window _ _ . body . x ) ;
2024-12-03 10:36:03 +00:00
this . _ _window _ _ . body . _ _properties _ _ . content = page . content ;
2024-12-08 07:00:24 +00:00
if ( ( SESSION _EXT === 'vtx' ) && contents . input _fields ) {
log ( LOG _DEBUG , '|--- Input Fields: ' + contents . input _fields . length ) ;
this . input _fields = contents . input _fields ;
// Our fields are sorted in x descending order
} else if ( page . input _fields . length )
2024-12-03 10:36:03 +00:00
this . input _fields = page . input _fields . sort ( function ( a , b ) { return a . x < b . x ? 1 : - 1 ; } ) ;
2024-12-08 07:00:24 +00:00
if ( ( SESSION _EXT === 'tex' ) && page . dynamic _fields )
this . dynamic _fields = page . dynamic _fields ;
2024-12-03 04:54:17 +00:00
// Work out frame type
2024-12-03 10:36:03 +00:00
this . attrs = contents . attrs ;
this . cost = contents . cost ;
this . key = contents . key ;
2024-12-03 23:13:28 +00:00
this . raw = contents . content ;
2024-12-07 08:32:42 +00:00
this . date = contents . date ;
2024-12-03 10:36:03 +00:00
2024-12-03 04:54:17 +00:00
break ;
2022-12-09 06:19:33 +00:00
2024-12-03 04:54:17 +00:00
default :
log ( LOG _ERROR , '|-! Unknown frame version : ' + contents . version ) ;
this . get ( new PageObject ( FRAME _SYSTEM _ERROR ) ) ;
return null ;
2022-12-09 06:19:33 +00:00
}
this . loadcomplete ( ) ;
2023-12-27 11:24:20 +00:00
log ( LOG _DEBUG , '|= Frame complete : ' + this . name . toString ( ) ) ;
2022-12-09 06:19:33 +00:00
break ;
default :
throw new Error ( 'Unsupported filetype:' + ext ) ;
}
// Successful load
return true ;
}
/ * *
2023-12-24 06:44:02 +00:00
* Save the frame to the message base
2022-12-09 06:19:33 +00:00
* /
this . save = function ( ) {
2024-12-19 10:52:35 +00:00
var msgbase = new MsgBase ( FRAMES _MSG _BASE ) ;
2022-12-09 06:19:33 +00:00
2024-12-03 23:13:28 +00:00
if ( ! msgbase . open ( ) ) {
log ( LOG _ERROR , '! Message Base cannot be opened (save): [' + msgbase . error + ']' ) ;
return false ;
2023-12-24 06:44:02 +00:00
}
2024-12-03 23:13:28 +00:00
var hdr = {
to : this . name ,
from : 'SYSTEM' ,
tags : this . name ,
2024-12-17 05:12:36 +00:00
date : this . date ,
2024-12-03 23:13:28 +00:00
subject : 'Content' ,
} ;
var page = {
'version' : 2 ,
'attrs' : p . attrs ,
'cost' : p . cost ,
'key' : p . key ,
'content' : p . raw ,
} ;
2024-12-07 09:19:42 +00:00
var body = LZString . compressToBase64 ( JSON . stringify ( page ) ) + FRAMES _EOF _MARKER ;
2024-12-03 23:13:28 +00:00
2024-12-19 10:52:35 +00:00
if ( ! msgbase . save _msg ( hdr , body ) )
2024-12-03 23:13:28 +00:00
log ( LOG _ERROR , ' ! Error saving frame: [' + this . name . toString ( ) + ']' ) ;
2023-12-24 06:44:02 +00:00
2024-12-03 23:13:28 +00:00
msgbase . close ( ) ;
return true ;
2022-12-09 06:19:33 +00:00
}
2023-12-21 23:25:32 +00:00
2023-12-27 11:24:20 +00:00
this . scroll = function ( x , y ) {
this . _ _compiled _ _ . build = null ;
// @todo Check that we can scroll and if we are out of bounds.
this . _ _window _ _ . body . scroll ( x , y ) ;
}
2023-12-21 23:25:32 +00:00
init . apply ( this , arguments ) ;
2022-12-09 06:19:33 +00:00
}
function PageObject ( frame , index ) {
this . _ _properties _ _ = {
frame : '0' , // Frame number
index : 'a' , // Frame index
}
function init ( frame , index ) {
if ( typeof frame === 'object' ) {
2024-01-01 06:03:25 +00:00
this . _ _properties _ _ . frame = frame . frame . toString ( ) ;
2023-12-21 23:25:32 +00:00
this . index = frame . index ;
2022-12-09 06:19:33 +00:00
2024-12-03 10:36:03 +00:00
} else if ( ( frame !== undefined ) && ( index === undefined ) ) {
if ( /^\d+[a-z]$/ . test ( frame ) ) {
var split = frame . split ( /(\d+)/ ) ;
this . _ _properties _ _ . frame = split [ 1 ] ;
this . index = split [ 2 ] ;
} else {
this . _ _properties _ _ . frame = frame ;
}
2023-12-21 23:25:32 +00:00
2024-12-03 10:36:03 +00:00
} else if ( ( frame !== undefined ) && ( index !== undefined ) ) {
this . _ _properties _ _ . frame = frame ;
this . index = index ;
2022-12-09 06:19:33 +00:00
}
}
PageObject . prototype . _ _defineGetter _ _ ( 'frame' , function ( ) {
return this . _ _properties _ _ . frame ;
} ) ;
// @todo validate that string only has digits
PageObject . prototype . _ _defineSetter _ _ ( 'frame' , function ( string ) {
if ( typeof string !== 'string' )
throw new Error ( 'Page.number must be a string' ) ;
this . _ _properties _ _ . frame = string ;
} ) ;
PageObject . prototype . _ _defineGetter _ _ ( 'index' , function ( ) {
return this . _ _properties _ _ . index ;
} ) ;
PageObject . prototype . _ _defineSetter _ _ ( 'index' , function ( string ) {
if ( typeof string !== 'string' )
throw new Error ( 'Page.index must be a string' ) ;
if ( string . length !== 1 )
throw new Error ( 'Page.index can only be 1 char' ) ;
this . _ _properties _ _ . index = string ;
} ) ;
PageObject . prototype . _ _defineGetter _ _ ( 'next' , function ( ) {
var next = undefined ;
2023-12-21 23:25:32 +00:00
if ( this . index !== 'z' ) {
log ( LOG _DEBUG , 'page_next: Current page:' + this . frame + ', current index:' + this . index ) ;
2022-12-09 06:19:33 +00:00
2023-12-21 23:25:32 +00:00
next = new PageObject ( this . frame , String . fromCharCode ( this . index . charCodeAt ( 0 ) + 1 ) ) ;
2022-12-09 06:19:33 +00:00
}
return next ;
} ) ;
2023-12-24 06:44:02 +00:00
PageObject . prototype . toString = function ( ) {
return ( this . frame && this . index ) ? this . frame + this . index : null ;
}
init . apply ( this , arguments ) ;
2022-12-09 06:19:33 +00:00
}