This repository has been archived by the owner on Mar 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
tag.js
90 lines (85 loc) · 2.69 KB
/
tag.js
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
/**
* Passbolt ~ Open source password manager for teams
* Copyright (c) Passbolt SARL (https://www.passbolt.com)
*
* Licensed under GNU Affero General Public License version 3 of the or any later version.
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Passbolt SARL (https://www.passbolt.com)
* @license https://opensource.org/licenses/AGPL-3.0 AGPL License
* @link https://www.passbolt.com Passbolt(tm)
* @since 2.0.0
*/
import Ajax from '../../net/ajax';
import connect from 'can-connect';
import connectDataUrl from 'can-connect/data/url/url';
import connectParse from 'can-connect/data/parse/parse';
import connectConstructor from 'can-connect/constructor/constructor';
import connectMap from 'can-connect/can/map/map';
import DefineList from 'passbolt-mad/model/list/list';
import DefineMap from 'passbolt-mad/model/map/map';
const Tag = DefineMap.extend('passbolt.model.Tag', {
id: 'string',
slug: 'string',
user_id: 'string',
is_shared: 'boolean'
});
DefineMap.setReference('Tag', Tag);
Tag.List = DefineList.extend({'#': {Type: Tag}});
/*
* Default validation rules.
* Keep these rules in sync with the passbolt API.
*/
Tag.validationRules = {
id: [
{rule: 'uuid'}
],
slug: [
{rule: 'required', message: __('Tag can not be empty')},
{rule: ['maxLength', 128], message: __('Tag can not be more than %s characters in length', 128)},
{rule: 'utf8Extended', message: __('Tag should be a valid utf8 string.')}
]
};
/**
* Update the resource tags
* @param {string} resourceId The target resource to update the tags for
* @param {array} slugs The list tags
*/
Tag.updateResourceTags = function(resourceId, slugs) {
return Ajax.request({
url: 'tags/{resourceId}.json?api-version=v2',
type: 'POST',
params: {Tags: slugs, resourceId: resourceId}
}).then(data => Promise.resolve(new Tag.List(data)));
};
Tag.connection = connect([connectParse, connectDataUrl, connectConstructor, connectMap], {
Map: Tag,
List: Tag.List,
url: {
resource: '/',
getListData: function(params) {
params = params || {};
params['api-version'] = 'v2';
return Ajax.request({
url: 'tags.json',
type: 'GET',
params: params
});
},
destroyData: function(params) {
return Ajax.request({
url: `tags/${params.id}.json?api-version=v2`,
type: 'DELETE'
});
},
updateData: function(params) {
return Ajax.request({
url: `tags/${params.id}.json?api-version=v2`,
type: 'PUT',
params: params
});
}
}
});
export default Tag;