Skip to content

Commit

Permalink
Added Scaladoc. Changed License to Apache 2.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
fbettag committed Jan 22, 2012
1 parent afa01ed commit a57cf94
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 91 deletions.
43 changes: 15 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ BHtml

It is a wrapper-library build around Liftweb's ultra-flexible SHtml and Mapper-ORM for easy validation.

Sample Project is in the works!


## How to use

Expand All @@ -27,40 +29,25 @@ BHtml.text[Long, MapperClass](myMapperRecord.name, false, enableSubmitJs, disabl
```


## ToDo

- Scaladoc
- Sample Project


## Thanks

Thanks to everybody in the Lift Community and on [Liftweb Google Groups](http://groups.google.com/group/liftweb).


## License

```
Copyright (c) 2011, Franz Bettag <franz@bett.ag>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY BETTAG SYSTEMS UG ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL BETTAG SYSTEMS UG BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
``` Copyright 2012 Franz Bettag <franz@bett.ag>
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.
```

120 changes: 79 additions & 41 deletions src/main/scala/ag/bett/lift/bhtml/BHtml.scala
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
/* {{{
* Copyright (c) 2011, Franz Bettag <franz@bett.ag>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY BETTAG SYSTEMS UG ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL BETTAG SYSTEMS UG BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* Copyright 2012 Franz Bettag <franz@bett.ag>
*
* 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 ag.bett.lift.bhtml
Expand All @@ -41,49 +33,110 @@ import org.joda.time.format._
import scala.xml._


/**
* Wraps around Liftweb's ultra-flexible SHtml and Mapper-ORM for easy validation with a
* [[http://twitter.github.com/bootstrap/javascript.html#popover Twitter BootstrapJS Popover]]
*
* Validation with Popover supported with [[http://twitter.github.com/bootstrap/javascript.html#popover Twitter BootstrapJS]].
*
* All selectors are [[http://api.jquery.com/category/selectors/ jQuery Selectors]].
*
* The <b>save</b>-parameters make fields autocommit whole records.
*
*/
object BHtml {

/**
* Delay a JsCmd
*/
def delay(d: Int, js: JsCmd) = JsRaw("""window.setTimeout(function() {%s}, %s);""".format(js.toJsCmd, d))

/**
* Hide elements matching the jQuery CSS-Selector
*/
def hide(sel: String): JsCmd = JsRaw("""$('%s').hide()""".format(sel))

/**
* Show elements matching the jQuery CSS-Selector
*/
def show(sel: String): JsCmd = JsRaw("""$('%s').show()""".format(sel))

/**
* Toggle elements matching the jQuery CSS-Selector
*/
def toggle(sel: String): JsCmd = JsRaw("""$('%s').toggle()""".format(sel))

object Fx {
/**
* Pulsate elements matching the jQuery CSS-Selector
*/
def success(sel: String): JsCmd =
JsRaw("""$('%s').effect('pulsate', {times: 2}, 200)""".format(sel)).cmd

/**
* Pulsate elements matching the jQuery CSS-Selector
*/
def failed(sel: String): JsCmd =
JsRaw("""$('%s').effect('pulsate', {times: 2}, 200)""".format(sel)).cmd

/**
* Render a List[FieldError] as <code><ul><li/></ul></code> and mark elements invalid using the jQuery CSS-Selector
*/
def invalid(sel: String, errors: List[FieldError]): JsCmd =
invalidated(sel, S.??("Error"), <ul>{errors.map(fe => <li>{fe.msg}</li>)}</ul>)

/**
* Add the CSS-Class <i>success</i> and remove <i>error</i>, also repair from popover for elements matching the jQuery CSS-Selector
*/
def validated(sel: String): JsCmd =
JsRaw("""$('%s').removeClass('error').addClass('success').removeAttr('title data-original-title data-content')""".format(sel)).cmd

/**
* Remove the CSS-Class <i>success</i> and add <i>error</i> for elements matching the jQuery CSS-Selector
*/
def invalidated(sel: String): JsCmd =
JsRaw("""$('%s').removeClass('success').addClass('error')""".format(sel)).cmd

/**
* Remove the CSS-Class <i>success</i> and add <i>error</i> for elements matching the jQuery CSS-Selector and also generate a
* [[http://twitter.github.com/bootstrap/javascript.html#popover JavaScript Popover]].
*/
def invalidated(sel: String, title: String, body: String): JsCmd =
invalidated(sel, title, Text(body))

/**
* Remove the CSS-Class <i>success</i> and add <i>error</i> for elements matching the jQuery CSS-Selector and also generate a
* [[http://twitter.github.com/bootstrap/javascript.html#popover JavaScript Popover]].
*/
def invalidated(sel: String, title: String, body: NodeSeq): JsCmd =
invalidated(sel) & popover(sel, title, body) & delay(2000, JsRaw("""$('%s').popover('hide')""".format(sel)).cmd)

/**
* Reset the CSS-Classes <i>success</i>, <i>error</i> and repair from popover for elements matching the jQuery CSS-Selector
*/
def reset(sel: String): JsCmd =
JsRaw("""$('%s').removeClass('error').removeClass('success').removeAttr('title data-original-title data-content')""".format(sel)).cmd

/**
* Fadeout elements matching the jQuery CSS-Selector
*/
def remove(sel: String): JsCmd =
JsRaw("""$('%s').fadeOut(400).hide(function() { $('%s').remove(); })""".format(sel, sel)).cmd

/**
* Generate a [[http://twitter.github.com/bootstrap/javascript.html#popover JavaScript Popover]]
*/
def popover(sel: String, title: String, body: NodeSeq): JsCmd =
JsRaw("""$('%s').attr('title', "%s").attr('data-content', "%s").popover({ offset: 10, html: true }).popover('show').click(function(e) { e.preventDefault() })"""
.format(sel, title.replaceAll("\"", "\\\""), body.toString.replaceAll("\"", "\\\""))).cmd

}


/**
* Generates an ID unique for this record <b>(restrictions apply)</b>
* <b>If the MappedField's fieldOwner is NOT SAVED, this will have -1. Be sure to only have one unsaved per Model on your page.</b>
*/
def getCssId[K, T <: KeyedMapper[K, T]](a: MappedField[K, T]) = "BF-%s-%s".format(a.uniqueFieldId.open_!, a.fieldOwner.primaryKeyField.is)


Expand All @@ -93,8 +146,9 @@ object BHtml {
def save[K, T <: KeyedMapper[K, T]](a: T, jsSuccess: () => JsCmd = () => Noop, jsFail: () => JsCmd = () => Noop): JsCmd = {
if (a.validate.length == 0 && a.save) jsSuccess()
else jsFail() & a.allFields.map(bf => {
if (bf.validate.length == 0) Noop
else Fx.invalid(".BF-%s-%s".format(bf.uniqueFieldId.open_!, a.primaryKeyField.is), bf.validate)
val css = ".BF-%s-%s".format(bf.uniqueFieldId.open_!, a.primaryKeyField.is)
if (bf.validate.length == 0) Fx.reset(css)
else Fx.invalid(css, bf.validate)
})
}

Expand All @@ -105,9 +159,6 @@ object BHtml {
a.allFields.map(bf => Fx.reset(".BF-%s-%s".format(bf.uniqueFieldId.open_!, a.primaryKeyField.is)))


/**
* Mapped Boolean
*/
def checkbox[K, T <: KeyedMapper[K, T]](a: MappedBoolean[T], save: Boolean, jsSuccess: JsCmd): NodeSeq =
checkbox[K, T](a, save, Empty, jsSuccess)

Expand All @@ -130,9 +181,6 @@ object BHtml {
}


/**
* Mapped String
*/
def text[K, T <: KeyedMapper[K, T]](a: MappedString[T], save: Boolean): NodeSeq =
text[K, T](a, Empty, save, Empty)

Expand Down Expand Up @@ -163,9 +211,6 @@ object BHtml {
}


/**
* Mapped Int
*/
def int[K, T <: KeyedMapper[K, T]](a: MappedInt[T], cssClass: String): NodeSeq =
int[K, T](a, false, Full(cssClass))

Expand Down Expand Up @@ -193,9 +238,7 @@ object BHtml {
SHtml.ajaxText(a.is.toString, update(_), "class" -> css, "pattern" -> "[0-9]+")
}

/**
* Mapped Nullable Int
*/

def intOptional[K, T <: KeyedMapper[K, T]](a: MappedNullableInt[T], cssClass: String): NodeSeq =
intOptional[K, T](a, false, Full(cssClass))

Expand Down Expand Up @@ -224,9 +267,6 @@ object BHtml {
}


/**
* Mapped Decimal
*/
def float[K, T <: KeyedMapper[K, T]](a: MappedDecimal[T], cssClass: String): NodeSeq =
float[K, T](a, false, Full(cssClass))

Expand Down Expand Up @@ -254,9 +294,7 @@ object BHtml {
SHtml.ajaxText(a.is.toString, update(_), "class" -> css, "pattern" -> "[0-9.,]+")
}

/**
* Mapped Nullable Int
*/

def floatOptional[K, T <: KeyedMapper[K, T]](a: MappedNullableDecimal[T], cssClass: String): NodeSeq =
floatOptional[K, T](a, false, Full(cssClass))

Expand Down
35 changes: 13 additions & 22 deletions src/main/scala/ag/bett/lift/bhtml/Mapper.scala
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
/* {{{
* Copyright (c) 2011, Franz Bettag <franz@bett.ag>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY BETTAG SYSTEMS UG ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL BETTAG SYSTEMS UG BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* Copyright 2012 Franz Bettag <franz@bett.ag>
*
* 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 ag.bett.lift.bhtml
Expand All @@ -38,7 +30,6 @@ import java.lang.reflect.Method
import java.util.{Date, Calendar, TimeZone}
import java.math.{MathContext, RoundingMode}


import scala.xml._


Expand Down
33 changes: 33 additions & 0 deletions src/main/scala/ag/bett/lift/bhtml/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ag.bett.lift

/**
* Wrapper for Liftweb's ultra-flexible SHtml and Mapper-ORM for easy validation with a
* [[http://twitter.github.com/bootstrap/javascript.html#popover Twitter BootstrapJS Popover]]
*
* Source-Code: [[https://github.com/fbettag/lift-bhtml]]
*
* Developed by [[http://twitter.com/fbettag Franz Bettag]]
*
* Developer's Blog [[http://www.uberblo.gs Developer Blog]]
*
* ==License==
* {{{
* Copyright 2012 Franz Bettag <franz@bett.ag>
*
* 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 object bhtml {

}

0 comments on commit a57cf94

Please sign in to comment.