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