-
Notifications
You must be signed in to change notification settings - Fork 3
/
edcal.js
2699 lines (2294 loc) · 95.5 KB
/
edcal.js
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
*
* 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.
*
******************************************************************************/
/*
This is the WordPress editorial calendar. It is a continuous
calendar in both directions. That means instead of showing only
one month at a time it shows the months running together. Users
can scroll from one month to the next using the up and down
arrow keys, the page up and page down keys, the next and previous
month buttons, and their mouse wheel.
The calendar shows five weeks visible at a time and maintains 11
weeks of rendered HTML. Only the middle weeks are visible.
Week 1
Week 2
Week 3
- Week 4 -
| Week 5 |
| Week 6 |
| Week 7 |
- Week 8 -
Week 9
Week 10
Week 11
When the user scrolls down one week the new week is added at the
end of the calendar and the first week is removed. In this way
the calendar will only ever have 11 weeks total and won't use up
excessive memory.
This calendar uses AJAX to call into the functions defined in
edcal.php. These functions get posts and change post dates.
The HTML structure of the calendar is:
<div id="cal">
<div id="row08Nov2009">
<div id="row08Nov2009row">
<div class="day sunday nov" id="08Nov2009">
<div class="dayobj">
<div class="daylabel">8</div>
<ul class="postlist">
</ul>
</div>
</div>
</div>
</div>
</div>
*/
var edcal = {
/*
This final string represents the date which indicates to WordPress
that a post doesn't have a date.
*/
NO_DATE: '00000000',
/*
This value is the number of weeks the user wants to see at one time
in the calendar.
*/
weeksPref: 3,
/*
This is a preference value indicating if you see the post status
*/
statusPref: true,
/*
This is a preference value indicating if you see the post author
*/
authorPref: false,
/*
This is a preference value indicating if you see the post time
*/
timePref: true,
/*
This is a preference value indicating if we should prompt for feeback
*/
doFeedbackPref: true,
/*
* True if the calendar is in the process of moving
*/
isMoving: false,
/*
* True if we are in the middle of dragging a post
*/
inDrag: false,
/*
True if the calendar is in the process of queueing scrolling
during a drag.
*/
isDragScrolling: false,
/*
* This is the format we use to dates that we use as IDs in the
* calendar. It is independant of the visible date which is
* formatted based on the user's locale.
*/
internalDateFormat: 'ddMMyyyy',
/*
This is the position of the calendar on the screen in pixels.
It is an array with two fields: top and bottom.
*/
position: null,
/*
* This is the first date of the current month
*/
firstDayOfMonth: null,
/*
* This is the first day of the next month
*/
firstDayOfNextMonth: null,
/*
* The date format used by wordpress
*/
wp_dateFormat: 'yyyy-MM-dd',
/*
* The cache of dates we have already loaded posts for.
*/
cacheDates: [],
/*
* The ID of the timer we use to batch new post requests
*/
tID: null,
/*
* The number of steps moving for this timer.
*/
steps: 0,
/*
* The constant for the concurrency error.
*/
CONCURRENCY_ERROR: 4,
/*
* The constant for the user permission error
*/
PERMISSION_ERROR: 5,
/*
* The constant for the nonce error
*/
NONCE_ERROR: 6,
/*
The direction the calendar last moved.
true = down = to the future
false = up = to the past
*/
currentDirection: true,
/*
This date is our index. When the calendar moves we
update this date to indicate the next rows we need
to add.
*/
_wDate: Date.today(),
/*
* The date since the previous move
*/
moveDate: null,
/*
* This is a number from 0-6 indicating when the start
* of the week is. The user sets this in the Settings >
* General page and it is a single value for the entire
* server. We are setting this value in edcal.php
*/
startOfWeek: null,
/*
A cache of all the posts we have loaded so far. The
data structure is:
posts [date - ddMMMyyyy][posts array - post object from JSON data]
*/
posts: [],
/*
IE will sometimes fire the resize event twice for the same resize
action. We save it so we only resize the calendar once and avoid
any flickering.
*/
windowHeight: 0,
/*
This variable indicates if the calendar is in left to right or right to
left display mode.
*/
ltr: 'ltr',
/*
This variable indicates if the drafts drawer is visible or not.
*/
isDraftsDrawerVisible: false,
/*
* Initializes the calendar
*/
init: function() {
if (jQuery('#edcal_scrollable').length === 0) {
/*
* This means we are on a page without the editorial
* calendar
*/
return;
}
edcal.addFeedbackSection();
var draftsDrawerVisible = jQuery.cookie('edcal_drafts_drawer');
if (draftsDrawerVisible === 'true') {
edcal.isDraftsDrawerVisible = true;
edcal.setDraftsDrawerVisible(edcal.isDraftsDrawerVisible);
}
jQuery('#loading').hide();
jQuery('#edcal_scrollable').css('height', edcal.getCalHeight() + 'px');
edcal.windowHeight = jQuery(window).height();
/*
* Add the days of the week
*/
edcal.createDaysHeader();
/*
* We start by initializting the scrollable. We use this to manage the
* scrolling of the calendar, but don't actually call it to animate the
* scrolling. We specify an easing here because the default is "swing"
* and that has a conflict with JavaScript used in the BuddyPress plugin/
*
* This doesn't really change anything since the animation happens offscreen.
*/
jQuery('#edcal_scrollable').scrollable({
vertical: true,
size: edcal.weeksPref,
keyboard: false,
keyboardSteps: 1,
speed: 100,
easing: 'linear'
});
var api = jQuery('#edcal_scrollable').scrollable();
api.getConf().keyboard = false;
/*
When the user moves the calendar around we remember their
date and save it in a cookie. Then we read the cookie back
when we reload so the calendar stays where the user left
it last.
*/
var curDate = jQuery.cookie('edcal_date');
if (curDate) {
curDate = Date.parseExact(curDate, 'yyyy-dd-MM');
edcal.output('Resetting to date from the edcal_Date cookie: ' + curDate);
} else {
curDate = Date.today();
}
edcal.moveTo(curDate.clone());
jQuery('#edcal_scrollable').bind('mousewheel', function(event, delta) {
var dir = delta > 0 ? false : true, vel = Math.abs(delta);
edcal.output(dir + ' at a velocity of ' + vel);
if (!edcal.isMoving && vel > 0.2) {
edcal.move(1, dir);
}
return false;
});
/*
We are handling all of our own events so we just cancel all events from
the scrollable.
*/
api.onBeforeSeek(function(evt, direction) {
return false;
});
/*
* We also want to listen for a few other key events
*/
jQuery(document).bind('keydown', function(evt) {
//if (evt.altKey || evt.ctrlKey) { return; }
//edcal.output("evt.altKey: " + evt.altKey);
//edcal.output("evt.keyCode: " + evt.keyCode);
//edcal.output("evt.ctrlKey: " + evt.ctrlKey);
if (evt.keyCode === 27) { //escape key
return false;
}
if (jQuery('#edcal_quickedit').is(':visible')) {
return;
}
if ((evt.keyCode === 40 && !(evt.altKey || evt.ctrlKey))) { // down arrow key
edcal.move(1, true);
return false;
} else if ((evt.keyCode === 38 && !(evt.altKey || evt.ctrlKey))) { // up arrow key
edcal.move(1, false);
return false;
} else if ((evt.keyCode === 34 && !(evt.altKey || evt.ctrlKey)) || //page down
evt.keyCode === 40 && evt.ctrlKey) { // Ctrl+down down arrow
edcal.move(edcal.weeksPref, true);
return false;
} else if ((evt.keyCode === 33 && !(evt.altKey || evt.ctrlKey)) || //page up
evt.keyCode === 38 && evt.ctrlKey) { // Ctrl+up up arrow
edcal.move(edcal.weeksPref, false);
return false;
}
});
edcal.getPosts(edcal.nextStartOfWeek(curDate).add(-3).weeks(),
edcal.nextStartOfWeek(curDate).add(edcal.weeksPref + 3).weeks());
/*
Now we bind the listeners for all of our links and the window
resize.
*/
jQuery('#moveToToday').click(function() {
edcal.moveTo(Date.today());
edcal.getPosts(edcal.nextStartOfWeek(Date.today()).add(-3).weeks(),
edcal.nextStartOfWeek(Date.today()).add(edcal.weeksPref + 3).weeks());
return false;
});
jQuery('#moveToLast').click(function() {
if (edcal.lastPostDate === '-1') {
/*
* This happens when the blog doesn't have any posts
*/
return;
}
var d = Date.parseExact(edcal.lastPostDate, 'ddMMyyyy');
edcal.moveTo(d);
edcal.getPosts(edcal.nextStartOfWeek(d).add(-3).weeks(),
edcal.nextStartOfWeek(d).add(edcal.weeksPref + 3).weeks());
return false;
});
jQuery('#prevmonth').click(function() {
edcal.move(edcal.weeksPref, false);
return false;
});
jQuery('#nextmonth').click(function() {
edcal.move(edcal.weeksPref, true);
return false;
});
/*
We used to listen to resize events so we could make the calendar the right size
for the current window when it changed size, but this was causing a problem with
WordPress 3.3 and it never worked properly because the scroll position was a little
off so we are just skipping it.
*/
/*function resizeWindow(e) {
if (edcal.windowHeight != jQuery(window).height()) {
jQuery('#edcal_scrollable').css('height', edcal.getCalHeight() + 'px');
edcal.windowHeight = jQuery(window).height();
edcal.savePosition();
}
}
jQuery(window).bind('resize', resizeWindow);*/
jQuery('#newPostScheduleButton').on('click', function(evt) {
// if the button is disabled, don't do anything
if (jQuery(this).hasClass('disabled')) {
return false;
}
// Otherwise,
// make sure we can't make duplicate posts by clicking twice quickly
jQuery(this).addClass('disabled');
// and save the post
return edcal.savePost(null, false, true);
});
jQuery('#edcal-title-new-field').bind('keyup', function(evt) {
if (jQuery('#edcal-title-new-field').val().length > 0 &&
(!jQuery('#edcal-time').is(':visible') || jQuery('#edcal-time').val().length > 0)) {
jQuery('#newPostScheduleButton').removeClass('disabled');
} else {
jQuery('#newPostScheduleButton').addClass('disabled');
}
if (evt.keyCode === 13) { // enter key
/*
* If the user presses enter we want to save the draft.
*/
return edcal.savePost(null, true);
}
});
jQuery('#edcal-status').bind('change', function(evt) {
edcal.updatePublishButton();
});
jQuery('#edcal_weeks_pref').on('keyup', function(evt) {
if (jQuery('#edcal_weeks_pref').val().length > 0) {
jQuery('#edcal_applyoptions').removeClass('disabled');
} else {
jQuery('#edcal_applyoptions').addClass('disabled');
}
if (evt.keyCode === 13) { // enter key
edcal.saveOptions();
}
});
edcal.savePosition();
edcal.addOptionsSection();
jQuery('#edcal-time').timePicker({
show24Hours: edcal.timeFormat === 'H:i',
separator: ':',
step: 30
});
jQuery('#showdraftsdrawer').click(function() {
edcal.setDraftsDrawerVisible(!edcal.isDraftsDrawerVisible);
});
},
/*
* This function shows and hides the drafts drawer. Kind of clunky right now.
* Inits [loads content] only once.
*/
setDraftsDrawerVisible: function(/*boolean*/ visible, /*function*/ callback) {
var drawerwidth = '13%';
var drawerwidthmargin = '13.5%';
var showhideElement = jQuery('#showdraftsdrawer');
/* tells us if the drafts have been loaded for the first time */
if (!showhideElement.hasClass('isLoaded')) {
showhideElement.addClass('isLoaded');
edcal.setupDraftsdrawer(callback);
} else if (callback) {
/*
* If the drawer was already open we just call the callback
*/
callback();
}
if (visible) {
// edcal.output('showing draftsdrawer');
jQuery('#cal_cont').css({ 'margin-right': drawerwidthmargin });
jQuery('#draftsdrawer_cont').css({ display:'block', width:drawerwidth });
showhideElement.html(edcal.str_hidedrafts);
} else {
// edcal.output('hiding draftsdrawer');
jQuery('#cal_cont').css({ 'margin-right': '0' });
jQuery('#draftsdrawer_cont').css({ display:'none', width:'0' });
showhideElement.html(edcal.str_showdrafts);
}
edcal.isDraftsDrawerVisible = visible;
jQuery.cookie('edcal_drafts_drawer', visible, {expires: 2060});
},
/*
* Sets up the drafts drawer.
*/
setupDraftsdrawer: function(/*function*/ callback) {
jQuery('#draftsdrawer_loading').css({display:'block'});
edcal.getPosts(edcal.NO_DATE, null, function() {
edcal.initDraftsdrawer();
if (callback) {
callback();
}
});
},
/*
* Inits the drafts drawer, much like edcal.createRow()
* We could paginate this but right now we're just loading them all.
*/
initDraftsdrawer: function() {
var newrow = '';
newrow += '<a href="#" adddate="' + edcal.NO_DATE + '" class="daynewlink" style="margin-top: 5px;"' +
'title="' + edcal.str_newdraft + '" id="unscheduledNewLink" ' +
'onclick="edcal.addDraft(); return false;">' + edcal.str_addDraftLink + '</a>';
newrow += '<ul class="postlist">';
newrow += edcal.getPostItems(edcal.NO_DATE);
newrow += '</ul>';
edcal.draggablePost('#row' + edcal._wDate.toString(edcal.internalDateFormat) + ' li.post');
edcal.makeDroppable(jQuery('#draftsdrawer div.day'));
jQuery('#unscheduled').append(newrow);
jQuery('#draftsdrawer_loading').css({display:'none'});
var cal_cont = jQuery('#cal_cont');
jQuery('#unscheduled ul.postlist').css('min-height', ((cal_cont.height() - 10) -
jQuery('#draftsdrawer .draftsdrawerheadcont').height()) -
jQuery('#unscheduledNewLink').outerHeight());
jQuery('#unscheduled').mouseout(function() {
jQuery('#unscheduledNewLink').hide();
}).mouseover(function() {
jQuery('#unscheduledNewLink').show();
});
},
/*
This function aligns the grid in two directions. There
is a vertical grid with a row of each week and a horizontal
grid for each week with a list of days.
*/
alignGrid: function(/*string*/ gridid, /*int*/ cols, /*int*/ cellWidth, /*int*/ cellHeight, /*int*/ padding) {
if (jQuery(gridid).parent().attr('id') === 'draftsdrawer') {
return;
}
var x = 0;
var y = 0;
var count = 1;
jQuery(gridid).each(function() {
jQuery(this).css('position', 'relative');
var children = jQuery(this).children('div');
/*
In left to right languages the first day of the week shows
up on the left side of the calendar. In right to left languages
the first day of the week shows up on the right. We handle
this by changing the order of the cells in the layout code.
We only want to do this for the days of the week so we skip it
if we're dealing with just one column for the rows in the calendar.
*/
if (cols === 1 || edcal.ltr === 'ltr') {
for (var i = 0; i < children.length; i++) {
children.eq(i).css({
width: cellWidth + '%',
height: cellHeight + '%',
position: 'absolute',
left: x + '%',
top: y + '%'
});
if ((count % cols) === 0) {
x = 0;
y += cellHeight + padding;
} else {
x += cellWidth + padding;
}
count++;
}
} else {
for (var j = children.length - 1; j > -1; j--) {
children.eq(j).css({
width: cellWidth + '%',
height: cellHeight + '%',
position: 'absolute',
left: x + '%',
top: y + '%'
});
if ((count % cols) === 0) {
x = 0;
y += cellHeight + padding;
} else {
x += cellWidth + padding;
}
count++;
}
}
});
},
/*
This is a helper function to align the calendar so we don't
have to change the cell sizes in multiple places.
*/
alignCal: function() {
edcal.alignGrid('#cal', 1, 100, (100 / edcal.weeksPref) - 1, 1);
},
/*
This function creates the days header at the top of the
calendar.
*/
createDaysHeader: function() {
/*
* The first day of the week in the calendar depends on
* a wordpress setting and maybe the server locale. This
* means we need to determine the days of the week dynamically.
* Luckily the Date.js library already has these strings
* localized for us. All we need to do is figure out the
* first day of the week and then we can add a day from there.
*/
var date = Date.today().next().sunday();
/*
* We need to call nextStartOfWeek to make sure the
* edcal.startOfWeek variable gets initialized.
*/
edcal.nextStartOfWeek(date);
var html = '<div class="dayheadcont"><div class="dayhead firstday">' +
date.add(edcal.startOfWeek).days().toString('dddd') +
'</div>';
html += '<div class="dayhead">' + date.add(1).days().toString('dddd') + '</div>';
html += '<div class="dayhead">' + date.add(1).days().toString('dddd') + '</div>';
html += '<div class="dayhead">' + date.add(1).days().toString('dddd') + '</div>';
html += '<div class="dayhead">' + date.add(1).days().toString('dddd') + '</div>';
html += '<div class="dayhead">' + date.add(1).days().toString('dddd') + '</div>';
html += '<div class="dayhead lastday">' + date.add(1).days().toString('dddd') + '</div>';
jQuery('#cal_cont').prepend(html);
edcal.alignGrid('.dayheadcont', 7, 13.8, 100, 0.5);
},
/*
We have different styles for days in previous months,
the current month, and future months. This function
figures out the right class based on the date.
*/
getDateClass: function(/*Date*/ date) {
var monthstyle;
var daystyle;
if (date.compareTo(Date.today()) === -1) {
/*
* Date is before today
*/
daystyle = 'beforeToday';
} else {
/*
* Date is after today
*/
daystyle = 'todayAndAfter';
}
if (!edcal.firstDayOfMonth) {
/*
* We only need to figure out the first and last day
* of the month once
*/
edcal.firstDayOfMonth = Date.today().moveToFirstDayOfMonth().clearTime();
edcal.firstDayOfNextMonth = Date.today().moveToLastDayOfMonth().clearTime();
}
if (date.between(edcal.firstDayOfMonth, edcal.firstDayOfNextMonth)) {
/*
* If the date isn't before the first of the
* month and it isn't after the last of the
* month then it is in the current month.
*/
monthstyle = 'month-present';
} else if (date.compareTo(edcal.firstDayOfMonth) === 1) {
/*
* Then the date is after the current month
*/
monthstyle = 'month-future';
} else if (date.compareTo(edcal.firstDayOfNextMonth) === -1) {
/*
* Then the date is before the current month
*/
monthstyle = 'month-past';
}
if (date.toString('dd') === '01') {
/*
* This this date is the first day of the month
*/
daystyle += ' firstOfMonth';
}
return monthstyle + ' ' + daystyle;
},
/*
Show the add post link. This gets called when the mouse
is over a specific day.
*/
showAddPostLink: function(/*string*/ dayid) {
if (edcal.inDrag) {
return;
}
var createLink = jQuery('#' + dayid + ' a.daynewlink');
createLink.css('display', 'block');
createLink.bind('click', edcal.addPost);
},
/*
Hides the add new post link it is called when the mouse moves
outside of the calendar day.
*/
hideAddPostLink: function(/*string*/ dayid) {
var link = jQuery('#' + dayid + ' a.daynewlink').hide();
link.unbind('click', edcal.addPost);
},
/*
Creates a row of the calendar and adds all of the CSS classes
and listeners for each calendar day.
*/
createRow: function(/*jQuery*/ parent, /*bool*/ append) {
var _date = edcal._wDate.clone();
var newrow = '<div class="rowcont" id="' + 'row' + edcal._wDate.toString(edcal.internalDateFormat) + '">' +
'<div id="' + 'row' + edcal._wDate.toString(edcal.internalDateFormat) + 'row" class="edcal_row">';
for (var i = 0; i < 7; i++) {
/*
* Adding all of these calls in the string is kind of messy. We
* could do this with the JQuery live function, but there are a lot
* of days in the calendar and the live function gets a little slow.
*/
newrow += '<div onmouseover="edcal.showAddPostLink(\'' + _date.toString(edcal.internalDateFormat) + '\');" ' +
'onmouseout="edcal.hideAddPostLink(\'' + _date.toString(edcal.internalDateFormat) + '\');" ' +
'id="' + _date.toString(edcal.internalDateFormat) + '" class="day ' +
edcal.getDateClass(_date) + ' ' +
_date.toString('dddd').toLowerCase() + ' month-' +
_date.toString('MM').toLowerCase() + '">';
newrow += '<div class="dayobj">';
newrow += '<a href="#" adddate="' + _date.toString('MMMM d') + '" class="daynewlink" title="' +
sprintf(edcal.str_newpost, edcal.chineseAposWorkaround(_date.toString(Date.CultureInfo.formatPatterns.monthDay))) + '" ' +
'onclick="return false;">' + edcal.str_addPostLink + '</a>';
if (_date.toString('dd') === '01') {
newrow += '<div class="daylabel">' + _date.toString('MMM d');
} else {
newrow += '<div class="daylabel">' + _date.toString('d');
}
newrow += '</div>';
newrow += '<ul class="postlist">';
newrow += edcal.getPostItems(_date.toString(edcal.internalDateFormat));
newrow += '</ul>';
newrow += '</div>';
newrow += '</div>';
_date.add(1).days();
}
newrow += '</div></div>';
if (append) {
parent.append(newrow);
} else {
parent.prepend(newrow);
}
/*
* This is the horizontal alignment of an individual week
*/
edcal.alignGrid('#row' + edcal._wDate.toString(edcal.internalDateFormat) + 'row', 7, 13.9, 100, 0.5);
edcal.draggablePost('#row' + edcal._wDate.toString(edcal.internalDateFormat) + ' li.post');
edcal.makeDroppable(jQuery('#row' + edcal._wDate.toString(edcal.internalDateFormat) + ' > div > div.day'));
return jQuery('row' + edcal._wDate.toString(edcal.internalDateFormat));
},
/*
* Make a specific post droppable
*/
makeDroppable: function(/*jQuery*/ day) {
day.droppable({
hoverClass: 'day-active',
accept: function(ui) {
/*
We only let them drag draft posts into the past. If
they try to drag and scheduled post into the past we
reject the drag. Using the class here is a little
fragile, but it is much faster than doing date
arithmetic every time the mouse twitches.
*/
if (jQuery(this).hasClass('beforeToday')) {
if (ui.hasClass('draft')) {
return true;
} else {
return false;
}
} else {
return true;
}
},
greedy: true,
tolerance: 'pointer',
drop: function(event, ui) {
//edcal.output('dropped ui.draggable.attr("id"): ' + ui.draggable.attr("id"));
//edcal.output('dropped on jQuery(this).attr("id"): ' + jQuery(this).attr("id"));
//edcal.output('ui.draggable.html(): ' + ui.draggable.html());
var dayId = ui.draggable.parent().parent().parent().attr('id');
//edcal.output('dayId: ' + dayId);
edcal.doDrop(dayId, ui.draggable.attr('id'), jQuery(this).attr('id'));
}
});
},
/*
* Handle the drop when a user drags and drops a post.
*/
doDrop: function(/*string*/ parentId, /*string*/ postId, /*string*/ newDate, /*function*/ callback) {
//edcal.output('doDrop(' + parentId + ', ' + postId + ', ' + newDate + ')');
var dayId = parentId;
// Step 0. Get the post object from the map
var post = edcal.findPostForId(parentId, postId);
// Step 1. Remove the post from the posts map
edcal.removePostFromMap(parentId, postId);
/*
Step 2. Remove the old element from the old parent.
We would like to just remove the item right away,
but on IE with JQuery UI 1.8 that causes an error
because it tries to access the properties of the
object to reset the cursor and it can't since the
object is not longer part of the DOM. That is why
we detach it instead of removing it.
However, this causes a small memory leak since every
drag will detach an element and never remove it. To
clean up we wait half a second until the drag is done
and then remove the item. Hacky, but it works.
*/
var oldPost = jQuery('#' + postId);
oldPost.detach();
setTimeout(function() {
oldPost.remove();
}, 500);
// Step 3. Add the item to the new DOM parent
// Step 3a. Check whether we dropped it on a day or on the Drafts Drawer
jQuery('#' + newDate + ' .postlist').append(edcal.createPostItem(post, newDate));
if (dayId === newDate) {
/*
If they dropped back on to the day they started with we
don't want to go back to the server.
*/
edcal.draggablePost('#' + newDate + ' .post');
} else {
// Step6. Update the date on the server
edcal.changeDate(newDate, post, callback);
}
},
/*
* This is a helper method to make an individual post item draggable.
*/
draggablePost: function(/*post selector*/ post) {
jQuery(post).each(function() {
var postObj = edcal.findPostForId(jQuery(this).parent().parent().parent().attr('id'),
jQuery(this).attr('id'));
if (edcal.isPostMovable(postObj)) {
jQuery(this).draggable({
revert: 'invalid',
appendTo: 'body',
helper: 'clone',
distance: 1,
addClasses: false,
start: function() {
edcal.inDrag = true;
},
stop: function() {
edcal.inDrag = false;
},
drag: function(event, ui) {
edcal.handleDrag(event, ui);
},
scroll: false,
refreshPositions: true
});
jQuery(this).addClass('draggable');
}
});
},
/*
When the user is dragging we scroll the calendar when they get
close to the top or bottom of the calendar. This function handles
scrolling the calendar when that happens.
*/
handleDrag: function(event, ui) {
if (edcal.isMoving || edcal.isDragScrolling
/*
TODO: make sure that if we are on top of the drafts drawer
we don't dragScroll.
*/
) {
return;
}
edcal.isDragScrolling = true;
if (event.pageY < (edcal.position.top + 10)) {
/*
This means we're close enough to the top of the calendar to
start scrolling up.
*/
edcal.move(1, false);
} else if (event.pageY > (edcal.position.bottom - 10)) {
/*
This means we're close enough to the bottom of the calendar
to start scrolling down.
*/
edcal.move(1, true);
}
/*
We want to start scrolling as soon as the user gets their mouse
close to the top, but if we just scrolle with every event then
the screen flies by way too fast. We wait here so we scroll one
row and wait three quarters of a second. That way it gives a
smooth scroll that doesn't go too fast to track.
*/
setTimeout(function() {
edcal.isDragScrolling = false;
}, 300);
},
/*
This is a utility method to find a post and remove it
from the cache map.
*/
removePostFromMap: function(/*string*/ dayobjId, /*string*/ postId) {
if (edcal.posts[dayobjId]) {
for (var i = 0; i < edcal.posts[dayobjId].length; i++) {
if (edcal.posts[dayobjId][i] &&
'post-' + edcal.posts[dayobjId][i].id === postId) {
edcal.posts[dayobjId][i] = null;
return true;
}
}
}
return false;
},
/*