<commit_before>/* 
* @Author: <NAME>
* @Date:   2016-02-04 18:40:18
* @Last Modified 2016-02-05
*/

'use strict';

import Promise from 'bluebird'
import fs from 'fs'
import _ from 'underscore'

import adminBase from './adminBase'

const defaultOpts = {
  basePath: '/admin',
  adminTemplate: 'admin'
}

export var AdminBase = adminBase

export default class AdminUI {
  constructor(app) {
    this.app = app
    this.pages = {}
    this.nav = []
    this.opts = Object.assign(defaultOpts, this.app.config.adminUI)

    this.users = this.app.get('users')
    this.router = this.app.get('router')
    this.renderer = this.app.get('renderer')
    this.templater = this.app.get('templater')

    this.app.get('admin-ui').use(this)
      .gather('adminPage')
      .gather('adminRoute')

    this._setupRoutes()
  }

  _setupRoutes() {
    this.users.protectedRoute(this.opts.basePath)
    this.users.protectedRoute(this.opts.basePath+'/*')

    this.router.middleware('use', this.opts.basePath+"/*", (req, res, next) => {
      req.adminOpts = this.opts;
      next()
    })
  }

  adminPage(title, path, opts, handler) {
    if(!handler) {
      handler = opts
      opts = {}
    }
    if(path[0] != "/") path = "/"+path
    path = this.opts.basePath+path
    this.app.log('registering admin page', path)
    this.pages[path] = {title, handler, opts}
    this.router.route('get', path, this._renderPage.bind(this))
    if(opts && opts.nav != false) this.nav.push({title, path, opts})
  }

  adminRoute(method, path, handler) {
    if(!handler) {
      handler = path
      path = method
      method = 'post'
    }
    if(path[0] != "/") path = "/"+path
    this.app.log('registering admin page', path)
    path = this.opts.basePath+path
    this.pages[path] = {handler}
    this.router.route(method, path, handler)
  }

  _renderPage(req, res) {
    let route = req.route ? req.route.path : req.originalUrl
    if(!this.pages[route] || !this.pages[route].handler) return
    let handler = this.pages[route].handler
    let title = this.pages[route].title
    let nav = this._getNav();
    
    if(typeof handler == 'string') {
      if(fs.existsSync(handler)) {
        return this.templater.renderPartial(handler, this.opts.adminTemplate, {title, nav, opts: this.app.config}).then(res.send.bind(res));
      }
      return this.templater.render(this.opts.adminTemplate, {title, nav, content: handler, opts: this.app.config}).then(res.send.bind(res));
    } else {
      return Promise.resolve(handler(req, res)).then((content) => {
        if(content) this.templater.render(this.opts.adminTemplate, {title, nav, content, opts: this.app.config}).then(res.send.bind(res));
      }).catch((e) => {
        this.app.log('Caught error rendering admin handler', e)
      })
    }
  }
<commit_msg>Added default admin page<commit_after>/* 
* @Author: <NAME>
* @Date:   2016-02-04 18:40:18
* @Last Modified 2016-02-09
*/

'use strict';

import Promise from 'bluebird'
import fs from 'fs'
import _ from 'underscore'

import adminBase from './adminBase'

const defaultOpts = {
  basePath: '/admin',
  adminTemplate: 'admin'
}

export var AdminBase = adminBase

export default class AdminUI {
  constructor(app) {
    this.app = app
    this.pages = {}
    this.nav = []
    this.opts = Object.assign(defaultOpts, this.app.config.adminUI)

    this.users = this.app.get('users')
    this.router = this.app.get('router')
    this.renderer = this.app.get('renderer')
    this.templater = this.app.get('templater')

    this.app.get('admin-ui').use(this)
      .gather('adminPage')
      .gather('adminRoute')

    this._setupRoutes()

    this._addDefaultRoute()
  }

  _setupRoutes() {
    this.users.protectedRoute(this.opts.basePath)
    this.users.protectedRoute(this.opts.basePath+'/*')

    this.router.middleware('use', this.opts.basePath+"/*", (req, res, next) => {
      req.adminOpts = this.opts;
      next()
    })
  }

  _addDefaultRoute() {
    this.app.log('adding admin route')
    this.provideAfter('adminPage', "Home", '', {iconClass: 'fa fa-home'}, (req, res) => {
      return "Welcome to Nxus Admin!"
    })
  }

  adminPage(title, path, opts, handler) {
    if(!handler) {
      handler = opts
      opts = {}
    }
    if(path[0] != "/") path = "/"+path
    path = this.opts.basePath+path
    this.app.log('registering admin page', path)
    this.pages[path] = {title, handler, opts}
    this.router.route('get', path, this._renderPage.bind(this))
    if(opts && opts.nav != false) this.nav.push({title, path, opts})
  }

  adminRoute(method, path, handler) {
    if(!handler) {
      handler = path
      path = method
      method = 'post'
    }
    if(path[0] != "/") path = "/"+path
    this.app.log('registering admin page', path)
    path = this.opts.basePath+path
    this.pages[path] = {handler}
    this.router.route(method, path, handler)
  }

  _renderPage(req, res) {
    let route = req.route ? req.route.path : req.originalUrl
    if(!this.pages[route] || !this.pages[route].handler) return
    let handler = this.pages[route].handler
    let title = this.pages[route].title
    let nav = this._getNav();
    
    if(typeof handler == 'string') {
      if(fs.existsSync(handler)) {
        return this.templater.renderPartial(handler, this.opts.adminTemplate, {title, nav, opts: this.app.config}).then(res.send.bind(res));
      }
      return this.templater.render(this.opts.adminTemplate, {title, nav, content: handler, opts: this.app.config}).then(res.send.bind(res));
    } else {
      return Promise.resolve(handler(req, res)).then((content) => {
        if(content) this.templater.render(this.opts.adminTemplate, {title, nav, content, opts: this.app.config}).then(res.send.bind(res));
      }).catch((e) => {
        this.app.log.error('Caught error rendering admin handler', e)
      })
    }
  }

{'fraction_non_alphanumeric': 0.16729581331503088, 'fraction_numerical': 0.008236101578586136, 'mean_word_length': 3.079076277116865, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1591108', 'n_tokens_mistral': 2002, 'n_tokens_neox': 1963, 'n_words': 382}