liquid_feedback_frontend

view app/main/draft/diff.lua @ 1309:32cc544d5a5b

Cumulative patch for upcoming frontend version 4
author bsw/jbe
date Sun Jul 15 14:07:29 2018 +0200 (2018-07-15)
parents 02772bc49467
children 3ab878ba277e
line source
1 local old_draft_id = param.get("old_draft_id", atom.integer)
2 local new_draft_id = param.get("new_draft_id", atom.integer)
3 local initiative_id = param.get("initiative_id", atom.number)
5 if not old_draft_id
6 or not new_draft_id
7 or old_draft_id == new_draft_id
8 then
9 slot.reset_all()
10 slot.select("error", function()
11 ui.tag{ content = _"Please choose two different versions of the draft to compare" }
12 end )
13 request.redirect{
14 module = "draft", view = "list", params = {
15 initiative_id = initiative_id
16 }
17 }
18 return
19 end
21 if old_draft_id > new_draft_id then
22 old_draft_id, new_draft_id = new_draft_id, old_draft_id
23 end
25 local old_draft = Draft:by_id(old_draft_id)
26 local new_draft = Draft:by_id(new_draft_id)
28 local initiative = new_draft.initiative
30 if app.session.member then
31 initiative:load_everything_for_member_id(app.session.member_id)
32 initiative.issue:load_everything_for_member_id(app.session.member_id)
33 end
36 local old_draft_content = string.gsub(string.gsub(util.html_to_text(old_draft.content), "\n", " ###ENTER###\n"), " ", "\n")
37 local new_draft_content = string.gsub(string.gsub(util.html_to_text(new_draft.content), "\n", " ###ENTER###\n"), " ", "\n")
39 local key = multirand.string(24, "0123456789abcdefghijklmnopqrstuvwxyz")
41 local old_draft_filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "diff-" .. key .. "-old.tmp")
42 local new_draft_filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "diff-" .. key .. "-new.tmp")
44 local old_draft_file = assert(io.open(old_draft_filename, "w"))
45 old_draft_file:write(old_draft_content)
46 old_draft_file:write("\n")
47 old_draft_file:close()
49 local new_draft_file = assert(io.open(new_draft_filename, "w"))
50 new_draft_file:write(new_draft_content)
51 new_draft_file:write("\n")
52 new_draft_file:close()
54 local output, err, status = extos.pfilter(nil, "sh", "-c", "diff -a -U 1000000000 '" .. old_draft_filename .. "' '" .. new_draft_filename .. "' | grep --binary-files=text -v ^--- | grep --binary-files=text -v ^+++ | grep --binary-files=text -v ^@")
56 os.remove(old_draft_filename)
57 os.remove(new_draft_filename)
59 local last_state = "first_run"
61 local function process_line(line)
62 local state_char = string.sub(line, 1, 1)
63 local state
64 if state_char == "+" then
65 state = "added"
66 elseif state_char == "-" then
67 state = "removed"
68 elseif state_char == " " then
69 state = "unchanged"
70 end
71 local state_changed = false
72 if state ~= last_state then
73 if last_state ~= "first_run" then
74 slot.put("</span> ")
75 end
76 last_state = state
77 state_changed = true
78 slot.put("<span class=\"diff_" .. tostring(state) .. "\">")
79 end
81 line = string.sub(line, 2, #line)
82 if line ~= "###ENTER###" then
83 if not state_changed then
84 slot.put(" ")
85 end
86 --slot.put(encode.html(line))
87 slot.put(line)
88 else
89 slot.put("<br />")
90 end
91 end
93 execute.view{ module = "issue", view = "_head", params = { issue = initiative.issue } }
95 ui.grid{ content = function()
96 ui.cell_main{ content = function()
97 ui.container{ attr = { class = "mdl-card mdl-card__fullwidth mdl-shadow--2dp" }, content = function()
99 ui.container{ attr = { class = "mdl-card__title mdl-card--border" }, content = function ()
100 ui.heading {
101 attr = { class = "mdl-card__title-text" },
102 content = function()
103 ui.link{
104 module = "initiative", view = "show", id = initiative.id,
105 content = initiative.display_name
106 }
107 end
108 }
109 ui.container{ content = _("Comparision of revisions #{id1} and #{id2}", {
110 id1 = old_draft.id,
111 id2 = new_draft.id
112 } ) }
113 end }
115 if app.session.member_id and not new_draft.initiative.revoked then
116 local supporter = app.session.member:get_reference_selector("supporters")
117 :add_where{ "initiative_id = ?", new_draft.initiative_id }
118 :optional_object_mode()
119 :exec()
120 if supporter and supporter.draft_id == old_draft.id and new_draft.id == initiative.current_draft.id then
121 ui.container {
122 attr = { class = "mdl-card__content mdl-card--no-bottom-pad mdl-card--notice" },
123 content = _"The draft of this initiative has been updated!"
124 }
125 ui.container {
126 attr = { class = "mdl-card__actions mdl-card--action-border mdl-card--notice" },
127 content = function ()
128 ui.link{
129 attr = { class = "mdl-button mdl-js-button mdl-button--raised" },
130 text = _"refresh my support",
131 module = "initiative",
132 action = "add_support",
133 id = new_draft.initiative.id,
134 params = { draft_id = new_draft.id },
135 routing = {
136 default = {
137 mode = "redirect",
138 module = "initiative",
139 view = "show",
140 id = new_draft.initiative.id
141 }
142 }
143 }
145 slot.put(" &nbsp; ")
147 ui.link{
148 attr = { class = "mdl-button mdl-js-button mdl-button--raised" },
149 text = _"remove my support",
150 module = "initiative",
151 action = "remove_support",
152 id = new_draft.initiative.id,
153 routing = {
154 default = {
155 mode = "redirect",
156 module = "initiative",
157 view = "show",
158 id = new_draft.initiative.id
159 }
160 }
161 }
163 slot.put(" &nbsp; ")
165 ui.link{
166 attr = { class = "mdl-button mdl-js-button" },
167 text = _"cancel",
168 module = "initiative",
169 view = "show",
170 id = new_draft.initiative.id,
171 }
172 end
173 }
174 end
175 end
177 ui.container {
178 attr = { class = "draft mdl-card__content mdl-card--border" },
179 content = function ()
180 if not status then
181 ui.field.text{ value = _"The drafts do not differ" }
182 else
183 ui.container{
184 tag = "div",
185 attr = { class = "diff" },
186 content = function()
187 output = output:gsub("[^\n\r]+", function(line)
188 process_line(line)
189 end)
190 end
191 }
192 end
193 end
194 }
195 end }
196 end }
197 ui.cell_sidebar{ content = function()
199 execute.view{ module = "issue", view = "_sidebar", params = {
200 initiative = initiative,
201 issue = initiative.issue
202 } }
204 execute.view {
205 module = "issue", view = "_sidebar_whatcanido",
206 params = { initiative = initiative }
207 }
209 execute.view {
210 module = "issue", view = "_sidebar_members", params = {
211 issue = initiative.issue, initiative = initiative
212 }
213 }
214 end }
215 end }

Impressum / About Us