Skip to content

Commit 9484588

Browse files
committed
Merge pull request #31 from caxy/feature-table_diffing
New Feature: Table Diffing
2 parents 5c013fa + ae2b145 commit 9484588

29 files changed

+2680
-325
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
composer.lock
22
vendor/
3+
/demo/bower_components
4+
/demo/node_modules
5+
.DS_Store

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"issues": "https://github.com/caxy/php-htmldiff/issues"
2020
},
2121
"require": {
22-
"php": ">=5.3.3"
22+
"php": ">=5.3.3",
23+
"ezyang/htmlpurifier": "^4.7"
2324
},
2425
"autoload": {
2526
"psr-0": { "Caxy\\HtmlDiff": "lib/" }

demo/bower.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "php-htmldiff-demo",
3+
"dependencies": {
4+
"bootstrap": "v4.0.0-alpha.2",
5+
"angular": "1.5.0",
6+
"clipboard": "^1.5.8",
7+
"font-awesome": "^4.5.0",
8+
"angular-sanitize": "^1.5.0",
9+
"tether": "^1.2.0",
10+
"ng-ckeditor": "^0.2.1",
11+
"ckeditor": "^4.5.7",
12+
"angular-ui": "^0.4.0",
13+
"AngularJS-Toaster": "angularjs-toaster#^1.2.0",
14+
"angular-bootstrap": "^1.1.2"
15+
}
16+
}

