mirror of
https://github.com/wickedest/Mergely.git
synced 2026-04-01 10:18:36 +08:00
Using jQuery instead of $ for compatability with other libraries.
This commit is contained in:
104
lib/mergely.js
104
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, {
|
||||
loaded: function() { },
|
||||
height: function(h) { return h - 20; },
|
||||
width: function(w) { return w; },
|
||||
resize: function() {
|
||||
var w = $(el).parent().width();
|
||||
resize: function() {
|
||||
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 content_height = h;
|
||||
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' });
|
||||
@@ -385,29 +385,29 @@ $.extend(Mgly.mergely.prototype, {
|
||||
this.lhs_cmsettings = {};
|
||||
this.rhs_cmsettings = {};
|
||||
|
||||
// save this element for faster queries
|
||||
// save this element for faster queries
|
||||
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);
|
||||
// save options if there are any
|
||||
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
|
||||
// bind if the element is destroyed
|
||||
this.element.bind("destroyed", jQuery.proxy(this.teardown, this));
|
||||
|
||||
// save this instance in jQuery data
|
||||
// save this instance in jQuery data
|
||||
jQuery.data(el, this.name, this);
|
||||
|
||||
this._setup(el);
|
||||
},
|
||||
// bind events to this instance's methods
|
||||
bind: function() {
|
||||
bind: function() {
|
||||
var rhstx = jQuery('#' + this.id + '-rhs').get(0);
|
||||
if (!rhstx) {
|
||||
console.error('rhs textarea not defined - Mergely not initialized properly');
|
||||
return;
|
||||
}
|
||||
}
|
||||
var lhstx = jQuery('#' + this.id + '-lhs').get(0);
|
||||
if (!rhstx) {
|
||||
console.error('lhs textarea not defined - Mergely not initialized properly');
|
||||
@@ -435,7 +435,7 @@ $.extend(Mgly.mergely.prototype, {
|
||||
self.editor[self.id + '-lhs'].refresh();
|
||||
self.editor[self.id + '-rhs'].refresh();
|
||||
self._changing(self.id + '-lhs', self.id + '-rhs');
|
||||
}
|
||||
}
|
||||
jQuery(window).resize(
|
||||
function () {
|
||||
if (sz_timeout1) clearTimeout(sz_timeout1);
|
||||
@@ -477,7 +477,7 @@ $.extend(Mgly.mergely.prototype, {
|
||||
re.centerOnCursor();
|
||||
}
|
||||
},
|
||||
options: function(opts) {
|
||||
options: function(opts) {
|
||||
jQuery.extend(this.settings, opts);
|
||||
},
|
||||
swap: function() {
|
||||
@@ -536,8 +536,8 @@ $.extend(Mgly.mergely.prototype, {
|
||||
var d = new Mgly.diff(lhs, rhs, retain_lines = true);
|
||||
return d.normal_form();
|
||||
},
|
||||
_setup: function(el) {
|
||||
$(this.element).hide();//hide
|
||||
_setup: function(el) {
|
||||
jQuery(this.element).hide();//hide
|
||||
this.id = jQuery(el).attr('id');
|
||||
var height = this.settings.editor_height;
|
||||
var width = this.settings.editor_width;
|
||||
@@ -547,7 +547,7 @@ $.extend(Mgly.mergely.prototype, {
|
||||
this.cursor = [];
|
||||
this.change_exp = new RegExp(/(\d+(?:,\d+)?)([acd])(\d+(?:,\d+)?)/);
|
||||
var merge_lhs_button;
|
||||
var merge_rhs_button;
|
||||
var merge_rhs_button;
|
||||
if (jQuery.button != undefined) {
|
||||
//jquery ui
|
||||
merge_lhs_button = '<button title="Merge left"></button>';
|
||||
@@ -558,24 +558,24 @@ $.extend(Mgly.mergely.prototype, {
|
||||
var style = 'width:1em;height:1em;background-color:#888;cursor:pointer;text-align:center;color:#eee;border:1px solid: #222;margin-right:5px;';
|
||||
merge_lhs_button = '<div style="' + style + '" title="Merge left"><</div>';
|
||||
merge_rhs_button = '<div style="' + style + '" title="Merge right">></div>';
|
||||
}
|
||||
this.merge_rhs_button = $(merge_rhs_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($('<div class="mergely-margin" style="height: ' + height + '"><canvas id="' + this.id + '-lhs-margin" width="8px" height="' + height + '"></canvas></div>'));
|
||||
$(this.element).append($('<div style="width:' + width + '; height:' + height + '" id="' + this.id + '-editor-lhs" class="mergely-column"><textarea style="" id="' + this.id + '-lhs"></textarea></div>'));
|
||||
$(this.element).append($('<div class="mergely-canvas" style="height: ' + height + '"><canvas id="' + this.id + '-lhs-' + this.id + '-rhs-canvas" style="width:28px" width="28px" height="' + height + '"></canvas></div>'));
|
||||
$(this.element).append($('<div style="width:' + width + '; height:' + height + '" id="' + this.id + '-editor-rhs" class="mergely-column"><textarea style="" id="' + this.id + '-rhs"></textarea></div>'));
|
||||
// create the textarea and canvas elements
|
||||
jQuery(this.element).append(jQuery('<div class="mergely-margin" style="height: ' + height + '"><canvas id="' + this.id + '-lhs-margin" width="8px" height="' + height + '"></canvas></div>'));
|
||||
jQuery(this.element).append(jQuery('<div style="width:' + width + '; height:' + height + '" id="' + this.id + '-editor-lhs" class="mergely-column"><textarea style="" id="' + this.id + '-lhs"></textarea></div>'));
|
||||
jQuery(this.element).append(jQuery('<div class="mergely-canvas" style="height: ' + height + '"><canvas id="' + this.id + '-lhs-' + this.id + '-rhs-canvas" style="width:28px" width="28px" height="' + height + '"></canvas></div>'));
|
||||
jQuery(this.element).append(jQuery('<div style="width:' + width + '; height:' + height + '" id="' + this.id + '-editor-rhs" class="mergely-column"><textarea style="" id="' + this.id + '-rhs"></textarea></div>'));
|
||||
jQuery(this.element).append(jQuery('<div class="mergely-margin" style="height: ' + height + '"><canvas id="' + this.id + '-rhs-margin" width="8px" height="' + height + '"></canvas></div>'));
|
||||
//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; }';
|
||||
}
|
||||
}
|
||||
jQuery('<style type="text/css">' + cmstyle + '</style>').appendTo('head');
|
||||
this.bind();
|
||||
if (this.settings.lhs) this.settings.lhs( this.editor[this.id + '-lhs'].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']};
|
||||
var ed = {lhs:this.editor[this.id + '-lhs'], rhs:this.editor[this.id + '-rhs']};
|
||||
jQuery('.merge-button').live('click', function(ev){
|
||||
console.log('lhs hover over', ev, this);
|
||||
// side of mouseenter
|
||||
var side = 'rhs';
|
||||
var oside = 'lhs';
|
||||
var oside = 'lhs';
|
||||
var parent = jQuery(this).parents('#' + self.id + '-editor-lhs');
|
||||
if (parent.length) {
|
||||
side = 'lhs';
|
||||
@@ -602,7 +602,7 @@ $.extend(Mgly.mergely.prototype, {
|
||||
|
||||
// get the change id
|
||||
var cid = null;
|
||||
var info = ed[side].lineInfo(pos.line);
|
||||
var info = ed[side].lineInfo(pos.line);
|
||||
jQuery.each(info.bgClass.split(' '), function(i, clazz) {
|
||||
console.log('clazz', i, clazz);
|
||||
if (clazz.indexOf('cid-') == 0) {
|
||||
@@ -642,7 +642,7 @@ $.extend(Mgly.mergely.prototype, {
|
||||
// scrolling one side causes the other to event - ignore it
|
||||
self._skipscroll = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
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, {
|
||||
this.trace('scroll', 'scroll', scroll);
|
||||
if (scroll) {
|
||||
// scroll the other side
|
||||
this.trace('scroll', 'scrolling other side', top_to - top_adjust);
|
||||
this.trace('scroll', 'scrolling other side', top_to - top_adjust);
|
||||
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, {
|
||||
//var topnode = this.element.find('.CodeMirror-gutter-text pre').first();
|
||||
//this.em_height = topnode.get(0).offsetHeight;
|
||||
// this is the distance from the top of the screen
|
||||
|
||||
|
||||
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 = 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;
|
||||
this.draw_lhs_min = 0.5;
|
||||
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, {
|
||||
if (!c.width()) {
|
||||
console.error('canvas width is 0');
|
||||
return;
|
||||
}
|
||||
}
|
||||
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;
|
||||
@@ -850,7 +850,7 @@ $.extend(Mgly.mergely.prototype, {
|
||||
}
|
||||
return changes;
|
||||
},
|
||||
_markup_changes: function (editor_name1, editor_name2, changes) {
|
||||
_markup_changes: function (editor_name1, editor_name2, changes) {
|
||||
jQuery('.merge-button').remove(); // clear
|
||||
|
||||
var self = this;
|
||||
@@ -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();
|
||||
_draw_diff: function(editor_name1, editor_name2, changes) {
|
||||
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 margin_ratio = (visible_page_height / gutter_height);
|
||||
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, {
|
||||
this.trace('draw', 'rhs-scroller-top', rhs_scroller.scrollTop());
|
||||
|
||||
var dcanvas = document.getElementById(editor_name1 + '-' + editor_name2 + '-canvas');
|
||||
if (dcanvas == undefined) throw 'Failed to find: ' + editor_name1 + '-' + editor_name2 + '-canvas';
|
||||
$.each($('canvas'), function () {
|
||||
if (dcanvas == undefined) throw 'Failed to find: ' + editor_name1 + '-' + editor_name2 + '-canvas';
|
||||
jQuery.each(jQuery('canvas'), function () {
|
||||
jQuery(this).get(0).height = visible_page_height;
|
||||
});
|
||||
var clhs = $('#' + this.id + '-lhs-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 mcanvas_rhs = crhs.get(0);
|
||||
var lhs_xyoffset = jQuery(clhs).offset();
|
||||
var rhs_xyoffset = jQuery(crhs).offset();
|
||||
|
||||
var ctx = dcanvas.getContext('2d');
|
||||
@@ -1207,16 +1207,16 @@ $.extend(Mgly.mergely.prototype, {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
jQuery.pluginMaker = function(plugin) {
|
||||
// add the plugin function as a jQuery plugin
|
||||
// add the plugin function as a jQuery plugin
|
||||
jQuery.fn[plugin.prototype.name] = function(options) {
|
||||
// get the arguments
|
||||
// get the arguments
|
||||
var args = jQuery.makeArray(arguments),
|
||||
after = args.slice(1);
|
||||
var rc = undefined;
|
||||
this.each(function() {
|
||||
// see if we have an instance
|
||||
// see if we have an instance
|
||||
var instance = jQuery.data(this, plugin.prototype.name);
|
||||
if (instance) {
|
||||
// call a method on the instance
|
||||
@@ -1235,4 +1235,4 @@ $.pluginMaker = function(plugin) {
|
||||
};
|
||||
};
|
||||
|
||||
// make the mergely widget
|
||||
// make the mergely widget
|
||||
|
||||
Reference in New Issue
Block a user