sidebarCustomFields.js 7.3 KB

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