Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / caxy / php-htmldiff / demo / demo.controller.js
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 })();