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