/lib/controls/code_editor.js

https://github.com/ajanthanm/cssfilterlab · JavaScript · 83 lines · 51 code · 13 blank · 19 comment · 3 complexity · a1ae43c0a5609a1b1e41b9d1831ef368 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 CodeEditor(el) {
  18. CodeEditor.$super.call(this);
  19. // For performance reasons we will keep the code-mirror editor in a absolute posioned element
  20. // attached directly to body. This way we will avoid long layouts and long paint times due to
  21. // flex-box implementation.
  22. // We will keep the original element around to make it easy to steal its coordinates.
  23. this.uiElement = el;
  24. this.element = $("<div />").addClass("code-editor").appendTo("#codemirror-editors-view");
  25. this.isSupported = true;
  26. if (typeof CodeMirror !== 'function'){
  27. console.warn("Missing CodeMirror library. See README file")
  28. this.isSupported = false;
  29. return
  30. }
  31. var self = this;
  32. this.editor = new CodeMirror(this.element[0], {
  33. lineNumbers: true,
  34. matchBrackets: true,
  35. mode: "text/x-csrc",
  36. onChange: function() {
  37. self.fire("valueChanged", [self.getValue()]);
  38. }
  39. });
  40. }
  41. Global.Utils.extend(CodeEditor).from(Global.EventDispatcher);
  42. $.extend(CodeEditor.prototype, {
  43. setValue: function(value) {
  44. this.editor.setValue(value);
  45. },
  46. getValue: function() {
  47. return this.editor.getValue();
  48. },
  49. setActive: function(active) {
  50. this.element.toggleClass("active-shader-view", active);
  51. if (active)
  52. this.refresh();
  53. },
  54. refresh: function() {
  55. if (!this.isSupported){
  56. return
  57. }
  58. var clientRect = this.uiElement[0].getBoundingClientRect();
  59. this.element.css({
  60. "left": clientRect.left,
  61. "top": clientRect.top,
  62. "width": clientRect.width,
  63. "height": clientRect.height
  64. });
  65. $(this.editor.getScrollerElement()).height(clientRect.height);
  66. this.editor.refresh();
  67. }
  68. });
  69. Global.CodeEditor = CodeEditor;
  70. })();