cobalt.nodes.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. (function($) {
  2. $(document).bind('cobalt-load', function(evt, cobalt) {
  3. var plugin = {
  4. 'version': 0,
  5. 'catalogs': {},
  6. 'handlers': []
  7. };
  8. // Utility function for fetching data from the server
  9. // Used by the updater and the current node check
  10. var get_node_data = function (op, value, callback) {
  11. $.getJSON(Drupal.settings.basePath + 'cobalt/data/nodes_' + op + '/' + value, {}, function (data) {
  12. if (typeof(data.nodes)!='undefined') {
  13. var num_nodes = data.nodes.length;
  14. for (var i=0; i<num_nodes; i++) {
  15. cobalt.addEntry({id:data.nodes[i][0], name:data.nodes[i][1], information:{'perm': data.nodes[i][2]}, catalog:'nodes', classname:'node'});
  16. }
  17. }
  18. if (typeof(data.deleted)!='undefined') {
  19. var num_deletes = data.deleted.length;
  20. for (var i=0; i<num_deletes; i++) {
  21. cobalt.deleteEntry('nodes', data.deleted[i]);
  22. }
  23. }
  24. if (typeof(callback)=='function') {
  25. callback(data);
  26. }
  27. });
  28. };
  29. plugin['catalogs']['nodes'] = {
  30. 'update': function(last_update, callback) {
  31. get_node_data('update', Math.round((last_update/1000)), function(){ callback(true); });
  32. },
  33. 'install': function() {
  34. },
  35. 'uninstall': function() {
  36. },
  37. 'item_formatter': function(item) {
  38. return item.name + ' <small>node/' + item.id + '</small>';
  39. },
  40. 'update_rate': 60000
  41. };
  42. plugin['handlers'].push({
  43. 'id': 'node_view',
  44. 'name': Drupal.t('View'),
  45. 'data_class': 'node',
  46. 'applicable': function(text, item) {
  47. return item.information.perm.indexOf('r') >= 0;
  48. },
  49. 'handler': function(text, item) {
  50. window.location.href = Drupal.settings.basePath + 'cobalt/alias/node/' + item.id;
  51. }
  52. });
  53. plugin['handlers'].push({
  54. 'id': 'node_edit',
  55. 'name': Drupal.t('Edit'),
  56. 'data_class': 'node',
  57. 'applicable': function(text, item) {
  58. return item.information.perm.indexOf('w') >= 0;
  59. },
  60. 'handler': function(text, item) {
  61. window.location.href = Drupal.settings.basePath + 'node/' + item.id + '/edit?destination=' + Drupal.settings.cobalt.path;
  62. }
  63. });
  64. plugin['handlers'].push({
  65. 'id': 'node_delete',
  66. 'name': Drupal.t('Delete'),
  67. 'data_class': 'node',
  68. 'applicable': function(text, item) {
  69. return item.information.perm.indexOf('d') >= 0;
  70. },
  71. 'handler': function(text, item) {
  72. window.location.href = Drupal.settings.basePath + 'node/' + item.id + '/delete?destination=' + Drupal.settings.cobalt.path;
  73. }
  74. });
  75. cobalt.registerPlugin('cobaltnodes', plugin);
  76. var add_temporary_entries = function(nid, perm) {
  77. var rp = 'node/' + nid;
  78. var ep = 'node/' + nid + '/edit';
  79. var dp = 'node/' + nid + '/delete';
  80. if (perm.indexOf('r') >= 0 && Drupal.settings.cobalt.path != rp) {
  81. cobalt.addTemporaryEntry('node_context_view', Drupal.t('View current node'), rp, 'url_data');
  82. }
  83. if (perm.indexOf('w') >= 0 && Drupal.settings.cobalt.path != ep) {
  84. cobalt.addTemporaryEntry('node_context_edit', Drupal.t('Edit current node'), ep, 'url_data');
  85. }
  86. if (perm.indexOf('d') >= 0 && Drupal.settings.cobalt.path != dp) {
  87. cobalt.addTemporaryEntry('node_context_delete', Drupal.t('Delete current node'), {'path': dp, 'destination': false}, 'url_data');
  88. }
  89. };
  90. //Run the current-node-check on init
  91. $(document).bind('cobalt-init', function(evt, cobalt) {
  92. // Make sure that we have the current node among our entries, this is a easy
  93. // way to make sure that we have the nodes the user expects.
  94. if (typeof(Drupal.settings.cobalt.nodes_current) != 'undefined') {
  95. var nid = Drupal.settings.cobalt.nodes_current;
  96. cobalt.loadEntry('nodes', nid, function(item) {
  97. // Make sure that context sensitive items are added
  98. if (!item) {
  99. get_node_data('single', nid, function(data){
  100. if (typeof(data['nodes'])!='undefined' && data.nodes.length) {
  101. add_temporary_entries(data.nodes[0][0], data.nodes[0][2]);
  102. }
  103. });
  104. }
  105. else {
  106. add_temporary_entries(item.id, item.information.perm);
  107. }
  108. });
  109. }
  110. });
  111. });
  112. })(jQuery);