on 0cfbca3 help sign in
unity8/plugins/Dash/CardCreator.js
906 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 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 top anchor of summary
355// %2 is used as topMargin anchor of summary
356// %3 is used as color of summary
357var kSummaryLabelCode = 'Label { \n\
358 id: summary; \n\
359 objectName: "summaryLabel"; \n\
360 anchors { \n\
361 top: %1; \n\
362 left: parent.left; \n\
363 right: parent.right; \n\
364 margins: units.gu(1); \n\
365 topMargin: %2; \n\
366 } \n\
367 wrapMode: Text.Wrap; \n\
368 maximumLineCount: 5; \n\
369 elide: Text.ElideRight; \n\
370 text: cardData && cardData["summary"] || ""; \n\
371 height: text ? implicitHeight : 0; \n\
372 fontSize: "small"; \n\
373 color: %3; \n\
374 }\n';
376// %1 is used as bottom anchor of audio progress bar
377// %2 is used as left anchor of audio progress bar
378// %3 is used as text color
379var kAudioProgressBarCode = 'CardAudioProgress { \n\
380 id: audioProgressBar; \n\
381 duration: (cardData["quickPreviewData"] && cardData["quickPreviewData"]["duration"]) || 0; \n\
382 source: (cardData["quickPreviewData"] && cardData["quickPreviewData"]["uri"]) || ""; \n\
383 anchors { \n\
384 bottom: %1; \n\
385 left: %2; \n\
386 right: parent.right; \n\
387 margins: units.gu(1); \n\
388 } \n\
389 color: %3; \n\
390 }';
392function sanitizeColor(colorString) {
393 if (colorString !== undefined) {
394 if (colorString.match(/^[#a-z0-9]*$/i) === null) {
395 // This is not the perfect regexp for color
396 // but what we're trying to do here is just protect
397 // against injection so it's ok
398 return "";
399 }
400 }
401 return colorString;
404function cardString(template, components, isCardTool) {
405 var code;
407 var templateInteractive = (template == null ? true : (template["non-interactive"] !== undefined ? !template["non-interactive"] : true)) ? "true" : "false";
409 code = 'AbstractButton { \n\
410 id: root; \n\
411 property var cardData; \n\
412 property string artShapeStyle: "inset"; \n\
413 property string backgroundShapeStyle: "inset"; \n\
414 property real fontScale: 1.0; \n\
415 property var scopeStyle: null; \n\
416 %2\
417 property size fixedArtShapeSize: Qt.size(-1, -1); \n\
418 readonly property string title: cardData && cardData["title"] || ""; \n\
419 property bool showHeader: true; \n\
420 implicitWidth: childrenRect.width; \n\
421 enabled: %1; \n\
422 \n'.arg(templateInteractive);
424 code = code.arg(isCardTool ? "" : "property int fixedHeaderHeight: -1; \n");
426 var hasArt = components["art"] && components["art"]["field"] || false;
427 var hasSummary = components["summary"] || false;
428 var isConciergeMode = components["art"] && components["art"]["conciergeMode"] || false;
429 var artAndSummary = hasArt && hasSummary && !isConciergeMode;
430 var isHorizontal = template["card-layout"] === "horizontal";
431 var hasBackground = (!isHorizontal && (template["card-background"] || components["background"] || artAndSummary)) ||
432 (hasSummary && (template["card-background"] || components["background"]));
433 var hasTitle = components["title"] || false;
434 var hasMascot = components["mascot"] || false;
435 var hasEmblem = components["emblem"] && !(hasMascot && template["card-size"] === "small") || false;
436 var headerAsOverlay = hasArt && template && template["overlay"] === true && (hasTitle || hasMascot);
437 var hasSubtitle = hasTitle && components["subtitle"] || false;
438 var hasHeaderRow = hasMascot && hasTitle;
439 var hasAttributes = hasTitle && components["attributes"] && components["attributes"]["field"] || false;
440 var isAudio = template["quick-preview-type"] === "audio";
441 var asynchronous = isCardTool ? "false" : "true";
443 if (isAudio) {
444 // For now we only support audio cards with [optional] art, title, subtitle
445 // in horizontal mode
446 // Anything else makes it behave not like an audio card
447 if (hasSummary) isAudio = false;
448 if (!isHorizontal) isAudio = false;
449 if (hasMascot) isAudio = false;
450 if (hasEmblem) isAudio = false;
451 if (headerAsOverlay) isAudio = false;
452 if (hasAttributes) isAudio = false;
453 }
455 if (hasBackground) {
456 var templateCardBackground;
457 if (template && typeof template["card-background"] === "string") {
458 templateCardBackground = 'decodeURI("' + encodeURI(template["card-background"]) + '")';
459 } else {
460 templateCardBackground = '""';
461 }
463 var backgroundElements0;
464 var backgroundElements1;
465 if (template && typeof template["card-background"] === "object" && (template["card-background"]["type"] === "color" || template["card-background"]["type"] === "gradient")) {
466 var element0 = sanitizeColor(template["card-background"]["elements"][0]);
467 var element1 = sanitizeColor(template["card-background"]["elements"][1]);
468 if (element0 !== undefined) {
469 backgroundElements0 = '"%1"'.arg(element0);
470 }
471 if (element1 !== undefined) {
472 backgroundElements1 = '"%1"'.arg(element1);
473 }
474 }
475 code += kBackgroundLoaderCode.arg(backgroundElements0).arg(backgroundElements1).arg(asynchronous).arg(templateCardBackground);
476 }
478 if (hasArt) {
479 code += 'readonly property size artShapeSize: artShapeLoader.item ? Qt.size(artShapeLoader.item.width, artShapeLoader.item.height) : Qt.size(-1, -1);\n';
481 var widthCode, heightCode;
482 var artAnchors;
483 if (isHorizontal) {
484 artAnchors = 'left: parent.left';
485 if (hasMascot || hasTitle) {
486 widthCode = 'height * artShape.aspect'
487 heightCode = 'headerHeight + 2 * units.gu(1)';
488 } else {
489 // This side of the else is a bit silly, who wants an horizontal layout without mascot and title?
490 // So we define a "random" height of the image height + 2 gu for the margins
491 widthCode = 'height * artShape.aspect'
492 heightCode = 'units.gu(7.625)';
493 }
494 } else {
495 artAnchors = 'horizontalCenter: parent.horizontalCenter;';
496 widthCode = 'root.width'
497 heightCode = 'width / artShape.aspect';
498 }
500 var aspectRatio = components["art"] && components["art"]["aspect-ratio"] || 1;
501 if (isNaN(aspectRatio)) {
502 aspectRatio = 1;
503 }
504 var fallback = !isCardTool && components["art"] && components["art"]["fallback"] || "";
505 fallback = encodeURI(fallback);
506 var fallbackStatusCode = "";
507 var fallbackURICode = '""';
508 if (fallback !== "") {
509 // fallbackStatusCode has %6 in it because we want to substitute it for fallbackURICode
510 // which in kArtShapeHolderCode is %8
511 fallbackStatusCode += 'onStatusChanged: if (status === Image.Error) source = %8;';
512 fallbackURICode = 'decodeURI("%1")'.arg(fallback);
513 }
514 code += kArtShapeHolderCode.arg(artAnchors)
515 .arg(widthCode)
516 .arg(heightCode)
517 .arg(isConciergeMode ? "false" : "true")
518 .arg(aspectRatio)
519 .arg(asynchronous)
520 .arg(fallbackStatusCode)
521 .arg(fallbackURICode);
522 } else {
523 code += 'readonly property size artShapeSize: Qt.size(-1, -1);\n'
524 }
526 if (headerAsOverlay) {
527 var headerHeightCode = isCardTool ? "headerHeight" : "root.fixedHeaderHeight";
528 code += kOverlayLoaderCode.arg(asynchronous).arg(headerHeightCode);
529 }
531 var headerVerticalAnchors;
532 if (headerAsOverlay) {
533 headerVerticalAnchors = 'bottom: artShapeHolder.bottom; \n\
534 bottomMargin: units.gu(1);\n';
535 } else {
536 if (hasArt) {
537 if (isHorizontal) {
538 headerVerticalAnchors = 'top: artShapeHolder.top; \n\
539 topMargin: units.gu(1);\n';
540 } else {
541 headerVerticalAnchors = 'top: artShapeHolder.bottom; \n\
542 topMargin: units.gu(1);\n';
543 }
544 } else {
545 headerVerticalAnchors = 'top: parent.top; \n\
546 topMargin: units.gu(1);\n';
547 }
548 }
550 var headerLeftAnchor;
551 var headerLeftAnchorHasMargin = false;
552 if (isHorizontal && hasArt) {
553 headerLeftAnchor = 'left: artShapeHolder.right; \n\
554 leftMargin: units.gu(1);\n';
555 headerLeftAnchorHasMargin = true;
556 } else if (isHorizontal && isAudio) {
557 headerLeftAnchor = 'left: audioButton.right; \n\
558 leftMargin: units.gu(1);\n';
559 headerLeftAnchorHasMargin = true;
560 } else {
561 headerLeftAnchor = 'left: parent.left;\n';
562 }
564 var touchdownOnArtShape = !hasBackground && hasArt && !hasMascot && !hasSummary && !isAudio;
566 if (hasHeaderRow) {
567 code += 'readonly property int headerHeight: row.height;\n'
568 } else if (hasMascot) {
569 code += 'readonly property int headerHeight: mascotImage.height;\n'
570 } else if (hasAttributes) {
571 if (hasTitle && hasSubtitle) {
572 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin + attributesRow.height + attributesRow.anchors.topMargin;\n'
573 } else if (hasTitle) {
574 code += 'readonly property int headerHeight: titleLabel.height + attributesRow.height + attributesRow.anchors.topMargin;\n'
575 } else {
576 code += 'readonly property int headerHeight: attributesRow.height;\n'
577 }
578 } else if (isAudio) {
579 if (hasSubtitle) {
580 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin + audioProgressBar.height + audioProgressBar.anchors.topMargin;\n'
581 } else if (hasTitle) {
582 code += 'readonly property int headerHeight: titleLabel.height + audioProgressBar.height + audioProgressBar.anchors.topMargin;\n'
583 } else {
584 code += 'readonly property int headerHeight: audioProgressBar.height;\n'
585 }
586 } else if (hasSubtitle) {
587 code += 'readonly property int headerHeight: titleLabel.height + subtitleLabel.height + subtitleLabel.anchors.topMargin;\n'
588 } else if (hasTitle) {
589 code += 'readonly property int headerHeight: titleLabel.height;\n'
590 } else {
591 code += 'readonly property int headerHeight: 0;\n'
592 }
594 var mascotShapeCode = '';
595 var mascotCode = '';
596 if (hasMascot) {
597 var useMascotShape = !hasBackground && !headerAsOverlay;
598 var mascotAnchors = '';
599 if (!hasHeaderRow) {
600 mascotAnchors += headerLeftAnchor;
601 mascotAnchors += headerVerticalAnchors;
602 if (!headerLeftAnchorHasMargin) {
603 mascotAnchors += 'leftMargin: units.gu(1);\n'
604 }
605 } else {
606 mascotAnchors = 'verticalCenter: parent.verticalCenter;'
607 }
609 if (useMascotShape) {
610 mascotShapeCode = kMascotShapeLoaderCode.arg(mascotAnchors).arg(asynchronous);
611 }
613 var mascotImageVisible = useMascotShape ? 'false' : 'showHeader';
614 var fallback = !isCardTool && components["mascot"] && components["mascot"]["fallback"] || "";
615 fallback = encodeURI(fallback);
616 var fallbackStatusCode = "";
617 var fallbackURICode = '""';
618 if (fallback !== "") {
619 // fallbackStatusCode has %4 in it because we want to substitute it for fallbackURICode
620 // which in kMascotImageCode is %4
621 fallbackStatusCode += 'onStatusChanged: if (status === Image.Error) source = %4;';
622 fallbackURICode = 'decodeURI("%1")'.arg(fallback);
623 }
624 mascotCode = kMascotImageCode.arg(mascotAnchors).arg(mascotImageVisible).arg(fallbackStatusCode).arg(fallbackURICode);
625 }
627 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")';
629 var hasTitleContainer = hasTitle && (hasEmblem || (hasMascot && (hasSubtitle || hasAttributes)));
630 var titleSubtitleCode = '';
631 if (hasTitle) {
632 var titleColor;
633 if (headerAsOverlay) {
634 titleColor = 'root.scopeStyle && overlayLoader.item ? root.scopeStyle.getTextColor(overlayLoader.item.luminance) : (overlayLoader.item && overlayLoader.item.luminance > 0.7 ? theme.palette.normal.baseText : "white")';
635 } else if (hasSummary) {
636 titleColor = 'summary.color';
637 } else if (hasBackground) {
638 titleColor = summaryColorWithBackground;
639 } else {
640 titleColor = 'root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText';
641 }
643 var titleAnchors;
644 var subtitleAnchors;
645 var attributesAnchors;
646 var titleContainerAnchors;
647 var titleRightAnchor;
648 var titleWidth = "undefined";
650 var extraRightAnchor = '';
651 var extraLeftAnchor = '';
652 if (!touchdownOnArtShape) {
653 extraRightAnchor = 'rightMargin: units.gu(1); \n';
654 extraLeftAnchor = 'leftMargin: units.gu(1); \n';
655 } else if (headerAsOverlay && !hasEmblem) {
656 extraRightAnchor = 'rightMargin: units.gu(1); \n';
657 }
659 if (hasMascot) {
660 titleContainerAnchors = 'verticalCenter: parent.verticalCenter; ';
661 } else {
662 titleContainerAnchors = 'right: parent.right; ';
663 titleContainerAnchors += headerLeftAnchor;
664 titleContainerAnchors += headerVerticalAnchors;
665 if (!headerLeftAnchorHasMargin) {
666 titleContainerAnchors += extraLeftAnchor;
667 }
668 }
669 if (hasEmblem) {
670 titleRightAnchor = 'right: emblemIcon.left; \n\
671 rightMargin: emblemIcon.width > 0 ? units.gu(0.5) : 0; \n';
672 } else {
673 titleRightAnchor = 'right: parent.right; \n'
674 titleRightAnchor += extraRightAnchor;
675 }
677 if (hasTitleContainer) {
678 // Using headerTitleContainer
679 titleAnchors = titleRightAnchor;
680 titleAnchors += 'left: parent.left; \n\
681 top: parent.top;';
682 subtitleAnchors = 'right: parent.right; \n\
683 left: parent.left; \n';
684 subtitleAnchors += extraRightAnchor;
685 if (hasSubtitle) {
686 attributesAnchors = subtitleAnchors + 'top: subtitleLabel.bottom;\n';
687 subtitleAnchors += 'top: titleLabel.bottom;\n';
688 } else {
689 attributesAnchors = subtitleAnchors + 'top: titleLabel.bottom;\n';
690 }
691 } else if (hasMascot) {
692 // Using row without titleContainer
693 titleAnchors = 'verticalCenter: parent.verticalCenter;\n';
694 titleWidth = "parent.width - x";
695 } else {
696 if (headerAsOverlay) {
697 // Using anchors to the overlay
698 titleAnchors = titleRightAnchor;
699 titleAnchors += 'left: parent.left; \n\
700 leftMargin: units.gu(1); \n\
701 top: overlayLoader.top; \n\
702 topMargin: units.gu(1) + overlayLoader.height - overlayLoader.overlayHeight; \n';
703 } else {
704 // Using anchors to the mascot/parent
705 titleAnchors = titleRightAnchor;
706 titleAnchors += headerLeftAnchor;
707 titleAnchors += headerVerticalAnchors;
708 if (!headerLeftAnchorHasMargin) {
709 titleAnchors += extraLeftAnchor;
710 }
711 }
712 subtitleAnchors = 'left: titleLabel.left; \n\
713 leftMargin: titleLabel.leftMargin; \n';
714 subtitleAnchors += extraRightAnchor;
715 if (hasEmblem) {
716 // using container
717 subtitleAnchors += 'right: parent.right; \n';
718 } else {
719 subtitleAnchors += 'right: titleLabel.right; \n';
720 }
722 if (hasSubtitle) {
723 attributesAnchors = subtitleAnchors + 'top: subtitleLabel.bottom;\n';
724 subtitleAnchors += 'top: titleLabel.bottom;\n';
725 } else {
726 attributesAnchors = subtitleAnchors + 'top: titleLabel.bottom;\n';
727 }
728 }
730 var titleAlignment = "Text.AlignHCenter";
731 if (template["card-layout"] === "horizontal"
732 || typeof components["title"] !== "object"
733 || components["title"]["align"] === "left") titleAlignment = "Text.AlignLeft";
734 var keys = ["mascot", "emblem", "subtitle", "attributes", "summary"];
735 for (var key in keys) {
736 key = keys[key];
737 try {
738 if (typeof components[key] === "string"
739 || typeof components[key]["field"] === "string") titleAlignment = "Text.AlignLeft";
740 } catch (e) {
741 continue;
742 }
743 }
745 // code for different elements
746 var titleLabelVisibleExtra = (headerAsOverlay ? '&& overlayLoader.active': '');
747 var titleCode = kTitleLabelCode.arg(titleAnchors).arg(titleColor).arg(titleLabelVisibleExtra).arg(titleWidth).arg(titleAlignment);
748 var subtitleCode;
749 var attributesCode;
751 // code for the title container
752 var containerCode = [];
753 var containerHeight = 'titleLabel.height';
754 containerCode.push(titleCode);
755 if (hasSubtitle) {
756 subtitleCode = kSubtitleLabelCode.arg(subtitleAnchors).arg(titleColor);
757 containerCode.push(subtitleCode);
758 containerHeight += ' + subtitleLabel.height';
759 }
760 if (hasEmblem) {
761 containerCode.push(kEmblemIconCode.arg(extraRightAnchor).arg(titleColor));
762 }
763 if (hasAttributes) {
764 attributesCode = kAttributesRowCode.arg(attributesAnchors).arg(titleColor);
765 containerCode.push(attributesCode);
766 containerHeight += ' + attributesRow.height';
767 }
769 if (hasTitleContainer) {
770 // use container
771 titleSubtitleCode = kHeaderContainerCodeGenerator(titleContainerAnchors, containerHeight, containerCode);
772 } else {
773 // no container
774 titleSubtitleCode = titleCode;
775 if (hasSubtitle) {
776 titleSubtitleCode += subtitleCode;
777 }
778 if (hasAttributes) {
779 titleSubtitleCode += attributesCode;
780 }
781 }
782 }
784 if (hasHeaderRow) {
785 var rowCode = [mascotCode, titleSubtitleCode];
786 if (mascotShapeCode != '') {
787 rowCode.unshift(mascotShapeCode);
788 }
789 code += kHeaderRowCodeGenerator(isCardTool, headerVerticalAnchors + headerLeftAnchor, rowCode)
790 } else {
791 code += mascotShapeCode + mascotCode + titleSubtitleCode;
792 }
794 if (isAudio) {
795 var audioProgressBarLeftAnchor = 'audioButton.right';
796 var audioProgressBarBottomAnchor = 'audioButton.bottom';
797 var audioProgressBarTextColor = 'root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText';
799 code += kAudioProgressBarCode.arg(audioProgressBarBottomAnchor)
800 .arg(audioProgressBarLeftAnchor)
801 .arg(audioProgressBarTextColor);
803 var audioButtonAnchorsFill;
804 var audioButtonWidth;
805 var audioButtonHeight;
806 if (hasArt) {
807 audioButtonAnchorsFill = 'artShapeHolder';
808 audioButtonWidth = 'undefined';
809 audioButtonHeight = 'undefined';
810 } else {
811 audioButtonAnchorsFill = 'undefined';
812 audioButtonWidth = 'height';
813 audioButtonHeight = isCardTool ? 'headerHeight + 2 * units.gu(1)'
814 : 'root.fixedHeaderHeight + 2 * units.gu(1)';
815 }
816 code += kAudioButtonCode.arg(audioButtonAnchorsFill).arg(audioButtonWidth).arg(audioButtonHeight).arg(asynchronous);
817 }
819 if (hasSummary) {
820 var summaryTopAnchor;
821 if (isHorizontal && hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
822 else if (headerAsOverlay && hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
823 else if (hasHeaderRow) summaryTopAnchor = 'row.bottom';
824 else if (hasTitleContainer) summaryTopAnchor = 'headerTitleContainer.bottom';
825 else if (hasMascot) summaryTopAnchor = 'mascotImage.bottom';
826 else if (hasAttributes) summaryTopAnchor = 'attributesRow.bottom';
827 else if (hasSubtitle) summaryTopAnchor = 'subtitleLabel.bottom';
828 else if (hasTitle) summaryTopAnchor = 'titleLabel.bottom';
829 else if (hasArt) summaryTopAnchor = 'artShapeHolder.bottom';
830 else summaryTopAnchor = 'parent.top';
832 var summaryColor;
833 if (hasBackground) {
834 summaryColor = summaryColorWithBackground;
835 } else {
836 summaryColor = 'root.scopeStyle ? root.scopeStyle.foreground : theme.palette.normal.baseText';
837 }
839 var summaryTopMargin = (hasMascot || hasSubtitle || hasAttributes ? 'anchors.margins' : '0');
841 code += kSummaryLabelCode.arg(summaryTopAnchor).arg(summaryTopMargin).arg(summaryColor);
842 }
844 var touchdownAnchors;
845 if (hasBackground) {
846 touchdownAnchors = 'fill: backgroundLoader';
847 } else if (touchdownOnArtShape) {
848 touchdownAnchors = 'fill: artShapeHolder';
849 } else {
850 touchdownAnchors = 'fill: root'
851 }
852 code += kTouchdownCode.arg(touchdownAnchors);
854 var implicitHeight = 'implicitHeight: ';
855 if (hasSummary) {
856 implicitHeight += 'summary.y + summary.height + units.gu(1);\n';
857 } else if (isAudio) {
858 implicitHeight += 'audioButton.height;\n';
859 } else if (headerAsOverlay) {
860 implicitHeight += 'artShapeHolder.height;\n';
861 } else if (hasHeaderRow) {
862 implicitHeight += 'row.y + row.height + units.gu(1);\n';
863 } else if (hasMascot) {
864 implicitHeight += 'mascotImage.y + mascotImage.height;\n';
865 } else if (hasTitleContainer) {
866 implicitHeight += 'headerTitleContainer.y + headerTitleContainer.height + units.gu(1);\n';
867 } else if (hasAttributes) {
868 implicitHeight += 'attributesRow.y + attributesRow.height + units.gu(1);\n';
869 } else if (hasSubtitle) {
870 implicitHeight += 'subtitleLabel.y + subtitleLabel.height + units.gu(1);\n';
871 } else if (hasTitle) {
872 implicitHeight += 'titleLabel.y + titleLabel.height + units.gu(1);\n';
873 } else if (hasArt) {
874 implicitHeight += 'artShapeHolder.height;\n';
875 } else {
876 implicitHeight = '';
877 }
879 // Close the AbstractButton
880 code += implicitHeight + '}\n';
882 return code;
885function createCardComponent(parent, template, components, isCardTool, identifier) {
886 var imports = 'import QtQuick 2.4; \n\
887 import Ubuntu.Components 1.3; \n\
888 import Ubuntu.Settings.Components 0.1; \n\
889 import Dash 0.1;\n\
890 import Utils 0.1;\n';
891 var card = cardString(template, components, isCardTool);
892 var code = imports + 'Component {\n' + card + '}\n';
894 try {
895 return Qt.createQmlObject(code, parent, identifier);
896 } catch (e) {
897 console.error("ERROR: Invalid component created.");
898 console.error("Template:");
899 console.error(JSON.stringify(template));
900 console.error("Components:");
901 console.error(JSON.stringify(components));
902 console.error("Code:");
903 console.error(code);
904 throw e;
905 }