|
1 /* |
|
2 * Copyright 2012 Tomas Zeman <tzeman@volny.cz> |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 package net.datatables |
|
17 |
|
18 import fis.base.ui._ |
|
19 import net.liftweb.http._ |
|
20 import net.liftweb.http.js._ |
|
21 import net.liftweb.http.js.JE._ |
|
22 import net.liftweb.http.js.JsCmds._ |
|
23 import net.liftweb.http.js.jquery._ |
|
24 import net.liftweb.http.js.jquery.JqJE._ |
|
25 import net.liftweb.http.js.jquery.JqJsCmds._ |
|
26 import net.liftweb.json._ |
|
27 import net.liftweb.json.Extraction._ |
|
28 import net.liftweb.record.RecordHelpers._ |
|
29 import net.liftweb.sitemap.Loc.Snippet |
|
30 import net.liftweb.util._ |
|
31 import net.liftweb.util.Helpers._ |
|
32 import net.tz.lift.snippet._ |
|
33 import scala.xml.NodeSeq |
|
34 |
|
35 /** |
|
36 * datatables.net typesafe datatable. |
|
37 */ |
|
38 object DataTables { |
|
39 |
|
40 def init() { |
|
41 LiftRules.snippets.append { |
|
42 case List("datatables") => { _ => initHeader(loadTpl) } |
|
43 } |
|
44 } |
|
45 |
|
46 protected def loadTpl = |
|
47 Templates(List("templates-hidden", "datatables")) openOr NodeSeq.Empty |
|
48 |
|
49 protected def initHeader: CssTr = { |
|
50 val rows = Props.getInt("datatables.rows") map { i => |
|
51 SetExp(JsVar("$.fn.dataTable.defaults.iDisplayLength"), i) } |
|
52 val lang = Props.get("datatables.lang." + CurLanguage.getLanguage) map { l => |
|
53 SetExp(JsVar("$.fn.dataTable.defaults.oLanguage.sUrl"), l) } |
|
54 "#datatables-defaults" #> Script((rows ++ lang) toSeq) |
|
55 } |
|
56 |
|
57 def onLoad(selector: String, opts: Options): NodeSeq = { |
|
58 val s = Script(JqOnLoad(jsRender(selector, opts))) |
|
59 <head> |
|
60 {s} |
|
61 </head> |
|
62 } |
|
63 |
|
64 def jsRender(selector: String, opts: Options): JsExp = |
|
65 JqId(selector) ~> fun(opts) |
|
66 |
|
67 private def fun(opts: Options): JsMember = |
|
68 new JsRaw("dataTable(%s);".format(opt2js(opts).toJsCmd)) with JsMember |
|
69 |
|
70 private def opt2js(opts: Options): JsExp = { |
|
71 implicit val formats = DefaultFormats |
|
72 decompose(opts) |
|
73 } |
|
74 |
|
75 case class Options(oLanguage: Option[Language], aoColumnDefs: List[ColumnDef]) |
|
76 { |
|
77 def lang(l: Language) = this.copy(oLanguage = Some(l)) |
|
78 def col(c: ColumnDef) = this.copy(aoColumnDefs = c :: aoColumnDefs) |
|
79 } |
|
80 object Options extends Options(None, Nil) |
|
81 |
|
82 case class Language(sUrl: Option[String]) { |
|
83 def url(url: String) = this.copy(sUrl = Some(url)) |
|
84 } |
|
85 object Language extends Language(None) |
|
86 |
|
87 case class ColumnDef(sType: Option[String], aTargets: List[Int]) { |
|
88 def dtype(t: String) = this.copy(sType = Some(t)) |
|
89 def dtype(t: Symbol) = this.copy(sType = Some(t.name)) |
|
90 def target(i: Int) = this.copy(aTargets = i :: aTargets) |
|
91 } |
|
92 object ColumnDef extends ColumnDef(None, Nil) |
|
93 |
|
94 val TYPE_EU_DATE = 'eu_date |
|
95 } |
|
96 // vim: set ts=2 sw=2 et: |