PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/pancake-web/pancake/web/static/js/models/websearchmodel.js

https://bitbucket.org/mozillapancake/pancake
JavaScript | 86 lines | 66 code | 11 blank | 9 comment | 6 complexity | 5757ae25d2fbf3aba824d1f8fbe2fbc6 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, MIT, Apache-2.0
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. define([
  5. 'config',
  6. 'config/searchProviders',
  7. 'underscore',
  8. 'backbone',
  9. 'models/stackmodel',
  10. 'lib/template',
  11. 'lib/urlhelper',
  12. 'logger'
  13. ], function (config, searchProviders, util, Backbone, StackModel, template, URLHelper, logger) {
  14. var WebSearchModel = Backbone.Model.extend({
  15. urlTemplate: '',
  16. initialize: function (attributes, options) {
  17. options = options || {};
  18. if (!this.get('search_provider')) this.set(
  19. { search_provider: searchProviders['default']},
  20. { silent: true }
  21. );
  22. if (!this.urlHelper) this.urlHelper = new URLHelper({
  23. template: '{ base }search/{ provider }{ query }',
  24. tokens: {
  25. base: config.applicationRoot,
  26. provider: this.get('search_provider')
  27. },
  28. query: {
  29. q: this.get('search_terms')
  30. }
  31. });
  32. this.urlTemplate = template(options.template || this.urlTemplate);
  33. },
  34. url: function() {
  35. // Make sure URL is up-to-date with current variables on model.
  36. var provider = { provider: this.get('search_provider') };
  37. var query = { q: this.get('search_terms') };
  38. return this.urlHelper.url(provider, query);
  39. },
  40. getResultModelFromElement: function(el) {
  41. var terms = this.get('search_terms');
  42. var model = new StackModel({
  43. 'place_title': el.getAttribute('data-title') || "Missing title",
  44. 'place_url': el.getAttribute('data-url') || el.href,
  45. 'search_terms': terms,
  46. // TODO: GB: hard-coded. This should be pulled from a list of registered
  47. // search providers -- probably from an API somewhere. The client should
  48. // be dumb.
  49. 'search_provider': 'bing',
  50. search_url: template(config.searchResults)({
  51. query: terms,
  52. provider: 'bing'
  53. })
  54. });
  55. // Set URL root on this model -- it's an orphan (doesn't belong)
  56. // to a collection, so we need to tell Backbone what to hit.
  57. this.urlHelper = new URLHelper({
  58. template: config.latticeUrl + '{ query }',
  59. tokens: {
  60. latticeRoot: config.latticeRoot,
  61. username: config.username,
  62. service: 'stack',
  63. method: 'search'
  64. },
  65. query: {
  66. v: 2,
  67. q: terms
  68. }
  69. });
  70. model.urlRoot = this.urlHelper.url();
  71. return model;
  72. }
  73. });
  74. return WebSearchModel;
  75. });