jquery-ace.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. (function() {
  2. var __hasProp = {}.hasOwnProperty,
  3. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  4. window.jQueryAce = {
  5. initialize: function(element, options) {
  6. var klass;
  7. klass = (function() {
  8. switch (true) {
  9. case $(element).is('textarea'):
  10. return jQueryAce.TextareaEditor;
  11. default:
  12. return jQueryAce.BaseEditor;
  13. }
  14. })();
  15. return new klass(element, options);
  16. },
  17. defaults: {
  18. theme: null,
  19. lang: null,
  20. mode: null,
  21. width: null,
  22. height: null
  23. },
  24. version: '1.0.3',
  25. require: function() {
  26. switch (true) {
  27. case typeof ace.require === 'function':
  28. return ace.require.apply(null, arguments);
  29. case typeof window.require === 'function':
  30. return window.require.apply(null, arguments);
  31. default:
  32. throw "Can't find 'require' function";
  33. }
  34. }
  35. };
  36. jQueryAce.BaseEditor = (function() {
  37. function BaseEditor(element, options) {
  38. if (options == null) {
  39. options = {};
  40. }
  41. this.element = $(element);
  42. this.options = $.extend({}, jQueryAce.defaults, options);
  43. }
  44. BaseEditor.prototype.create = function() {
  45. this.editor = new jQueryAce.AceDecorator(ace.edit(this.element));
  46. return this.update();
  47. };
  48. BaseEditor.prototype.update = function(options) {
  49. var lang;
  50. if (options != null) {
  51. this.options = $.extend({}, this.options, options);
  52. }
  53. if (this.options.theme != null) {
  54. this.editor.theme(this.options.theme);
  55. }
  56. lang = this.options.lang || this.options.mode;
  57. if (lang != null) {
  58. return this.editor.lang(lang);
  59. }
  60. };
  61. BaseEditor.prototype.destroy = function() {
  62. this.element.data('ace', null);
  63. this.editor.destroy();
  64. return this.element.empty();
  65. };
  66. return BaseEditor;
  67. })();
  68. jQueryAce.TextareaEditor = (function(_super) {
  69. __extends(TextareaEditor, _super);
  70. function TextareaEditor() {
  71. return TextareaEditor.__super__.constructor.apply(this, arguments);
  72. }
  73. TextareaEditor.prototype.show = function() {
  74. var _ref;
  75. if ((_ref = this.container) != null) {
  76. _ref.show();
  77. }
  78. return this.element.hide();
  79. };
  80. TextareaEditor.prototype.hide = function() {
  81. var _ref;
  82. if ((_ref = this.container) != null) {
  83. _ref.hide();
  84. }
  85. return this.element.show();
  86. };
  87. TextareaEditor.prototype.create = function() {
  88. var _this = this;
  89. this.container = this.createAceContainer();
  90. this.editor = new jQueryAce.AceDecorator(ace.edit(this.container.get(0)));
  91. this.update();
  92. this.editor.value(this.element.val());
  93. this.editor.ace.on('change', function(e) {
  94. return _this.element.val(_this.editor.value());
  95. });
  96. return this.show();
  97. };
  98. TextareaEditor.prototype.destroy = function() {
  99. TextareaEditor.__super__.destroy.call(this);
  100. this.hide();
  101. return this.container.remove();
  102. };
  103. TextareaEditor.prototype.createAceContainer = function() {
  104. return this.buildAceContainer().insertAfter(this.element);
  105. };
  106. TextareaEditor.prototype.buildAceContainer = function() {
  107. return $('<div></div>').css({
  108. display: 'none',
  109. position: 'relative',
  110. width: this.options.width || this.element.width(),
  111. height: this.options.height || this.element.height()
  112. });
  113. };
  114. return TextareaEditor;
  115. })(jQueryAce.BaseEditor);
  116. jQueryAce.AceDecorator = (function() {
  117. function AceDecorator(ace) {
  118. this.ace = ace;
  119. }
  120. AceDecorator.prototype.theme = function(themeName) {
  121. return this.ace.setTheme("ace/theme/" + themeName);
  122. };
  123. AceDecorator.prototype.lang = function(modeName) {
  124. var klass;
  125. klass = jQueryAce.require("ace/mode/" + modeName).Mode;
  126. return this.session().setMode(new klass);
  127. };
  128. AceDecorator.prototype.mode = function(modeName) {
  129. return this.lang(modeName);
  130. };
  131. AceDecorator.prototype.session = function() {
  132. return this.ace.getSession();
  133. };
  134. AceDecorator.prototype.destroy = function() {
  135. return this.ace.destroy();
  136. };
  137. AceDecorator.prototype.value = function(text) {
  138. if (text != null) {
  139. return this.ace.insert(text);
  140. } else {
  141. return this.ace.getValue();
  142. }
  143. };
  144. return AceDecorator;
  145. })();
  146. (function($) {
  147. $.ace = function(element, options) {
  148. return $(element).ace(options);
  149. };
  150. return $.fn.ace = function(options) {
  151. return this.each(function() {
  152. var editor;
  153. editor = $(this).data('ace');
  154. if (editor != null) {
  155. return editor.update(options);
  156. } else {
  157. editor = jQueryAce.initialize(this, options);
  158. editor.create();
  159. return $(this).data('ace', editor);
  160. }
  161. });
  162. };
  163. })(jQuery);
  164. }).call(this);