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