/*! * jQuery-Merge-for-php-diff - A jQuery plugin for handling the conflicts between two documents. * v0.3.2 - 2014-05-13 3:53:31 PM UTC * Copyright (c) 2014 Hannes Diercks ; License: MIT */ var $ = jQuery; (function($) { 'use strict'; var pluginName = 'phpdiffmerge', defaults = { left: '', right: '', debug: false, pupupResult: false, pupupSources: false, merged: $.noop() }, count = 0; /* PHPDiffMerge constructor */ function PHPDiffMerge(element, options) { var self = this; self._id = 1 + count++; self.$el = $(element); self.options = $.extend({}, defaults, options); self._defaults = defaults; self._name = pluginName; /*** PUBLIC VARIABLES ***/ /* The conflict elements */ self.$conflicts = $(); /* Collection of conflict classes */ self.conflicts = []; /* Counters of conflicts left */ self.toResolve = 0; self.toMerge = 0; /* Indicator if the diff is inline or side-by-side */ self.inline = false; /* The resulting merge */ self.result = []; self.lineOffset = 0; self._tmpLineOffset = 0; self._init(); } PHPDiffMerge.prototype = { /*** CONSTRUCTION ***/ _init: function() { var self = this; /* Say Hello */ self._debug('PHPDiffMerge about to be initiated with options:', self.options); /* Find conflict containers */ self.$conflicts = self.$el.find('.ChangeReplace, .ChangeDelete, .ChangeInsert'); /* Set the counters */ self.toResolve = self.$conflicts.length; /* Check if we have enough data to work */ if (self.toResolve <= 0 || (self.options.left === '' && self.options.right === '')) { self._debug('Nothing to merge or merge sources not available - ' + 'Please submit left and right on plug-in initiation!'); return false; } /* Check if table style is inline */ this.inline = self.$el.hasClass('DifferencesInline'); self._ensurePresenceOfMergeButton(); /* Initiate Conflicts */ $.each(self.$conflicts, function() { self.conflicts.push(new Conflict(this, self)); }); /* Register event listeners for completion actions */ self.$conflicts.on('xiphe_phpdiffmerge_resolved', $.proxy(self._conflictResolved, self)); self.$conflicts.on('xiphe_phpdiffmerge_merged', $.proxy(self._conflictMerged, self)); self.$conflicts.on('xiphe_phpdiffmerge_merged', $.proxy(self._updateLineOffset, self)); self._debug('PHPDiffMerge initiated', self); }, /*** PUBLIC METHODS ***/ /** * Select all changes on the right side. */ useRight: function() { var self = this; self.$conflicts.find('td.Right').click(); if (self.$el.hasClass('DifferencesInline')) { self.$el.find('.ChangeDelete td.Left').click().click(); } }, /** * Selects all changes on the right. */ useLeft: function() { var self = this; self.$conflicts.find('td.Left').click(); if (self.$el.hasClass('DifferencesInline')) { self.$el.find('.ChangeInsert td.Right').click().click(); } }, /** * Start the merge process */ merge: function(event) { var self = this; if (typeof event !== 'undefined' && event !== null) { event.preventDefault(); } /* Don't work, if any conflicts are unresolved */ if (self.options.button.attr('disabled') === 'disabled') { self._debug('Unable to merge: not all conflicts have been resolved.'); return; } /* Initiate the end by cloning the left side. */ self.result = self.options.left.slice(0); /* Reset line offset */ self.lineOffset = 0; /* Reset the todo counter */ self.toMerge = self.$conflicts.length; for (var i = 0; i < self.conflicts.length; i++) { self.conflicts[i].merge(); } }, /** * change options AFTER initialization * * @param {object} options a plain options object */ option: function(options) { var self = this; if ($.isPlainObject(options)) { self.options = $.extend(true, self.options, options); } }, /*** PROTECTED METHODS ***/ /* * Check if a merge button is available or generate one. */ _ensurePresenceOfMergeButton: function() { var self = this; if (typeof self.options.button === 'undefined' || !$(self.options.button).length) { self.options.button = $('