liquid_feedback_frontend
view app/main/draft/diff.lua @ 1124:9ea935c67a61
Allow custom draft template
| author | bsw | 
|---|---|
| date | Wed Dec 24 12:08:26 2014 +0100 (2014-12-24) | 
| parents | 701a5cf6b067 | 
| children | b79085a2f92e | 
 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   local tmp = old_draft_id
    23   old_draft_id = new_draft_id
    24   new_draft_id = tmp
    25 end
    27 local old_draft = Draft:by_id(old_draft_id)
    28 local new_draft = Draft:by_id(new_draft_id)
    30 local initiative = new_draft.initiative
    32 if app.session.member then
    33   initiative:load_everything_for_member_id(app.session.member_id)
    34   initiative.issue:load_everything_for_member_id(app.session.member_id)
    35 end
    38 execute.view{ module = "issue", view = "_sidebar_state", params = {
    39   initiative = initiative
    40 } }
    42 execute.view { 
    43   module = "issue", view = "_sidebar_issue", 
    44   params = {
    45     issue = initiative.issue,
    46     highlight_initiative_id = initiative.id
    47   }
    48 }
    50 execute.view {
    51   module = "issue", view = "_sidebar_whatcanido",
    52   params = { initiative = initiative }
    53 }
    55 execute.view { 
    56   module = "issue", view = "_sidebar_members", params = {
    57     issue = initiative.issue, initiative = initiative
    58   }
    59 }
    63 execute.view {
    64   module = "issue", view = "_head", params = {
    65     issue = initiative.issue
    66   }
    67 }
    71 local old_draft_content = string.gsub(string.gsub(old_draft.content, "\n", " ###ENTER###\n"), " ", "\n")
    72 local new_draft_content = string.gsub(string.gsub(new_draft.content, "\n", " ###ENTER###\n"), " ", "\n")
    74 local key = multirand.string(26, "123456789bcdfghjklmnpqrstvwxyz");
    76 local old_draft_filename = encode.file_path(request.get_app_basepath(), 'tmp', "diff-" .. key .. "-old.tmp")
    77 local new_draft_filename = encode.file_path(request.get_app_basepath(), 'tmp', "diff-" .. key .. "-new.tmp")
    79 local old_draft_file = assert(io.open(old_draft_filename, "w"))
    80 old_draft_file:write(old_draft_content)
    81 old_draft_file:write("\n")
    82 old_draft_file:close()
    84 local new_draft_file = assert(io.open(new_draft_filename, "w"))
    85 new_draft_file:write(new_draft_content)
    86 new_draft_file:write("\n")
    87 new_draft_file:close()
    89 local output, err, status = extos.pfilter(nil, "sh", "-c", "diff -U 1000000000 '" .. old_draft_filename .. "' '" .. new_draft_filename .. "' | grep -v ^--- | grep -v ^+++ | grep -v ^@")
    91 os.remove(old_draft_filename)
    92 os.remove(new_draft_filename)
    94 local last_state = "first_run"
    96 local function process_line(line)
    97   local state_char = string.sub(line, 1, 1)
    98   local state
    99   if state_char == "+" then
   100     state = "added"
   101   elseif state_char == "-" then
   102     state = "removed"
   103   elseif state_char == " " then
   104     state = "unchanged"
   105   end
   106   local state_changed = false
   107   if state ~= last_state then
   108     if last_state ~= "first_run" then
   109       slot.put("</span> ")
   110     end
   111     last_state = state
   112     state_changed = true
   113     slot.put("<span class=\"diff_" .. tostring(state) .. "\">")
   114   end
   116   line = string.sub(line, 2, #line)
   117   if line ~= "###ENTER###" then
   118     if not state_changed then
   119       slot.put(" ")
   120     end
   121     slot.put(encode.html(line))
   122   else
   123     slot.put("<br />")
   124   end
   125 end
   127 ui.section( function()
   128   ui.sectionHead( function()
   129     ui.link{
   130       module = "initiative", view = "show", id = initiative.id,
   131       content = function ()
   132         ui.heading { 
   133           level = 1,
   134           content = initiative.display_name
   135         }
   136       end
   137     }
   138     ui.heading{ level = 2, content = _("Comparision of revisions #{id1} and #{id2}", {
   139       id1 = old_draft.id,
   140       id2 = new_draft.id 
   141     } ) }
   142   end )
   144   if app.session.member_id and not new_draft.initiative.revoked then
   145     local supporter = app.session.member:get_reference_selector("supporters")
   146       :add_where{ "initiative_id = ?", new_draft.initiative_id }
   147       :optional_object_mode()
   148       :exec()
   149     if supporter and supporter.draft_id ~= new_draft.id then
   150       ui.sectionRow("draft_updated_info", function()
   151         ui.container{ 
   152           attr = { class = "info" },
   153           content = _"The draft of this initiative has been updated!"
   154         }
   155         slot.put(" ")
   156         ui.link{
   157           text   = _"refresh my support",
   158           module = "initiative",
   159           action = "add_support",
   160           id     = new_draft.initiative.id,
   161           params = { draft_id = new_draft.id },
   162           routing = {
   163             default = {
   164               mode = "redirect",
   165               module = "initiative",
   166               view = "show",
   167               id = new_draft.initiative.id
   168             }
   169           }
   170         }
   172         slot.put(" · ")
   174         ui.link{
   175           text   = _"remove my support",
   176           module = "initiative",
   177           action = "remove_support",
   178           id     = new_draft.initiative.id,
   179           routing = {
   180             default = {
   181               mode = "redirect",
   182               module = "initiative",
   183               view = "show",
   184               id = new_draft.initiative.id
   185             }
   186           }
   187         }        
   189       end )
   190     end
   191   end
   193   ui.sectionRow( function()
   195     if not status then
   196       ui.field.text{ value = _"The drafts do not differ" }
   197     else
   198       ui.container{
   199         tag = "div",
   200         attr = { class = "diff" },
   201         content = function()
   202           output = output:gsub("[^\n\r]+", function(line)
   203             process_line(line)
   204           end)
   205         end
   206       }
   207     end 
   209   end )
   210 end )
