preprocessor.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. var zrUtil = require("zrender/lib/core/util");
  2. var DEFAULT_TOOLBOX_BTNS = ['rect', 'polygon', 'keep', 'clear'];
  3. function _default(option, isNew) {
  4. var brushComponents = option && option.brush;
  5. if (!zrUtil.isArray(brushComponents)) {
  6. brushComponents = brushComponents ? [brushComponents] : [];
  7. }
  8. if (!brushComponents.length) {
  9. return;
  10. }
  11. var brushComponentSpecifiedBtns = [];
  12. zrUtil.each(brushComponents, function (brushOpt) {
  13. var tbs = brushOpt.hasOwnProperty('toolbox') ? brushOpt.toolbox : [];
  14. if (tbs instanceof Array) {
  15. brushComponentSpecifiedBtns = brushComponentSpecifiedBtns.concat(tbs);
  16. }
  17. });
  18. var toolbox = option && option.toolbox;
  19. if (zrUtil.isArray(toolbox)) {
  20. toolbox = toolbox[0];
  21. }
  22. if (!toolbox) {
  23. toolbox = {
  24. feature: {}
  25. };
  26. option.toolbox = [toolbox];
  27. }
  28. var toolboxFeature = toolbox.feature || (toolbox.feature = {});
  29. var toolboxBrush = toolboxFeature.brush || (toolboxFeature.brush = {});
  30. var brushTypes = toolboxBrush.type || (toolboxBrush.type = []);
  31. brushTypes.push.apply(brushTypes, brushComponentSpecifiedBtns);
  32. removeDuplicate(brushTypes);
  33. if (isNew && !brushTypes.length) {
  34. brushTypes.push.apply(brushTypes, DEFAULT_TOOLBOX_BTNS);
  35. }
  36. }
  37. function removeDuplicate(arr) {
  38. var map = {};
  39. zrUtil.each(arr, function (val) {
  40. map[val] = 1;
  41. });
  42. arr.length = 0;
  43. zrUtil.each(map, function (flag, val) {
  44. arr.push(val);
  45. });
  46. }
  47. module.exports = _default;