Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CustomFieldConverter.js « Converter « src - git.mdns.eu/nextcloud/passwords-client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a5e2e781c9b8f7acc21340c03c88fd344a399552 (plain)
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
export default class CustomFieldConverter {

    /**
     * @param {BasicPasswordsClient} client
     */
    constructor(client) {
        this._client = client;
    }

    /**
     * @param {String} string
     *
     * @return {(CustomFieldCollection|AbstractField)}
     * @api
     */
    fromJSON(string) {
        let data = JSON.parse(string);

        if(Array.isArray(data)) {
            return this.fromArray(data);
        }

        if(typeof data === 'object' && data instanceof Object) {
            return this.fromObject(data);
        }
    }

    /**
     * @param {Object} object
     * @return {AbstractField}
     * @api
     */
    fromObject(object) {
        if(!object.hasOwnProperty('type') || object.type === null || object.type === undefined) {
            this._client.getLogger().error('Found invalid custom field', {field: object});
            return this._client.getClass(`model.defectField`, {label:'##ERROR##', value:JSON.stringify(object)})
        }

        let type = object.type;

        return this._client.getClass(`model.${type}Field`, object);
    }

    /**
     * @param {Object[]} array
     * @return {CustomFieldCollection}
     * @api
     */
    fromArray(array) {
        let fields = [];

        for(let field of array) {
            fields.push(this.fromObject(field));
        }

        return this._client.getClass('collection.field', fields);
    }

    /**
     * @param {CustomFieldCollection} collection
     * @return {Object[]}
     */
    toArray(collection) {
        let array = [];

        for(let field of collection) {
            array.push(this.toObject(field));
        }

        return array;
    }

    /**
     * @param {AbstractField} field
     * @return {Object}
     */
    toObject(field) {
        return field.getProperties();
    }

    /**
     * @param {AbstractField} field
     * @return {String}
     */
    toJSON(field) {
        return JSON.stringify(field);
    }
}