sidebarCustomFields.js 8.5 KB

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