PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/share/spice/rx_info/rx_info.js

http://github.com/duckduckgo/zeroclickinfo-spice
JavaScript | 97 lines | 77 code | 13 blank | 7 comment | 11 complexity | 407f71883431e6eb245cd2e1ff56f0be MD5 | raw file
Possible License(s): Apache-2.0
  1. (function (env) {
  2. "use strict";
  3. // Concatenate Ingredients
  4. function concatIngredients(ingredients) {
  5. var concatted = '';
  6. // Protect against the chance ingredients aren't part of the API result
  7. if (ingredients !== null && (typeof ingredients !== 'undefined')) {
  8. for (var i = 0; i < ingredients.length; i++) {
  9. concatted += ingredients[i] + ", ";
  10. }
  11. if (concatted.length > 0) {
  12. concatted = concatted.substring(0, concatted.length - 2);
  13. }
  14. }
  15. return concatted;
  16. }
  17. env.ddg_spice_rx_info = function(api_result){
  18. if (!api_result || api_result.error || !api_result.replyStatus || !api_result.replyStatus.totalImageCount || api_result.replyStatus.totalImageCount < 1) {
  19. return Spice.failed('rx_info');
  20. }
  21. var script = $('[src*="/js/spice/rx_info/"]')[0],
  22. source = $(script).attr("src"),
  23. query = decodeURIComponent(source.match(/rx_info\/([^\/]+)/)[1]),
  24. triggerWordMatch = parseInt(source.match(/rx_info\/[^\/]+\/(\d)/)[1]),
  25. relCheck;
  26. // meta information
  27. var sourceName = "More at DailyMed",
  28. sourceUrl = "http://dailymed.nlm.nih.gov/",
  29. skip_words = ['pill', 'rxinfo', 'capsule', 'tablet', 'softgel', 'caplets'];
  30. // perform relevancy checking if triggerWordMatch true
  31. if(triggerWordMatch) {
  32. relCheck = {
  33. skip_words : skip_words,
  34. primary: [
  35. { key: 'name' }, { key: 'ndc11' }
  36. ]
  37. };
  38. }
  39. Spice.add({
  40. id: "rx_info",
  41. name: "RxInfo",
  42. data: api_result.nlmRxImages,
  43. meta: {
  44. searchTerm: query,
  45. sourceName: sourceName,
  46. sourceUrl: sourceUrl
  47. },
  48. templates: {
  49. group: 'products_simple',
  50. item_detail: 'base_item_detail',
  51. options: {
  52. content: Spice.rx_info.rx_info,
  53. description_content: Spice.rx_info.rx_info_description,
  54. brand: false,
  55. rating: false
  56. }
  57. },
  58. normalize: function(item) {
  59. var heading = item.ndc11,
  60. active = concatIngredients(item.ingredients.active),
  61. inactive = concatIngredients(item.ingredients.inactive);
  62. // Not all items will have a name (drug name),
  63. // so when it's available use it, otherwise the
  64. // ndc-11 will suffice.
  65. if (item.name) {
  66. heading = item.name;
  67. }
  68. return {
  69. image: item.imageUrl,
  70. imageAlt: "Image for NDC: " + item.ndc11,
  71. abstract: item.ndc11,
  72. heading: heading,
  73. title: heading,
  74. subtitle: item.labeler,
  75. ratingText: item.ndc11,
  76. active: active,
  77. inactive: inactive,
  78. url: item.splSetId ? 'https://dailymed.nlm.nih.gov/dailymed/lookup.cfm?setid=' + item.splSetId : ''
  79. }
  80. },
  81. relevancy: relCheck,
  82. });
  83. };
  84. }(this));