liquid_feedback_frontend
view app/main/draft/diff.lua @ 1231:e7fc3fed1593
Refactored password reset functionality
| author | bsw | 
|---|---|
| date | Tue Dec 01 18:12:00 2015 +0100 (2015-12-01) | 
| parents | 02772bc49467 | 
| children | 32cc544d5a5b | 
 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 execute.view{ module = "issue", view = "_sidebar_state", params = {
    37   initiative = initiative
    38 } }
    40 execute.view { 
    41   module = "issue", view = "_sidebar_issue", 
    42   params = {
    43     issue = initiative.issue,
    44     highlight_initiative_id = initiative.id
    45   }
    46 }
    48 execute.view {
    49   module = "issue", view = "_sidebar_whatcanido",
    50   params = { initiative = initiative }
    51 }
    53 execute.view { 
    54   module = "issue", view = "_sidebar_members", params = {
    55     issue = initiative.issue, initiative = initiative
    56   }
    57 }
    61 execute.view {
    62   module = "issue", view = "_head", params = {
    63     issue = initiative.issue
    64   }
    65 }
    69 local old_draft_content = string.gsub(string.gsub(old_draft.content, "\n", " ###ENTER###\n"), " ", "\n")
    70 local new_draft_content = string.gsub(string.gsub(new_draft.content, "\n", " ###ENTER###\n"), " ", "\n")
    72 local key = multirand.string(24, "0123456789abcdefghijklmnopqrstuvwxyz")
    74 local old_draft_filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "diff-" .. key .. "-old.tmp")
    75 local new_draft_filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "diff-" .. key .. "-new.tmp")
    77 local old_draft_file = assert(io.open(old_draft_filename, "w"))
    78 old_draft_file:write(old_draft_content)
    79 old_draft_file:write("\n")
    80 old_draft_file:close()
    82 local new_draft_file = assert(io.open(new_draft_filename, "w"))
    83 new_draft_file:write(new_draft_content)
    84 new_draft_file:write("\n")
    85 new_draft_file:close()
    87 local output, err, status = extos.pfilter(nil, "sh", "-c", "diff -U 1000000000 '" .. old_draft_filename .. "' '" .. new_draft_filename .. "' | grep -v ^--- | grep -v ^+++ | grep -v ^@")
    89 os.remove(old_draft_filename)
    90 os.remove(new_draft_filename)
    92 local last_state = "first_run"
    94 local function process_line(line)
    95   local state_char = string.sub(line, 1, 1)
    96   local state
    97   if state_char == "+" then
    98     state = "added"
    99   elseif state_char == "-" then
   100     state = "removed"
   101   elseif state_char == " " then
   102     state = "unchanged"
   103   end
   104   local state_changed = false
   105   if state ~= last_state then
   106     if last_state ~= "first_run" then
   107       slot.put("</span> ")
   108     end
   109     last_state = state
   110     state_changed = true
   111     slot.put("<span class=\"diff_" .. tostring(state) .. "\">")
   112   end
   114   line = string.sub(line, 2, #line)
   115   if line ~= "###ENTER###" then
   116     if not state_changed then
   117       slot.put(" ")
   118     end
   119     slot.put(encode.html(line))
   120   else
   121     slot.put("<br />")
   122   end
   123 end
   125 ui.section( function()
   126   ui.sectionHead( function()
   127     ui.link{
   128       module = "initiative", view = "show", id = initiative.id,
   129       content = function ()
   130         ui.heading { 
   131           level = 1,
   132           content = initiative.display_name
   133         }
   134       end
   135     }
   136     ui.heading{ level = 2, content = _("Comparision of revisions #{id1} and #{id2}", {
   137       id1 = old_draft.id,
   138       id2 = new_draft.id 
   139     } ) }
   140   end )
   142   if app.session.member_id and not new_draft.initiative.revoked then
   143     local supporter = app.session.member:get_reference_selector("supporters")
   144       :add_where{ "initiative_id = ?", new_draft.initiative_id }
   145       :optional_object_mode()
   146       :exec()
   147     if supporter and supporter.draft_id ~= new_draft.id then
   148       ui.sectionRow("draft_updated_info", function()
   149         ui.container{ 
   150           attr = { class = "info" },
   151           content = _"The draft of this initiative has been updated!"
   152         }
   153         slot.put(" ")
   154         ui.link{
   155           text   = _"refresh my support",
   156           module = "initiative",
   157           action = "add_support",
   158           id     = new_draft.initiative.id,
   159           params = { draft_id = new_draft.id },
   160           routing = {
   161             default = {
   162               mode = "redirect",
   163               module = "initiative",
   164               view = "show",
   165               id = new_draft.initiative.id
   166             }
   167           }
   168         }
   170         slot.put(" · ")
   172         ui.link{
   173           text   = _"remove my support",
   174           module = "initiative",
   175           action = "remove_support",
   176           id     = new_draft.initiative.id,
   177           routing = {
   178             default = {
   179               mode = "redirect",
   180               module = "initiative",
   181               view = "show",
   182               id = new_draft.initiative.id
   183             }
   184           }
   185         }        
   187       end )
   188     end
   189   end
   191   ui.sectionRow( function()
   193     if not status then
   194       ui.field.text{ value = _"The drafts do not differ" }
   195     else
   196       ui.container{
   197         tag = "div",
   198         attr = { class = "diff" },
   199         content = function()
   200           output = output:gsub("[^\n\r]+", function(line)
   201             process_line(line)
   202           end)
   203         end
   204       }
   205     end 
   207   end )
   208 end )
