Advertisement
Uno-Dan

Async Setup

Feb 10th, 2021
939
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. jQuery(document).ready( () => {
  2.   let count = 10;
  3.   let debug =  true;
  4.   let force_restore = true;
  5.  
  6.   // -------------------------
  7.  
  8.   let db_name = 'mettatea';
  9.   let db_stores = [ 'app', 'table_magic' ];
  10.   let db_version = 1;
  11.  
  12.   let load = async ( app, store, defaults ) => {
  13.     try {
  14.       return await app.db.get( store, defaults.id );
  15.     } catch( err ) {
  16.       try {
  17.         await app.db.add( store, defaults );
  18.         return defaults;
  19.       } catch( err ) { log( 'ERROR: [load] Could not open store.', err ); }
  20.     }  
  21.   };
  22.  
  23.   let app_new = async () => {
  24.     try {
  25.       app = await new
  26.       App( db_name, db_version, db_stores, force_restore, debug );
  27.       return app;
  28.     } catch( err ) {
  29.       log( 'ERROR: [setup] could not open database [' +  + ']', err );
  30.     }
  31.   };
  32.  
  33.   let app_setup = async ( app, args ) => {
  34.     try {
  35.       app.settings = await load( app, args.store, args.defaults );
  36.       return app;
  37.     } catch( err ) {
  38.       log( 'ERROR: [setup] could not open database [' +  + ']', err );
  39.     }
  40.   };
  41.  
  42.   let tm_setup = async ( app, args ) => {
  43.     let id = args.defaults.id;
  44.     await load( app, 'table_magic', args.defaults )
  45.     .then( () => {
  46.       app.magic_tables[ id ] =
  47.       new TableMagic( $( '#' + id ), args.defaults.attrs, args.defaults.data );
  48.     } );
  49.     await app.init( id );
  50.   };
  51.    
  52.   app_new()
  53.   .then( app => {
  54.     return app_setup( app, {
  55.       store: 'app',
  56.       defaults: { id: 1, focus: '#tbl1' }
  57.     } );
  58.   } )
  59.  
  60.   .then( app => {
  61.     tm_setup( app, {
  62.       store: 'table_magic',
  63.       defaults: tm_defaults( count )[ 0 ]
  64.     } );
  65.   } );
  66.  
  67.   let tm_defaults = ( count ) => {
  68.     let row_count = count;
  69.     let rows = {};
  70.    
  71.     let row = [
  72.       0, 'Whole Foods', 'Whole Foods Yorkville - YRK', 0, '163.456.7890',
  73.       '432.546.4325', 'sam@abc.com', 'address', '#745', 'M5R-3R9',
  74.       'Canada', 'Ontario', 'Toronto',
  75.       'This is a test message...'
  76.     ];
  77.    
  78.     var mockup_data = {
  79.       keys: [
  80.         'status_id', 'customer_id', 'branch_id', 'agent_id', 'phone1',
  81.         'phone2', 'email_address', 'address', 'unit', 'postalcode', 'country_id',
  82.         'zone_id', 'location_id', 'notes'
  83.       ],
  84.       head: {
  85.         selector: 1,
  86.         columns: {
  87.           labels: [
  88.             'Status', 'Customer', 'Branch', 'Agent', 'Phone1', 'Phone2',
  89.             'Email Address', 'Address', 'Unit', 'Post Code', 'Country',
  90.             'State/Prov', 'City/Town', 'Notes'
  91.           ],
  92.           widths: []
  93.         }
  94.       },
  95.       body: {
  96.         selector: 0,
  97.         rows: {}
  98.       },
  99.       children: {
  100.         status_id: {
  101.           type: 'option',
  102.           children: { 0: '', 1: 'Active', 2: 'Prospect', 3: 'Pending', 4: 'On Hold' }
  103.         },
  104.         agent_id: {
  105.           type: 'option',
  106.           children: { 0: '', 1: 'Dan', 2: 'Danny', 3: 'Daniel', 4: 'Uno' }
  107.         }
  108.       }
  109.     };
  110.    
  111.     for ( let id = 1; id < row_count + 1; id++ ) {
  112.       let  r = [ ...row ];
  113.       r[ 0 ] = Math.floor( Math.random() * 4 ) + 1;
  114.       r[ 2 ] = 'Whole Foods Store - [' + (
  115.       Math.floor( Math.random() * 1000 ) + 1 ) + ']';
  116.       r[ 3 ] = Math.floor( Math.random() * 4 ) + 1;
  117.       rows[ id ] = r;
  118.     }
  119.     mockup_data.body.rows = rows;
  120.    
  121.     return [
  122.       {
  123.         id: 'tbl1',
  124.         data: mockup_data,
  125.         attrs: {
  126.           index: true,
  127.           selector: true,
  128.           colorize: {
  129.             th: {
  130.               color: 'white',
  131.               background: 'blue'
  132.             }
  133.           },
  134.           sortable: true,
  135.           context_menu: true,
  136.           column_sizing: true
  137.         },
  138.         timestamp: new Date()
  139.       },
  140.     ];
  141.   };
  142. });  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement