WidgetBorder.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. SC.loadPackage({ 'WidgetBorder': {
  2. comment: 'I am the widget controlling border styles.',
  3. traits: ['SliderWidget'],
  4. sharedProperties: {
  5. widgetMenu: { initValue: '<div class="sg-editing-widget-container"><button id="sg-editing-widget-border" class="sg-editing-widget-button"></button></div>' }
  6. },
  7. methods: {
  8. init: {
  9. comment: 'I init the widget.',
  10. code: function(theSelection){
  11. this.delegate('Widget', 'init', theSelection);
  12. this.do('initSliderWidget', {
  13. theSelection: theSelection,
  14. startValue: theSelection.get('elements').length === 1
  15. ? (theSelection.get('elements')[0].get('borderWidth') / 24)
  16. : 0,
  17. setCallback: function(sliderVal){
  18. var borderVal = Math.round(sliderVal * 24),
  19. elements = theSelection.get('elements');
  20. for(var i = 0, l = elements.length; i < l; i++){
  21. if(borderVal !== 0 && !elements[i].get('borderColor')){
  22. elements[i].set({ borderColor: '#000000' });
  23. }
  24. elements[i].set({ borderWidth: borderVal });
  25. elements[i].set({
  26. width: elements[i].get('width'),
  27. height: elements[i].get('height')
  28. });
  29. }
  30. this.set({ aValueWasChoosen: true });
  31. }
  32. });
  33. }
  34. },
  35. createState: {
  36. comment: 'I create a reflection function to restore a state.',
  37. code: function(){
  38. return (function(elements){
  39. var savedBorders = []
  40. for(var i = 0, l = elements.length; i < l; i++){
  41. savedBorders.push({
  42. borderColor: elements[i].get('borderColor'),
  43. borderWidth: elements[i].get('borderWidth')
  44. })
  45. }
  46. return function(){
  47. for(var i = 0, l = elements.length; i < l; i++){
  48. elements[i].set({
  49. borderColor: savedBorders[i].borderColor,
  50. borderWidth: savedBorders[i].borderWidth
  51. });
  52. elements[i].set({
  53. width: elements[i].get('width'),
  54. height: elements[i].get('height')
  55. });
  56. }
  57. }
  58. }).call(this, this.get('selection').get('elements'));
  59. }
  60. }
  61. }
  62. }});