123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- /**
- * jQuery ACE plugin.
- * @author Sam Mousa
- * @url https://github.com/SamMousa/jquery-ace
- */
- (function($) {
- var methods = {
- 'init': function(options) {
- var config = $.extend({
- 'mode' : 'html',
- 'tabSize' : 4,
- 'softTabs' : true,
- 'highlightActiveLine' : true,
- 'idPostfix' : '__ace',
- 'toolbarCallback' : null,
- 'wrapperClass' : 'jquery-ace-wrapper'
- }, options);
- return this.each(function() {
- var data = $(this).data('ace');
- if (!data) {
- var textarea = $(this);
- // Setup here.
- if ($(this).attr('id'))
- {
- var id = textarea.attr('id') + config.idPostfix;
- }
- var h = textarea.height();
- var w = textarea.width();
- textarea.hide();
- var wrapperDiv = $('<div/>').insertAfter(textarea).height(h).width(w).addClass(config.wrapperClass);
- // Check if we have a toolbar.
- var editorDiv = $('<div/>').appendTo(wrapperDiv).attr('id', id).height(h).width(w);
- var editor = ace.edit(id);
- if (typeof config.toolbarCallback == 'function')
- {
- var toolbarDiv = $('<div/>').prependTo(wrapperDiv).width(w);
- config.toolbarCallback(toolbarDiv, editor);
- // Resize editor.
- if (toolbarDiv.height() > 0)
- {
- editorDiv.height(h - toolbarDiv.height());
- editor.resize();
- }
- }
- data = {
- 'wrapperDiv' : wrapperDiv,
- 'editorDiv' : editorDiv,
- 'editor' : editor
- };
- var session = editor.getSession();
- session.setMode('ace/mode/' + config.mode);
- session.setTabSize(config.tabSize);
- session.setUseSoftTabs(config.softTabs);
- textarea.data('ace', data);
- textarea.ace('val', textarea.val());
- editor.setHighlightActiveLine(config.highlightActiveLine);
- editor.clearSelection();
- session.on('change', function(e) {
- textarea.val(e.target.getValue());
- });
- }
- });
- },
- 'get': function() {
- if (this.first().data('ace'))
- {
- return this.first().data('ace').editor;
- }
- },
- 'val': function(value) {
- if (typeof value == 'undefined')
- {
- return this.first().data('ace').editor.getValue();
- }
- else
- {
- this.each(function() {
- var data = $(this).data('ace');
- if (data)
- {
- data.editor.setValue(value);
- }
- });
- }
- return this;
- },
- 'check': function() {
- return typeof this.first().data('ace') != 'undefined';
- }
- };
- $.fn.ace = function(method) {
- if ( methods[method] ) {
- return methods[method].apply(this, Array.prototype.slice.call( arguments, 1 ));
- } else if ( typeof method === 'object' || ! method ) {
- return methods.init.apply(this, arguments);
- } else {
- $.error('Method ' + method + ' does not exist on jQuery.ace');
- }
- };
- })(jQuery);
|