combox.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. initCombox: function (){
  18. var me = this;
  19. this.items = this.items || [];
  20. for (var i=0; i<this.items.length; i++) {
  21. var item = this.items[i];
  22. item.uiName = 'listitem';
  23. item.index = i;
  24. item.onclick = function (){
  25. me.selectByIndex(this.index);
  26. };
  27. }
  28. this.popup = new Menu({
  29. items: this.items,
  30. uiName: 'list',
  31. editor:this.editor
  32. });
  33. this.initSplitButton();
  34. },
  35. _SplitButton_postRender: SplitButton.prototype.postRender,
  36. postRender: function (){
  37. this._SplitButton_postRender();
  38. this.setLabel(this.label || '');
  39. this.setValue(this.initValue || '');
  40. },
  41. showPopup: function (){
  42. var rect = uiUtils.getClientRect(this.getDom());
  43. rect.top += 1;
  44. rect.bottom -= 1;
  45. rect.height -= 2;
  46. this.popup.showAnchorRect(rect);
  47. },
  48. getValue: function (){
  49. return this.value;
  50. },
  51. setValue: function (value){
  52. var index = this.indexByValue(value);
  53. if (index != -1) {
  54. this.selectedIndex = index;
  55. this.setLabel(this.items[index].label);
  56. this.value = this.items[index].value;
  57. } else {
  58. this.selectedIndex = -1;
  59. this.setLabel(this.getLabelForUnknowValue(value));
  60. this.value = value;
  61. }
  62. },
  63. setLabel: function (label){
  64. this.getDom('button_body').innerHTML = label;
  65. this.label = label;
  66. },
  67. getLabelForUnknowValue: function (value){
  68. return value;
  69. },
  70. indexByValue: function (value){
  71. for (var i=0; i<this.items.length; i++) {
  72. if (value == this.items[i].value) {
  73. return i;
  74. }
  75. }
  76. return -1;
  77. },
  78. getItem: function (index){
  79. return this.items[index];
  80. },
  81. selectByIndex: function (index){
  82. if (index < this.items.length && this.fireEvent('select', index) !== false) {
  83. this.selectedIndex = index;
  84. this.value = this.items[index].value;
  85. this.setLabel(this.items[index].label);
  86. }
  87. }
  88. };
  89. utils.inherits(Combox, SplitButton);
  90. })();