cardCustomFields.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. Template.cardCustomFieldsPopup.helpers({
  2. hasCustomField() {
  3. const card = Cards.findOne(Session.get('currentCard'));
  4. const customFieldId = this._id;
  5. return card.customFieldIndex(customFieldId) > -1;
  6. },
  7. });
  8. Template.cardCustomFieldsPopup.events({
  9. 'click .js-select-field'(event) {
  10. const card = Cards.findOne(Session.get('currentCard'));
  11. const customFieldId = this._id;
  12. card.toggleCustomField(customFieldId);
  13. event.preventDefault();
  14. },
  15. 'click .js-settings'(event) {
  16. EscapeActions.executeUpTo('detailsPane');
  17. Sidebar.setView('customFields');
  18. event.preventDefault();
  19. },
  20. });
  21. // cardCustomField
  22. const CardCustomField = BlazeComponent.extendComponent({
  23. getTemplate() {
  24. return `cardCustomField-${this.data().definition.type}`;
  25. },
  26. onCreated() {
  27. const self = this;
  28. self.card = Cards.findOne(Session.get('currentCard'));
  29. self.customFieldId = this.data()._id;
  30. },
  31. canModifyCard() {
  32. return (
  33. Meteor.user() &&
  34. Meteor.user().isBoardMember() &&
  35. !Meteor.user().isCommentOnly()
  36. );
  37. },
  38. });
  39. CardCustomField.register('cardCustomField');
  40. // cardCustomField-text
  41. (class extends CardCustomField {
  42. onCreated() {
  43. super.onCreated();
  44. }
  45. events() {
  46. return [
  47. {
  48. 'submit .js-card-customfield-text'(event) {
  49. event.preventDefault();
  50. const value = this.currentComponent().getValue();
  51. this.card.setCustomField(this.customFieldId, value);
  52. },
  53. },
  54. ];
  55. }
  56. }.register('cardCustomField-text'));
  57. // cardCustomField-number
  58. (class extends CardCustomField {
  59. onCreated() {
  60. super.onCreated();
  61. }
  62. events() {
  63. return [
  64. {
  65. 'submit .js-card-customfield-number'(event) {
  66. event.preventDefault();
  67. const value = parseInt(this.find('input').value, 10);
  68. this.card.setCustomField(this.customFieldId, value);
  69. },
  70. },
  71. ];
  72. }
  73. }.register('cardCustomField-number'));
  74. // cardCustomField-checkbox
  75. (class extends CardCustomField {
  76. onCreated() {
  77. super.onCreated();
  78. }
  79. toggleItem() {
  80. this.card.setCustomField(this.customFieldId, !this.data().value);
  81. }
  82. events() {
  83. return [
  84. {
  85. 'click .js-checklist-item .check-box-container': this.toggleItem,
  86. },
  87. ];
  88. }
  89. }.register('cardCustomField-checkbox'));
  90. // cardCustomField-currency
  91. (class extends CardCustomField {
  92. onCreated() {
  93. super.onCreated();
  94. this.currencyCode = this.data().definition.settings.currencyCode;
  95. }
  96. formattedValue() {
  97. const locale = TAPi18n.getLanguage();
  98. return new Intl.NumberFormat(locale, {
  99. style: 'currency',
  100. currency: this.currencyCode,
  101. }).format(this.data().value);
  102. }
  103. events() {
  104. return [
  105. {
  106. 'submit .js-card-customfield-currency'(event) {
  107. event.preventDefault();
  108. // To allow input separated by comma, the comma is replaced by a period.
  109. const value = Number(this.find('input').value.replace(/,/i, '.'), 10);
  110. this.card.setCustomField(this.customFieldId, value);
  111. },
  112. },
  113. ];
  114. }
  115. }.register('cardCustomField-currency'));
  116. // cardCustomField-date
  117. (class extends CardCustomField {
  118. onCreated() {
  119. super.onCreated();
  120. const self = this;
  121. self.date = ReactiveVar();
  122. self.now = ReactiveVar(moment());
  123. window.setInterval(() => {
  124. self.now.set(moment());
  125. }, 60000);
  126. self.autorun(() => {
  127. self.date.set(moment(self.data().value));
  128. });
  129. }
  130. showDate() {
  131. // this will start working once mquandalle:moment
  132. // is updated to at least moment.js 2.10.5
  133. // until then, the date is displayed in the "L" format
  134. return this.date.get().calendar(null, {
  135. sameElse: 'llll',
  136. });
  137. }
  138. showISODate() {
  139. return this.date.get().toISOString();
  140. }
  141. classes() {
  142. if (
  143. this.date.get().isBefore(this.now.get(), 'minute') &&
  144. this.now.get().isBefore(this.data().value)
  145. ) {
  146. return 'current';
  147. }
  148. return '';
  149. }
  150. showTitle() {
  151. return `${TAPi18n.__('card-start-on')} ${this.date.get().format('LLLL')}`;
  152. }
  153. events() {
  154. return [
  155. {
  156. 'click .js-edit-date': Popup.open('cardCustomField-date'),
  157. },
  158. ];
  159. }
  160. }.register('cardCustomField-date'));
  161. // cardCustomField-datePopup
  162. (class extends DatePicker {
  163. onCreated() {
  164. super.onCreated();
  165. const self = this;
  166. self.card = Cards.findOne(Session.get('currentCard'));
  167. self.customFieldId = this.data()._id;
  168. this.data().value && this.date.set(moment(this.data().value));
  169. }
  170. _storeDate(date) {
  171. this.card.setCustomField(this.customFieldId, date);
  172. }
  173. _deleteDate() {
  174. this.card.setCustomField(this.customFieldId, '');
  175. }
  176. }.register('cardCustomField-datePopup'));
  177. // cardCustomField-dropdown
  178. (class extends CardCustomField {
  179. onCreated() {
  180. super.onCreated();
  181. this._items = this.data().definition.settings.dropdownItems;
  182. this.items = this._items.slice(0);
  183. this.items.unshift({
  184. _id: '',
  185. name: TAPi18n.__('custom-field-dropdown-none'),
  186. });
  187. }
  188. selectedItem() {
  189. const selected = this._items.find(item => {
  190. return item._id === this.data().value;
  191. });
  192. return selected
  193. ? selected.name
  194. : TAPi18n.__('custom-field-dropdown-unknown');
  195. }
  196. events() {
  197. return [
  198. {
  199. 'submit .js-card-customfield-dropdown'(event) {
  200. event.preventDefault();
  201. const value = this.find('select').value;
  202. this.card.setCustomField(this.customFieldId, value);
  203. },
  204. },
  205. ];
  206. }
  207. }.register('cardCustomField-dropdown'));
  208. // cardCustomField-stringtemplate
  209. (class extends CardCustomField {
  210. onCreated() {
  211. super.onCreated();
  212. this.stringtemplateFormat = this.data().definition.settings.stringtemplateFormat;
  213. this.stringtemplateSeparator = this.data().definition.settings.stringtemplateSeparator;
  214. this.stringtemplateItems = new ReactiveVar(this.data().value ?? []);
  215. }
  216. formattedValue() {
  217. return (this.data().value ?? [])
  218. .filter(value => !!value.trim())
  219. .map(value => this.stringtemplateFormat.replace(/%\{value\}/gi, value))
  220. .join(this.stringtemplateSeparator ?? '');
  221. }
  222. getItems() {
  223. return Array.from(this.findAll('input'))
  224. .map(input => input.value)
  225. .filter(value => !!value.trim());
  226. }
  227. events() {
  228. return [
  229. {
  230. 'submit .js-card-customfield-stringtemplate'(event) {
  231. event.preventDefault();
  232. const items = this.getItems();
  233. this.card.setCustomField(this.customFieldId, items);
  234. },
  235. 'keydown .js-card-customfield-stringtemplate-item'(event) {
  236. if (event.keyCode === 13) {
  237. event.preventDefault();
  238. if (event.metaKey || event.ctrlKey) {
  239. this.find('button[type=submit]').click();
  240. } else if (event.target.value.trim()) {
  241. const inputLast = this.find('input.last');
  242. let items = this.getItems();
  243. if (event.target === inputLast) {
  244. inputLast.value = '';
  245. } else if (event.target.nextSibling === inputLast) {
  246. inputLast.focus();
  247. } else {
  248. event.target.blur();
  249. const idx = Array.from(this.findAll('input'))
  250. .indexOf(event.target);
  251. items.splice(idx + 1, 0, '');
  252. Tracker.afterFlush(() => {
  253. const element = this.findAll('input')[idx + 1];
  254. element.focus();
  255. element.value = '';
  256. });
  257. }
  258. this.stringtemplateItems.set(items);
  259. }
  260. }
  261. },
  262. 'blur .js-card-customfield-stringtemplate-item'(event) {
  263. if (!event.target.value.trim() || event.target === this.find('input.last')) {
  264. const items = this.getItems();
  265. this.stringtemplateItems.set(items);
  266. this.find('input.last').value = '';
  267. }
  268. },
  269. 'click .js-close-inlined-form'(event) {
  270. this.stringtemplateItems.set(this.data().value ?? []);
  271. },
  272. },
  273. ];
  274. }
  275. }.register('cardCustomField-stringtemplate'));