12479: Get remote vocabulary definition, use a fallback is it doesn't exist.
[arvados.git] / apps / workbench / app / assets / javascripts / models / tags.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 // Fallback vocabulary that accepts any tag type. Will be used if
6 // no custom vocabulary provided.
7 var vocabulary = {
8     "strict": false,
9     "types": {}
10 }
11
12 window.Vocabulary = function(url) {
13     var v = this
14     Object.assign(v, {
15         url: url,
16         data: {},
17         load: function() {
18             // Load vocabulary from rails' public directory
19             m.request(v.url.origin + '/vocabulary.json').then(function(resp) {
20                 console.log('Vocabulary loaded')
21                 v.data = resp
22             }).catch(function(err) {
23                 // Not found, use a default vocabulary
24                 console.log('Using default vocabulary')
25                 v.data = vocabulary
26             })
27         },
28         getDef: function(tagName) {
29             if (tagName in v.data.types) {
30                 return v.data.types[tagName]
31             } else {
32                 return {"type": "text"} // Default 
33             }
34         },
35         getTypes: function() {
36             return Object.keys(v.data.types)
37         }
38     })
39 }
40
41 window.Tags = function(db, uuid, objType) {
42     var t = this
43     Object.assign(t, {
44         db: db,
45         uuid: uuid,
46         objType: objType,
47         objPath: '/arvados/v1/' + objType + '/' + uuid,
48         tagIdx: 0, // Will use this as the tag access key
49         data: {},
50         clear: function() {
51             t.data = {}
52         },
53         load: function() {
54             // Get the tag list from the API server
55             return db.request(
56                 db.loadLocal(), 
57                 t.objPath).then(function(obj){
58                     t.clear()
59                     Object.keys(obj.properties).map(function(k) {
60                         t.addTag(k, obj.properties[k])
61                     })
62                 }
63             )
64         },
65         save: function() {
66             return db.request(
67                 db.loadLocal(),
68                 t.objPath, {
69                     method: "PUT",
70                     data: {properties: JSON.stringify(t.getAll())}
71                 }
72             )
73         },
74         getAll: function() {
75             // return hash to be POSTed to API server
76             var tags = {}
77             Object.keys(t.data).map(function(k) {
78                 a_tag = t.data[k]
79                 tags[a_tag.name] = a_tag.value
80             })
81             return tags
82         },
83         addTag: function(name, value) {
84             name = name || ""
85             value = value || ""
86             t.data[t.tagIdx] = {
87                 "name": name,
88                 "value": value
89             },
90             t.tagIdx++
91         },
92         removeTag: function(tagIdx) {
93             if (tagIdx in t.data) {
94                 delete t.data[tagIdx]
95             }
96         },
97         getName: function(tagIdx) {
98             if (tagIdx in t.data) {
99                 return t.data[tagIdx].name
100             }
101         },
102         get: function(tagIdx) {
103             if (tagIdx in t.data) {
104                 return t.data[tagIdx]
105             }
106         }
107     })
108 }