Sin descripción

jquery.grp_related_generic.js 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * GRAPPELLI RELATED FK
  3. * generic lookup
  4. */
  5. (function($){
  6. var methods = {
  7. init: function(options) {
  8. options = $.extend({}, $.fn.grp_related_generic.defaults, options);
  9. return this.each(function() {
  10. var $this = $(this);
  11. // add placeholder
  12. var val = $(options.content_type).val() || $(options.content_type).find(':checked').val();
  13. if (val) {
  14. $this.after(options.placeholder).after(lookup_link($this.attr('id'),val));
  15. }
  16. // add related class
  17. $this.addClass('grp-has-related-lookup');
  18. // lookup
  19. if (val) {
  20. lookup_id($this, options); // lookup when loading page
  21. }
  22. $this.on("change focus keyup", function() { // id-handler
  23. lookup_id($this, options);
  24. });
  25. $(options.content_type).on("change", function() { // content-type-handler
  26. update_lookup($(this), options);
  27. });
  28. });
  29. }
  30. };
  31. $.fn.grp_related_generic = function(method) {
  32. if (methods[method]) {
  33. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  34. } else if (typeof method === 'object' || ! method) {
  35. return methods.init.apply(this, arguments);
  36. } else {
  37. $.error('Method ' + method + ' does not exist on jQuery.grp_related_generic');
  38. }
  39. return false;
  40. };
  41. var lookup_link = function(id, val) {
  42. var lookuplink = $('<a class="related-lookup"></a>');
  43. lookuplink.attr('id', 'lookup_'+id);
  44. lookuplink.attr('href', window.ADMIN_URL + MODEL_URL_ARRAY[val].app + "/" + MODEL_URL_ARRAY[val].model + '/?');
  45. lookuplink.attr('onClick', 'return showRelatedObjectLookupPopup(this);');
  46. return lookuplink;
  47. };
  48. var update_lookup = function(elem, options) {
  49. var obj = $(options.object_id);
  50. obj.val('');
  51. obj.parent().find('a.related-lookup').remove();
  52. obj.parent().find('.grp-placeholder-related-generic').remove();
  53. var val = $(elem).val() || $(elem).find(':checked').val();
  54. if (val) {
  55. obj.after(options.placeholder).after(lookup_link(obj.attr('id'),val));
  56. }
  57. };
  58. var lookup_id = function(elem, options) {
  59. var text = elem.next().next();
  60. $.getJSON(options.lookup_url, {
  61. object_id: elem.val(),
  62. app_label: grappelli.get_app_label(elem),
  63. model_name: grappelli.get_model_name(elem),
  64. query_string: grappelli.get_query_string(elem)
  65. }, function(data) {
  66. if (data[0].label === "") {
  67. text.hide();
  68. } else {
  69. text.show();
  70. }
  71. if (data[0].safe) {
  72. text.html($('<span class="grp-placeholder-label"></span>').html(data[0].label + '\u200E'));
  73. } else {
  74. text.html($('<span class="grp-placeholder-label"></span>').text(data[0].label + '\u200E'));
  75. }
  76. });
  77. };
  78. $.fn.grp_related_generic.defaults = {
  79. placeholder: '<div class="grp-placeholder-related-generic" style="display:none"></div>',
  80. repr_max_length: 30,
  81. lookup_url: '',
  82. content_type: '',
  83. object_id: ''
  84. };
  85. })(grp.jQuery);