diff -r 2ba4569f2bd6 -r 8f0eddd7aa85 src/main/scala/net/datatables/DataTables.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/scala/net/datatables/DataTables.scala Tue Apr 24 16:42:34 2012 +0200 @@ -0,0 +1,96 @@ +/* + * Copyright 2012 Tomas Zeman + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.datatables + +import fis.base.ui._ +import net.liftweb.http._ +import net.liftweb.http.js._ +import net.liftweb.http.js.JE._ +import net.liftweb.http.js.JsCmds._ +import net.liftweb.http.js.jquery._ +import net.liftweb.http.js.jquery.JqJE._ +import net.liftweb.http.js.jquery.JqJsCmds._ +import net.liftweb.json._ +import net.liftweb.json.Extraction._ +import net.liftweb.record.RecordHelpers._ +import net.liftweb.sitemap.Loc.Snippet +import net.liftweb.util._ +import net.liftweb.util.Helpers._ +import net.tz.lift.snippet._ +import scala.xml.NodeSeq + +/** + * datatables.net typesafe datatable. + */ +object DataTables { + + def init() { + LiftRules.snippets.append { + case List("datatables") => { _ => initHeader(loadTpl) } + } + } + + protected def loadTpl = + Templates(List("templates-hidden", "datatables")) openOr NodeSeq.Empty + + protected def initHeader: CssTr = { + val rows = Props.getInt("datatables.rows") map { i => + SetExp(JsVar("$.fn.dataTable.defaults.iDisplayLength"), i) } + val lang = Props.get("datatables.lang." + CurLanguage.getLanguage) map { l => + SetExp(JsVar("$.fn.dataTable.defaults.oLanguage.sUrl"), l) } + "#datatables-defaults" #> Script((rows ++ lang) toSeq) + } + + def onLoad(selector: String, opts: Options): NodeSeq = { + val s = Script(JqOnLoad(jsRender(selector, opts))) + + {s} + + } + + def jsRender(selector: String, opts: Options): JsExp = + JqId(selector) ~> fun(opts) + + private def fun(opts: Options): JsMember = + new JsRaw("dataTable(%s);".format(opt2js(opts).toJsCmd)) with JsMember + + private def opt2js(opts: Options): JsExp = { + implicit val formats = DefaultFormats + decompose(opts) + } + + case class Options(oLanguage: Option[Language], aoColumnDefs: List[ColumnDef]) + { + def lang(l: Language) = this.copy(oLanguage = Some(l)) + def col(c: ColumnDef) = this.copy(aoColumnDefs = c :: aoColumnDefs) + } + object Options extends Options(None, Nil) + + case class Language(sUrl: Option[String]) { + def url(url: String) = this.copy(sUrl = Some(url)) + } + object Language extends Language(None) + + case class ColumnDef(sType: Option[String], aTargets: List[Int]) { + def dtype(t: String) = this.copy(sType = Some(t)) + def dtype(t: Symbol) = this.copy(sType = Some(t.name)) + def target(i: Int) = this.copy(aTargets = i :: aTargets) + } + object ColumnDef extends ColumnDef(None, Nil) + + val TYPE_EU_DATE = 'eu_date +} +// vim: set ts=2 sw=2 et: