Skip to content

Commit

Permalink
Merge pull request xuanye#30 from nirgal/master
Browse files Browse the repository at this point in the history
Fix in ConcatEvents loosing events on long events
  • Loading branch information
xuanye authored Jun 27, 2017
2 parents 2e6f1be + f4d7514 commit 2d4ec33
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 94 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,9 @@ xgcalendar

**请求返回**

>{"events":[],"issort":true,"start":"\/Date(1261353600000)\/","end":"\/Date(1261958399000)\/","error":null}
>{"events":[],"start":"\/Date(1261353600000)\/","end":"\/Date(1261958399000)\/","error":null}
`events`的结构同参数`eventItems`的结构,
`issort`是否已在服务端排序(请在服务端做好排序),
`start``end`是本次请求的开始时间和结束时间,这里是date的json表示法
`error`是业务异常对象,可在服务端生成,结构为:`{ErrorCode:””,ErrorMsg:””}` 可在`onRequestDataError`中捕获,并做友好的提示。

Expand Down Expand Up @@ -198,4 +197,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3 changes: 1 addition & 2 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,9 @@ The server must compute the start date and end date, from the given showdate, vi

**response data sample**

>{"events":[],"issort":true,"start":"\/Date(1261353600000)\/","end":"\/Date(1261958399000)\/","error":null}
>{"events":[],"start":"\/Date(1261353600000)\/","end":"\/Date(1261958399000)\/","error":null}
`events`: Data format is identical to `eventItems`.
`issort`: Whether are sorted (please do that on the server side).
`start` and `end`: Current view date range, json date format.
`error`: An exception object can be generated by the server. Format is `{ErrorCode:””,ErrorMsg:””}`. It is handled by `onRequestDataError`.

