File size: 1,606 Bytes
ae32abe
757f418
 
 
 
 
 
 
 
ae32abe
 
757f418
ae32abe
757f418
 
 
 
 
 
 
 
 
 
 
ae32abe
 
 
757f418
 
ae32abe
757f418
 
 
 
 
 
 
 
ae32abe
757f418
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* app-dataset.js - demo dataset loader for the Bergamo FSR demo */

(function () {
  'use strict';

  var FSR = window.FSR = window.FSR || {};

  FSR.createDemoDataController = function (options) {
    var utils = options.utils;
    var selectedId = 'STANDARD';
    var catalog = { defaultId: selectedId, availableIds: [selectedId] };
    var loading = false;
    var el = document.createDocumentFragment();

    return {
      bootstrap: bootstrap,
      el: el,
      getCatalog: function () { return catalog; },
      getSelectedId: function () { return selectedId; },
      isLoading: function () { return loading; },
      resolvePlan: resolvePlan,
    };

    function bootstrap() {
      setLoading(true);
      notifyCatalog();
      return utils.fetchDemoPlan(selectedId)
        .then(function (plan) { notifyPlan(plan); })
        .catch(function (err) { notifyError(err); })
        .finally(function () { setLoading(false); });
    }

    function resolvePlan() {
      var currentPlan = options.getCurrentPlan();
      if (currentPlan) return Promise.resolve(utils.clonePlan(currentPlan));
      return utils.fetchDemoPlan(selectedId);
    }

    function setLoading(nextLoading) {
      loading = nextLoading;
      if (options.onLoadingChange) options.onLoadingChange(loading);
    }

    function notifyCatalog() {
      if (options.onCatalog) options.onCatalog(catalog, selectedId);
    }

    function notifyPlan(plan) {
      if (options.onPlan) options.onPlan(plan, selectedId);
    }

    function notifyError(err) {
      if (options.onError) options.onError(err);
    }
  };
})();