assets.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // 处理属性是否显示只读样式的判断
  2. exports.updateElementReadonly = function(element, readonly) {
  3. if (readonly === undefined) {
  4. readonly = this.asset.readonly;
  5. }
  6. if (readonly) {
  7. element.setAttribute('readonly', true);
  8. } else {
  9. element.removeAttribute('readonly');
  10. }
  11. const parentElement = element.parentElement;
  12. if (parentElement && parentElement.tagName === 'UI-PROP') {
  13. if (readonly) {
  14. parentElement.setAttribute('disabled', true);
  15. parentElement.setAttribute('readonly', true);
  16. } else {
  17. parentElement.removeAttribute('disabled');
  18. parentElement.removeAttribute('readonly');
  19. }
  20. } else {
  21. if (readonly) {
  22. element.setAttribute('disabled', true);
  23. } else {
  24. element.removeAttribute('disabled');
  25. }
  26. }
  27. };
  28. // 处理多选资源后同一属性是否可编辑的逻辑判断
  29. exports.updateElementInvalid = function(element, prop) {
  30. const propNames = prop.split('.');
  31. let thisPropValue = this.meta.userData;
  32. const invalid = this.metaList.some((meta) => {
  33. let target = meta.userData;
  34. const lastIndex = propNames.length - 1;
  35. const lastPropName = propNames[lastIndex];
  36. if (propNames.length > 1) {
  37. for (let i = 0; i < lastIndex; i++) {
  38. const propName = propNames[i];
  39. if (target[propName] !== undefined) {
  40. target = target[propName];
  41. }
  42. if (thisPropValue[propName] !== undefined) {
  43. thisPropValue = thisPropValue[propName];
  44. }
  45. }
  46. }
  47. return target[lastPropName] !== thisPropValue[lastPropName];
  48. });
  49. element.invalid = invalid;
  50. };
  51. // 从 value 对象里取 prop 的值,没有的话,defaultValue 为保底值
  52. exports.getPropValue = function(value, defaultValue, prop) {
  53. let target = value;
  54. if (prop) {
  55. const propNames = prop.split('.');
  56. for (let i = 0; i < propNames.length; i++) {
  57. const propName = propNames[i];
  58. if (target === undefined || typeof target !== 'object') {
  59. return defaultValue;
  60. }
  61. target = target[propName];
  62. }
  63. }
  64. if (target === undefined) {
  65. return defaultValue;
  66. }
  67. return target;
  68. };
  69. // 给属性 prop 赋值,支持 prop 为 a.b.c 格式,给属性 c 赋值,event 是 ui 组件交互的事件
  70. exports.setPropValue = function(prop, type, event) {
  71. const propNames = prop.split('.');
  72. this.metaList.forEach((meta) => {
  73. let target = meta.userData;
  74. const lastIndex = propNames.length - 1;
  75. const lastPropName = propNames[lastIndex];
  76. if (propNames.length > 1) {
  77. for (let i = 0; i < lastIndex; i++) {
  78. const propName = propNames[i];
  79. if (!target[propName] || typeof target[propName] !== 'object') {
  80. target[propName] = {};
  81. }
  82. target = target[propName];
  83. }
  84. }
  85. let value = event.target.value;
  86. if (type === 'number') {
  87. value = Number(value);
  88. } else if (type === 'boolean') {
  89. value = Boolean(value);
  90. }
  91. target[lastPropName] = value;
  92. });
  93. };