~andreserl/maas/precise_packaging_sru

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*
YUI 3.5.1 (build 22)
Copyright 2012 Yahoo! Inc. All rights reserved.
Licensed under the BSD License.
http://yuilibrary.com/license/
*/
YUI.add('calendar', function(Y) {

/**
 * The Calendar component is a UI widget that allows users
 * to view dates in a two-dimensional month grid, as well as
 * to select one or more dates, or ranges of dates. Calendar
 * is generated dynamically and relies on the developer to
 * provide for a progressive enhancement alternative.
 *
 *
 * @module calendar
 */

var getCN             = Y.ClassNameManager.getClassName,
    CALENDAR          = 'calendar',
    KEY_DOWN          = 40,
    KEY_UP            = 38,
    KEY_LEFT          = 37,
    KEY_RIGHT         = 39,
    KEY_ENTER         = 13,
    KEY_SPACE         = 32,
    CAL_HD            = getCN(CALENDAR, 'header'),
    CAL_DAY_SELECTED  = getCN(CALENDAR, 'day-selected'),
    CAL_DAY_HILITED   = getCN(CALENDAR, 'day-highlighted'),
    CAL_DAY           = getCN(CALENDAR, 'day'),
    CAL_PREVMONTH_DAY = getCN(CALENDAR, 'prevmonth-day'),
    CAL_NEXTMONTH_DAY = getCN(CALENDAR, 'nextmonth-day'),
    CAL_GRID          = getCN(CALENDAR, 'grid'),
    ydate             = Y.DataType.Date,
    delegate          = Y.delegate,
    CAL_PANE          = getCN(CALENDAR, 'pane'),
    os                = Y.UA.os;

/** Create a calendar view to represent a single or multiple
  * month range of dates, rendered as a grid with date and
  * weekday labels.
  * 
  * @class Calendar
  * @extends CalendarBase
  * @param config {Object} Configuration object (see Configuration attributes)
  * @constructor
  */
function Calendar(config) {
  Calendar.superclass.constructor.apply ( this, arguments );
}

Y.Calendar = Y.extend(Calendar, Y.CalendarBase, {

    _keyEvents: [],

    _highlightedDateNode: null,

  /**
   * A property tracking the last selected date on the calendar, for the
   * purposes of multiple selection.
   *
   * @property _lastSelectedDate
   * @type Date
   * @default null
   * @private
   */  
    _lastSelectedDate: null,

  /**
   * Designated initializer. Activates the navigation plugin for the calendar.
   *
   * @method initializer
   */ 
  initializer : function () {
    this.plug(Y.Plugin.CalendarNavigator);


    this._keyEvents = [];
    this._highlightedDateNode = null;
    this._lastSelectedDate = null;
  },

  /**
    * syncUI implementation
    *
    * Update the scroll position, based on the current value of scrollY
    * @method syncUI
    */  
  syncUI : function () {

  },

  /**
   * Overrides the _bindCalendarEvents placeholder in CalendarBase
   * and binds calendar events during bindUI stage.
   * @method _bindCalendarEvents
   * @protected
   */   
  _bindCalendarEvents : function () {
    var contentBox = this.get('contentBox'),
        pane       = contentBox.one("." + CAL_PANE);
    pane.on("selectstart", function (ev) { ev.preventDefault();});
    pane.delegate("click", this._clickCalendar, "." + CAL_DAY, this);
    pane.delegate("keydown", this._keydownCalendar, "." + CAL_GRID, this);
    pane.delegate("focus", this._focusCalendarGrid, "." + CAL_GRID, this);
    pane.delegate("focus", this._focusCalendarCell, "." + CAL_DAY, this);
    pane.delegate("blur", this._blurCalendarGrid, "." + CAL_GRID + ",." + CAL_DAY, this);
  },

  /**
   * Highlights a specific date node with keyboard highlight class
   * @method _highlightDateNode
   * @param oDate {Date} Date corresponding the node to be highlighted
   * @protected
   */   
  _highlightDateNode : function (oDate) {
    this._unhighlightCurrentDateNode();
    var newNode = this._dateToNode(oDate);
    newNode.focus();
    newNode.addClass(CAL_DAY_HILITED);
  },

  /**
   * Unhighlights a specific date node currently highlighted with keyboard highlight class
   * @method _unhighlightCurrentDateNode
   * @protected
   */   
  _unhighlightCurrentDateNode : function () {
    var allHilitedNodes = this.get("contentBox").all("." + CAL_DAY_HILITED);
    if (allHilitedNodes) {
      allHilitedNodes.removeClass(CAL_DAY_HILITED);
    }
  },

  /**
   * Returns the grid number for a specific calendar grid (for multi-grid templates)
   * @method _getGridNumber
   * @param gridNode {Node} Node corresponding to a specific grid
   * @protected
   */   
  _getGridNumber : function (gridNode) {
    var idParts = gridNode.get("id").split("_").reverse();
        return parseInt(idParts[0], 10);
  },

  /**
   * Handler for loss of focus of calendar grid
   * @method _blurCalendarGrid
   * @protected
   */   
   _blurCalendarGrid : function (ev) {
      this._unhighlightCurrentDateNode();
   },


  /**
   * Handler for gain of focus of calendar cell
   * @method _focusCalendarCell
   * @protected
   */ 
   _focusCalendarCell : function (ev) {
       this._highlightedDateNode = ev.target;
       ev.stopPropagation();
   },

  /**
   * Handler for gain of focus of calendar grid
   * @method _focusCalendarGrid
   * @protected
   */ 
   _focusCalendarGrid : function (ev) {     
       this._unhighlightCurrentDateNode();
       this._highlightedDateNode = null;
   },

  /**
   * Handler for keyboard press on a calendar grid
   * @method _keydownCalendar
   * @protected
   */ 
   _keydownCalendar : function (ev) {
    var gridNum = this._getGridNumber(ev.target),
        curDate = !this._highlightedDateNode ? null : this._nodeToDate(this._highlightedDateNode),
        keyCode = ev.keyCode,
        dayNum = 0,
        dir = '';

        switch(keyCode) {
          case KEY_DOWN: 
            dayNum = 7;
            dir = 's';
          break;
          case KEY_UP: 
            dayNum = -7;
            dir = 'n';
          break;
          case KEY_LEFT: 
            dayNum = -1;
            dir = 'w';
          break;
          case KEY_RIGHT:
            dayNum = 1;
            dir = 'e';
          break;
          case KEY_SPACE: case KEY_ENTER:
            ev.preventDefault();
            if (this._highlightedDateNode) {
            var selMode = this.get("selectionMode");
            if (selMode === "single" && !this._highlightedDateNode.hasClass(CAL_DAY_SELECTED)) {
                this._clearSelection(true);
                this._addDateToSelection(curDate);
            }
            else if (selMode === "multiple" || selMode === "multiple-sticky") {
                if (this._highlightedDateNode.hasClass(CAL_DAY_SELECTED)) {
                  this._removeDateFromSelection(curDate);
                }
                else {
                  this._addDateToSelection(curDate);
                }
             }
            }
          break;
        }
 

      if (keyCode == KEY_DOWN || keyCode == KEY_UP || keyCode == KEY_LEFT || keyCode == KEY_RIGHT) {

      if (!curDate) {
             curDate = ydate.addMonths(this.get("date"), gridNum);
             dayNum = 0;
      }
              ev.preventDefault();
          var newDate = ydate.addDays(curDate, dayNum),
              startDate = this.get("date"),
              endDate = ydate.addMonths(this.get("date"), this._paneNumber - 1),
              lastPaneDate = new Date(endDate);
              endDate.setDate(ydate.daysInMonth(endDate));
          
          if (ydate.isInRange(newDate, startDate, endDate)) {
/*
              var paneShift = (newDate.getMonth() - curDate.getMonth()) % 10;


              if (paneShift != 0) {
                var newGridNum = gridNum + paneShift,
                    contentBox = this.get('contentBox'),
                    newPane = contentBox.one("#" + this._calendarId + "_pane_" + newGridNum);
                    newPane.focus();
              }
*/
              this._highlightDateNode(newDate);
          }
          else if (ydate.isGreater(startDate, newDate)) {
            if (!ydate.isGreaterOrEqual(this.get("minimumDate"), startDate)) {
                 this.set("date", ydate.addMonths(startDate, -1));
                 this._highlightDateNode(newDate);
            }
          }
          else if (ydate.isGreater(newDate, endDate)) {
            if (!ydate.isGreaterOrEqual(lastPaneDate, this.get("maximumDate"))) {
                 this.set("date", ydate.addMonths(startDate, 1));
                 this._highlightDateNode(newDate);
            }
          }

        }
   },

  /**
   * Handles the calendar clicks based on selection mode.
   * @method _clickCalendar
   * @param {Event} ev A click event
   * @private
   */   
    _clickCalendar : function (ev) {
        var clickedCell = ev.target,
            clickedCellIsDay = clickedCell.hasClass(CAL_DAY) && 
                               !clickedCell.hasClass(CAL_PREVMONTH_DAY) && 
                               !clickedCell.hasClass(CAL_NEXTMONTH_DAY),
            clickedCellIsSelected = clickedCell.hasClass(CAL_DAY_SELECTED);
        switch (this.get("selectionMode")) {
          case("single"):
               if (clickedCellIsDay) {
                  if (!clickedCellIsSelected) {
                    this._clearSelection(true);  
                    this._addDateToSelection(this._nodeToDate(clickedCell));
                  }
             }
               break;
            case("multiple-sticky"):
               if (clickedCellIsDay) {
                 if (clickedCellIsSelected) {
                  this._removeDateFromSelection(this._nodeToDate(clickedCell));
                 }
                 else {
                  this._addDateToSelection(this._nodeToDate(clickedCell));
                 }
               }
               break;
            case("multiple"):
               if (!ev.metaKey && !ev.ctrlKey && !ev.shiftKey) {
                    this._clearSelection(true);
                    this._lastSelectedDate = this._nodeToDate(clickedCell);
                    this._addDateToSelection(this._lastSelectedDate);
               }
               else if (((os == 'macintosh' && ev.metaKey) || (os != 'macintosh' && ev.ctrlKey)) && !ev.shiftKey) {
                  if (clickedCellIsSelected) {
                    this._removeDateFromSelection(this._nodeToDate(clickedCell));
                    this._lastSelectedDate = null;
                  }
                  else {
                    this._lastSelectedDate = this._nodeToDate(clickedCell);
                    this._addDateToSelection(this._lastSelectedDate);
                  }
               }
               else if (((os == 'macintosh' && ev.metaKey) || (os != 'macintosh' && ev.ctrlKey)) && ev.shiftKey) {
                  if (this._lastSelectedDate) {
                    var selectedDate = this._nodeToDate(clickedCell);
                    this._addDateRangeToSelection(selectedDate, this._lastSelectedDate);
                    this._lastSelectedDate = selectedDate;
                  }
                  else {
                    this._lastSelectedDate = this._nodeToDate(clickedCell);
                    this._addDateToSelection(this._lastSelectedDate);
                  }

               }
               else if (ev.shiftKey) {
                    if (this._lastSelectedDate) {
                      var selectedDate = this._nodeToDate(clickedCell);
                      this._clearSelection(true);
                      this._addDateRangeToSelection(selectedDate, this._lastSelectedDate);
                      this._lastSelectedDate = selectedDate;
                    }
                    else {
                      this._clearSelection(true);
                      this._lastSelectedDate = this._nodeToDate(clickedCell);
                        this._addDateToSelection(this._lastSelectedDate);
                    }
               }
               break;
        }

      if (clickedCellIsDay) {
   /**
     * Fired when a specific date cell in the calendar is clicked. The event carries a 
     * payload which includes a `cell` property corresponding to the node of the actual
     * date cell, and a `date` property, with the `Date` that was clicked.
     *
     * @event dateClick
     */
        this.fire("dateClick", {cell: clickedCell, date: this._nodeToDate(clickedCell)});
      }
      else if (clickedCell.hasClass(CAL_PREVMONTH_DAY)) {
   /**
     * Fired when any of the previous month's days displayed before the calendar grid
     * are clicked.
     *
     * @event prevMonthClick
     */
        this.fire("prevMonthClick");
      }
      else if (clickedCell.hasClass(CAL_NEXTMONTH_DAY)) {
   /**
     * Fired when any of the next month's days displayed after the calendar grid
     * are clicked.
     *
     * @event nextMonthClick
     */
        this.fire("nextMonthClick");
      }
    },

  /**
   * Subtracts one month from the current calendar view.
   * @method subtractMonth
   */   
  subtractMonth : function (e) {
    this.set("date", ydate.addMonths(this.get("date"), -1));
    e.halt();
  },

  /**
   * Subtracts one year from the current calendar view.
   * @method subtractYear
   */ 
  subtractYear : function (e) {
    this.set("date", ydate.addYears(this.get("date"), -1));
    e.halt();
  },

  /**
   * Adds one month to the current calendar view.
   * @method addMonth
   */   
  addMonth : function (e) {    
    this.set("date", ydate.addMonths(this.get("date"), 1));
    e.halt();
  },

  /**
   * Adds one year to the current calendar view.
   * @method addYear
   */   
  addYear : function (e) {
    this.set("date", ydate.addYears(this.get("date"), 1));
    e.halt();
  }
},

{
   /**
    * The identity of the widget.
    *
    * @property NAME
    * @type String
    * @default 'calendar'
    * @readOnly
    * @protected
    * @static
    */  
  NAME: "calendar",

   /**
    * Static property used to define the default attribute configuration of
    * the Widget.
    *
    * @property ATTRS
    * @type {Object}
    * @protected
    * @static
    */  
  ATTRS: {

    /**
     * A setting specifying the type of selection the calendar allows.
     * Possible values include:
     * <ul>
     *   <li>`single` - One date at a time</li>
     *   <li>`multiple-sticky` - Multiple dates, selected one at a time (the dates "stick"). This option
     *   is appropriate for mobile devices, where function keys from the keyboard are not available.</li>
     *   <li>`multiple` - Multiple dates, selected with Ctrl/Meta keys for additional single
     *   dates, and Shift key for date ranges.</li>
     *
     * @attribute selectionMode
     * @type String
     * @default single
     */
    selectionMode: {
      value: "single"
    },

    /**
     * The date corresponding to the current calendar view. Always
     * normalized to the first of the month that contains the date
     * at assignment time. Used as the first date visible in the
     * calendar.
     *
     * @attribute date
     * @type Date
     * @default Today's date as set on the user's computer.
     */
    date: {
      value: new Date(),
      lazyAdd: false,
      setter: function (val) {

        var newDate = this._normalizeDate(val),
            newTopDate = ydate.addMonths(newDate, this._paneNumber - 1);
        var minDate = this.get("minimumDate");
        var maxDate = this.get("maximumDate");
            if ((!minDate || ydate.isGreaterOrEqual(newDate, minDate)) && 
                (!maxDate || ydate.isGreaterOrEqual(maxDate, newTopDate))) {
                return newDate;
            }

            else if (minDate && ydate.isGreater(minDate, newDate)) {
                   return minDate;
            }

            else if (maxDate && ydate.isGreater(newTopDate, maxDate)) {
                var actualMaxDate = ydate.addMonths(maxDate, -1*(this._paneNumber - 1));
                  return actualMaxDate;
            }
     }
    },

    /**
     * The minimum date that can be displayed by the calendar. The calendar will not
     * allow dates earlier than this one to be set, and will reset any earlier date to
     * this date. Should be `null` if no minimum date is needed.
     *
     * @attribute minimumDate
     * @type Date
     * @default null
     */
    minimumDate: {
      value: null,
      setter: function (val) {
        if (val) {
          var curDate = this.get('date'),
              newMinDate = this._normalizeDate(val);
          if (curDate && !ydate.isGreaterOrEqual(curDate, newMinDate)) {
              this.set('date', newMinDate);
          }
          return newMinDate;
        }
        else {
          return this._normalizeDate(val);
        }
      }
    },

    /**
     * The maximum date that can be displayed by the calendar. The calendar will not
     * allow dates later than this one to be set, and will reset any later date to
     * this date. Should be `null` if no maximum date is needed.
     *
     * @attribute maximumDate
     * @type Date
     * @default null
     */
    maximumDate: {
      value: null,
      setter: function (val) {
        if (val) {
          var curDate = this.get('date'),
              newMaxDate = this._normalizeDate(val);
          if (curDate && !ydate.isGreaterOrEqual(val, ydate.addMonths(curDate, this._paneNumber - 1))) {
              this.set('date', ydate.addMonths(newMaxDate, -1*(this._paneNumber -1)));
          }
          return newMaxDate;
        }
        else {
          return val;
        }
      }
    }
  }
});


}, '3.5.1' ,{requires:['calendar-base', 'calendarnavigator'], lang:['de', 'en', 'fr', 'ja', 'nb-NO', 'pt-BR', 'ru', 'zh-HANT-TW']});