combox.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. ///import core
  2. ///import uicore
  3. ///import ui/menu.js
  4. ///import ui/splitbutton.js
  5. (function (){
  6. // todo: menu和item提成通用list
  7. var utils = baidu.editor.utils,
  8. uiUtils = baidu.editor.ui.uiUtils,
  9. Menu = baidu.editor.ui.Menu,
  10. SplitButton = baidu.editor.ui.SplitButton,
  11. Combox = baidu.editor.ui.Combox = function (options){
  12. this.initOptions(options);
  13. this.initCombox();
  14. };
  15. Combox.prototype = {
  16. uiName: 'combox',
  17. onbuttonclick:function () {
  18. this.showPopup();
  19. },
  20. initCombox: function (){
  21. var me = this;
  22. this.items = this.items || [];
  23. for (var i=0; i<this.items.length; i++) {
  24. var item = this.items[i];
  25. item.uiName = 'listitem';
  26. item.index = i;
  27. item.onclick = function (){
  28. me.selectByIndex(this.index);
  29. };
  30. }
  31. this.popup = new Menu({
  32. items: this.items,
  33. uiName: 'list',
  34. editor:this.editor,
  35. captureWheel: true,
  36. combox: this
  37. });
  38. this.initSplitButton();
  39. },
  40. _SplitButton_postRender: SplitButton.prototype.postRender,
  41. postRender: function (){
  42. this._SplitButton_postRender();
  43. this.setLabel(this.label || '');
  44. this.setValue(this.initValue || '');
  45. },
  46. showPopup: function (){
  47. var rect = uiUtils.getClientRect(this.getDom());
  48. rect.top += 1;
  49. rect.bottom -= 1;
  50. rect.height -= 2;
  51. this.popup.showAnchorRect(rect);
  52. },
  53. getValue: function (){
  54. return this.value;
  55. },
  56. setValue: function (value){
  57. var index = this.indexByValue(value);
  58. if (index != -1) {
  59. this.selectedIndex = index;
  60. this.setLabel(this.items[index].label);
  61. this.value = this.items[index].value;
  62. } else {
  63. this.selectedIndex = -1;
  64. this.setLabel(this.getLabelForUnknowValue(value));
  65. this.value = value;
  66. }
  67. },
  68. setLabel: function (label){
  69. this.getDom('button_body').innerHTML = label;
  70. this.label = label;
  71. },
  72. getLabelForUnknowValue: function (value){
  73. return value;
  74. },
  75. indexByValue: function (value){
  76. for (var i=0; i<this.items.length; i++) {
  77. if (value == this.items[i].value) {
  78. return i;
  79. }
  80. }
  81. return -1;
  82. },
  83. getItem: function (index){
  84. return this.items[index];
  85. },
  86. selectByIndex: function (index){
  87. if (index < this.items.length && this.fireEvent('select', index) !== false) {
  88. this.selectedIndex = index;
  89. this.value = this.items[index].value;
  90. this.setLabel(this.items[index].label);
  91. }
  92. }
  93. };
  94. utils.inherits(Combox, SplitButton);
  95. })();