Guest User

Untitled

a guest
Oct 16th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. // define-attribute! :: String, Object, Element -> IO String
  2. function define_attribute(key, subject, source) {
  3. var value = source.getAttribute(key) || subject[key]
  4. return subject[key] = value }
  5.  
  6. function make_menu(subject, source) {
  7. return this._buildMenuObj(source, createId) }
  8.  
  9. function make_item(subject, source) {
  10. var item = make_item(item)
  11. define_attribute('label', subject, source)
  12. define_attribute('icon', subject, source)
  13. return item }
  14.  
  15. function build_menu(children) {
  16. var menuObj = { type: 'menu' }
  17. menuObj.items = children.map(to_menu_item.bind(this, menuObj))
  18. return menuObj }
  19.  
  20. function to_menu_item(subject, child) {
  21. return child.nodeName == 'MENU'? make_menu.call(this, subject, child)
  22. : child.nodeName == 'MENUITEM'? make_item.call(this, subject, child)
  23. : /* otherwise */ throw Error('Unknow type: ' + child.nodeName) }
  24.  
  25.  
  26. /* -- Original code --
  27. var menuObj = {
  28. type: 'menu',
  29. items: []
  30. };
  31. if (menu.getAttribute('label')) {
  32. menuObj.label = menu.getAttribute('label');
  33. }
  34.  
  35. for (var i = 0, child; child = menu.children[i++];) {
  36. if (child.nodeName === 'MENU') {
  37. menuObj.items.push(this._buildMenuObj(child, createId));
  38. break;
  39. }
  40. if (child.nodeName === 'MENUITEM') {
  41. var menuitem = {id: createId(child), type: 'menuitem'};
  42. if (child.getAttribute('label')) {
  43. menuitem.label = child.getAttribute('label');
  44. }
  45. if (child.getAttribute('icon')) {
  46. menuitem.icon = child.getAttribute('icon');
  47. }
  48. menuObj.items.push(menuitem);
  49. }
  50. }
  51.  
  52. return menuObj;
  53. */
Add Comment
Please, Sign In to add comment