You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
1.6 KiB

  1. package;
  2. typedef CalEvent = {
  3. name:String,
  4. //start:Date,
  5. //end:Date,
  6. color:Int,
  7. }
  8. /**
  9. * Calendar utility
  10. *
  11. * @author fbarbut<francois.barbut@gmail.com>
  12. */
  13. class Calendar
  14. {
  15. public static var COLOR_CONTRACT = 0xC91F25;
  16. public static var COLOR_DELIVERY = 0x7BAD1C;
  17. public static var COLOR_ORDER = 0xFF9615;
  18. public function new()
  19. {
  20. }
  21. public static function getMonthViewMap():Map<String,Array<CalEvent>> {
  22. var n = Date.now();
  23. var m = n.getMonth();//0-11
  24. var pointer = Date.now();
  25. var out = new Map<String,Array<CalEvent>>();
  26. //find last monday
  27. for ( i in 0...40) {
  28. if ( pointer.getDay() == 1 ) {
  29. break;
  30. }
  31. pointer = DateTools.delta(pointer, -1000.0 * 60 * 60 * 24);
  32. }
  33. //go ahead for at least 27 days
  34. for ( i in 0...28) {
  35. out.set( pointer.toString().substr(0, 10), [] );
  36. pointer = DateTools.delta(pointer, 1000.0 * 60 * 60 * 24);
  37. }
  38. //find end
  39. for ( i in 0...40) {
  40. //if ( pointer.getDay() == 1 && pointer.getMonth() != m) break;
  41. out.set( pointer.toString().substr(0, 10), [] );
  42. pointer = DateTools.delta(pointer, 1000.0 * 60 * 60 * 24);
  43. }
  44. return out;
  45. }
  46. /**
  47. * get ordered CalEvents from an unordered stringMap
  48. */
  49. public static function mapToArray(input : Map<String,Array<CalEvent>>) : Array<{d:Date,events:Array<CalEvent>}> {
  50. var keys = [];
  51. for (k in input.keys()) keys.push(k);
  52. keys.sort(function(a, b) {
  53. return Math.round(Date.fromString(a).getTime()/1000) - Math.round(Date.fromString(b).getTime()/1000);
  54. });
  55. var out = [];
  56. for ( k in keys) {
  57. var x = input.get(k);
  58. out.push( { d:Date.fromString(k),events:x } );
  59. }
  60. return out;
  61. }
  62. }