on 439ffe9 help sign in
unity8/plugins/Dash/CardCreator.js
996 lines 48 kB history raw
1/*
2 * Copyright (C) 2014 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
19// %1 is the template["card-background"]["elements"][0]
20// %2 is the template["card-background"]["elements"][1]
21// %3 is whether the loader should be asynchronous or not
22// %4 is the template["card-background"] string
23var kBackgroundLoaderCode = 'Loader {\n\
24 id: backgroundLoader; \n\
25 objectName: "backgroundLoader"; \n\
26 anchors.fill: parent; \n\
27 asynchronous: %3; \n\
28 visible: status === Loader.Ready; \n\
29 sourceComponent: UbuntuShape { \n\
30 objectName: "background"; \n\
31 radius: "medium"; \n\
32 aspect: { \n\
33 switch (root.backgroundShapeStyle) { \n\
34 case "inset": return UbuntuShape.Inset; \n\
35 case "shadow": return UbuntuShape.DropShadow; \n\
36 default: \n\
37 case "flat": return UbuntuShape.Flat; \n\
38 } \n\
39 } \n\
40 backgroundColor: getColor(0) || "white"; \n\
41 secondaryBackgroundColor: getColor(1) || backgroundColor; \n\
42 backgroundMode: UbuntuShape.VerticalGradient; \n\
43 anchors.fill: parent; \n\
44 source: backgroundImage.source ? backgroundImage : null; \n\
45 property real luminance: Style.luminance(backgroundColor); \n\
46 property Image backgroundImage: Image { \n\
47 objectName: "backgroundImage"; \n\
48 source: { \n\
49 if (cardData && typeof cardData["background"] === "string") return cardData["background"]; \n\
50 else return %4; \n\
51 } \n\
52 } \n\
53 function getColor(index) { \n\
54 if (cardData && typeof cardData["background"] === "object" \n\
55 && (cardData["background"]["type"] === "color" || cardData["background"]["type"] === "gradient")) { \n\
56 return cardData["background"]["elements"][index]; \n\
57 } else return index === 0 ? %1 : %2; \n\
58 } \n\
59 } \n\
60 }\n';
62// %1 is the aspect of the UbuntuShape
63var kArtUbuntuShapeCode = 'UbuntuShape { \n\
64 anchors.fill: parent; \n\
65 source: artImage; \n\
66 sourceFillMode: UbuntuShape.PreserveAspectCrop; \n\
67 radius: "medium"; \n\
68 aspect: %1; \n\
69 }';
71var kArtProportionalShapeCode = 'ProportionalShape { \n\
72 anchors.left: parent.left; \n\
73 anchors.right: parent.right; \n\
74 source: artImage; \n\
75 aspect: UbuntuShape.DropShadow; \n\
76 }';
78// %1 is used as anchors of artShapeLoader
79// %2 is used as image width
80// %3 is used as image height
81// %4 is whether the image should be visible
82// %5 is whether the loader should be asynchronous or not
83// %6 is the shape code we want to use
84// %7 is injected as code to artImage
85// %8 is used as image fallback
86var kArtShapeHolderCode = 'Loader { \n\
87 id: artShapeLoader; \n\
88 height: root.fixedArtShapeSize.height; \n\
89 width: root.fixedArtShapeSize.width; \n\
90 anchors { %1 } \n\
91 objectName: "artShapeLoader"; \n\
92 readonly property string cardArt: cardData && cardData["art"] || %8; \n\
93 onCardArtChanged: { if (item) { item.image.source = cardArt; } } \n\
94 active: cardArt != ""; \n\
95 asynchronous: %5; \n\
96 visible: status === Loader.Ready; \n\
97 sourceComponent: Item { \n\
98 id: artShape; \n\
99 objectName: "artShape"; \n\
100 visible: image.status === Image.Ready; \n\
101 readonly property alias image: artImage; \n\
102 %6 \n\
103 width: root.fixedArtShapeSize.width; \n\
104 height: root.fixedArtShapeSize.height; \n\
105 CroppedImageMinimumSourceSize { \n\
106 id: artImage; \n\
107 objectName: "artImage"; \n\
108 source: artShapeLoader.cardArt; \n\
109 asynchronous: %5; \n\
110 visible: %4; \n\
111 width: %2; \n\
112 height: %3; \n\
113 %7 \n\
114 } \n\
115 } \n\
116 }\n';
118// %1 is used as anchors of artShapeLoader
119// %2 is used as image width
120// %3 is used as image height
121// %4 is whether the image should be visible
122// %5 is whether the loader should be asynchronous or not
123// %6 is the shape code we want to use
124// %7 is injected as code to artImage
125// %8 is used as image fallback
126var kArtShapeHolderCodeCardToolCard = 'Loader { \n\
127 id: artShapeLoader; \n\
128 anchors { %1 } \n\
129 objectName: "artShapeLoader"; \n\
130 readonly property string cardArt: cardData && cardData["art"] || %8; \n\
131 onCardArtChanged: { if (item) { item.image.source = cardArt; } } \n\
132 active: cardArt != ""; \n\
133 asynchronous: %5; \n\
134 visible: status === Loader.Ready; \n\
135 sourceComponent: Item { \n\
136 id: artShape; \n\
137 objectName: "artShape"; \n\
138 visible: image.status === Image.Ready; \n\
139 readonly property alias image: artImage; \n\
140 %6 \n\
141 width: image.status !== Image.Ready ? 0 : image.width; \n\
142 height: image.status !== Image.Ready ? 0 : image.height; \n\
143 CroppedImageMinimumSourceSize { \n\
144 id: artImage; \n\
145 objectName: "artImage"; \n\
146 source: artShapeLoader.cardArt; \n\
147 asynchronous: %5; \n\
148 visible: %4; \n\
149 width: %2; \n\
150 height: %3; \n\
151 %7 \n\
152 } \n\
153 } \n\
154 }\n';
156// %1 is anchors.fill
157// %2 is width
158// %3 is height
159// %4 is whether the icon should be asynchronous or not
160var kAudioButtonCode = 'AbstractButton { \n\
161 id: audioButton; \n\
162 anchors.fill: %1; \n\
163 width: %2; \n\
164 height: %3; \n\
165 readonly property url source: (cardData["quickPreviewData"] && cardData["quickPreviewData"]["uri"]) || ""; \n\
166 UbuntuShape { \n\
167 anchors.fill: parent; \n\
168 visible: parent.pressed; \n\
169 radius: "medium"; \n\
170 } \n\
171 Rectangle { \n\
172 color: Qt.rgba(0, 0, 0, 0.5); \n\
173 anchors.centerIn: parent; \n\
174 width: parent.width * 0.5; \n\
175 height: width; \n\
176 radius: width / 2; \n\
177 } \n\
178 Icon { \n\
179 anchors.centerIn: parent; \n\
180 width: parent.width * 0.3; \n\
181 height: width; \n\
182 opacity: 0.9; \n\
183 name: DashAudioPlayer.playing && AudioUrlComparer.compare(parent.source, DashAudioPlayer.currentSource) ? "media-playback-pause" : "media-playback-start"; \n\
184 color: "white"; \n\
185 asynchronous: %4; \n\
186 } \n\
187 onClicked: { \n\
188 if (AudioUrlComparer.compare(source, DashAudioPlayer.currentSource)) { \n\
189 if (DashAudioPlayer.playing) { \n\
190 DashAudioPlayer.pause(); \n\
191 } else { \n\
192 DashAudioPlayer.play(); \n\
193 } \n\
194 } else { \n\
195 var playlist = (cardData["quickPreviewData"] && cardData["quickPreviewData"]["playlist"]) || null; \n\
196 DashAudioPlayer.playSource(source, playlist); \n\
197 } \n\
198 } \n\
199 onPressAndHold: { \n\
200 root.pressAndHold(); \n\
201 } \n\
202 }';
204// %1 is whether the loader should be asynchronous or not
205// %2 is the header height code
206var kOverlayLoaderCode = 'Loader { \n\
207 id: overlayLoader; \n\
208 readonly property real overlayHeight: %2 + units.gu(2); \n\
209 anchors.fill: artShapeLoader; \n\
210 active: artShapeLoader.active && artShapeLoader.item && artShapeLoader.item.image.status === Image.Ready || false; \n\
211 asynchronous: %1; \n\
212 visible: showHeader && status === Loader.Ready; \n\
213 sourceComponent: UbuntuShapeOverlay { \n\
214 id: overlay; \n\
215 property real luminance: Style.luminance(overlayColor); \n\
216 aspect: UbuntuShape.Flat; \n\
217 radius: "medium"; \n\
218 overlayColor: cardData && cardData["overlayColor"] || "#99000000"; \n\
219 overlayRect: Qt.rect(0, 1 - overlayLoader.overlayHeight / height, 1, 1); \n\
220 } \n\
221 }\n';
223// multiple row version of HeaderRowCode
224function kHeaderRowCodeGenerator() {
225 var kHeaderRowCodeTemplate = 'Row { \n\
226 id: row; \n\
227 objectName: "outerRow"; \n\
228 property real margins: units.gu(1); \n\
229 spacing: margins; \n\
230 %2\
231 anchors { %1 } \n\
232 anchors.right: parent.right; \n\
233 anchors.margins: margins; \n\
234 anchors.rightMargin: 0; \n\
235 data: [ \n\
236 %3 \n\
237 ] \n\
238 }\n';
239 var args = Array.prototype.slice.call(arguments);
240 var isCardTool = args.shift();
241 var heightCode = isCardTool ? "" : "height: root.fixedHeaderHeight; \n";
242 var code = kHeaderRowCodeTemplate.arg(args.shift()).arg(heightCode).arg(args.join(',\n'));
243 return code;
246// multiple item version of kHeaderContainerCode
247function kHeaderContainerCodeGenerator() {
248 var headerContainerCodeTemplate = 'Item { \n\
249 id: headerTitleContainer; \n\
250 anchors { %1 } \n\
251 width: parent.width - x; \n\
252 implicitHeight: %2; \n\
253 data: [ \n\
254 %3 \n\
255 ]\n\
256 }\n';
257 var args = Array.prototype.slice.call(arguments);
258 var code = headerContainerCodeTemplate.arg(args.shift()).arg(args.shift()).arg(args.join(',\n'));
259 return code;
262// %1 is used as anchors of mascotShapeLoader
263// %2 is whether the loader should be asynchronous or not
264var kMascotShapeLoaderCode = 'Loader { \n\
265 id: mascotShapeLoader; \n\
266 objectName: "mascotShapeLoader"; \n\
267 asynchronous: %2; \n\
268 active: mascotImage.status === Image.Ready; \n\
269 visible: showHeader && active && status === Loader.Ready; \n\
270 width: units.gu(6); \n\
271 height: units.gu(5.625); \n\
272 sourceComponent: UbuntuShape { image: mascotImage } \n\
273 anchors { %1 } \n\
274 }\n';
276// %1 is used as anchors of mascotImage
277// %2 is used as visible of mascotImage
278// %3 is injected as code to mascotImage
279// %4 is used as fallback image
280var kMascotImageCode = 'CroppedImageMinimumSourceSize { \n\
281 id: mascotImage; \n\
282 objectName: "mascotImage"; \n\
283 anchors { %1 } \n\
284 source: cardData && cardData["mascot"] || %4; \n\
285 width: units.gu(6); \n\
286 height: units.gu(5.625); \n\
287 horizontalAlignment: Image.AlignHCenter; \n\
288 verticalAlignment: Image.AlignVCenter; \n\
289 visible: %2; \n\
290 %3 \n\
291 }\n';
293// %1 is used as anchors of titleLabel
294// %2 is used as color of titleLabel
295// %3 is used as extra condition for visible of titleLabel
296// %4 is used as title width
297// %5 is used as horizontal alignment
298var kTitleLabelCode = 'Label { \n\
299 id: titleLabel; \n\
300 objectName: "titleLabel"; \n\
301 anchors { %1 } \n\
302 elide: Text.ElideRight; \n\
303 fontSize: "small"; \n\
304 wrapMode: Text.Wrap; \n\
305 maximumLineCount: 2; \n\
306 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
307 color: %2; \n\
308 visible: showHeader %3; \n\
309 width: %4; \n\
310 text: root.title; \n\
311 font.weight: cardData && cardData["subtitle"] ? Font.DemiBold : Font.Normal; \n\
312 horizontalAlignment: %5; \n\
313 }\n';
315// %1 is used as extra anchors of emblemIcon
316// %2 is used as color of emblemIcon
317// FIXME The width code is a
318// Workaround for bug https://bugs.launchpad.net/ubuntu/+source/ubuntu-ui-toolkit/+bug/1421293
319var kEmblemIconCode = 'Icon { \n\
320 id: emblemIcon; \n\
321 objectName: "emblemIcon"; \n\
322 anchors { \n\
323 bottom: titleLabel.baseline; \n\
324 right: parent.right; \n\
325 %1 \n\
326 } \n\
327 source: cardData && cardData["emblem"] || ""; \n\
328 color: %2; \n\
329 height: source != "" ? titleLabel.font.pixelSize : 0; \n\
330 width: implicitWidth > 0 && implicitHeight > 0 ? (implicitWidth / implicitHeight * height) : implicitWidth; \n\
331 }\n';
333// %1 is used as anchors of touchdown effect
334var kTouchdownCode = 'UbuntuShape { \n\
335 id: touchdown; \n\
336 objectName: "touchdown"; \n\
337 anchors { %1 } \n\
338 visible: root.pressed; \n\
339 radius: "medium"; \n\
340 borderSource: "radius_pressed.sci" \n\
341 }\n';
343// %1 is used as anchors of subtitleLabel
344// %2 is used as color of subtitleLabel
345var kSubtitleLabelCode = 'Label { \n\
346 id: subtitleLabel; \n\
347 objectName: "subtitleLabel"; \n\
348 anchors { %1 } \n\
349 anchors.topMargin: units.dp(2); \n\
350 elide: Text.ElideRight; \n\
351 maximumLineCount: 1; \n\
352 fontSize: "x-small"; \n\
353 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
354 color: %2; \n\
355 visible: titleLabel.visible && titleLabel.text; \n\
356 text: cardData && cardData["subtitle"] || ""; \n\
357 font.weight: Font.Light; \n\
358 }\n';
360// %1 is used as anchors of attributesRow
361// %2 is used as color of attributesRow
362var kAttributesRowCode = 'CardAttributes { \n\
363 id: attributesRow; \n\
364 objectName: "attributesRow"; \n\
365 anchors { %1 } \n\
366 color: %2; \n\
367 fontScale: root.fontScale; \n\
368 model: cardData && cardData["attributes"]; \n\
369 }\n';
371// %1 is used as anchors of socialActionsRow
372// %2 is used as color of socialActionsRow
373var kSocialActionsRowCode = 'CardSocialActions { \n\
374 id: socialActionsRow; \n\
375 objectName: "socialActionsRow"; \n\
376 anchors { %1 } \n\
377 color: %2; \n\
378 model: cardData && cardData["socialActions"]; \n\
379 onClicked: root.action(actionId); \n\
380 }\n';
382// %1 is used as top anchor of summary
383// %2 is used as topMargin anchor of summary
384// %3 is used as color of summary
385var kSummaryLabelCode = 'Label { \n\
386 id: summary; \n\
387 objectName: "summaryLabel"; \n\
388 anchors { \n\
389 top: %1; \n\
390 left: parent.left; \n\
391 right: parent.right; \n\
392 margins: units.gu(1); \n\
393 topMargin: %2; \n\
394 } \n\
395 wrapMode: Text.Wrap; \n\
396 maximumLineCount: 5; \n\
397 elide: Text.ElideRight; \n\
398 text: cardData && cardData["summary"] || ""; \n\
399 height: text ? implicitHeight : 0; \n\
400 fontSize: "small"; \n\
401 color: %3; \n\
402 }\n';
404// %1 is used as bottom anchor of audio progress bar
405// %2 is used as left anchor of audio progress bar
406// %3 is used as text color
407var kAudioProgressBarCode = 'CardAudioProgress { \n\
408 id: audioProgressBar; \n\
409 duration: (cardData["quickPreviewData"] && cardData["quickPreviewData"]["duration"]) || 0; \n\
410 source: (cardData["quickPreviewData"] && cardData["quickPreviewData"]["uri"]) || ""; \n\
411 anchors { \n\
412 bottom: %1; \n\
413 left: %2; \n\
414 right: parent.right; \n\
415 margins: units.gu(1); \n\
416 } \n\
417 color: %3; \n\
418 }';
420function sanitizeColor(colorString) {
421 if (colorString !== undefined) {
422 if (colorString.match(/^[#a-z0-9]*$/i) === null) {
423 // This is not the perfect regexp for color
424 // but what we're trying to do here is just protect
425 // against injection so it's ok
426 return "";
427 }
428 }
429 return colorString;
432function cardString(template, components, isCardTool, artShapeStyle, categoryLayout) {
433 var code;
435 var templateInteractive = (template == null ? true : (template["non-interactive"] !== undefined ? !template["non-interactive"] : true)) ? "true" : "false";
437 code = 'AbstractButton { \n\
438 id: root; \n\
439 property var cardData; \n\
440 property string backgroundShapeStyle: "inset"; \n\
441 property real fontScale: 1.0; \n\
442 property var scopeStyle: null; \n\
443 readonly property string title: cardData && cardData["title"] || ""; \n\
444 property bool showHeader: true; \n\
445 implicitWidth: childrenRect.width; \n\
446 enabled: %1; \n\
447 \n'.arg(templateInteractive);
449 if (!isCardTool) {
450 code += "property int fixedHeaderHeight: -1; \n\
451 property size fixedArtShapeSize: Qt.size(-1, -1); \n";
452 }
454 var hasArt = components["art"] && components["art"]["field"] || false;
455 var hasSummary = components["summary"] || false;
456 var isConciergeMode = components["art"] && components["art"]["conciergeMode"] || false;
457 var artAndSummary = hasArt && hasSummary && !isConciergeMode;
458 var isHorizontal = template["card-layout"] === "horizontal";
459 var hasBackground = (!isHorizontal && (template["card-background"] || components["background"] || artAndSummary)) ||
460 (hasSummary && (template["card-background"] || components["background"]));
461 var hasTitle = components["title"] || false;
462 var hasMascot = components["mascot"] || false;
463 var hasEmblem = components["emblem"] && !(hasMascot && template["card-size"] === "small") || false;
464 var headerAsOverlay = hasArt && template && template["overlay"] === true && (hasTitle || hasMascot);
465 var hasSubtitle = hasTitle && components["subtitle"] || false;
466 var hasHeaderRow = hasMascot && hasTitle;
467 var hasAttributes = hasTitle && components["attributes"] && components["attributes"]["field"] || false;
468 var hasSocialActions = hasTitle && components["social-actions"] || false;
469 var isAudio = template["quick-preview-type"] === "audio";
470 var asynchronous = isCardTool ? "false" : "true";
472 code += 'signal action(var actionId);\n';
473 if (isAudio) {
474 // For now we only support audio cards with [optional] art, title, subtitle
475 // in horizontal mode
476 // Anything else makes it behave not like an audio card
477 if (hasSummary) isAudio = false;
478 if (!isHorizontal) isAudio = false;
479 if (hasMascot) isAudio = false;
480 if (hasEmblem) isAudio = false;
481 if (headerAsOverlay) isAudio = false;
482 if (hasAttributes) isAudio = false;
483 }
485 if (hasBackground) {
486 var templateCardBackground;
487 if (template && typeof template["card-background"] === "string") {
488 templateCardBackground = 'decodeURI("' + encodeURI(template["card-background"]) + '")';
489 } else {
490 templateCardBackground = '""';
491 }
493 var backgroundElements0;
494 var backgroundElements1;
495 if (template && typeof template["card-background"] === "object" && (template["card-background"]["type"] === "color" || template["card-background"]["type"] === "gradient")) {
496 var element0 = sanitizeColor(template["card-background"]["elements"][0]);
497 var element1 = sanitizeColor(template["card-background"]["elements"][1]);
498 if (element0 !== undefined) {
499 backgroundElements0 = '"%1"'.arg(element0);
500 }
501 if (element1 !== undefined) {
502 backgroundElements1 = '"%1"'.arg(element1);
503 }
504 }
505 code += kBackgroundLoaderCode.arg(backgroundElements0).arg(backgroundElements1).arg(asynchronous).arg(templateCardBackground);
506 }
508 if (hasArt) {
509 code += 'readonly property size artShapeSize: artShapeLoader.item ? Qt.size(artShapeLoader.item.width, artShapeLoader.item.height) : Qt.size(-1, -1);\n';
511 var artShapeAspect;
512 if (isCardTool) {
513 var artShapeAspect = components["art"] && components["art"]["aspect-ratio"] || 1;
514 if (isNaN(artShapeAspect)) {
515 artShapeAspect = 1;
516 }
517 } else {
518 artShapeAspect = "(root.fixedArtShapeSize.width / root.fixedArtShapeSize.height)";
519 }
521 var widthCode, heightCode;
522 var artAnchors;
523 if (isHorizontal) {
524 artAnchors = 'left: parent.left';
525 if (hasMascot || hasTitle) {
526 widthCode = 'height * ' + artShapeAspect;
527 heightCode = 'headerHeight + 2 * units.gu(1)';
528 } else {
529 // This side of the else is a bit silly, who wants an horizontal layout without mascot and title?
530 // So we define a "random" height of the image height + 2 gu for the margins
531 widthCode = 'height * ' + artShapeAspect
532 heightCode = 'units.gu(7.625)';
533 }
534 } else {
535 artAnchors = 'horizontalCenter: parent.horizontalCenter;';
536 widthCode = 'root.width'
537 heightCode = 'width / ' + artShapeAspect;
538 }
540 var fallback = !isCardTool && components["art"] && components["art"]["fallback"] || "";
541 fallback = encodeURI(fallback);
542 var fallbackStatusCode = "";
543 var fallbackURICode = '""';
544 if (fallback !== "") {
545 // fallbackStatusCode has %8 in it because we want to substitute it for fallbackURICode
546 // which in kArtShapeHolderCode is %8
547 fallbackStatusCode += 'onStatusChanged: if (status === Image.Error) source = %8;';
548 fallbackURICode = 'decodeURI("%1")'.arg(fallback);
549 }
550 var artShapeHolderShapeCode;
551 if (!isConciergeMode) {
552 if (artShapeStyle === "icon") {
553 artShapeHolderShapeCode = kArtProportionalShapeCode;
554 } else {
555 var artShapeHolderShapeAspect;
556 switch (artShapeStyle) {
557 case "inset": artShapeHolderShapeAspect = "UbuntuShape.Inset"; break;
558 case "shadow": artShapeHolderShapeAspect = "UbuntuShape.DropShadow"; break;
559 default:
560 case "flat": artShapeHolderShapeAspect = "UbuntuShape.Flat"; break;
561 }
562 artShapeHolderShapeCode = kArtUbuntuShapeCode.arg(artShapeHolderShapeAspect);
563 }
564 } else {
565 artShapeHolderShapeCode = "";
566 }
567 var artShapeHolderCode = isCardTool ? kArtShapeHolderCodeCardToolCard : kArtShapeHolderCode;
568 code += artShapeHolderCode.arg(artAnchors)
569 .arg(widthCode)
570 .arg(heightCode)
571 .arg(isConciergeMode ? "true" : "false")
572 .arg(asynchronous)
573 .arg(artShapeHolderShapeCode)
574 .arg(fallbackStatusCode)
575 .arg(fallbackURICode);
576 } else {
577 code += 'readonly property size artShapeSize: Qt.size(-1, -1);\n'
578 }
580 if (headerAsOverlay) {
581 var headerHeightCode = isCardTool ? "headerHeight" : "root.fixedHeaderHeight";
582 code += kOverlayLoaderCode.arg(asynchronous).arg(headerHeightCode);
583 }
585 var headerVerticalAnchors;
586 if (headerAsOverlay) {
587 headerVerticalAnchors = 'bottom: artShapeLoader.bottom; \n\
588 bottomMargin: units.gu(1);\n';
589 } else {
590 if (hasArt) {
591 if (isHorizontal) {
592 headerVerticalAnchors = 'top: artShapeLoader.top; \n\
593 topMargin: units.gu(1);\n';
594 } else {
595 headerVerticalAnchors = 'top: artShapeLoader.bottom; \n\
596 topMargin: units.gu(1);\n';
597 }
598 } else {
599 headerVerticalAnchors = 'top: parent.top; \n\
600 topMargin: units.gu(1);\n';
601 }
602 }
604 var headerLeftAnchor;
605 var headerLeftAnchorHasMargin = false;
606 if (isHorizontal && hasArt) {
607 headerLeftAnchor = 'left: artShapeLoader.right; \n\
608 leftMargin: units.gu(1);\n';
609 headerLeftAnchorHasMargin = true;
610 } else if (isHorizontal && isAudio) {
611 headerLeftAnchor = 'left: audioButton.right; \n\
612 leftMargin: units.gu(1);\n';
613 headerLeftAnchorHasMargin = true;
614 } else {
615 headerLeftAnchor = 'left: parent.left;\n';
616 }
618 var touchdownOnArtShape = !hasBackground && hasArt && !hasMascot && !hasSummary && !isAudio;
620 if (hasHeaderRow) {
621 code += 'readonly property int headerHeight: row.height;\n'
622 } else if (hasMascot) {
623 code += 'readonly property int headerHeight: mascotImage.height;\n'
624 } else if (hasAttributes) {
625 if (hasTitle && hasSubtitle) {
626 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin + attributesRow.height + attributesRow.anchors.topMargin;\n'
627 } else if (hasTitle) {
628 code += 'readonly property int headerHeight: titleLabel.height + attributesRow.height + attributesRow.anchors.topMargin;\n'
629 } else {
630 code += 'readonly property int headerHeight: attributesRow.height;\n'
631 }
632 } else if (isAudio) {
633 if (hasSubtitle) {
634 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin + audioProgressBar.height + audioProgressBar.anchors.topMargin;\n'
635 } else if (hasTitle) {
636 code += 'readonly property int headerHeight: titleLabel.height + audioProgressBar.height + audioProgressBar.anchors.topMargin;\n'
637 } else {
638 code += 'readonly property int headerHeight: audioProgressBar.height;\n'
639 }
640 } else if (hasSubtitle) {
641 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin;\n'
642 } else if (hasTitle) {
643 code += 'readonly property int headerHeight: titleLabel.height;\n'
644 } else {
645 code += 'readonly property int headerHeight: 0;\n'
646 }
648 var mascotShapeCode = '';
649 var mascotCode = '';
650 if (hasMascot) {
651 var useMascotShape = !hasBackground && !headerAsOverlay;
652 var mascotAnchors = '';
653 if (!hasHeaderRow) {
654 mascotAnchors += headerLeftAnchor;
655 mascotAnchors += headerVerticalAnchors;
656 if (!headerLeftAnchorHasMargin) {
657 mascotAnchors += 'leftMargin: units.gu(1);\n'
658 }
659 } else {
660 mascotAnchors = 'verticalCenter: parent.verticalCenter;'
661 }
663 if (useMascotShape) {
664 mascotShapeCode = kMascotShapeLoaderCode.arg(mascotAnchors).arg(asynchronous);
665 }
667 var mascotImageVisible = useMascotShape ? 'false' : 'showHeader';
668 var fallback = !isCardTool && components["mascot"] && components["mascot"]["fallback"] || "";
669 fallback = encodeURI(fallback);
670 var fallbackStatusCode = "";
671 var fallbackURICode = '""';
672 if (fallback !== "") {
673 // fallbackStatusCode has %4 in it because we want to substitute it for fallbackURICode
674 // which in kMascotImageCode is %4
675 fallbackStatusCode += 'onStatusChanged: if (status === Image.Error) source = %4;';
676 fallbackURICode = 'decodeURI("%1")'.arg(fallback);
677 }
678 mascotCode = kMascotImageCode.arg(mascotAnchors).arg(mascotImageVisible).arg(fallbackStatusCode).arg(fallbackURICode);
679 }
681 var summaryColorWithBackground = 'backgroundLoader.active && backgroundLoader.item && root.scopeStyle ? root.scopeStyle.getTextColor(backgroundLoader.item.luminance) : (backgroundLoader.item && backgroundLoader.item.luminance > 0.7 ? theme.palette.normal.baseText : "white")';
683 var hasTitleContainer = hasTitle && (hasEmblem || (hasMascot && (hasSubtitle || hasAttributes)));
684 var titleSubtitleCode = '';
685 if (hasTitle) {
686 var titleColor;
687 if (headerAsOverlay) {
688 titleColor = 'root.scopeStyle && overlayLoader.item ? root.scopeStyle.getTextColor(overlayLoader.item.luminance) : (overlayLoader.item && overlayLoader.item.luminance > 0.7 ? theme.palette.normal.baseText : "white")';
689 } else if (hasSummary) {
690 titleColor = 'summary.color';
691 } else if (hasBackground) {
692 titleColor = summaryColorWithBackground;
693 } else {
694 titleColor = 'root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText';
695 }
697 var titleAnchors;
698 var subtitleAnchors;
699 var attributesAnchors;
700 var titleContainerAnchors;
701 var titleRightAnchor;
702 var titleWidth = "undefined";
704 var extraRightAnchor = '';
705 var extraLeftAnchor = '';
706 if (!touchdownOnArtShape) {
707 extraRightAnchor = 'rightMargin: units.gu(1); \n';
708 extraLeftAnchor = 'leftMargin: units.gu(1); \n';
709 } else if (headerAsOverlay && !hasEmblem) {
710 extraRightAnchor = 'rightMargin: units.gu(1); \n';
711 }
713 if (hasMascot) {
714 titleContainerAnchors = 'verticalCenter: parent.verticalCenter; ';
715 } else {
716 titleContainerAnchors = 'right: parent.right; ';
717 titleContainerAnchors += headerLeftAnchor;
718 titleContainerAnchors += headerVerticalAnchors;
719 if (!headerLeftAnchorHasMargin) {
720 titleContainerAnchors += extraLeftAnchor;
721 }
722 }
723 if (hasEmblem) {
724 titleRightAnchor = 'right: emblemIcon.left; \n\
725 rightMargin: emblemIcon.width > 0 ? units.gu(0.5) : 0; \n';
726 } else {
727 titleRightAnchor = 'right: parent.right; \n'
728 titleRightAnchor += extraRightAnchor;
729 }
731 if (hasTitleContainer) {
732 // Using headerTitleContainer
733 titleAnchors = titleRightAnchor;
734 titleAnchors += 'left: parent.left; \n\
735 top: parent.top;';
736 subtitleAnchors = 'right: parent.right; \n\
737 left: parent.left; \n';
738 subtitleAnchors += extraRightAnchor;
739 if (hasSubtitle) {
740 attributesAnchors = subtitleAnchors + 'top: subtitleLabel.bottom;\n';
741 subtitleAnchors += 'top: titleLabel.bottom;\n';
742 } else {
743 attributesAnchors = subtitleAnchors + 'top: titleLabel.bottom;\n';
744 }
745 } else if (hasMascot) {
746 // Using row without titleContainer
747 titleAnchors = 'verticalCenter: parent.verticalCenter;\n';
748 titleWidth = "parent.width - x";
749 } else {
750 if (headerAsOverlay) {
751 // Using anchors to the overlay
752 titleAnchors = titleRightAnchor;
753 titleAnchors += 'left: parent.left; \n\
754 leftMargin: units.gu(1); \n\
755 top: overlayLoader.top; \n\
756 topMargin: units.gu(1) + overlayLoader.height - overlayLoader.overlayHeight; \n';
757 } else {
758 // Using anchors to the mascot/parent
759 titleAnchors = titleRightAnchor;
760 titleAnchors += headerLeftAnchor;
761 titleAnchors += headerVerticalAnchors;
762 if (!headerLeftAnchorHasMargin) {
763 titleAnchors += extraLeftAnchor;
764 }
765 }
766 subtitleAnchors = 'left: titleLabel.left; \n\
767 leftMargin: titleLabel.leftMargin; \n';
768 subtitleAnchors += extraRightAnchor;
769 if (hasEmblem) {
770 // using container
771 subtitleAnchors += 'right: parent.right; \n';
772 } else {
773 subtitleAnchors += 'right: titleLabel.right; \n';
774 }
776 if (hasSubtitle) {
777 attributesAnchors = subtitleAnchors + 'top: subtitleLabel.bottom;\n';
778 subtitleAnchors += 'top: titleLabel.bottom;\n';
779 } else {
780 attributesAnchors = subtitleAnchors + 'top: titleLabel.bottom;\n';
781 }
782 }
784 var titleAlignment = "Text.AlignHCenter";
785 if (template["card-layout"] === "horizontal"
786 || typeof components["title"] !== "object"
787 || components["title"]["align"] === "left") titleAlignment = "Text.AlignLeft";
788 var keys = ["mascot", "emblem", "subtitle", "attributes", "summary"];
789 for (var key in keys) {
790 key = keys[key];
791 try {
792 if (typeof components[key] === "string"
793 || typeof components[key]["field"] === "string") titleAlignment = "Text.AlignLeft";
794 } catch (e) {
795 continue;
796 }
797 }
799 // code for different elements
800 var titleLabelVisibleExtra = (headerAsOverlay ? '&& overlayLoader.active': '');
801 var titleCode = kTitleLabelCode.arg(titleAnchors).arg(titleColor).arg(titleLabelVisibleExtra).arg(titleWidth).arg(titleAlignment);
802 var subtitleCode;
803 var attributesCode;
805 // code for the title container
806 var containerCode = [];
807 var containerHeight = 'titleLabel.height';
808 containerCode.push(titleCode);
809 if (hasSubtitle) {
810 subtitleCode = kSubtitleLabelCode.arg(subtitleAnchors).arg(titleColor);
811 containerCode.push(subtitleCode);
812 containerHeight += ' + subtitleLabel.height';
813 }
814 if (hasEmblem) {
815 containerCode.push(kEmblemIconCode.arg(extraRightAnchor).arg(titleColor));
816 }
817 if (hasAttributes) {
818 attributesCode = kAttributesRowCode.arg(attributesAnchors).arg(titleColor);
819 containerCode.push(attributesCode);
820 containerHeight += ' + attributesRow.height';
821 }
823 if (hasTitleContainer) {
824 // use container
825 titleSubtitleCode = kHeaderContainerCodeGenerator(titleContainerAnchors, containerHeight, containerCode);
826 } else {
827 // no container
828 titleSubtitleCode = titleCode;
829 if (hasSubtitle) {
830 titleSubtitleCode += subtitleCode;
831 }
832 if (hasAttributes) {
833 titleSubtitleCode += attributesCode;
834 }
835 }
836 }
838 if (hasHeaderRow) {
839 var rowCode = [mascotCode, titleSubtitleCode];
840 if (mascotShapeCode != '') {
841 rowCode.unshift(mascotShapeCode);
842 }
843 code += kHeaderRowCodeGenerator(isCardTool, headerVerticalAnchors + headerLeftAnchor, rowCode)
844 } else {
845 code += mascotShapeCode + mascotCode + titleSubtitleCode;
846 }
848 if (isAudio) {
849 var audioProgressBarLeftAnchor = 'audioButton.right';
850 var audioProgressBarBottomAnchor = 'audioButton.bottom';
851 var audioProgressBarTextColor = 'root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText';
853 code += kAudioProgressBarCode.arg(audioProgressBarBottomAnchor)
854 .arg(audioProgressBarLeftAnchor)
855 .arg(audioProgressBarTextColor);
857 var audioButtonAnchorsFill;
858 var audioButtonWidth;
859 var audioButtonHeight;
860 if (hasArt) {
861 audioButtonAnchorsFill = 'artShapeLoader';
862 audioButtonWidth = 'undefined';
863 audioButtonHeight = 'undefined';
864 } else {
865 audioButtonAnchorsFill = 'undefined';
866 audioButtonWidth = 'height';
867 audioButtonHeight = isCardTool ? 'headerHeight + 2 * units.gu(1)'
868 : 'root.fixedHeaderHeight + 2 * units.gu(1)';
869 }
870 code += kAudioButtonCode.arg(audioButtonAnchorsFill).arg(audioButtonWidth).arg(audioButtonHeight).arg(asynchronous);
871 }
873 if (hasSummary) {
874 var summaryTopAnchor;
875 if (isHorizontal && hasArt) summaryTopAnchor = 'artShapeLoader.bottom';
876 else if (headerAsOverlay && hasArt) summaryTopAnchor = 'artShapeLoader.bottom';
877 else if (hasHeaderRow) summaryTopAnchor = 'row.bottom';
878 else if (hasTitleContainer) summaryTopAnchor = 'headerTitleContainer.bottom';
879 else if (hasMascot) summaryTopAnchor = 'mascotImage.bottom';
880 else if (hasAttributes) summaryTopAnchor = 'attributesRow.bottom';
881 else if (hasSubtitle) summaryTopAnchor = 'subtitleLabel.bottom';
882 else if (hasTitle) summaryTopAnchor = 'titleLabel.bottom';
883 else if (hasArt) summaryTopAnchor = 'artShapeLoader.bottom';
884 else summaryTopAnchor = 'parent.top';
886 var summaryColor;
887 if (hasBackground) {
888 summaryColor = summaryColorWithBackground;
889 } else {
890 summaryColor = 'root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText';
891 }
893 var summaryTopMargin = (hasMascot || hasSubtitle || hasAttributes ? 'anchors.margins' : '0');
895 code += kSummaryLabelCode.arg(summaryTopAnchor).arg(summaryTopMargin).arg(summaryColor);
896 }
898 if (hasSocialActions) {
899 var socialAnchors;
900 var socialTopAnchor;
902 if (hasSummary) socialTopAnchor = 'summary.bottom;';
903 else if (isHorizontal && hasArt) socialTopAnchor = 'artShapeLoader.bottom;';
904 else if (headerAsOverlay && hasArt) socialTopAnchor = 'artShapeLoader.bottom;';
905 else if (hasHeaderRow) socialTopAnchor = 'row.bottom;';
906 else if (hasTitleContainer) socialTopAnchor = 'headerTitleContainer.bottom;';
907 else if (hasMascot) socialTopAnchor = 'mascotImage.bottom;';
908 else if (hasAttributes) socialTopAnchor = 'attributesRow.bottom;';
909 else if (hasSubtitle) socialTopAnchor = 'subtitleLabel.bottom;';
910 else if (hasTitle) socialTopAnchor = 'titleLabel.bottom;';
911 else if (hasArt) socialTopAnchor = 'artShapeLoader.bottom;';
912 else socialTopAnchor = 'parent.top';
914 socialAnchors = 'top: ' + socialTopAnchor + ' left: parent.left; right: parent.right; topMargin: units.gu(1);'
916 var socialColor;
917 if (hasBackground) {
918 socialColor = summaryColorWithBackground;
919 } else {
920 socialColor = 'root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText';
921 }
923 code += kSocialActionsRowCode.arg(socialAnchors).arg(socialColor);
924 }
926 if (artShapeStyle != "shadow" && artShapeStyle != "icon") {
927 var touchdownAnchors;
928 if (hasBackground) {
929 touchdownAnchors = 'fill: backgroundLoader';
930 } else if (touchdownOnArtShape) {
931 touchdownAnchors = 'fill: artShapeLoader';
932 } else {
933 touchdownAnchors = 'fill: root'
934 }
935 code += kTouchdownCode.arg(touchdownAnchors);
936 }
938 if (isCardTool || categoryLayout !== "grid") {
939 var implicitHeight = 'implicitHeight: ';
940 if (hasSocialActions) {
941 implicitHeight += 'socialActionsRow.y + socialActionsRow.height + units.gu(1);\n';
942 } else if (hasSummary) {
943 implicitHeight += 'summary.y + summary.height + units.gu(1);\n';
944 } else if (isAudio) {
945 implicitHeight += 'audioButton.height;\n';
946 } else if (headerAsOverlay) {
947 implicitHeight += 'artShapeLoader.height;\n';
948 } else if (hasHeaderRow) {
949 implicitHeight += 'row.y + row.height + units.gu(1);\n';
950 } else if (hasMascot) {
951 implicitHeight += 'mascotImage.y + mascotImage.height;\n';
952 } else if (hasTitleContainer) {
953 implicitHeight += 'headerTitleContainer.y + headerTitleContainer.height + units.gu(1);\n';
954 } else if (hasAttributes) {
955 implicitHeight += 'attributesRow.y + attributesRow.height + units.gu(1);\n';
956 } else if (hasSubtitle) {
957 implicitHeight += 'subtitleLabel.y + subtitleLabel.height + units.gu(1);\n';
958 } else if (hasTitle) {
959 implicitHeight += 'titleLabel.y + titleLabel.height + units.gu(1);\n';
960 } else if (hasArt) {
961 implicitHeight += 'artShapeLoader.height;\n';
962 } else {
963 implicitHeight = '';
964 }
965 } else {
966 implicitHeight = '';
967 }
969 // Close the AbstractButton
970 code += implicitHeight + '}\n';
972 return code;
975function createCardComponent(parent, template, components, isCardTool, artShapeStyle, categoryLayout, identifier) {
976 var imports = 'import QtQuick 2.4; \n\
977 import Ubuntu.Components 1.3; \n\
978 import Ubuntu.Settings.Components 0.1; \n\
979 import Dash 0.1;\n\
980 import Utils 0.1;\n';
981 var card = cardString(template, components, isCardTool, artShapeStyle, categoryLayout);
982 var code = imports + 'Component {\n' + card + '}\n';
984 try {
985 return Qt.createQmlObject(code, parent, identifier);
986 } catch (e) {
987 console.error("ERROR: Invalid component created.");
988 console.error("Template:");
989 console.error(JSON.stringify(template));
990 console.error("Components:");
991 console.error(JSON.stringify(components));
992 console.error("Code:");
993 console.error(code);
994 throw e;
995 }