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