From c1ff6a2d44a173a9a95ccde1f5337d72c306c66a Mon Sep 17 00:00:00 2001 From: Jamie Peabody Date: Wed, 14 Nov 2012 22:00:29 +0000 Subject: [PATCH] Using jQuery instead of $ for compatability with other libraries. --- lib/mergely.js | 104 ++++++++++++++++++++++++------------------------- 1 file changed, 52 insertions(+), 52 deletions(-) diff --git a/lib/mergely.js b/lib/mergely.js index 8204810..ceca451 100644 --- a/lib/mergely.js +++ b/lib/mergely.js @@ -33,7 +33,7 @@ Mgly.LCS = function(x, y) { } this.ready = 1; } -$.extend(Mgly.LCS.prototype, { +jQuery.extend(Mgly.LCS.prototype, { clear: function() { this.ready = 0; }, diff: function(added, removed) { this._diff(this.x.length - 1, this.y.length - 1, added, removed); @@ -121,7 +121,7 @@ Mgly.diff = function(lhs, rhs, retain_lines) { this.rhs_lines = rhs_lines; } }; -$.extend(Mgly.diff.prototype, { +jQuery.extend(Mgly.diff.prototype, { changes: function() { return this.items; }, normal_form: function() { var nf = ''; @@ -334,7 +334,7 @@ Mgly.mergely = function(el, options) { } }; -$.extend(Mgly.mergely.prototype, { +jQuery.extend(Mgly.mergely.prototype, { name: "mergely", //http://jupiterjs.com/news/writing-the-perfect-jquery-plugin init: function(el, options) { @@ -353,13 +353,13 @@ $.extend(Mgly.mergely.prototype, { height: function(h) { return h - 20; }, width: function(w) { return w; }, resize: function() { - var w = $(el).parent().width(); - var h = $(window).height(); + var w = jQuery(el).parent().width(); + var h = jQuery(window).height(); if (this.width) w = this.width(w); if (this.height) h = this.height(h); var content_width = w / 2.0 - 2 * 8 - 8; var content_height = h; - var self = $(el); + var self = jQuery(el); self.find('.mergely-column').css({ 'width': content_width + 'px' }); self.find('.mergely-column, .mergely-canvas, .mergely-margin, .mergely-column textarea, .CodeMirror-scroll').css({ 'height': content_height + 'px' }); self.find('.mergely-canvas').css({ 'height': content_height + 'px' }); @@ -385,29 +385,29 @@ $.extend(Mgly.mergely.prototype, { this.rhs_cmsettings = {}; // save this element for faster queries - this.element = $(el); + this.element = jQuery(el); // save options if there are any - if (options && options.cmsettings) $.extend(this.lhs_cmsettings, cmsettings, options.cmsettings, options.lhs_cmsettings); - if (options && options.cmsettings) $.extend(this.rhs_cmsettings, cmsettings, options.cmsettings, options.rhs_cmsettings); - if (options) $.extend(this.settings, options); + if (options && options.cmsettings) jQuery.extend(this.lhs_cmsettings, cmsettings, options.cmsettings, options.lhs_cmsettings); + if (options && options.cmsettings) jQuery.extend(this.rhs_cmsettings, cmsettings, options.cmsettings, options.rhs_cmsettings); + if (options) jQuery.extend(this.settings, options); // bind if the element is destroyed - this.element.bind("destroyed", $.proxy(this.teardown, this)); + this.element.bind("destroyed", jQuery.proxy(this.teardown, this)); // save this instance in jQuery data - $.data(el, this.name, this); + jQuery.data(el, this.name, this); this._setup(el); }, // bind events to this instance's methods bind: function() { - var rhstx = $('#' + this.id + '-rhs').get(0); + var rhstx = jQuery('#' + this.id + '-rhs').get(0); if (!rhstx) { console.error('rhs textarea not defined - Mergely not initialized properly'); return; } - var lhstx = $('#' + this.id + '-lhs').get(0); + var lhstx = jQuery('#' + this.id + '-lhs').get(0); if (!rhstx) { console.error('lhs textarea not defined - Mergely not initialized properly'); return; @@ -435,7 +435,7 @@ $.extend(Mgly.mergely.prototype, { self.editor[self.id + '-rhs'].refresh(); self._changing(self.id + '-lhs', self.id + '-rhs'); } - $(window).resize( + jQuery(window).resize( function () { if (sz_timeout1) clearTimeout(sz_timeout1); sz_timeout1 = setTimeout(sz, self.settings.resize_timeout); @@ -477,7 +477,7 @@ $.extend(Mgly.mergely.prototype, { } }, options: function(opts) { - $.extend(this.settings, opts); + jQuery.extend(this.settings, opts); }, swap: function() { if (this.lhs_cmsettings.readOnly || this.rhs_cmsettings.readOnly) return; @@ -536,8 +536,8 @@ $.extend(Mgly.mergely.prototype, { return d.normal_form(); }, _setup: function(el) { - $(this.element).hide();//hide - this.id = $(el).attr('id'); + jQuery(this.element).hide();//hide + this.id = jQuery(el).attr('id'); var height = this.settings.editor_height; var width = this.settings.editor_width; this.changed_timeout = null; @@ -547,7 +547,7 @@ $.extend(Mgly.mergely.prototype, { this.change_exp = new RegExp(/(\d+(?:,\d+)?)([acd])(\d+(?:,\d+)?)/); var merge_lhs_button; var merge_rhs_button; - if ($.button != undefined) { + if (jQuery.button != undefined) { //jquery ui merge_lhs_button = ''; merge_rhs_button = ''; @@ -558,24 +558,24 @@ $.extend(Mgly.mergely.prototype, { merge_lhs_button = '
<
'; merge_rhs_button = '
>
'; } - this.merge_rhs_button = $(merge_rhs_button); - this.merge_lhs_button = $(merge_lhs_button); + this.merge_rhs_button = jQuery(merge_rhs_button); + this.merge_lhs_button = jQuery(merge_lhs_button); if (this.merge_rhs_button.corner) this.merge_rhs_button.corner('3px'); if (this.merge_lhs_button.corner) this.merge_lhs_button.corner('3px'); // create the textarea and canvas elements - $(this.element).append($('
')); - $(this.element).append($('
')); - $(this.element).append($('
')); - $(this.element).append($('
')); - $(this.element).append($('
')); + jQuery(this.element).append(jQuery('
')); + jQuery(this.element).append(jQuery('
')); + jQuery(this.element).append(jQuery('
')); + jQuery(this.element).append(jQuery('
')); + jQuery(this.element).append(jQuery('
')); //codemirror var cmstyle = '#' + this.id + ' .CodeMirror-gutter-text { padding: 5px 0 0 0; }' + '#' + this.id + ' .CodeMirror-lines pre, ' + '#' + this.id + ' .CodeMirror-gutter-text pre { line-height: 18px; }'; if (this.settings.autoresize) { cmstyle += this.id + ' .CodeMirror-scroll { height: 100%; overflow: auto; }'; } - $('').appendTo('head'); + jQuery('').appendTo('head'); this.bind(); if (this.settings.lhs) this.settings.lhs( this.editor[this.id + '-lhs'].setValue ); if (this.settings.rhs) this.settings.rhs( this.editor[this.id + '-rhs'].setValue ); @@ -586,12 +586,12 @@ $.extend(Mgly.mergely.prototype, { // merge var self = this; var ed = {lhs:this.editor[this.id + '-lhs'], rhs:this.editor[this.id + '-rhs']}; - $('.merge-button').live('click', function(ev){ + jQuery('.merge-button').live('click', function(ev){ console.log('lhs hover over', ev, this); // side of mouseenter var side = 'rhs'; var oside = 'lhs'; - var parent = $(this).parents('#' + self.id + '-editor-lhs'); + var parent = jQuery(this).parents('#' + self.id + '-editor-lhs'); if (parent.length) { side = 'lhs'; oside = 'rhs'; @@ -602,7 +602,7 @@ $.extend(Mgly.mergely.prototype, { // get the change id var cid = null; var info = ed[side].lineInfo(pos.line); - $.each(info.bgClass.split(' '), function(i, clazz) { + jQuery.each(info.bgClass.split(' '), function(i, clazz) { console.log('clazz', i, clazz); if (clazz.indexOf('cid-') == 0) { cid = parseInt(clazz.split('-')[1]); @@ -642,7 +642,7 @@ $.extend(Mgly.mergely.prototype, { self._skipscroll = false; return; } - var scroller = $(this.editor[editor_name].getScrollerElement()); + var scroller = jQuery(this.editor[editor_name].getScrollerElement()); if (this.midway == undefined) { this.midway = (scroller.height() / 2.0 + scroller.offset().top).toFixed(2); } @@ -691,7 +691,7 @@ $.extend(Mgly.mergely.prototype, { if (scroll) { // scroll the other side this.trace('scroll', 'scrolling other side', top_to - top_adjust); - var scroller = $(this.editor[name].getScrollerElement()); + var scroller = jQuery(this.editor[name].getScrollerElement()); self._skipscroll = true;//disable next event scroller.scrollTop(top_to - top_adjust).scrollLeft(left_to); } @@ -782,18 +782,18 @@ $.extend(Mgly.mergely.prototype, { //this.em_height = topnode.get(0).offsetHeight; // this is the distance from the top of the screen - var topnode = $('#' + this.id + '-lhs-margin').first(); + var topnode = jQuery('#' + this.id + '-lhs-margin').first(); var top_offset = topnode.offset().top; if (!top_offset) return;//try again this.draw_top_offset = 0.5 - top_offset; - this.em_height = $('.CodeMirror-lines pre').get(0).offsetHeight + this.em_height = jQuery('.CodeMirror-lines pre').get(0).offsetHeight if (!this.em_height) { console.warn('Failed to calculate offsets, using 18 by default'); this.em_height = 18; } this.draw_lhs_min = 0.5; - var c = $('#' + editor_name1 + '-' + editor_name2 + '-canvas'); + var c = jQuery('#' + editor_name1 + '-' + editor_name2 + '-canvas'); if (!c.length) { console.error('failed to find canvas', '#' + editor_name1 + '-' + editor_name2 + '-canvas'); } @@ -801,7 +801,7 @@ $.extend(Mgly.mergely.prototype, { console.error('canvas width is 0'); return; } - this.draw_rhs_max = $('#' + editor_name1 + '-' + editor_name2 + '-canvas').width() - 0.5; //24.5; + this.draw_rhs_max = jQuery('#' + editor_name1 + '-' + editor_name2 + '-canvas').width() - 0.5; //24.5; this.draw_lhs_width = 5; this.draw_rhs_width = 5; this.trace('calc', 'change offsets calculated', {top_offset: top_offset, lhs_min: this.draw_lhs_min, rhs_max: this.draw_rhs_max, lhs_width: this.draw_lhs_width, rhs_width: this.draw_rhs_width}); @@ -850,7 +850,7 @@ $.extend(Mgly.mergely.prototype, { return changes; }, _markup_changes: function (editor_name1, editor_name2, changes) { - $('.merge-button').remove(); // clear + jQuery('.merge-button').remove(); // clear var self = this; var led = this.editor[editor_name1]; @@ -1075,12 +1075,12 @@ $.extend(Mgly.mergely.prototype, { }); }, _draw_diff: function(editor_name1, editor_name2, changes) { - var visible_page_height = $(this.editor[editor_name1].getScrollerElement()).height(); - var gutter_height = $(this.editor[editor_name1].getScrollerElement()).children(':first-child').height(); + var visible_page_height = jQuery(this.editor[editor_name1].getScrollerElement()).height(); + var gutter_height = jQuery(this.editor[editor_name1].getScrollerElement()).children(':first-child').height(); var visible_page_ratio = (visible_page_height / gutter_height); var margin_ratio = (visible_page_height / gutter_height); - var lhs_scroller = $(this.editor[editor_name1].getScrollerElement()); - var rhs_scroller = $(this.editor[editor_name2].getScrollerElement()); + var lhs_scroller = jQuery(this.editor[editor_name1].getScrollerElement()); + var rhs_scroller = jQuery(this.editor[editor_name2].getScrollerElement()); var lhs_lines = this.editor[editor_name1].lineCount(); var rhs_lines = this.editor[editor_name2].lineCount(); @@ -1092,17 +1092,17 @@ $.extend(Mgly.mergely.prototype, { var dcanvas = document.getElementById(editor_name1 + '-' + editor_name2 + '-canvas'); if (dcanvas == undefined) throw 'Failed to find: ' + editor_name1 + '-' + editor_name2 + '-canvas'; - $.each($('canvas'), function () { - $(this).get(0).height = visible_page_height; + jQuery.each(jQuery('canvas'), function () { + jQuery(this).get(0).height = visible_page_height; }); - var clhs = $('#' + this.id + '-lhs-margin'); - var crhs = $('#' + this.id + '-rhs-margin'); + var clhs = jQuery('#' + this.id + '-lhs-margin'); + var crhs = jQuery('#' + this.id + '-rhs-margin'); clhs.unbind('click'); crhs.unbind('click'); var mcanvas_lhs = clhs.get(0); var mcanvas_rhs = crhs.get(0); - var lhs_xyoffset = $(clhs).offset(); - var rhs_xyoffset = $(crhs).offset(); + var lhs_xyoffset = jQuery(clhs).offset(); + var rhs_xyoffset = jQuery(crhs).offset(); var ctx = dcanvas.getContext('2d'); var ctx_lhs = mcanvas_lhs.getContext('2d'); @@ -1207,16 +1207,16 @@ $.extend(Mgly.mergely.prototype, { } }); -$.pluginMaker = function(plugin) { +jQuery.pluginMaker = function(plugin) { // add the plugin function as a jQuery plugin - $.fn[plugin.prototype.name] = function(options) { + jQuery.fn[plugin.prototype.name] = function(options) { // get the arguments - var args = $.makeArray(arguments), + var args = jQuery.makeArray(arguments), after = args.slice(1); var rc = undefined; this.each(function() { // see if we have an instance - var instance = $.data(this, plugin.prototype.name); + var instance = jQuery.data(this, plugin.prototype.name); if (instance) { // call a method on the instance if (typeof options == "string") { @@ -1235,4 +1235,4 @@ $.pluginMaker = function(plugin) { }; // make the mergely widget -$.pluginMaker(Mgly.mergely); +jQuery.pluginMaker(Mgly.mergely);