AppBar.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. define( ["text!./AppBar.css"], function( sAppBarCss ) {
  2. "use strict";
  3. function AppBar()
  4. {
  5. };
  6. AppBar.prototype.draw = function( oControlHost )
  7. {
  8. var el = oControlHost.container;
  9. var o = oControlHost.configuration;
  10. if ( !o )
  11. {
  12. throw new scriptableReportError( "AppBar", "", "Expected configuration." );
  13. }
  14. this.m_bHorizontal = !!o && ( o.Orientation == "horizontal" );
  15. var sOrientation = this.m_bHorizontal ? "horizontal" : "vertical"
  16. var sAlign = ( this.m_bHorizontal && o ) ? ( o.Align || "" ) : "";
  17. var sSelector = "CCAppBar";
  18. var sUniqueSelector = sSelector + "_" + el.id;
  19. var sUniqueButtonSelector = sUniqueSelector + "Button";
  20. this.m_sCardNames = [];
  21. var aButtons = o.Buttons;
  22. if ( !aButtons )
  23. {
  24. aButtons = this.generateButtonFromDataStore( oControlHost );
  25. }
  26. var aHtml = [];
  27. aHtml.push( '<style>' );
  28. aHtml.push( sAppBarCss.replace( new RegExp( sSelector, "g" ), sUniqueSelector ) );
  29. if ( o["Background color"] )
  30. {
  31. aHtml.push( '\r\n.' + sUniqueSelector + ' { background-color:' + o["Background color"] + ';}' );
  32. }
  33. if ( o.Fill )
  34. {
  35. aHtml.push( '\r\n.' + sUniqueSelector + ' { fill:' + o.Fill + '; color:' + o.Fill + ';}' );
  36. }
  37. if ( o["Selection fill"] )
  38. {
  39. aHtml.push( '\r\n.' + sUniqueSelector + '.' + sOrientation + ' .' + sUniqueButtonSelector + ':hover, .' + sUniqueSelector + '.' + sOrientation + ' .' + sUniqueButtonSelector + '.selected { fill:' + o["Selection fill"] + '; color:' + o["Selection fill"] + ';}' );
  40. }
  41. if ( o["Selection border color"] )
  42. {
  43. aHtml.push( '\r\n.' + sUniqueSelector + '.' + sOrientation + ' .' + sUniqueButtonSelector + '.selected { border-' + ( this.m_bHorizontal ? 'bottom' : 'right' ) + '-color:' + o["Selection border color"] + ';}' );
  44. }
  45. if ( o["Selection background color"] )
  46. {
  47. aHtml.push( '\r\n.' + sUniqueSelector + '.' + sOrientation + ' .' + sUniqueButtonSelector + '.selected { background-color:' + o["Selection background color"] + ';}' );
  48. }
  49. aHtml.push( '</style>' );
  50. for ( var i = 0; i < aButtons.length; i++ )
  51. {
  52. var oButton = aButtons[i];
  53. this.m_sCardNames[i] = oButton.card;
  54. aHtml.push( '<div cardIndex="' + i + '" class="' + sUniqueButtonSelector + '">' );
  55. if ( oButton.label )
  56. {
  57. aHtml.push( oButton.label );
  58. }
  59. else if ( oButton.icon )
  60. {
  61. aHtml.push( oButton.icon.join( "" ) );
  62. }
  63. aHtml.push( '</div>' );
  64. if ( oButton.selected || ( ( i == 0 ) && o["Select first card"] ) )
  65. {
  66. this.m_iSelectedCard = i;
  67. }
  68. }
  69. el.innerHTML = aHtml.join( "" );
  70. el.classList.add( sUniqueSelector );
  71. el.classList.add( sOrientation );
  72. if ( sAlign )
  73. {
  74. el.style.textAlign = sAlign;
  75. }
  76. this.m_nlButtonDivs = el.querySelectorAll( "." + sUniqueButtonSelector );
  77. this.f_displayCard( this.m_iSelectedCard, oControlHost );
  78. };
  79. AppBar.prototype.generateButtonFromDataStore = function( oControlHost )
  80. {
  81. var aDataStores = oControlHost.control.dataStores;
  82. var oDataStore = ( aDataStores.length > 0 ) ? aDataStores[0] : null;
  83. if ( !oDataStore )
  84. {
  85. return null;
  86. }
  87. this.m_sCards = oControlHost.configuration.Cards;
  88. var aButtons = [];
  89. var iRowCount = oDataStore.rowCount;
  90. for ( var iRow = 0; iRow < iRowCount; iRow++ )
  91. {
  92. var oButton = {};
  93. oButton.label = oDataStore.getFormattedCellValue( iRow, 0 );
  94. oButton.card = this.m_sCards;
  95. aButtons.push( oButton );
  96. }
  97. return aButtons;
  98. };
  99. AppBar.prototype.f_displayCard = function( iCardIndex, oControlHost )
  100. {
  101. for ( var i = 0; i < this.m_nlButtonDivs.length; i++ )
  102. {
  103. var div = this.m_nlButtonDivs.item( i );
  104. div.classList[( i == iCardIndex ) ? "add" : "remove"]( "selected" );
  105. if ( !this.m_bSetupEventHandlers )
  106. {
  107. div.onmousedown = this.f_displayCard.bind( this, i, oControlHost );
  108. }
  109. }
  110. this.m_bSetupEventHandlers = true;
  111. var oPage = oControlHost.page;
  112. if ( this.m_sCards )
  113. {
  114. var a = oPage.getControlsByName( this.m_sCards );
  115. for ( var i = 0; i < a.length; i++ )
  116. {
  117. a[i].setDisplay( i == iCardIndex );
  118. }
  119. return;
  120. }
  121. for ( var i = 0; i < this.m_sCardNames.length; i++ )
  122. {
  123. oPage.getControlByName( this.m_sCardNames[i] ).setDisplay( i == iCardIndex );
  124. }
  125. };
  126. return AppBar;
  127. });