File size: 2,671 Bytes
c02f046
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import Async from 'async-lite'; // async helper functions
import Gator from 'gator'; // event delegation

import { SamsonApp } from '../index.js';
import SamsonRouter from '../router/router.js';

import addCustomLogging from '../utils/addCustomLogging.js';
import makeObjectAnEventBus from '../utils/makeObjectAnEventBus.js';
import extendObject from '../utils/extendObject.js';
import { Storage } from '../utils/localStorage.js';

import { justCallback } from '../utils/functions.js';

import cacheSamsonDOMElements from './cacheSamsonDOMElements.js';

import launchSamsonApp from './launchSamsonApp.js';

// Properties on the App object reserved by Samson. All properties starting with _ are also reserved
import { RESERVED_PROPS } from './reserved_app_properties.js';

// Creates the Samson App object
export default function createBaseSamsonAppObject (SamsonAppBundle) {
  SamsonApp.Name = SamsonAppBundle.Name || 'App';
  SamsonApp.DEBUG = SamsonAppBundle.DEBUG || (window && window.DEBUG);

  // Make the app object an event bus
  makeObjectAnEventBus(SamsonApp);

  // Add custom logging to the Samson App if in DEBUG mode
  SamsonApp.log = addCustomLogging(SamsonApp.Name, SamsonApp.DEBUG);

  // Add the localStorage module to the Samson App object
  SamsonApp.Storage = Storage;

  // setup the app's DOM Element cache
  SamsonApp.DOM = {};
  cacheSamsonDOMElements();

  // setup the app's data cache
  SamsonApp.Data = SamsonAppBundle.Data || {};

  // setup the app's custom config
  SamsonApp.Config = SamsonAppBundle.Config || {};

  // setup the app's 3rd party modules accessible from App.Modules[module_name]
  SamsonApp.Modules = SamsonAppBundle.Modules || {};

  // setup the app's pages
  SamsonApp.Pages = SamsonAppBundle.Pages || {};

  // setup the app's base components
  SamsonApp.__components = SamsonAppBundle.Components || {};
  SamsonApp.Components = {};

  // extract the Setup object from the SamsonAppBundle
  const Setup = SamsonAppBundle.Setup || {};

  // setup the app's router after loading any extra components
  SamsonApp.Router = new SamsonRouter(Setup.router || {});

  // attach any unreserved properties from the SamsonAppBundle object to the App object
  extendObject(SamsonApp, SamsonAppBundle, RESERVED_PROPS);

  // setup the Samson App delegate for DOM event listeners
  SamsonApp.delegate = Gator(SamsonApp.DOM.App);

  // attach the Setup loadState and launch methods to the app object
  const launchTasks = Setup.launchTasks || {};

  SamsonApp.loadState = Setup.loadState || justCallback;

  SamsonApp.launch = launchSamsonApp(launchTasks);
}