cobalt.menu.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. (function($) {
  2. $(document).bind('cobalt-load', function(evt, cobalt) {
  3. var plugin = {
  4. 'version': 0,
  5. 'catalogs': {},
  6. 'handlers': []
  7. };
  8. plugin['catalogs']['menu'] = {
  9. 'update': function(last_update, callback) {
  10. $.getJSON(Drupal.settings.basePath + 'cobalt/data/menu_json', {}, function (data) {
  11. cobalt.emptyCatalog('menu');
  12. for (var id in data) {
  13. cobalt.addEntry({id:id, name:data[id][1], extra:data[id][0], information: data[id][0], catalog:'menu', classname:'url_data'});
  14. }
  15. callback(false);
  16. });
  17. },
  18. 'install': function() {
  19. },
  20. 'uninstall': function() {
  21. },
  22. 'item_formatter': function(item) {
  23. return item.name + ' <small>' + item.information + '</small>';
  24. },
  25. 'update_rate': 60000
  26. };
  27. var uri_from_item = function(item, omitDestination) {
  28. var path = item.information;
  29. var destination = Drupal.settings.cobalt.path;
  30. if (typeof(path) == 'object') {
  31. destination = path.destination;
  32. path = path.path;
  33. }
  34. if (path=='<front>') {
  35. path = '';
  36. }
  37. if (destination && !omitDestination) {
  38. path = path + '?destination=' + destination;
  39. }
  40. return Drupal.settings.basePath + path;
  41. };
  42. plugin['handlers'].push({
  43. 'id': 'menu_goto',
  44. 'name': Drupal.t('Go to and return'),
  45. 'data_class': 'url_data',
  46. 'handler': function(text, item) {
  47. window.location.href = uri_from_item(item);
  48. }
  49. });
  50. plugin['handlers'].push({
  51. 'id': 'menu_goto_stay',
  52. 'name': Drupal.t('Go to'),
  53. 'data_class': 'url_data',
  54. 'handler': function(text, item) {
  55. window.location.href = uri_from_item(item, true);
  56. }
  57. });
  58. plugin['handlers'].push({
  59. 'id': 'menu_open_in_new_window',
  60. 'name': Drupal.t('Open in new window'),
  61. 'data_class': 'url_data',
  62. 'handler': function(text, item) {
  63. var form = document.createElement("form");
  64. $(form).attr({
  65. 'method': 'GET',
  66. 'action': uri_from_item(item),
  67. 'target': '_blank'
  68. }).appendTo('body');
  69. try {
  70. form.submit();
  71. }
  72. catch(e) {
  73. var message = $('<div></div>');
  74. message.append('<h1>' + Drupal.t('Could not open window') + '</h1>');
  75. message.append('<p>' + Drupal.t('You might be using a popup blocker, which stopped Cobalt from opening a new window.') + '</p>');
  76. cobalt.showHtml(message);
  77. }
  78. $(form).remove();
  79. }
  80. });
  81. cobalt.registerPlugin('cobalt_menu', plugin);
  82. });
  83. })(jQuery);