PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/js/toolbar.js

https://bitbucket.org/worldsayshi/master_thesis-gui
JavaScript | 119 lines | 92 code | 13 blank | 14 comment | 4 complexity | 372e66b4731346c698456dd9dd94a98a MD5 | raw file
  1. // "Tools" are the small buttons hovering in some elements
  2. // controlling some aspects of their model. Used for toggling
  3. // states or removing objects
  4. // ** Tool view **
  5. var ToolView = Backbone.View.extend({
  6. el:'<ul>',
  7. render: function () {
  8. return this.$el.empty().addClass('toolbar').append(
  9. this.model.map(_.bind(this.renderOneTool,this)));
  10. },
  11. renderOneTool: function (toolModel) {
  12. assert(toolModel.useTool,'Tool has no use method');
  13. if(!toolModel.getIcon()){
  14. return $('<li>')
  15. .addClass('tool')
  16. .append(
  17. $('<button>')
  18. .button()
  19. .click(_.bind(this.runTool,this,toolModel))
  20. .text(toolModel.getLabel()));
  21. }
  22. return $('<li>')
  23. .click(_.bind(toolModel.useTool,toolModel))
  24. .addClass('tool')
  25. .html(
  26. $('<span>')
  27. .addClass('ui-icon')
  28. .addClass(toolModel.getIcon()));
  29. },
  30. runTool: function (toolModel,ev) {
  31. ev.preventDefault();
  32. toolModel.useTool();
  33. }
  34. });
  35. // ** Tool models **
  36. var ToolsCollection = Backbone.Collection.extend({});
  37. var ToolModel = Backbone.Model.extend({
  38. getIcon: function () {
  39. if(_.isArray(this.get('icon'))){
  40. if(this.get('toggle')){
  41. return this.get('icon')[1];
  42. } else {
  43. return this.get('icon')[0];
  44. }
  45. }
  46. return this.get('icon');
  47. }
  48. });
  49. // Tool for removing filter
  50. var RemoveFilter = ToolModel.extend({
  51. initialize: function () {
  52. this.set('toggle',false);
  53. this.set('icon','ui-icon-close');
  54. },
  55. useTool: function (id) {
  56. this.get('parentModel').dontUseAsFilter(id);
  57. this.set('toggle',!this.get('toggle'));
  58. }
  59. });
  60. // Tool for (un)locking value updates of a filter
  61. var LockValues = ToolModel.extend({
  62. initialize: function () {
  63. var self = this;
  64. this.set('icon',['ui-icon-locked','ui-icon-unlocked']);
  65. var parentModel = this.get('parentModel');
  66. this.listenTo(parentModel,'change:allowUpdateValues',function(){
  67. self.set('toggle',parentModel.get('allowUpdateValues'));
  68. });
  69. this.set('toggle',parentModel.get('allowUpdateValues'));
  70. },
  71. useTool: function (id) {
  72. var parentModel = this.get('parentModel');
  73. parentModel.set('allowUpdateValues',!parentModel.get('allowUpdateValues'));
  74. }
  75. });
  76. // Tool for toggling sorting of the result list:
  77. // according to score
  78. // OR
  79. // according to ascending property value
  80. // (and according to order of filters)
  81. var ToggleGlobalSorting = ToolModel.extend({
  82. initialize: function () {
  83. },
  84. useTool: function (id) {
  85. assert(this.get('parentModel') instanceof ApplicationModel, this.model);
  86. this.get('parentModel').cycleGlobalSorting();
  87. this.trigger('change');
  88. },
  89. getLabel: function () {
  90. return this.get('id')+":"+this.get('parentModel').get('query').get('sorting');
  91. }
  92. });
  93. var ToggleFuzziness = ToolModel.extend({
  94. initialize: function () {
  95. },
  96. useTool: function (id) {
  97. assert(this.get('parentModel') instanceof ApplicationModel, this.model);
  98. this.get('parentModel').toggleFuzziness();
  99. this.set('toggle',this.get('parentModel').get('query').get('fuzziness'));
  100. },
  101. getLabel: function () {
  102. return this.get('id')+":"+this.get('parentModel').get('query').get('fuzziness');
  103. }
  104. });
  105. // TODO
  106. //var HideFacetList = ToolModel.extend({ ...