sidebarCustomFields.js 8.4 KB

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