Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 117 lines (99 sloc) 3.246 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
load 'application'

before use 'userRequired'

loadPage = ->
    Page.findById req.params.id, (err, page) =>
        if err or !page
            redirect path_to.pages
        else
            @page = page
            next()

before loadPage,
    only: ['show', 'edit', 'update', 'destroy']

action 'new', ->
    @page = new Page
    @page.path = req.param('path') if req.param('path')?
    
    render title: 'New page'

action 'create', ->
    if Page.index[req.body.path]
        flash 'error', 'Page for this path already exists'
        render 'new', title: 'New page'
    else
        @page = new Page
        @page["title"] = req.body["title"]
        @page["content"] = req.body["content"]
        @page["path"] = req.body["path"]
        @page.save (errors) =>
            if errors
                flash 'error', 'Page can not be created'
                render 'new', title: 'New page'
            else
                flash 'info', 'Page created'
                Page.buildIndex =>
                    redirect @page.path

action 'index', ->
    @pages = Page.getSorted()

    if req.params.format == 'json'
        send @pages
    else
        render
            title: 'Pages index'

action 'show', ->
    render title: 'Page show'

action 'edit', ->
    render title: 'Page edit'

action 'update', ->
    if Page.index[req.body.path] && Page.index[req.body.path].id != @page.id
        flash 'error', 'Page can not be updated'
        render 'edit', title: 'Edit page details'
    else
        @page["title"] = req.body["title"]
        @page["content"] = req.body["content"]
        @page["path"] = req.body["path"]
        @page.save (err) =>
            if !err
                flash 'info', 'Page updated'
                Page.buildIndex =>
                    redirect @page.path
            else
                flash 'error', 'Page can not be updated'
                render 'edit', title: 'Edit page details'

action 'destroy', ->
    @page.remove (error) ->
        if error
            flash 'error', 'Can not destroy page'
        else
            flash 'info', 'Page successfully removed'

        Page.buildIndex () ->
            send "'" + path_to.pages + "'"

action 'reorder', ->
    wait = 0
    req.body.order.forEach (path, index) ->
        wait += 1
        Page.index[path].pos = index
        Page.index[path].save ->
            if --wait == 0
                Page.buildIndex ->
                    Page.getSorted().forEach (p) ->
                        console.log p.pos
                    send 'ok'

action 'backup', ->
    require('fs').writeFileSync(app.root + '/db/dump.json', JSON.stringify(Page.getSorted().map (page) -> page.doc))
    send 'ok'

action 'restore', ->
    wait = 0
    waitSaving = 0
    saved = ->
        if --waitSaving == 0
            send '0k'
    done = ->
        if --wait == 0
            pages = JSON.parse(require('fs').readFileSync(app.root + '/db/dump.json').toString('utf8'))
            pages.forEach (page) ->
                delete page._id
                p = new Page(page)
                waitSaving += 1
                p.save(saved)

    Page.find (err, pages) ->
        pages.forEach (page) ->
            wait += 1
            page.remove(done)
Something went wrong with that request. Please try again.