PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/contrib/civicrm/js/model/crm.designer.js

https://gitlab.com/virtualrealms/d7civicrm
JavaScript | 119 lines | 76 code | 5 blank | 38 comment | 10 complexity | 36e4d740d07c177281ac87734bc3a42c MD5 | raw file
  1. (function($, _) {
  2. if (!CRM.Designer) CRM.Designer = {};
  3. // TODO Optimize this class
  4. CRM.Designer.PaletteFieldModel = CRM.Backbone.Model.extend({
  5. defaults: {
  6. /**
  7. * @var {string} required; a form-specific binding to an entity instance (eg 'student', 'mother')
  8. */
  9. entityName: null,
  10. /**
  11. * @var {string}
  12. */
  13. fieldName: null
  14. },
  15. initialize: function() {
  16. },
  17. getFieldSchema: function() {
  18. return this.getRel('ufGroupModel').getFieldSchema(this.get('entityName'), this.get('fieldName'));
  19. },
  20. getLabel: function() {
  21. // Note: if fieldSchema were a bit tighter, then we need to get a label from PaletteFieldModel at all
  22. return this.getFieldSchema().title || this.get('fieldName');
  23. },
  24. getSectionName: function() {
  25. // Note: if fieldSchema were a bit tighter, then we need to get a section from PaletteFieldModel at all
  26. return this.getFieldSchema().section || 'default';
  27. },
  28. getSection: function() {
  29. return this.getRel('ufGroupModel').getModelClass(this.get('entityName')).prototype.sections[this.getSectionName()];
  30. },
  31. /**
  32. * Add a new UFField model to a UFFieldCollection (if doing so is legal).
  33. * If it fails, display an alert.
  34. *
  35. * @param {int} ufGroupId
  36. * @param {CRM.UF.UFFieldCollection} ufFieldCollection
  37. * @param {Object} addOptions
  38. * @return {CRM.UF.UFFieldModel} or null (if the field is not addable)
  39. */
  40. addToUFCollection: function(ufFieldCollection, addOptions) {
  41. var name, paletteFieldModel = this;
  42. var ufFieldModel = paletteFieldModel.createUFFieldModel(ufFieldCollection.getRel('ufGroupModel'));
  43. ufFieldModel.set('uf_group_id', ufFieldCollection.uf_group_id);
  44. if (!ufFieldCollection.isAddable(ufFieldModel)) {
  45. CRM.alert(
  46. ts('The field "%1" is already included.', {
  47. 1: paletteFieldModel.getLabel()
  48. }),
  49. ts('Duplicate'),
  50. 'alert'
  51. );
  52. return null;
  53. }
  54. ufFieldCollection.add(ufFieldModel, addOptions);
  55. // Load metadata and set defaults
  56. // TODO: currently only works for custom fields
  57. name = this.get('fieldName').split('_');
  58. if (name[0] === 'custom') {
  59. CRM.api('custom_field', 'getsingle', {id: name[1]}, {success: function(field) {
  60. ufFieldModel.set(_.pick(field, 'help_pre', 'help_post', 'is_required'));
  61. }});
  62. }
  63. return ufFieldModel;
  64. },
  65. createUFFieldModel: function(ufGroupModel) {
  66. var model = new CRM.UF.UFFieldModel({
  67. is_active: 1,
  68. label: this.getLabel(),
  69. entity_name: this.get('entityName'),
  70. field_type: this.getFieldSchema().civiFieldType,
  71. field_name: this.get('fieldName')
  72. });
  73. return model;
  74. }
  75. });
  76. /**
  77. *
  78. * options:
  79. * - ufGroupModel: UFGroupModel
  80. */
  81. CRM.Designer.PaletteFieldCollection = CRM.Backbone.Collection.extend({
  82. model: CRM.Designer.PaletteFieldModel,
  83. initialize: function(models, options) {
  84. this.initializeCopyToChildrenRelation('ufGroupModel', options.ufGroupModel, models);
  85. },
  86. /**
  87. * Look up a palette-field
  88. *
  89. * @param entityName
  90. * @param fieldName
  91. * @return {CRM.Designer.PaletteFieldModel}
  92. */
  93. getFieldByName: function(entityName, fieldName) {
  94. if (fieldName.indexOf('formatting') === 0) {
  95. fieldName = 'formatting';
  96. }
  97. return this.find(function(paletteFieldModel) {
  98. return ((!entityName || paletteFieldModel.get('entityName') == entityName) && paletteFieldModel.get('fieldName') == fieldName);
  99. });
  100. },
  101. /**
  102. * Get a list of all fields, grouped into sections by "entityName+sectionName".
  103. *
  104. * @return {Object} keys are sections ("entityName+sectionName"); values are CRM.Designer.PaletteFieldModel
  105. */
  106. getFieldsByEntitySection: function() {
  107. // TODO cache
  108. var fieldsByEntitySection = this.groupBy(function(paletteFieldModel) {
  109. return paletteFieldModel.get('entityName') + '-' + paletteFieldModel.getSectionName();
  110. });
  111. return fieldsByEntitySection;
  112. }
  113. });
  114. })(CRM.$, CRM._);