index.js 1.6 KB
Newer Older
N
nem035 已提交
1 2 3
'use strict';

const RSVP = require('rsvp');
4
const app = require('./app');
N
nem035 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
const AppConstructor = require('./app/constructor');
const DOM = require('./dom');
const Server = require('./server');

const modules = require('./module');

const {
  extend
} = $;

$.ajaxSetup({
  cache: false,
  dataType: 'text'
});

20 21 22 23 24 25 26 27
const {
  isScratchPaper
} = require('./utils');

const {
  getPath
} = require('./server/helpers');

N
nem035 已提交
28
// set global promise error handler
29
RSVP.on('error', function (reason) {
N
nem035 已提交
30 31 32 33 34 35
  console.assert(false, reason);
});

$(() => {

  // initialize the application and attach in to the instance module
36 37
  const appConstructor = new AppConstructor();
  extend(true, app, appConstructor);
N
nem035 已提交
38 39 40 41 42

  // load modules to the global scope so they can be evaled
  extend(true, window, modules);

  Server.loadCategories().then((data) => {
43
    app.setCategories(data);
44
    DOM.addCategories();
N
nem035 已提交
45 46 47

    // determine if the app is loading a pre-existing scratch-pad
    // or the home page
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    const {
      category,
      algorithm,
      file
    } = getPath();
    if (isScratchPaper(category)) {
      if (algorithm) {
        Server.loadScratchPaper(algorithm).then(({category, algorithm, data}) => {
          DOM.showAlgorithm(category, algorithm, data);
        });
      } else {
        Server.loadAlgorithm(category).then((data) => {
          DOM.showAlgorithm(category, null, data);
        });
      }
    } else if (category && algorithm) {
      DOM.showRequestedAlgorithm(category, algorithm, file);
N
nem035 已提交
65 66 67 68
    } else {
      DOM.showFirstAlgorithm();
    }
  });
J
show md  
Jason Park 已提交
69 70

  Server.loadWikiList().then((data) => {
J
Jason Park 已提交
71
    app.setWikiList(data.wikis);
J
Jason Park 已提交
72 73

    DOM.showWiki('Tracer');
J
show md  
Jason Park 已提交
74
  })
N
nem035 已提交
75
});