You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

136 lines
3.4 KiB

  1. // opts = { 'title': 'My Sidebar',
  2. // 'sections': [
  3. // {'title': 'Actions',
  4. // 'items': [{type: , label:, onclick function / string to eval], ]
  5. // 'render': function (optional)
  6. // }
  7. //
  8. // item types = link, button, html, inpu
  9. //
  10. // Page Sidebar
  11. //
  12. wn.widgets.PageSidebar = function(parent, opts) {
  13. this.opts = opts
  14. this.sections = {}
  15. this.wrapper = $a(parent, 'div', 'psidebar-wrapper')
  16. // refresh sidebar - make head and sections
  17. this.refresh = function() {
  18. this.wrapper.innerHTML = ''
  19. if(this.opts.title)
  20. this.make_head();
  21. for(var i=0; i<this.opts.sections.length; i++) {
  22. var section = this.opts.sections[i];
  23. if((section.display && section.display()) || !section.display) {
  24. this.sections[section.title]
  25. = new wn.widgets.PageSidebarSection(this, section);
  26. }
  27. }
  28. if(this.opts.onrefresh) { this.opts.onrefresh(this) }
  29. }
  30. this.make_head = function() {
  31. this.head = $a(this.wrapper, 'div', 'psidebar-head', '', this.opts.title);
  32. }
  33. this.refresh();
  34. }
  35. //
  36. // Sidebar Section
  37. //
  38. wn.widgets.PageSidebarSection = function(sidebar, opts) {
  39. this.items = [];
  40. this.sidebar = sidebar;
  41. this.wrapper = $a(sidebar.wrapper, 'div', 'psidebar-section');
  42. this.head = $a(this.wrapper, 'div', 'psidebar-section-head', '', opts.title);
  43. this.body = $a(this.wrapper, 'div', 'psidebar-section-body');
  44. $br(this.wrapper, '3px');
  45. this.opts = opts;
  46. // make items
  47. this.make_items = function() {
  48. for(var i=0; i<this.opts.items.length; i++) {
  49. var item = this.opts.items[i];
  50. if((item.display && item.display()) || !item.display) {
  51. var div = $a(this.body, 'div', 'psidebar-section-item');
  52. this.make_one_item(item, div);
  53. }
  54. }
  55. }
  56. this.make_one_item = function(item, div) {
  57. if (item.type.toLowerCase()=='link')
  58. this.items[item.label] = new wn.widgets.PageSidebarLink(this, item, div);
  59. else if (item.type.toLowerCase()=='button')
  60. this.items[item.label] = new wn.widgets.PageSidebarButton(this, this.opts.items[i], div);
  61. //else if (item.type=='Input')
  62. // new wn.widgets.PageSidebarInput(this, this.opts.items[i], div);
  63. else if (item.type.toLowerCase()=='html')
  64. this.items[item.label] = new wn.widgets.PageSidebarHTML(this, this.opts.items[i], div);
  65. }
  66. // image
  67. this.add_icon = function(parent, icon) {
  68. if(icon.substr(0,3)=='ic-') {
  69. var img = $a(parent, 'div', 'wn-icon ' + icon,
  70. {cssFloat:'left', marginRight: '7px', marginBottom:'-3px'}
  71. );
  72. } else {
  73. var img = $a(parent, 'img', '', {marginRight: '7px', marginBottom:'-3px'});
  74. img.src = 'images/icons/' + icon;
  75. }
  76. }
  77. this.refresh = function() {
  78. this.body.innerHTML = '';
  79. if(this.opts.render) {
  80. this.opts.render(this.body); }
  81. else
  82. this.make_items();
  83. }
  84. this.refresh();
  85. }
  86. //
  87. // Elements
  88. //
  89. // link
  90. wn.widgets.PageSidebarLink = function(section, opts, wrapper) {
  91. this.wrapper = wrapper;
  92. this.section = section;
  93. this.opts = opts;
  94. var me = this;
  95. if(opts.icon) {
  96. section.add_icon(this.wrapper, opts.icon);
  97. }
  98. this.ln = $a(this.wrapper, 'span', 'link_type psidebar-section-link', opts.style, opts.label);
  99. this.ln.onclick = function() { me.opts.onclick(me) };
  100. }
  101. // button
  102. wn.widgets.PageSidebarButton = function(section, opts, wrapper) {
  103. this.wrapper = wrapper;
  104. this.section = section;
  105. this.opts = opts;
  106. var me = this;
  107. this.btn = $btn(this.wrapper, opts.label, opts.onclick, opts.style, opts.color);
  108. }
  109. // html
  110. wn.widgets.PageSidebarHTML = function(section, opts, wrapper) {
  111. wrapper.innerHTML = opts.content
  112. }