/AutoComplete/Scripts/IM.AutoComplete.js

http://AutoCompleteDotNet.codeplex.com · JavaScript · 145 lines · 118 code · 7 blank · 20 comment · 43 complexity · f1a558910157346d06c88b8fe2753d43 MD5 · raw file

  1. // Disables right menu for the control whose id is passed as argument.
  2. function DisableContextMenu(id) {
  3. $("#" + id).bind("contextmenu", function(e) {
  4. return false;
  5. });
  6. }
  7. // When invoked, this function initializes autocomplete functionalities for
  8. // the input field whose id is passed as argument.
  9. function InitializeAutocomplete(settings) {
  10. if (typeof jQuery == 'undefined') {
  11. // jQuery is not loaded
  12. alert('jQuery not loaded');
  13. return;
  14. }
  15. if (!jQuery.ui) {
  16. // jQuery UI not loaded
  17. alert('jQuery UI not loaded');
  18. return;
  19. }
  20. if (settings.Id == undefined) {
  21. // Id of the input textbox not specified
  22. alert('Id of the input textbox not specified');
  23. return;
  24. }
  25. if (settings.Url == undefined) {
  26. // DataSource url not specified
  27. alert('Missing DataSource Url');
  28. return;
  29. }
  30. if (settings.ValueField == undefined) {
  31. // Value of the field to display is not specified
  32. alert('ValueField is not specified');
  33. return;
  34. }
  35. if (settings.LabelField == undefined) {
  36. // Value of the field to display after selection, is not specified
  37. alert('LabelField is not specified');
  38. return;
  39. }
  40. $(function() {
  41. DisableContextMenu(settings.Id);
  42. $("#" + settings.Id).autocomplete({
  43. source: function(request, response) {
  44. RemoteRequest(request, response, settings);
  45. }, // end source
  46. minLength: settings.MinChars,
  47. select: function(event, ui) {
  48. if (settings.SelectionCallback == null ||
  49. ui.item.label == settings.NoResultsMessage ||
  50. ui.item.label == "<" + settings.NoResultsMessage + ">" ||
  51. ui.item.label == settings.ErrorMessage ||
  52. ui.item.label == "<" + settings.ErrorMessage + ">") {
  53. return;
  54. }
  55. settings.SelectionCallback(ui.item.object);
  56. }
  57. }); // end autocomplete
  58. }); // end document.ready
  59. }
  60. // Need to control ajax requests race conditions
  61. var previousRequest;
  62. // Invokes the url passed as argument expecting it to return a json result
  63. function RemoteRequest(request, response, settings) {
  64. var httpMethod = (settings.HttpMethod == undefined || settings.HttpMethod == null) ?
  65. "POST" : settings.HttpMethod;
  66. var contentType = (settings.HttpMethod == undefined || settings.HttpMethod == null) ?
  67. "application/x-www-form-urlencoded; charset=utf-8" : "application/json; charset=utf-8";
  68. previousRequest = $.ajax({
  69. // Need to control ajax requests race conditions
  70. beforeSend: function() {
  71. if ((previousRequest != undefined) && (previousRequest.readyState != 4)) {
  72. previousRequest.abort();
  73. }
  74. },
  75. type: httpMethod,
  76. contentType: contentType,
  77. url: settings.Url,
  78. dataType: "json",
  79. data: { startsWith: request.term },
  80. success: function(data) {
  81. if ((data == null) || (data.length == 0)) {
  82. ReturnEmptyResultSet(response, settings.NoResultsMessage);
  83. return;
  84. }
  85. var labelUndefined = ((data[0])[settings.LabelField] == undefined);
  86. var valueUndefined = ((data[0])[settings.ValueField] == undefined);
  87. if (labelUndefined || valueUndefined) {
  88. if (settings.ErrorCallback != null) {
  89. var error = "The following properties are not defined in the result object: ";
  90. error = labelUndefined ? error + settings.LabelField : error;
  91. error = valueUndefined ? error + " " + settings.ValueField : error;
  92. settings.ErrorCallback(error);
  93. }
  94. ReturnEmptyResultSet(response, settings.ErrorMessage);
  95. return;
  96. }
  97. response($.map(data,
  98. function(item) {
  99. return MappingFunction(item, settings.LabelField, settings.ValueField);
  100. }
  101. ) // end map
  102. ); // end response
  103. }, // end success
  104. error: function(xmlHttpRequest, status, error) {
  105. if (settings.ErrorCallback != null) {
  106. var message = (error == undefined) ? status + ": " + xmlHttpRequest.status : error;
  107. settings.ErrorCallback(message);
  108. }
  109. ReturnEmptyResultSet(response, settings.ErrorMessage);
  110. } // end error
  111. }); // end ajax
  112. }
  113. // This function accepts an item and uses it to initialize an object with
  114. // the properties: label, value and object.
  115. // It expects that the item received is an instance of an object supporting
  116. // two properties with the names corresponding to the strings
  117. // passed as the second and third parameters.
  118. function MappingFunction(item, labelField, valueField) {
  119. return {
  120. label: item[labelField],
  121. value: item[valueField],
  122. object: item
  123. }
  124. }
  125. // Used to return an empty result set as autosuggest list.
  126. // The list will be composed of a single line with the message
  127. // passed as argument if it's not null, otherwise no list will appear.
  128. function ReturnEmptyResultSet(response, errorMessage) {
  129. if (errorMessage != null) {
  130. var item = new Object();
  131. item.label = "<" + errorMessage + ">";
  132. item.value = "";
  133. response([item]);
  134. }
  135. else {
  136. response(null);
  137. }
  138. }