refactor front-end

This commit is contained in:
jysperm
2015-05-26 05:28:54 +08:00
parent 3f48c86117
commit c4c85ae2c9
16 changed files with 72 additions and 41 deletions

15
.npmignore Normal file
View File

@@ -0,0 +1,15 @@
*~
.DS_Store
.idea/
.vagrant/
node_modules/
bower_components/
/package.box
/npm-debug.log
/.remote-sync.json
/session.key
/config.coffee
/coverage-reporter.html

0
app.coffee Executable file → Normal file
View File

View File

@@ -7,7 +7,12 @@
"jquery": "~2.1.2", "jquery": "~2.1.2",
"backbone": "~1.1.2", "backbone": "~1.1.2",
"moment": "~2.9.0", "moment": "~2.9.0",
"jquery-tmpl": "vBeta1.0.0", "jquery-tmpl": "git://github.com/jquery/jquery-tmpl.git#b504c8afba",
"q": "~1.2.0" "q": "~1.2.0"
},
"overrides": {
"jquery-tmpl": {
"main": "jquery.tmpl.js"
}
} }
} }

View File

@@ -4,7 +4,7 @@ $ ->
CompontentCollection = Backbone.Collection.extend CompontentCollection = Backbone.Collection.extend
model: Component model: Component
url: '/component/resource/' url: '/components/'
ListItemView = Backbone.View.extend ListItemView = Backbone.View.extend
tagName: 'tr' tagName: 'tr'

View File

@@ -1,9 +1,4 @@
$ -> $ ->
_.templateSettings =
evaluate: /\{%([\s\S]+?)%\}/g,
interpolate: /\{:([\s\S]+?)\}\}/g
escape: /\{\{([\s\S]+?)\}\}/g
_.extend window.RP, _.extend window.RP,
i18n_data: {} i18n_data: {}

View File

@@ -3,9 +3,17 @@ methods = ['get', 'post', 'delete', 'put', 'patch', 'head', 'options']
window.root = window.root =
agent: {} agent: {}
# Private: (name) -> String
t: (name) -> t: (name) ->
return name return name
# Private: (selector) -> (view_data) -> String
tmpl: (selector) ->
cache = $(selector).template()
return (view_data) ->
return $.tmpl cache, view_data
methods.forEach (method) -> methods.forEach (method) ->
root.agent[method] = (url, data, options = {}) -> root.agent[method] = (url, data, options = {}) ->
unless method == 'get' unless method == 'get'

View File

@@ -1,4 +1,8 @@
$ -> $ ->
$.ajaxSetup
headers:
'X-Token': $.cookie 'token'
color_mapping = color_mapping =
closed: 'muted' closed: 'muted'
open: 'primary' open: 'primary'
@@ -12,7 +16,7 @@ $ ->
model: Reply model: Reply
Ticket = Backbone.Model.extend Ticket = Backbone.Model.extend
urlRoot: '/ticket/rest/' urlRoot: '/tickets'
idAttribute: '_id' idAttribute: '_id'
initialize: -> initialize: ->
@@ -24,7 +28,7 @@ $ ->
TicketCollection = Backbone.Collection.extend TicketCollection = Backbone.Collection.extend
model: Ticket model: Ticket
url: '/ticket/rest/' url: '/tickets'
CreateView = Backbone.View.extend CreateView = Backbone.View.extend
el: '#create-view' el: '#create-view'
@@ -38,14 +42,14 @@ $ ->
content: @$('[name=content]').val() content: @$('[name=content]').val()
ticket.save().success (ticket) -> ticket.save().success (ticket) ->
location.href = "/ticket/view/#{ticket._id}" location.href = "/tickets/#{ticket._id}/view"
ReplyView = Backbone.View.extend ReplyView = Backbone.View.extend
tagName: 'li' tagName: 'li'
className: 'list-group-item clearfix' className: 'list-group-item clearfix'
initialize: -> initialize: ->
@template = RP.tmpl '#reply-template' @template = root.tmpl '#reply-template'
@model.on 'change', @render.bind @ @model.on 'change', @render.bind @
render: -> render: ->
@@ -70,10 +74,10 @@ $ ->
@model.fetch() @model.fetch()
@templateContent = RP.tmpl '#content-template' @templateContent = root.tmpl '#content-template'
@templateActions = RP.tmpl '#actions-template' @templateActions = root.tmpl '#actions-template'
@templateAccountInfo = RP.tmpl '#account-info-template' @templateAccountInfo = root.tmpl '#account-info-template'
@templateMembers = RP.tmpl '#members-template' @templateMembers = root.tmpl '#members-template'
render: -> render: ->
view_data = @model.toJSON() view_data = @model.toJSON()
@@ -108,7 +112,7 @@ $ ->
tagName: 'tr' tagName: 'tr'
initialize: -> initialize: ->
@template = RP.tmpl '#list-item-template' @template = root.tmpl '#list-item-template'
render: -> render: ->
view_data = @model.toJSON() view_data = @model.toJSON()
@@ -132,9 +136,9 @@ $ ->
TicketRouter = Backbone.Router.extend TicketRouter = Backbone.Router.extend
routes: routes:
'ticket/create(/)': 'create' 'tickets/create(/)': 'create'
'ticket/list(/)': 'list' 'tickets/list(/)': 'list'
'ticket/view/:id(/)': 'view' 'tickets/:id/view(/)': 'view'
create: -> new CreateView() create: -> new CreateView()
list: -> new ListView() list: -> new ListView()

