Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "data:text/html,
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- body {
- font-family: Verdana;
- }
- h1 {
- clear: both;
- }
- label {
- display: block;
- }
- textarea {
- min-height: 8em;
- width: 100%;
- }
- .col {
- width: 48%;
- margin: 0 1% 1em 1%;
- float: left;
- }
- .col:after { /* clearfix */
- content: \"\";
- display: table;
- clear: both;
- }
- .diff {
- display: block;
- background-color: #eee;
- border-radius: 0.5em;
- padding: 1em;
- }
- .objectDiff {
- unicode-bidi: embed;
- font-family: monospace;
- white-space: pre;
- }
- .objectDiff .objectInner {
- padding-left: 2em;
- display: block;
- }
- .objectDiff .prop {
- display: block;
- }
- .objectDiff .added,
- .objectDiff.added {
- color: green;
- background-color: #E2F6EA;
- /* background-color: rgba(0,255,100,0.08); */
- }
- .objectDiff .removed,
- .objectDiff.removed {
- color: red;
- background-color: #F6E2E6;
- /* background-color: rgba(255,0,50,0.08); */
- }
- </style>
- </head>
- <script>
- (function ($) {
- var _isJQuery = function($elt){
- return ($elt instanceof $)
- };
- var _objectDiff = {
- NODIFF: 1,
- DIFF: 3,
- VALUEDIFF: 2,
- oldObjectClass: 'removed',
- newObjectClass: 'added',
- noDifferenceClass: 'nodiff',
- loopOverObject: function(obj, callback){
- var key, i = 0;
- for (key in obj) {
- callback(key, obj[key], i);
- i++;
- }
- },
- getDiff: function(obj1, obj2){
- var diffObj = {};
- var key;
- // If we're not comparing objects, then just compare values...
- if (obj1 === null || typeof obj1 !== 'object') {
- // If obj2 is also a primitive (string, number, null, boolean, etc.) and matches...
- if (obj1 === obj2) {
- return this.NODIFF;
- } else {
- return this.DIFF;
- }
- }
- for (key in obj1) {
- //console.log(key);
- if (obj2 === null || typeof obj2[key] === 'undefined') {
- diffObj[key] = this.DIFF;
- } else {
- if (typeof obj1[key] === 'object' && typeof obj2[key] === 'object'
- && obj1[key] !== null)
- {
- // Do some recursion
- diffObj[key] = this.getDiff(obj1[key], obj2[key]);
- } else {
- //console.log(typeof obj1[key], typeof obj2[key]);
- if (obj1[key] == obj2[key]) {
- diffObj[key] = this.NODIFF;
- } else {
- diffObj[key] = this.VALUEDIFF;
- }
- }
- }
- }
- return diffObj;
- },
- // Do the same thing as getDiff, but chop out any properties that have NODIFF
- getOnlyDiff: function(obj1, obj2){
- var diff = this.getDiff(obj1, obj2);
- var key;
- for (key in diff) {
- if (diff[key] == this.NODIFF) {
- delete diff[key];
- }
- };
- return diff;
- },
- getDiffArray: function(obj1, obj2){
- var diff = this.getDiff(obj1, obj2);
- return _getDiffArray(diff, obj1, []);
- },
- _getDiffArray: function(diff, obj, arr) {
- var key;
- for (key in diff) {
- if (diff[key] == this.DIFF || diff[key] == this.VALUEDIFF) {
- arr.push({ key: key, value: obj[key] });
- } else if (typeof diff[key] === 'object') {
- this._getDiffArray(diff[key], obj[key], arr)
- }
- }
- return arr;
- },
- getDiffHTML: function(obj, diffObj, diffClass, objClass){
- var o = this,
- isInsideArray = false,
- html = '',
- numOfProperties = 0,
- brackets = ['{', '}'];
- if (typeof objClass !== 'string') {
- objClass = '';
- }
- isInsideArray = (obj instanceof Array);
- if (isInsideArray) {
- brackets = ['[', ']'];
- }
- //console.log(obj);
- for (key in obj) { numOfProperties++; }
- this.loopOverObject(obj, function(key, value, i){
- var type = typeof value;
- var isArray = (value instanceof Array);
- // Diff object
- var diffValue = diffObj[key]; // (diffObj === null) ? null : diffObj[key];
- // Classes
- var keyClasses = 'key';
- var valueClasses = 'value';
- var seperatorClasses = 'sep';
- // Strings
- var valueString = '';
- var keyString = '';
- // If there's no diff value, then skip...
- //if (typeof diffValue === 'undefined') {
- // return;
- //}
- if (isInsideArray) {
- keyString = '';
- } else {
- keyString = '\"' + key + '\": ';
- }
- if (type === 'string') {
- valueString = '\"' + value + '\"';
- } else if (type === 'object' && value !== null) {
- if (typeof diffValue === 'object' && diffValue !== null) {
- valueString = o.getDiffHTML(value, diffObj[key], diffClass);
- } else {
- valueString = o.getDiffHTML(value, {}, diffClass, diffClass);
- }
- } else {
- valueString = '' + value;
- }
- // Get classes
- if (diffValue == o.DIFF) {
- keyClasses += ' ' + diffClass;
- seperatorClasses += ' ' + diffClass;
- }
- if (diffValue == o.DIFF || diffValue == o.VALUEDIFF) {
- valueClasses += ' ' + diffClass;
- }
- if (diffValue == o.NODIFF) {
- keyClasses += ' ' + o.noDifferenceClass;
- valueClasses += ' ' + o.noDifferenceClass;
- seperatorClasses += ' ' + o.noDifferenceClass;
- }
- // Add to the HTML
- html += '<span class=\"prop\">';
- if (keyString.length > 0) {
- html += '<span class=\"' + keyClasses + '\">' + keyString + '</span>';
- }
- if (valueString.length > 0) {
- html += '<span class=\"' + valueClasses + '\">' + valueString + '</span>';
- }
- if ((i + 1) < numOfProperties) {
- html += '<span class=\"' + seperatorClasses + '\">,</span>';
- }
- html += '</span>';
- });
- // Check for primitives (nulls, numbers, strings, etc.)
- if (obj === null || typeof obj !== 'object') {
- if (obj === null) {
- objClass += ' null';
- html = 'null';
- } else if (typeof obj === 'string') {
- html = '\"' + obj + '\"';
- } else {
- html = obj;
- }
- if (diffObj === this.DIFF || diffObj === this.VALUEDIFF) {
- objClass += ' ' + diffClass;
- }
- } else {
- html = (
- brackets[0]
- + '<span class=\"objectInner ' + objClass + '\">'
- + html
- + '</span>'
- + brackets[1]
- );
- }
- return '<span class=\"objectDiff ' + objClass + '\">' + html + '</span>';
- },
- writeDiff: function(obj1, obj2, container, styleClass, onlyDifferences){
- var diff = this.getDiff(obj1, obj2); //(onlyDifferences) ? this.getOnlyDiff(obj1, obj2) : this.getDiff(obj1, obj2);
- var h = this.getDiffHTML(obj1, diff, styleClass);
- //console.log(\"writeDiff...\ndiff:\", diff, \"\nhtml:\", h, \"\ncontainer:\", container);
- $(container).html( h );
- },
- writeBothDiff: function(oldObj, newObj, oldContainer, newContainer, onlyDifferences){
- //if (onlyDifferences) { console.log('Writing both diffs -- only the diffs, not properties that are the same.')};
- $(oldContainer).empty();
- $(newContainer).empty();
- this.writeDiff(oldObj, newObj, oldContainer, this.oldObjectClass, onlyDifferences);
- this.writeDiff(newObj, oldObj, newContainer, this.newObjectClass, onlyDifferences);
- if (onlyDifferences) {
- //console.log($(oldContainer).add($(newContainer)).find('.' + this.noDifferenceClass).length);
- $(oldContainer).add($(newContainer)).find('.' + this.noDifferenceClass).hide();
- this.addViewButton(oldContainer);
- this.addViewButton(newContainer);
- }
- },
- addViewButton: function(container){
- var o = this;
- var $viewNoDiffButton = $('<button type=\"button\" class=\"viewNoDiff\">View all properties</button>')
- .click(function(e){
- $(this).hide(300).parent().find('.' + o.noDifferenceClass).slideDown(400);
- });
- $(container).append($viewNoDiffButton);
- },
- getObject: function(obj){
- var objString, finalObj;
- //console.log(\"getting Object:\", obj);
- if (_isJQuery(obj)) {
- if (typeof obj.data('objectdiff_json') === 'undefined') {
- objString = $.trim(obj.html());
- } else {
- objString = obj.data('objectdiff_json');
- }
- } else if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean') {
- objString = obj;
- } else {
- objString = JSON.stringify(obj);
- }
- //console.log(\"getting object: \", objString);
- try {
- finalObj = JSON.parse(objString);
- } catch (err) {
- if (objString == 'null') {
- finalObj = null;
- } else {
- finalObj = objString;
- }
- console.log(\"Could not JSON-parse: \", objString, \"\n\", err, \"\nUsing raw value instead instead.\", finalObj, \"(\" + typeof finalObj + \")\");
- }
- return finalObj;
- },
- getJQueryContainer: function(elt){
- if (_isJQuery(elt)) {
- if (elt.length > 1) {
- console.warn(\"Only using first element for objectDiff.\", elt);
- }
- return elt.first();
- } else {
- return $([]);
- }
- }
- };
- // Expose the functionality as jQuery plugin as objectDiff
- $.objectDiff = function(base, comparison, options){
- var baseObj, comparisonObj, baseContainer, comparisonContainer;
- // If there's only one argument and it's not a jquery object, then use it as the options
- if (arguments.length == 1 && typeof arguments[0] === 'object' && (!_isJQuery(arguments[0]))) {
- options = arguments[0];
- base = $([]);
- comparison = $([]);
- }
- // Some defaults for options
- options = $.extend({
- baseObject: false,
- comparisonObject: false,
- baseContainer: false,
- comparisonContainer: false,
- onlyDifferences: false
- }, options);
- // Use the objects and containers from the options...
- // or, if not specified, then get the object and containers from base and comparison arguments
- baseObj = (options.baseObject) ? options.baseObject : _objectDiff.getObject(base);
- comparisonObj = (options.comparisonObject) ? options.comparisonObject : _objectDiff.getObject(comparison);
- if (options.baseContainer && _isJQuery(options.baseContainer)) {
- base = options.baseContainer;
- }
- baseContainer = _objectDiff.getJQueryContainer(base);
- if (options.comparisonContainer && _isJQuery(options.comparisonContainer)) {
- comparison = options.comparisonContainer;
- }
- comparisonContainer = _objectDiff.getJQueryContainer(comparison);
- // Save the JSON object for later in case objectDiff is run again
- //console.log(\"Adding data\", baseObj, comparisonObj, baseContainer, comparisonContainer);
- baseContainer.data('objectdiff_json', JSON.stringify(baseObj));
- comparisonContainer.data('objectdiff_json', JSON.stringify(comparisonObj));
- // Write the diff html to the containers
- _objectDiff.writeBothDiff(
- baseObj, comparisonObj,
- baseContainer, comparisonContainer,
- options.onlyDifferences
- );
- };
- $.fn.objectDiff = function(comparison, options) {
- $.objectDiff(this, comparison, options);
- return this;
- };
- })(jQuery);
- //==== Example-specific code below
- var $oldTextArea = $('#oldText');
- var $newTextArea = $('#newText');
- var $diffOld = $('#diffOld');
- var $diffNew = $('#diffNew');
- $oldTextArea.add($newTextArea).on('change', function(e){
- $diffOld.html($oldTextArea.val()).removeData('objectdiff_json');
- $diffNew.html($newTextArea.val()).removeData('objectdiff_json');
- console.log(\"\n\");
- $diffOld.objectDiff($diffNew);
- }).trigger('change');
- </script>
- </head>
- <body>
- <h1>Review the differences</h1>
- <h2>JSON Objects</h2>
- <div class=\"col\">
- <label>Old</label>
- <textarea id=\"oldText\">"
- & $$oldState
- &"</textarea>
- </div>
- <div class=\"col\">
- <label>New</label>
- <textarea id=\"newText\">"
- & $$newState
- & "</textarea>
- </div>
- <div class=\"col\">
- <h2>Old</h2>
- <div class=\"diff\">
- <div id=\"diffOld\"></div>
- </div>
- </div>
- <div class=\"col\">
- <h2>New</h2>
- <div class=\"diff\">
- <div id=\"diffNew\"></div>
- </div>
- </div>
- </body>
- </html>"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement