aboutsummaryrefslogtreecommitdiff
path: root/javascript/app/services/store.js
blob: 39a6f747655bb6911dd161f632e7121e39610626 (plain) (blame)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import Ember from 'ember';
import RSVP from 'rsvp';
import {urls} from '../utils/api-urls';


//********************************************************************************
//https://coderwall.com/p/zrlulq/parsing-a-link-header-in-javascript
function unquote(value) {
  if (value.charAt(0) == '"' && value.charAt(value.length - 1) == '"') {
    return value.substring(1, value.length - 1);
  } else {
    return value;
  }
}

function parseLinkHeader(header) {
  if(!header) {return {}}
  var linkexp = /<[^>]*>\s*(\s*;\s*[^\(\)<>@,;:"\/\[\]\?={} \t]+=(([^\(\)<>@,;:"\/\[\]\?={} \t]+)|("[^"]*")))*(,|$)/g;
  var paramexp = /[^\(\)<>@,;:"\/\[\]\?={} \t]+=(([^\(\)<>@,;:"\/\[\]\?={} \t]+)|("[^"]*"))/g;

  var matches = header.match(linkexp);
  var rels = new Object();
  for (let i = 0; i < matches.length; i++) {
    var split = matches[i].split('>');
    var href = split[0].substring(1);
    var ps = split[1];
    var link = new Object();
    link.href = href;
    var s = ps.match(paramexp);
    for (let j = 0; j < s.length; j++) {
      var p = s[j];
      var paramsplit = p.split('=');
      var name = paramsplit[0];
      link[name] = unquote(paramsplit[1]);
    }

    if (link.rel != undefined) {
      rels[link.rel] = link;
    }
  }
  return rels;
}
//********************************************************************************


export default Ember.Service.extend({
  init() {
    this.packages = {};
    this.files = {};
    this.haskellModules = {};
    this.definitionSites = {};
    this.modulePaths = {};
    this.expressions = {};
    this.references = {};
    this.globalReferences = {};
    this.hoogleDocs = {};
  },
  loadPackage(packageId) {
    const packageInfo = this.packages[packageId];
    if(packageInfo) {
      return new RSVP.Promise((resolve) => {resolve(packageInfo);});
    } else {
      const url = urls.packageInfoUrl(packageId);
      return Ember.$.getJSON(url).then((packageInfo) => {
        this.packages[packageId] = packageInfo;
        return packageInfo;
      });
    }
  },
  loadFile(packageId,filePath) {
    const fileId = packageId + "/" + filePath;
    const file = this.files[fileId];
    if(file) {
      return new RSVP.Promise((resolve) => {resolve(file);});
    } else {
      const url = urls.fileUrl(packageId,filePath);
      return Ember.$.get({url:url,dataType:"text"}).then((text) => {
        const file = {};
        file.text = text;
        file.packageId = packageId;
        file.isHaskellModule = false;
        file.id = filePath;
        this.files[fileId] = file;
        return file;
      });
    }
  },
  loadHaskellModule(packageId,filePath) {
    const moduleId = packageId + "/" + filePath ;
    const module = this.haskellModules[moduleId];
    if(module) {
      return new RSVP.Promise((resolve)=>{resolve(module);});
    } else {
      const url = urls.haskellModuleUrl(packageId,filePath);
      return Ember.$.getJSON(url).then((module) => {
        module.packageId = packageId;
        module.isHaskellModule = true;
        this.haskellModules[moduleId] = module;
        return module;
      });
    }
  },
  loadDefinitionSite(packageId,moduleName,componentId,entity,name) {
    const id = packageId + "/"+ componentId + "/" + moduleName + "/" + entity + "/" + name;
    const definitionSite = this.definitionSites[id];
    if(definitionSite) {
      return new RSVP.Promise((resolve)=>{resolve(definitionSite);});
    } else {
      const url = urls.identifierDefinitionSiteUrl(packageId,moduleName,componentId,entity,name);
      return Ember.$.getJSON(url).then((definitionSite) => {
        this.definitionSites[id] = definitionSite;
        return definitionSite;
      });
    }
  },
  loadExpressions(packageId,modulePath,lineStart,columnStart,lineEnd,columnEnd) {
    const id = packageId + "/" + encodeURIComponent(modulePath)
          + "/" + lineStart + "/" + columnStart + "/" + lineEnd + "/" + columnEnd;
    const exprs = this.expressions[id];
    if(exprs) {
      return new RSVP.Promise((resolve)=>{resolve(exprs);});
    } else {
      const url = urls.expressionsUrl(packageId,modulePath,lineStart,columnStart,lineEnd,columnEnd);
      return Ember.$.getJSON(url).then((exprs) => {
        this.expressions[id] = exprs;
        return exprs;
      });
    }
  },
  loadFromUrlPaginated(url) {
    return Ember.$.getJSON(url).then((items,textStatus,jqXHR) => {
      const linkHeaderText = jqXHR.getResponseHeader('Link');
      const totalCountHeaderText = jqXHR.getResponseHeader('x-total-count');
      const linkHeader = parseLinkHeader(linkHeaderText);
      const total = parseInt(totalCountHeaderText);
      return {
        items:items,
        total:total,
        linkHeader:linkHeader
      };
    });
  },
  loadGlobalReferences(externalId) {
    const globalReferences = this.globalReferences[externalId];
    if(globalReferences) {
      return new RSVP.Promise((resolve) => {resolve(globalReferences);});
    } else {
      const url = urls.globalReferencesUrl(externalId);
      return Ember.$.getJSON(url).then((refs) => {
        this.globalReferences[externalId] = refs;
        return refs;
      });
    }
  },
  loadHoogleDocs(packageId,moduleName,entity,name) {
    const id = packageId + "/" + moduleName + "/" + entity + "/" + name;
    const hoogleDoc = this.hoogleDocs[id];
    if(hoogleDoc) {
      return new RSVP.Promise((resolve)=>{resolve(hoogleDoc);});
    } else {
      const url = urls.hoogleDocsUrl(packageId,moduleName,entity,name);
      return Ember.$.getJSON(url).then((hoogleDoc) => {
        this.hoogleDocs[id] = hoogleDoc;
        return hoogleDoc;
      });
    }
  }
});