View File

@@ -16,6 +16,7 @@ router.use requireAuthenticate
### ###
router.get '/financials', (req, res, next) -> router.get '/financials', (req, res, next) ->
Q.all([ Q.all([
null
Financials.getDepositLogs req.account, req: req, limit: 10 Financials.getDepositLogs req.account, req: req, limit: 10
Financials.getBillingLogs req.account, limit: 10 Financials.getBillingLogs req.account, limit: 10
]).done ([payment_providers, deposit_logs, billing_logs]) -> ]).done ([payment_providers, deposit_logs, billing_logs]) ->

View File

@@ -1,4 +1,4 @@
describe 'account.register', -> describe 'router.account', ->
agent = createAgent agent = createAgent
baseUrl: '/account' baseUrl: '/account'

View File

@@ -1,4 +1,4 @@
describe 'tickets.user', -> describe 'router.tickets', ->
agent = createLoggedAgent agent = createLoggedAgent
baseUrl: '/tickets' baseUrl: '/tickets'

View File

@@ -18,7 +18,7 @@ block main
th= t('common.amount') th= t('common.amount')
th= t('view.financials.pay_method') th= t('view.financials.pay_method')
tbody tbody
for item in deposit_log for item in deposit_logs
tr tr
td= moment(item.created_at).format('YYYY-MM-DD HH:mm:ss') td= moment(item.created_at).format('YYYY-MM-DD HH:mm:ss')
td #{item.amount.toFixed(2)} #{t('plan.currency.' + config.billing.currency)} td #{item.amount.toFixed(2)} #{t('plan.currency.' + config.billing.currency)}
@@ -36,7 +36,7 @@ block main
th= t('common.details') th= t('common.details')
th= t('common.amount') th= t('common.amount')
tbody tbody
for item in billing_log for item in billing_logs
tr tr
td= moment(item.created_at).format('YYYY-MM-DD HH:mm:ss') td= moment(item.created_at).format('YYYY-MM-DD HH:mm:ss')
td td

View File

@@ -22,6 +22,6 @@ prepend sidebar
append footer append footer
script(id='list-item-template', type='text/x-jquery-tmpl') script(id='list-item-template', type='text/x-jquery-tmpl')
td td
a(href='/tickets/view/${_id}') ${title} a(href='/tickets/${_id}/view') ${title}
td td
span(class='text-${color}') ${RP.t('ticket_status.' + status)} span(class='text-${color}') ${root.t('ticket_status.' + status)}

View File

@@ -24,7 +24,7 @@ append footer
script(id='content-template', type='text/x-jquery-tmpl') script(id='content-template', type='text/x-jquery-tmpl')
header header
| ${title}   | ${title}  
span.small(class='text-${color}') ${RP.t('ticket_status.' + status)} span.small(class='text-${color}') ${root.t('ticket_status.' + status)}
p {{html content_html}} p {{html content_html}}
script(id='reply-template', type='text/x-jquery-tmpl') script(id='reply-template', type='text/x-jquery-tmpl')