Expand Down
103 changes: 25 additions & 78 deletions code/js/plugin/xgcalendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@
}
}
else {
responseData(data, data.start, data.end);
responseData(data);
pushER(data.start, data.end);
}
if (option.onAfterRequestData && $.isFunction(option.onAfterRequestData)) {
Expand Down Expand Up @@ -1169,90 +1169,37 @@
alert("url" + i18n.xgcalendar.i_undefined);
}
}
function responseData(data, start, end) {
var events;
if (data.issort == false) {
if (data.events && data.events.length > 0) {
events = data.sort(function(l, r) { return l[2] > r[2] ? -1 : 1; });
}
else {
events = [];
}
}
else {
events = data.events;
}
ConcatEvents(events, start, end);
function responseData(data) {
ConcatEvents(data.events);
render();

}
function clearrepeat(events, start, end) {
var jl = events.length;
if (jl > 0) {
var es = events[0][2];
var el = events[jl - 1][2];
for (var i = 0, l = option.eventItems.length; i < l; i++) {

if (option.eventItems[i][2] > el || jl == 0) {
break;
}
if (option.eventItems[i][2] >= es) {
for (var j = 0; j < jl; j++) {
if (option.eventItems[i][0] == events[j][0] && option.eventItems[i][2] < start) {
events.splice(j, 1); //重复了移除
jl--;
break;
}
}
}
}
}
}
function ConcatEvents(events, start, end) {
if (!events) {
function ConcatEvents(events) {
if (!events)
events = [];
}
if (events) {
if (option.eventItems.length == 0) {
option.eventItems = events;
}
else {
//清理重复
clearrepeat(events, start, end);
var l = events.length;
var sl = option.eventItems.length;
var sI = -1;
var eI = sl;
var s = start;
var e = end;
if (option.eventItems[0][2] > e) // 第一个的开始时间都要大于请求的最后一个的开始时间
{
option.eventItems = events.concat(option.eventItems);
return;
}
if (option.eventItems[sl - 1][2] < s) // 最后一个的开始时间都要小于请求的第一个的开始时间
{
option.eventItems = option.eventItems.concat(events);
return;
}
for (var i = 0; i < sl; i++) {
if (option.eventItems[i][2] >= s && sI < 0) {
sI = i;
//continue;
}
if (option.eventItems[i][2] > e) {
eI = i;
break;
}
}

var e1 = sI <= 0 ? [] : option.eventItems.slice(0, sI);
var e2 = eI == sl ? [] : option.eventItems.slice(eI);
option.eventItems = [].concat(e1, events, e2);
events = e1 = e2 = null;
}
events = [].concat(option.eventItems, events);

// sort by start date, then by event id
events.sort(function(l, r) {
if (l[2] < r[2])
return -1;
if (l[2] > r[2])
return 1;
// here l[2] == r[2]
return l[0] < r[0] ? -1 : 1;
});

// remove duplicates
for (i=0; i<events.length-1; ) {
if (events[i][0] == events[i+1][0])
events.splice(i+1, 1);
else
i++;
}
option.eventItems = events;
}

//region 工具函数开始 {
function weekormonthtoday(e) {
var th = $(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ public JsonCalendarViewData(List<object[]> eventList, DateTime startDate, DateTi
events = eventList;
start = startDate;
end = endDate;
issort = true;
}

public JsonCalendarViewData(List<object[]> eventList, DateTime startDate, DateTime endDate, bool isSort)
{
/* obsolete function */
start = startDate;
end = endDate;
events = eventList;
issort = isSort;
}
public JsonCalendarViewData(JsonError jsonError)
{
Expand All @@ -28,6 +27,7 @@ public JsonCalendarViewData(JsonError jsonError)
public List<object[]> events { get; private set; }
public bool issort
{
/* obsolete */
get;
private set;
}
Expand Down
2 changes: 1 addition & 1 deletion example/aspnet/AzureCalendarMvcWeb/Views/Home/Test.aspx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<script src="<%=Url.Content("~/Javascripts/jquery-1.4.js")%>" type="text/javascript"></script>
<script type="text/javascript">
debugger;
var data = "{\"events\":[[3032,\"hello\",new Date(1263571200000),new Date(1264262399000),1,1,0,-1,1,\"\",\"\"],[3033,\"english\",new Date(1263868200000),new Date(1263871800000),0,0,0,-1,1,null,\"\"],[3061,\"test\",new Date(1263879000000),new Date(1263884400000),0,0,0,-1,1,null,\"\"],[3030,\"red color in \",new Date(1263916800000),new Date(1264003199000),1,0,0,14,1,\"\",\"\"],[3041,\"testaaaaa\",new Date(1263916800000),new Date(1264003199000),1,0,0,-1,1,\"\",\"\"],[3026,\"test a\",new Date(1263960000000),new Date(1263967200000),0,0,0,12,1,\"\",\"\"],[3025,\"test\",new Date(1264051800000),new Date(1264057200000),0,0,0,-1,1,null,\"\"],[3080,\"test\",new Date(1264116600000),new Date(1264123800000),0,0,0,-1,1,null,\"\"],[3034,\"hello too\",new Date(1264140000000),new Date(1264143600000),0,0,0,-1,1,null,\"\"]],\"issort\":true,\"start\":new Date(1263744000000),\"end\":new Date(1264348799000),\"error\":null}";
var data = "{\"events\":[[3032,\"hello\",new Date(1263571200000),new Date(1264262399000),1,1,0,-1,1,\"\",\"\"],[3033,\"english\",new Date(1263868200000),new Date(1263871800000),0,0,0,-1,1,null,\"\"],[3061,\"test\",new Date(1263879000000),new Date(1263884400000),0,0,0,-1,1,null,\"\"],[3030,\"red color in \",new Date(1263916800000),new Date(1264003199000),1,0,0,14,1,\"\",\"\"],[3041,\"testaaaaa\",new Date(1263916800000),new Date(1264003199000),1,0,0,-1,1,\"\",\"\"],[3026,\"test a\",new Date(1263960000000),new Date(1263967200000),0,0,0,12,1,\"\",\"\"],[3025,\"test\",new Date(1264051800000),new Date(1264057200000),0,0,0,-1,1,null,\"\"],[3080,\"test\",new Date(1264116600000),new Date(1264123800000),0,0,0,-1,1,null,\"\"],[3034,\"hello too\",new Date(1264140000000),new Date(1264143600000),0,0,0,-1,1,null,\"\"]],\"start\":new Date(1263744000000),\"end\":new Date(1264348799000),\"error\":null}";
data = data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@");
data = data.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]");
data = data.replace(/(?:^|:|,)(?:\s*\[)+/g, "")
Expand Down
1 change: 0 additions & 1 deletion example/nodejs/routes/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ exports.query = function(req,res) {
ret.start = util.format("\/Date(%d)\/",dataformart.start.getTime());
ret.end = util.format("\/Date(%d)\/",dataformart.end.getTime());
ret.error = null;
ret.issort = true;
calendardao.QueryCalendar(qstart,qend,'demo',zonediff,function(dbdata){
ret.events = [];
for(var i=0,l=dbdata.length;i<l;i++)
Expand Down
5 changes: 2 additions & 3 deletions example/php-newstyle/calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function strtodate($strdata)
}
function GetCalendarsByRange()
{
//echo "{\"start\":\"\",\"end\":\"\",\"error\":null,\"issort\":true,\"events\":[]}";
//echo "{\"start\":\"\",\"end\":\"\",\"error\":null,\"events\":[]}";
//return ;
$ret = array();
$view_Type = getPref('viewtype'); // week,month,day
Expand All @@ -193,12 +193,11 @@ function GetCalendarsByRange()
$qstart = $dataformat['start_date'] +$zonediff*3600;
$qend = $dataformat['end_date'] +$zonediff*3600;
//查询数据库 GetClientIP();
// {"start":start,"end":end,"error":error,"issort":issort,"events":jsonlist}
// {"start":start,"end":end,"error":error,"events":jsonlist}
$ret["start"] = TimestampToJsonTime($dataformat['start_date']);
$ret["end"] = TimestampToJsonTime( $dataformat['end_date']);

$ret["error"] =NUll;
$ret["issort"] =TRUE;
//print_r($ret);
$ret["events"] = DbQueryCalendars(date("Y-m-d H:i:s",$qstart),date("Y-m-d H:i:s",$qend),GetClientIP(),$zonediff);

Expand Down
3 changes: 1 addition & 2 deletions example/php/calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,11 @@ function GetCalendarsByRange()
$qstart = $dataformat['start_date'] +$zonediff*3600;
$qend = $dataformat['end_date'] +$zonediff*3600;
//查询数据库 GetClientIP();
// {"start":start,"end":end,"error":error,"issort":issort,"events":jsonlist}
// {"start":start,"end":end,"error":error,"events":jsonlist}
$ret["start"] = TimestampToJsonTime($dataformat['start_date']);
$ret["end"] = TimestampToJsonTime( $dataformat['end_date']);

$ret["error"] =NUll;
$ret["issort"] =TRUE;
//print_r($ret);
$ret["events"] = DbQueryCalendars(date("Y-m-d H:i:s",$qstart),date("Y-m-d H:i:s",$qend),GetClientIP(),$zonediff);

Expand Down
4 changes: 2 additions & 2 deletions example/python/jsonhelp.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def convert_callist_to_strlist(callist,zonediff):
])
return strlist;
#----------------------------------------------------------------------
def build_json_calendar_viewdata(start,end,events,issort=True,error=None,zonediff=8):
def build_json_calendar_viewdata(start,end,events,error=None,zonediff=8):
"""创建日程视图的json对象"""
jsonlist =convert_callist_to_strlist(events,zonediff)
return {"start":start,"end":end,"error":error,"issort":issort,"events":jsonlist}
return {"start":start,"end":end,"error":error,"events":jsonlist}

0 comments on commit 2d4ec33

Please sign in to comment.