All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.liftmodules.widgets.calendars.CalendarWeekView.scala Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2007-2011 WorldWide Conferencing, LLC
 *
 * 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.liftmodules
package widgets
package calendars

import java.util.{Calendar, Locale}
import Calendar._

import xml._

import net.liftweb._
import common.Box
import util.Helpers._
import http.LiftRules
import http.js._
import JsCmds._
import JE._


object CalendarWeekView {

  /**
   * Call this function typically in boot
   */
  def init() {
    import _root_.net.liftweb.http.ResourceServer
    ResourceServer.allow({
      case "calendars" :: _ => true
      case "common" :: _ => true
    })
  }

  def apply(when: Calendar,
      calendars: List[CalendarItem],
      itemClick: Box[AnonFunc]) = new CalendarWeekView(when).render(calendars, itemClick)

  def apply(when: Calendar,
      meta: WeekViewMeta,
      calendars: List[CalendarItem],
      itemClick: Box[AnonFunc]) = new CalendarWeekView(when, meta) render(calendars, itemClick)

}

class CalendarWeekView(val when: Calendar, val meta: WeekViewMeta) {

  def this(when: Calendar) = this(when, WeekViewMeta(MONDAY, Locale getDefault))

  def makeHead(headCal: Calendar) = {
    (0 to 6) map(x => {
      try{
        meta.weekDaysFormatter format(headCal getTime)
      } finally {
        headCal add(DAY_OF_MONTH, 1)
      }
    })
  }


  def render(calendars: List[CalendarItem], itemClick: Box[AnonFunc]): NodeSeq = {

    val cal = when.clone().asInstanceOf[Calendar]
    cal.set(Calendar.MILLISECOND, 0)
    cal.set(Calendar.SECOND, 0)
    cal.set(Calendar.MINUTE, 0)
    cal.set(Calendar.HOUR_OF_DAY, 0)

    val delta = cal.get(DAY_OF_WEEK) - meta.firstDayOfWeek

    cal add(DAY_OF_MONTH, if (delta < 0) -delta-7 else -delta)

    val lastCal = cal.clone().asInstanceOf[Calendar]
    lastCal.add(DAY_OF_MONTH, 7);

    val startIndex = cal.get(DAY_OF_WEEK)

    val f : (Int, Int) => Int = {
      case (day, startIndex) =>
        var st = day + startIndex
        if (st > 7) {
          st = st - 7
        }
        st
    }

    val headCal = cal.clone().asInstanceOf[Calendar]