View File

@@ -2,6 +2,7 @@ del = require 'del'
gulp = require 'gulp' gulp = require 'gulp'
less = require 'gulp-less' less = require 'gulp-less'
shell = require 'gulp-shell' shell = require 'gulp-shell'
debug = require 'gulp-debug'
order = require 'gulp-order' order = require 'gulp-order'
rsync = require 'gulp-rsync' rsync = require 'gulp-rsync'
coffee = require 'gulp-coffee' coffee = require 'gulp-coffee'
@@ -34,7 +35,7 @@ gulp.task 'vendor:scripts', ->
gulp.task 'vendor:fonts', -> gulp.task 'vendor:fonts', ->
gulp.src bowerFiles() gulp.src bowerFiles()
.pipe filter ['*.eot', '*.svg', '*.ttf', '*.woff'] .pipe filter ['*.eot', '*.svg', '*.ttf', '*.woff', '*.woff2']
.pipe gulp.dest 'public/fonts' .pipe gulp.dest 'public/fonts'
gulp.task 'build:vendor', -> gulp.task 'build:vendor', ->
@@ -51,7 +52,7 @@ gulp.task 'build:styles', ->
gulp.src 'core/public/style/*.less' gulp.src 'core/public/style/*.less'
.pipe less() .pipe less()
.pipe concat 'core.css' .pipe concat 'core.css'
.pipe minifyCss() # .pipe minifyCss()
.pipe gulp.dest 'public' .pipe gulp.dest 'public'
gulp.task 'build:scripts', -> gulp.task 'build:scripts', ->
@@ -59,7 +60,7 @@ gulp.task 'build:scripts', ->
.pipe coffee() .pipe coffee()
.pipe order ['root.coffee', '*'] .pipe order ['root.coffee', '*']
.pipe concat 'core.js' .pipe concat 'core.js'
.pipe uglify() # .pipe uglify()
.pipe gulp.dest 'public' .pipe gulp.dest 'public'
gulp.task 'watch', -> gulp.task 'watch', ->

View File

@@ -17,8 +17,9 @@
], ],
"main": "core", "main": "core",
"scripts": { "scripts": {
"prepushlish": "gulp build",
"start": "coffee app.coffee", "start": "coffee app.coffee",
"test": "mocha --compilers coffee:coffee-script/register --require test/env -- core/test/*.test.coffee", "test": "mocha --compilers coffee:coffee-script/register --require test/env --recursive core/test",
"docker-build": "docker build -t=jysperm/rootpanel .", "docker-build": "docker build -t=jysperm/rootpanel .",
"docker-create": "docker run --name rootpanel -P -v `pwd`:/rootpanel -i -t jysperm/rootpanel", "docker-create": "docker run --name rootpanel -P -v `pwd`:/rootpanel -i -t jysperm/rootpanel",
"docker-start": "docker start -i -a rootpanel" "docker-start": "docker start -i -a rootpanel"
@@ -67,6 +68,7 @@
"gulp": "^3.8.11", "gulp": "^3.8.11",
"gulp-coffee": "^2.3.1", "gulp-coffee": "^2.3.1",
"gulp-concat": "^2.5.2", "gulp-concat": "^2.5.2",
"gulp-debug": "^2.0.1",
"gulp-filter": "^2.0.2", "gulp-filter": "^2.0.2",
"gulp-less": "^3.0.3", "gulp-less": "^3.0.3",
"gulp-minify-css": "^1.1.0", "gulp-minify-css": "^1.1.0",

View File

@@ -101,6 +101,17 @@ createLoggedAgent = (options) ->
return agent return agent
_.extend global, {
utils
ifEnabled
unlessTravis
randomAccount
createAgent
createLoggedAgent
}
printHttpResponse = ({httpVersion, statusCode, statusMessage, headers, body}) -> printHttpResponse = ({httpVersion, statusCode, statusMessage, headers, body}) ->
message = """ message = """
Response: Response:
@@ -119,14 +130,3 @@ printHttpResponse = ({httpVersion, statusCode, statusMessage, headers, body}) ->
message += "\n#{body}" message += "\n#{body}"
return message return message
_.extend global, {
utils
ifEnabled
unlessTravis
randomAccount
createAgent
createLoggedAgent
}