/lib/views/filter_store_view.js

https://github.com/ajanthanm/cssfilterlab · JavaScript · 209 lines · 161 code · 33 blank · 15 comment · 15 complexity · 2026cd9bf9ddaed834c0007503642d4a 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 FilterStoreView(filterStore, filterList, filterListView, shaderEditor, type) {
  18. FilterStoreView.$super.call(this);
  19. this.type = this.filterTypes[type];
  20. this.filterStore = filterStore;
  21. this.filterStore.on("filterAdded", this.onFilterAdded.bind(this));
  22. this.filterStore.on("filterDeleted", this.onFilterDeleted.bind(this));
  23. this.filterList = filterList;
  24. this.filterListView = filterListView;
  25. this.shaderEditor = shaderEditor;
  26. this.dockPanel = new Global.DockPanel(this.type.name);
  27. this.filterStockListEl = $("<div />").addClass("filter-stock-list");
  28. this.filterStockEl = $("<div />")
  29. .addClass("filter-stock")
  30. .append(this.filterStockListEl)
  31. .appendTo(this.dockPanel.el);
  32. this.filters = [];
  33. }
  34. Global.Utils.extend(FilterStoreView).from(Global.EventDispatcher);
  35. $.extend(FilterStoreView.prototype, {
  36. filterTypes: {
  37. "builtins": {
  38. name: "Built-in",
  39. check: function(filterConfig) {
  40. return filterConfig.isBuiltin;
  41. }
  42. },
  43. "custom": {
  44. name: "Custom",
  45. check: function(filterConfig) {
  46. return !filterConfig.isBuiltin && !filterConfig.isFork;
  47. }
  48. },
  49. "forked": {
  50. name: "Forked Custom",
  51. check: function(filterConfig) {
  52. return !filterConfig.isBuiltin && filterConfig.isFork;
  53. }
  54. }
  55. },
  56. onFilterAdded: function(filterConfig, loadedFromPreset) {
  57. if (!this.type.check(filterConfig))
  58. return;
  59. function addFilter(e){
  60. e.preventDefault()
  61. self.filterListView.dockPanel.setActive(true);
  62. var filter = self.filterList.addFilter(filterConfig);
  63. filter.setActive(true);
  64. self.fire("filterSelected", [filter]);
  65. }
  66. var self = this;
  67. var el = $("<div />")
  68. .addClass("filter-item")
  69. .addClass(filterConfig.isBuiltin ? "builtin-filter" : "custom-filter")
  70. .data("filterConfig", filterConfig)
  71. .attr("data-filterName", encodeURIComponent(filterConfig.name))
  72. .click(addFilter);
  73. if (filterConfig.isFork)
  74. el.addClass("forked-filter");
  75. var cssPreview = filterConfig.generatePreviewCode(),
  76. previewEl = $("<div />")
  77. .addClass("filter-preview")
  78. .css("-webkit-filter", cssPreview)
  79. .appendTo(el);
  80. var titleEl = $("<div />")
  81. .addClass("filter-label")
  82. .text(filterConfig.label)
  83. .appendTo(el);
  84. var buttonBox = $("<div class='button-box'>");
  85. el.append(buttonBox)
  86. if (!filterConfig.isBuiltin) {
  87. if (!filterConfig.isFork) {
  88. buttonBox.append($("<a href='#' />")
  89. .addClass("customize-link icon icon-fork")
  90. .text("Fork")
  91. .attr("title", "Fork filter")
  92. .click(function(e) {
  93. e.stopPropagation();
  94. e.preventDefault();
  95. self.forkFilter(filterConfig);
  96. }));
  97. } else {
  98. buttonBox.append($("<a href='#' />")
  99. .addClass("customize-link icon icon-edit")
  100. .text("Edit")
  101. .attr("title", "Edit filter")
  102. .click(function(e) {
  103. e.stopPropagation();
  104. e.preventDefault();
  105. self.customizeFilter(el, filterConfig);
  106. }));
  107. buttonBox.append($("<a href='#'/>")
  108. .addClass("icon icon-remove")
  109. .text("Delete")
  110. .attr("title", "Delete filter")
  111. .click(function(e) {
  112. e.stopPropagation();
  113. e.preventDefault();
  114. var message = "Are you sure you want to delete the '"+ filterConfig.label +"' filter?";
  115. if(window.confirm(message))
  116. self.deleteFilter(filterConfig);
  117. }));
  118. }
  119. }
  120. filterConfig.on("configChanged", this.onFilterConfigChanged.bind(this, filterConfig, titleEl, previewEl));
  121. this.insertFilterElement(el, filterConfig);
  122. if (filterConfig.isFork && !loadedFromPreset) {
  123. this.dockPanel.setActive(true);
  124. el[0].scrollIntoView();
  125. el.hide().effect("highlight", {}, 400);
  126. this.customizeFilter(el, filterConfig);
  127. var filter = self.filterList.addFilter(filterConfig);
  128. filter.setActive(true);
  129. }
  130. },
  131. onFilterDeleted: function(filterConfig){
  132. this.filters.forEach(function(filter, index){
  133. if (filter.data("filterConfig").name === filterConfig.name){
  134. filter.remove()
  135. this.filters.splice(index, 1)
  136. }
  137. }.bind(this))
  138. if (this.shaderEditor.filter && this.shaderEditor.filter.name === filterConfig.name){
  139. this.shaderEditor.hide()
  140. }
  141. },
  142. insertFilterElement: function(el, filterConfig) {
  143. this.filters.push(el);
  144. this.filters.sort(function(elA, elB) {
  145. var a = elA.data("filterConfig"),
  146. b = elB.data("filterConfig");
  147. return a.name.localeCompare(b.name);
  148. });
  149. var index = this.filters.indexOf(el);
  150. if (index == this.filters.length - 1)
  151. this.filterStockListEl.append(el);
  152. else
  153. this.filters[index + 1].before(el);
  154. },
  155. forkFilter: function(filterConfig) {
  156. this.filterStore.forkFilter(filterConfig);
  157. },
  158. deleteFilter: function(filterConfig){
  159. this.filterStore.deleteFilter(filterConfig)
  160. },
  161. customizeFilter: function(el, filterConfig) {
  162. $.each(this.filters, function(i, el) { el.removeClass("current"); });
  163. el.addClass("current");
  164. this.shaderEditor.loadFilter(filterConfig);
  165. },
  166. onFilterConfigChanged: function(filterConfig, titleEl, previewEl) {
  167. titleEl.text(filterConfig.label);
  168. previewEl.css("-webkit-filter", filterConfig.generatePreviewCode());
  169. },
  170. insertUnsupportedPopup: function(el) {
  171. this.unsupportedPopupEl = el.clone(true);
  172. this.filterStockListEl.before(this.unsupportedPopupEl);
  173. }
  174. });
  175. Global.FilterStoreView = FilterStoreView;
  176. })();