5ef25b98981a473e7192c9d90d34cb4e9807b305
[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 var vocabulary = {
6     "strict": false, // Allow other tags not described here
7     "types": {
8         "opt1": {
9             "type": "select",
10             "options": ["val1", "val2", "val3"],
11             "overridable": true // Allow value not listed in options
12         },
13         "opt2": {
14             "type": "select",
15             "options": ["val21", "val22", "val23"]
16         },
17         "opt3": {
18             "type": "select",
19             "options": ["val31", "val32", "val33"]
20         },
21         "text tag": {
22             "type": "text",
23             "max_length": 80,
24         },
25         "int tag": {
26             "type": "integer",
27             "min": 0,
28             "max": 1000
29         }
30     }
31 }
32
33 window.Vocabulary = function() {
34     var v = this
35     Object.assign(v, {
36         data: {},
37         load: function() {
38             // TODO: get the vocabulary file from http
39             v.data = vocabulary
40         },
41         getDef: function(tagName) {
42             if (tagName in v.data.types) {
43                 return v.data.types[tagName]
44             } else {
45                 return {"type": "text"} // Default 
46             }
47         },
48         getTypes: function() {
49             return Object.keys(v.data.types)
50         }
51     })
52 }
53
54 window.Tags = function(db, uuid, objType) {
55     var t = this
56     Object.assign(t, {
57         db: db,
58         uuid: uuid,
59         objType: objType,
60         objPath: '/arvados/v1/' + objType + '/' + uuid,
61         tagIdx: 0, // Will use this as the tag access key
62         data: {},
63         clear: function() {
64             t.data = {}
65         },
66         load: function() {
67             // Get the tag list from the API server
68             return db.request(
69                 db.loadLocal(), 
70                 t.objPath).then(function(obj){
71                     t.clear()
72                     Object.keys(obj.properties).map(function(k) {
73                         t.addTag(k, obj.properties[k])
74                     })
75                 }
76             )
77         },
78         save: function() {
79             return db.request(
80                 db.loadLocal(),
81                 t.objPath, {
82                     method: "PUT",
83                     data: {properties: JSON.stringify(t.getAll())}
84                 }
85             )
86         },
87         getAll: function() {
88             // return hash to be POSTed to API server
89             var tags = {}
90             Object.keys(t.data).map(function(k) {
91                 a_tag = t.data[k]
92                 tags[a_tag.name] = a_tag.value
93             })
94             return tags
95         },
96         addTag: function(name, value) {
97             name = name || ""
98             value = value || ""
99             t.data[t.tagIdx] = {
100                 "name": name,
101                 "value": value
102             },
103             t.tagIdx++
104         },
105         removeTag: function(tagIdx) {
106             if (tagIdx in t.data) {
107                 delete t.data[tagIdx]
108             }
109         },
110         getName: function(tagIdx) {
111             if (tagIdx in t.data) {
112                 return t.data[tagIdx].name
113             }
114         },
115         get: function(tagIdx) {
116             if (tagIdx in t.data) {
117                 return t.data[tagIdx]
118             }
119         }
120     })
121 }