Home Manual Reference Source Repository

es2015/LineTemplates/index.js

  1. import { TemplateAction } from '../LineActions';
  2. export var LineTemplateType;
  3. (function (LineTemplateType) {
  4. LineTemplateType.BUTTONS = 'buttons';
  5. LineTemplateType.CONFIRM = 'confirm';
  6. LineTemplateType.CAROUSEL = 'carousel';
  7. })(LineTemplateType || (LineTemplateType = {}));
  8. /* eslint-enable no-unused-vars, space-infix-ops */
  9. /**
  10. * @see https://devdocs.line.me/en/#template-messages
  11. */
  12. export class TemplateComponent {
  13. /* eslint-enable no-undef */
  14. /** @ignore */
  15. constructor({ type }) {
  16. /**
  17. * Identifier for the type of template.
  18. * @type {string}
  19. */
  20. this.type = type;
  21. }
  22. /** @ignore */
  23. static createFromObject(params) {
  24. switch (params.type) {
  25. case LineTemplateType.BUTTONS: {
  26. return new TemplateButtons(params);
  27. }
  28. case LineTemplateType.CONFIRM: {
  29. return new TemplateConfirm(params);
  30. }
  31. case LineTemplateType.CAROUSEL: {
  32. return new TemplateCarousel(params);
  33. }
  34. default: {
  35. return new TemplateComponent({ type: params.type });
  36. }
  37. }
  38. }
  39. }
  40. /**
  41. * @see https://devdocs.line.me/en/#buttons
  42. */
  43. export class TemplateButtons extends TemplateComponent {
  44. /* eslint-enable no-undef */
  45. /**
  46. * @param {Object} params
  47. * @param {string} [params.thumbnailImageUrl] Image URL (JPEG or PNG / HTTPS / 1:1.51)
  48. * @param {string} [params.title] Title (Max: 40 chars)
  49. * @param {string} params.text Message text (Max: 160 chars (no image, no title) / 60 chars)
  50. * @param {TemplateAction[] | any[]} params.actions Action when tapped (Max: 4)
  51. */
  52. constructor({ thumbnailImageUrl, title, text, actions }) {
  53. super({ type: LineTemplateType.BUTTONS });
  54. /**
  55. * Image URL (JPEG or PNG / HTTPS / 1:1.51)
  56. * @type {string}
  57. */
  58. this.thumbnailImageUrl = thumbnailImageUrl;
  59. /**
  60. * Title (Max: 40 chars)
  61. * @type {string}
  62. */
  63. this.title = title;
  64. /**
  65. * Message text (Max: 160 chars (no image, no title) / 60 chars)
  66. * @type {string}
  67. */
  68. this.text = text;
  69. /**
  70. * Action when tapped (Max: 4)
  71. * @type {TemplateAction[]}
  72. */
  73. this.actions = actions.map(action => TemplateAction.createFromObject(action));
  74. }
  75. }
  76. /**
  77. * @see https://devdocs.line.me/en/#confirm
  78. */
  79. export class TemplateConfirm extends TemplateComponent {
  80. /* eslint-enable no-undef */
  81. /**
  82. * @param {Object} params
  83. * @param {string} params.text Message text (Max: 240 chars)
  84. * @param {TemplateAction[] | any[]} params.actions Action when tapped (Max: 2)
  85. */
  86. constructor({ text, actions }) {
  87. super({ type: LineTemplateType.CONFIRM });
  88. /**
  89. * Message text (Max: 240 chars)
  90. * @type {string}
  91. */
  92. this.text = text;
  93. /**
  94. * Action when tapped (Max: 2)
  95. * @type {TemplateAction[]}
  96. */
  97. this.actions = actions.map(action => TemplateAction.createFromObject(action));
  98. }
  99. }
  100. /**
  101. * @see https://devdocs.line.me/en/#carousel
  102. */
  103. export class TemplateCarousel extends TemplateComponent {
  104. /* eslint-enable no-undef */
  105. /**
  106. * @param {Object} params
  107. * @param {TemplateColumn[] | any[]} params.columns Array of columns (Max: 5)
  108. */
  109. constructor({ columns }) {
  110. super({ type: LineTemplateType.CAROUSEL });
  111. /**
  112. * Array of columns (Max: 5)
  113. * @type {TemplateColumn[]}
  114. */
  115. this.columns = columns.map(column => TemplateColumn.createFromObject(column));
  116. }
  117. }
  118. /**
  119. * @see https://devdocs.line.me/en/#column-object
  120. */
  121. export class TemplateColumn {
  122. /* eslint-enable no-undef */
  123. /**
  124. * @param {Object} params
  125. * @param {string} [params.thumbnailImageUrl] Image URL (JPEG or PNG / HTTPS / 1:1.51)
  126. * @param {string} [params.title] Title (Max: 40 chars)
  127. * @param {string} params.text Message text (Max: 160 chars (no image, no title) / 60 chars)
  128. * @param {TemplateAction[] | any[]} params.actions Action when tapped (Max: 3)
  129. */
  130. constructor({ thumbnailImageUrl, title, text, actions }) {
  131. /**
  132. * Image URL (JPEG or PNG / HTTPS / 1:1.51)
  133. * @type {string}
  134. */
  135. this.thumbnailImageUrl = thumbnailImageUrl;
  136. /**
  137. * Title (Max: 40 chars)
  138. * @type {string}
  139. */
  140. this.title = title;
  141. /**
  142. * Message text (Max: 160 chars (no image, no title) / 60 chars)
  143. * @type {string}
  144. */
  145. this.text = text;
  146. /**
  147. * Action when tapped (Max: 3)
  148. * @type {TemplateAction[]}
  149. */
  150. this.actions = actions.map(action => TemplateAction.createFromObject(action));
  151. }
  152. /** @ignore */
  153. static createFromObject(params) {
  154. return new TemplateColumn(params);
  155. }
  156. }