sidebarCustomFields.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. BlazeComponent.extendComponent({
  2. customFields() {
  3. return CustomFields.find({
  4. boardIds: { $in: [Session.get('currentBoard')] },
  5. });
  6. },
  7. events() {
  8. return [
  9. {
  10. 'click .js-open-create-custom-field': Popup.open('createCustomField'),
  11. 'click .js-edit-custom-field': Popup.open('editCustomField'),
  12. },
  13. ];
  14. },
  15. }).register('customFieldsSidebar');
  16. const CreateCustomFieldPopup = BlazeComponent.extendComponent({
  17. _types: ['text', 'number', 'date', 'dropdown', 'currency', 'checkbox', 'stringtemplate'],
  18. _currencyList: [
  19. {
  20. name: 'US Dollar',
  21. code: 'USD',
  22. },
  23. {
  24. name: 'Euro',
  25. code: 'EUR',
  26. },
  27. {
  28. name: 'Yen',
  29. code: 'JPY',
  30. },
  31. {
  32. name: 'Pound Sterling',
  33. code: 'GBP',
  34. },
  35. {
  36. name: 'Australian Dollar',
  37. code: 'AUD',
  38. },
  39. {
  40. name: 'Canadian Dollar',
  41. code: 'CAD',
  42. },
  43. {
  44. name: 'Swiss Franc',
  45. code: 'CHF',
  46. },
  47. {
  48. name: 'Yuan Renminbi',
  49. code: 'CNY',
  50. },
  51. {
  52. name: 'Hong Kong Dollar',
  53. code: 'HKD',
  54. },
  55. {
  56. name: 'New Zealand Dollar',
  57. code: 'NZD',
  58. },
  59. ],
  60. onCreated() {
  61. this.type = new ReactiveVar(
  62. this.data().type ? this.data().type : this._types[0],
  63. );
  64. this.currencyCode = new ReactiveVar(
  65. this.data().settings && this.data().settings.currencyCode
  66. ? this.data().settings.currencyCode
  67. : this._currencyList[0].code,
  68. );
  69. this.dropdownItems = new ReactiveVar(
  70. this.data().settings && this.data().settings.dropdownItems
  71. ? this.data().settings.dropdownItems
  72. : [],
  73. );
  74. this.stringtemplateFormat = new ReactiveVar(
  75. this.data().settings && this.data().settings.stringtemplateFormat
  76. ? this.data().settings.stringtemplateFormat
  77. : "",
  78. );
  79. },
  80. types() {
  81. const currentType = this.data().type;
  82. return this._types.map(type => {
  83. return {
  84. value: type,
  85. name: TAPi18n.__(`custom-field-${type}`),
  86. selected: type === currentType,
  87. };
  88. });
  89. },
  90. isTypeNotSelected(type) {
  91. return this.type.get() !== type;
  92. },
  93. getCurrencyCodes() {
  94. const currentCode = this.currencyCode.get();
  95. return this._currencyList.map(({ name, code }) => {
  96. return {
  97. name: `${code} - ${name}`,
  98. value: code,
  99. selected: code === currentCode,
  100. };
  101. });
  102. },
  103. getDropdownItems() {
  104. const items = this.dropdownItems.get();
  105. Array.from(this.findAll('.js-field-settings-dropdown input')).forEach(
  106. (el, index) => {
  107. //console.log('each item!', index, el.value);
  108. if (!items[index])
  109. items[index] = {
  110. _id: Random.id(6),
  111. };
  112. items[index].name = el.value.trim();
  113. },
  114. );
  115. return items;
  116. },
  117. getStringtemplateFormat() {
  118. return this.stringtemplateFormat.get();
  119. },
  120. getSettings() {
  121. const settings = {};
  122. switch (this.type.get()) {
  123. case 'currency': {
  124. const currencyCode = this.currencyCode.get();
  125. settings.currencyCode = currencyCode;
  126. break;
  127. }
  128. case 'dropdown': {
  129. const dropdownItems = this.getDropdownItems().filter(
  130. item => !!item.name.trim(),
  131. );
  132. settings.dropdownItems = dropdownItems;
  133. break;
  134. }
  135. case 'stringtemplate': {
  136. const stringtemplateFormat = this.stringtemplateFormat.get();
  137. settings.stringtemplateFormat = stringtemplateFormat;
  138. break;
  139. }
  140. }
  141. return settings;
  142. },
  143. events() {
  144. return [
  145. {
  146. 'change .js-field-type'(evt) {
  147. const value = evt.target.value;
  148. this.type.set(value);
  149. },
  150. 'change .js-field-currency'(evt) {
  151. const value = evt.target.value;
  152. this.currencyCode.set(value);
  153. },
  154. 'keydown .js-dropdown-item.last'(evt) {
  155. if (evt.target.value.trim() && evt.keyCode === 13) {
  156. const items = this.getDropdownItems();
  157. this.dropdownItems.set(items);
  158. evt.target.value = '';
  159. }
  160. },
  161. 'input .js-field-stringtemplate'(evt) {
  162. const value = evt.target.value;
  163. this.stringtemplateFormat.set(value);
  164. },
  165. 'click .js-field-show-on-card'(evt) {
  166. let $target = $(evt.target);
  167. if (!$target.hasClass('js-field-show-on-card')) {
  168. $target = $target.parent();
  169. }
  170. $target.find('.materialCheckBox').toggleClass('is-checked');
  171. $target.toggleClass('is-checked');
  172. },
  173. 'click .js-field-automatically-on-card'(evt) {
  174. let $target = $(evt.target);
  175. if (!$target.hasClass('js-field-automatically-on-card')) {
  176. $target = $target.parent();
  177. }
  178. $target.find('.materialCheckBox').toggleClass('is-checked');
  179. $target.toggleClass('is-checked');
  180. },
  181. 'click .js-field-always-on-card'(evt) {
  182. let $target = $(evt.target);
  183. if (!$target.hasClass('js-field-always-on-card')) {
  184. $target = $target.parent();
  185. }
  186. $target.find('.materialCheckBox').toggleClass('is-checked');
  187. $target.toggleClass('is-checked');
  188. },
  189. 'click .js-field-showLabel-on-card'(evt) {
  190. let $target = $(evt.target);
  191. if (!$target.hasClass('js-field-showLabel-on-card')) {
  192. $target = $target.parent();
  193. }
  194. $target.find('.materialCheckBox').toggleClass('is-checked');
  195. $target.toggleClass('is-checked');
  196. },
  197. 'click .primary'(evt) {
  198. evt.preventDefault();
  199. const data = {
  200. name: this.find('.js-field-name').value.trim(),
  201. type: this.type.get(),
  202. settings: this.getSettings(),
  203. showOnCard: this.find('.js-field-show-on-card.is-checked') !== null,
  204. showLabelOnMiniCard:
  205. this.find('.js-field-showLabel-on-card.is-checked') !== null,
  206. automaticallyOnCard:
  207. this.find('.js-field-automatically-on-card.is-checked') !== null,
  208. alwaysOnCard:
  209. this.find('.js-field-always-on-card.is-checked') !== null,
  210. };
  211. // insert or update
  212. if (!this.data()._id) {
  213. data.boardIds = [Session.get('currentBoard')];
  214. CustomFields.insert(data);
  215. } else {
  216. CustomFields.update(this.data()._id, { $set: data });
  217. }
  218. Popup.back();
  219. },
  220. 'click .js-delete-custom-field': Popup.afterConfirm(
  221. 'deleteCustomField',
  222. function() {
  223. const customField = CustomFields.findOne(this._id);
  224. if (customField.boardIds.length > 1) {
  225. CustomFields.update(customField._id, {
  226. $pull: {
  227. boardIds: Session.get('currentBoard'),
  228. },
  229. });
  230. } else {
  231. CustomFields.remove(customField._id);
  232. }
  233. Popup.close();
  234. },
  235. ),
  236. },
  237. ];
  238. },
  239. });
  240. CreateCustomFieldPopup.register('createCustomFieldPopup');
  241. (class extends CreateCustomFieldPopup {
  242. template() {
  243. return 'createCustomFieldPopup';
  244. }
  245. }.register('editCustomFieldPopup'));
  246. /*Template.deleteCustomFieldPopup.events({
  247. 'submit'(evt) {
  248. const customFieldId = this._id;
  249. CustomFields.remove(customFieldId);
  250. Popup.close();
  251. }
  252. });*/