/lib/views/preset_store_view.js

https://github.com/ajanthanm/cssfilterlab · JavaScript · 139 lines · 100 code · 23 blank · 16 comment · 6 complexity · 5f325bb5fe558c257edb9102075233ff MD5 · raw file

  1. /*
  2. * Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. (function() {
  17. function PresetStoreView(presetStore) {
  18. this.presetStore = presetStore;
  19. this.dockPanel = new Global.DockPanel("Presets");
  20. this.activeAnimationEl = $("#active-animation");
  21. this.animationsEl = $("#animations");
  22. this.saveAsEl = $("#save-animation");
  23. this.animationsContainerEl = $("#animations-container").appendTo(this.dockPanel.el);
  24. this.presetsByName = {};
  25. this.outsideLayerEl = $("<div />").addClass("clicks-catcher");
  26. // move the animations el to the upper most layer
  27. this.animationsEl.appendTo(document.body);
  28. this.init();
  29. }
  30. PresetStoreView.prototype = {
  31. init: function() {
  32. this.saveAsEl.click(this.onSaveAsClicked.bind(this));
  33. this.presetStore.on("presetsLoaded", this.onPresetsLoaded.bind(this));
  34. this.presetStore.on("presetAdded", this.onPresetAdded.bind(this));
  35. this.presetStore.on("presetRemoved", this.onPresetRemoved.bind(this));
  36. this.presetStore.on("activePresetChanged", this.onActivePresetChanged.bind(this));
  37. this.animationsEl.hide();
  38. this.outsideLayerEl.click(this.hidePresets.bind(this));
  39. this.activeAnimationEl.click(this.showPresets.bind(this));
  40. },
  41. showPresets: function() {
  42. var topOffset = this.activeAnimationEl.offset(),
  43. height = this.activeAnimationEl.outerHeight(),
  44. width = this.activeAnimationEl.outerWidth();
  45. this.animationsEl.css("top", topOffset.top + height);
  46. this.animationsEl.css("left", topOffset.left);
  47. this.animationsEl.css("width", width);
  48. this.animationsEl.show();
  49. this.animationsEl.before(this.outsideLayerEl);
  50. },
  51. hidePresets: function() {
  52. this.animationsEl.hide();
  53. this.outsideLayerEl.detach();
  54. },
  55. enableSaveAs: function(){
  56. this.saveAsEl.removeClass('disabled');
  57. },
  58. onSaveAsClicked: function() {
  59. if (this.presetStore.savePresetAs())
  60. this.saveAsEl.addClass('disabled');
  61. return false;
  62. },
  63. onDeleteClicked: function(preset) {
  64. this.presetStore.deletePreset(preset);
  65. this.hidePresets();
  66. },
  67. onPresetsLoaded: function() {
  68. this.animationsContainerEl.show();
  69. },
  70. onPresetAdded: function(preset) {
  71. var key = "_" + preset;
  72. if (this.presetsByName.hasOwnProperty(key))
  73. return;
  74. var el = $("<li />")
  75. .append($('<a href="#">')
  76. .text(preset)
  77. .append($('<span>')
  78. .addClass('icon icon-remove')
  79. .click(function(){
  80. var message = "Are you sure you want to delete the '"+ preset +"' preset?";
  81. if(window.confirm(message))
  82. this.onDeleteClicked(preset)
  83. }.bind(this))
  84. )
  85. )
  86. .click(this.onPresetNameClicked.bind(this, preset))
  87. .appendTo(this.animationsEl);
  88. this.presetsByName[key] = el;
  89. return el;
  90. },
  91. onPresetNameClicked: function(preset) {
  92. this.hidePresets();
  93. this.presetStore.loadPreset(preset);
  94. return false;
  95. },
  96. onPresetRemoved: function(preset) {
  97. var key = "_" + preset,
  98. removedEl = this.presetsByName[key];
  99. if (!removedEl)
  100. return;
  101. delete this.presetsByName[key];
  102. removedEl.remove();
  103. },
  104. onActivePresetChanged: function(newPreset, oldPreset) {
  105. var oldEl = this.presetsByName["_" + oldPreset],
  106. newEl = this.presetsByName["_" + newPreset];
  107. if (oldEl)
  108. oldEl.removeClass("current");
  109. if (!newEl)
  110. newEl = this.onPresetAdded(newPreset);
  111. newEl.addClass("current");
  112. this.activeAnimationEl.text(newPreset);
  113. }
  114. };
  115. Global.PresetStoreView = PresetStoreView;
  116. })();