demo/demo.controller.js

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
(function() {
2+
'use strict';
3+
4+
angular
5+
.module('demo')
6+
.controller('DemoController', DemoController);
7+
8+
DemoController.$inject = ['$q', '$http', '$sce', '$timeout'];
9+
10+
function DemoController($q, $http, $sce, $timeout) {
11+
var vm = this;
12+
13+
vm.demos = [];
14+
vm.updateDelay = 800;
15+
vm.currentTimeout = null;
16+
vm.loading = false;
17+
vm.waiting = false;
18+
vm.diffName = '';
19+
vm.currentDemo = null;
20+
vm.debugOutput = {};
21+
vm.matchThreshold = 80;
22+
vm.overrides = [];
23+
vm.legislativeOverride = null;
24+
vm.tableDiffNumber = 1;
25+
vm.tableDiffing = true;
26+
vm.editorOptions = {};
27+
vm.ckEditorEnabled = true;
28+
29+
vm.trustHtml = trustHtml;
30+
vm.reset = reset;
31+
vm.update = update;
32+
vm.swapText = swapText;
33+
vm.diffDemo = diffDemo;
34+
vm.diffOverride = diffOverride;
35+
vm.diffTableDemo = diffTableDemo;
36+
vm.updateDemo = updateDemo;
37+
vm.saveNewDemo = saveNewDemo;
38+
vm.toggleCkEditor = toggleCkEditor;
39+
40+
activate();
41+
42+
function activate() {
43+
var promises = [loadDemos(), loadOverrides()];
44+
return $q.all(promises).then(function() {
45+
46+
});
47+
}
48+
49+
function trustHtml(text) {
50+
return typeof text !== 'undefined' ? $sce.trustAsHtml(text) : '';
51+
}
52+
53+
function toggleCkEditor() {
54+
vm.ckEditorEnabled = !vm.ckEditorEnabled;
55+
}
56+
57+
function reset() {
58+
vm.oldText = '';
59+
vm.newText = '';
60+
vm.diff = '';
61+
vm.loading = false;
62+
vm.waiting = false;
63+
vm.currentDemo = null;
64+
vm.legislativeOverride = null;
65+
if (vm.currentTimeout) {
66+
$timeout.cancel(vm.currentTimeout);
67+
}
68+
}
69+
70+
function update() {
71+
if (vm.currentTimeout) {
72+
$timeout.cancel(vm.currentTimeout);
73+
}
74+
vm.currentTimeout = $timeout(function () {
75+
getDiff();
76+
}, vm.updateDelay);
77+
78+
vm.diff = null;
79+
vm.waiting = true;
80+
}
81+
82+
function swapText() {
83+
var oldText = vm.oldText;
84+
vm.oldText = vm.newText;
85+
vm.newText = oldText;
86+
87+
getDiff();
88+
}
89+
90+
function diffDemo(index) {
91+
if (typeof index === 'undefined') {
92+
index = 0;
93+
}
94+
95+
vm.oldText = vm.demos[index]['old'];
96+
vm.newText = vm.demos[index]['new'];
97+
getDiff();
98+
vm.currentDemo = vm.demos[index];
99+
vm.legislativeOverride = vm.demos[index].hasOwnProperty('legislativeOverride') ? vm.demos[index]['legislativeOverride'] : null;
100+
}
101+
102+
function diffOverride(override, index) {
103+
vm.oldText = override.old;
104+
vm.newText = override.new;
105+
vm.legislativeOverride = override.override;
106+
getDiff();
107+
vm.currentDemo = override;
108+
if (!vm.currentDemo.name) {
109+
vm.currentDemo.name = 'Override Demo ' + (index + 1);
110+
}
111+
vm.currentDemo.isOverride = true;
112+
}
113+
114+
function diffTableDemo(index) {
115+
loadTableDiff(index)
116+
.then(function(response) {
117+
vm.oldText = response.data.old;
118+
vm.newText = response.data.new;
119+
vm.legislativeOverride = null;
120+
getDiff();
121+
vm.currentDemo = null;
122+
})
123+
.catch(function(e) {
124+
console.log(e);
125+
});
126+
}
127+
128+
function updateDemo() {
129+
vm.currentDemo.old = vm.oldText;
130+
vm.currentDemo.new = vm.newText;
131+
132+
return $http.post('save_demo.php', vm.currentDemo)
133+
.then(function (response) {
134+
return response;
135+
});
136+
}
137+
138+
function saveNewDemo() {
139+
var newIndex = vm.demos.length + 1;
140+
if (vm.diffName.length === 0) {
141+
vm.diffName = 'DEMO ' + newIndex;
142+
}
143+
144+
var newDemo = {'old': vm.oldText, 'new': vm.newText, 'name': vm.diffName, 'legislativeOverride': vm.legislativeOverride};
145+
146+
vm.demos.push(newDemo);
147+
148+
return $http.post('save_demo.php', newDemo)
149+
.then(function (response) {
150+
vm.currentDemo = newDemo;
151+
152+
return vm.currentDemo;
153+
});
154+
}
155+
156+
function loadTableDiff(index) {
157+
return $http({
158+
url: 'load_table_diff.php',
159+
method: 'POST',
160+
data: {index: index},
161+
header: {'Content-Type': 'application/json; charset=UTF-8'}
162+
});
163+
}
164+
165+
function getDiff() {
166+
vm.waiting = false;
167+
vm.loading = true;
168+
vm.diff = null;
169+
$http.post('index.php', {
170+
oldText: vm.oldText,
171+
newText: vm.newText,
172+
matchThreshold: vm.matchThreshold,
173+
tableDiffing: vm.tableDiffing
174+
})
175+
.then(function (response) {
176+
vm.diff = response.data.hasOwnProperty('diff') ? response.data.diff : response.data;
177+
vm.loading = false;
178+
addDebugOutput(response.data.debug);
179+
})
180+
.catch(function (response) {
181+
console.error('Gists error', response.status, response.data);
182+
});
183+
}
184+
185+
function loadDemos() {
186+
$http.get('demos.json')
187+
.success(function (data) {
188+
vm.demos = data;
189+
});
190+
}
191+
192+
function loadOverrides() {
193+
return $http.get('diff.json')
194+
.then(function (response) {
195+
vm.overrides = response.data;
196+
197+
return vm.overrides;
198+
});
199+
}
200+
201+
function addDebugOutput(data) {
202+
angular.forEach(data, function(value, key) {
203+
data[key] = {
204+
messages: value,
205+
isCollapsed: true
206+
};
207+
});
208+
209+
vm.debugOutput = data;
210+
}
211+
}
212+
})();

0 commit comments

Comments
 (0)