on 8a93021 help sign in
unity8/tests/qmltests/Dash/CardHelpers.js
113 lines 3.1 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17.pragma library
19var components = ["title", "art", "subtitle", "mascot", "emblem", "summary", "attributes", "overlayColor"]
21var defaultLayout = ' \
22{ \
23 "schema-version": 1, \
24 "template": { \
25 "category-layout": "grid", \
26 "card-layout": "vertical", \
27 "card-size": "medium", \
28 "overlay-mode": null, \
29 "collapsed-rows": 2 \
30 }, \
31 "components": { \
32 "title": null, \
33 "art": { \
34 "aspect-ratio": 1.0 \
35 }, \
36 "subtitle": null, \
37 "overlayColor": null, \
38 "mascot": null, \
39 "emblem": null, \
40 "summary": null, \
41 "attributes": { "max-count": 2 } \
42 }, \
43 "resources": {} \
44}'
46var fullMapping = ' \
47{ \
48 "title": "title", \
49 "art": "art", \
50 "subtitle": "subtitle", \
51 "mascot": "mascot", \
52 "emblem": "emblem", \
53 "overlayColor": "overlayColor", \
54 "summary": "summary", \
55 "attributes": "attributes" \
56}'
59function tryParse(json, errorLabel) {
60 var o = undefined;
61 if (errorLabel !== undefined) {
62 errorLabel.text = "";
63 }
64 try {
65 o = JSON.parse(json)
66 } catch(err) {
67 if (errorLabel !== undefined) {
68 errorLabel.text = err + "";
69 } else {
70 console.debug(err);
71 }
72 }
73 return o;
76function mapData(json, layout, errorLabel) {
77 var o = tryParse(json, errorLabel);
78 var d = undefined;
80 if (o !== undefined) {
81 d = Object();
82 for (var k in components) {
83 try {
84 if (typeof layout[components[k]] == "object") {
85 d[components[k]] = o[layout[components[k]]['field']];
86 } else {
87 d[components[k]] = o[layout[components[k]]];
88 }
89 } catch(err) {
90 d[components[k]] = undefined;
91 }
92 }
93 }
94 return d;
97function update(object, overrides) {
98 for (var k in overrides) {
99 if (typeof object[k] == "string" && typeof overrides[k] == "object") {
100 if (!overrides[k].hasOwnProperty('field')) overrides[k]['field'] = object[k];
101 }
102 if (object[k] === null) {
103 object[k] = overrides[k];
104 } else if (typeof object[k] == "object" && typeof overrides[k] == "string") {
105 object[k]['field'] = overrides[k];
106 } else if (typeof object[k] == "object") {
107 update(object[k], overrides[k]);
108 } else {
109 object[k] = overrides[k];
110 }
111 }
112 return object;