@ateliee/jq.schedule
Version:
jQuery time schedule plugin
3 lines (2 loc) • 13.4 kB
JavaScript
"use strict";!function(f){const t="jqSchedule";var _={calcStringTime:function(e){e=e.split(":");return 60*Number(e[0])*60+60*Number(e[1])},formatTime:function(e){var t=e%3600;return""+Math.floor(e/36e3)+Math.floor(e/3600%10)+":"+(""+Math.floor(t/600)+Math.floor(t/60%10))},_saveSettingData:function(e){return this.data(t+"Setting",e)},_loadSettingData:function(){return this.data(t+"Setting")},_saveData:function(e){e=f.extend({tableStartTime:0,tableEndTime:0,schedule:[],timeline:[]},e);return this.data(t,e)},_loadData:function(){return this.data(t)},scheduleData:function(){var e=f(this),e=_._loadData.apply(e);return e?e.schedule:[]},timelineData:function(){var e=f(this),t=_._loadData.apply(e);let i=[],a;for(a in t.timeline)i[a]=t.timeline[a],i[a].schedule=[];for(a in t.schedule){var n=t.schedule[a];void 0!==n.timeline&&void 0!==i[n.timeline]&&i[n.timeline].schedule.push(n)}return i},resetData:function(){return this.each(function(){let e=f(this),t=_._loadData.apply(e);for(var i in t.schedule=[],_._saveData.apply(e,[t]),e.find(".sc_bar").remove(),t.timeline)t.timeline[i].schedule=[],_._resizeRow.apply(e,[i,0]);_._saveData.apply(e,[t])})},addSchedule:function(i,a){return this.each(function(){var e=f(this);let t={start:a.start,end:a.end,startTime:_.calcStringTime(a.start),endTime:_.calcStringTime(a.end),text:a.text,timeline:i};a.data&&(t.data=a.data),_._addScheduleData.apply(e,[i,t]),_._resetBarPosition.apply(e,[i])})},addRow:function(t,i){return this.each(function(){var e=f(this);_._addRow.apply(e,[t,i])})},resetRowData:function(){return this.each(function(){let e=f(this),t=_._loadData.apply(e);t.schedule=[],t.timeline=[],_._saveData.apply(e,[t]),e.find(".sc_bar").remove(),e.find(".timeline").remove(),e.find(".sc_data").height(0)})},setRows:function(i){return this.each(function(){var e,t=f(this);for(e in _.resetRowData.apply(t,[]),i)_.addRow.apply(t,[e,i[e]])})},setDraggable:function(i){return this.each(function(){let e=f(this),t=_._loadSettingData.apply(e);i!==t.draggable&&(t.draggable=i,_._saveSettingData.apply(e,t),i?e.find(".sc_bar").draggable("enable"):e.find(".sc_bar").draggable("disable"))})},setResizable:function(i){return this.each(function(){let e=f(this),t=_._loadSettingData.apply(e);i!==t.resizable&&(t.resizable=i,_._saveSettingData.apply(e,t),i?e.find(".sc_bar").resizable("enable"):e.find(".sc_bar").resizable("disable"))})},_getTimeLineNumber:function(t,e){var i=f(this),a=_._loadSettingData.apply(i);let n=0,l=0;var s,d=Math.ceil(e/(a.timeLineY+a.timeLinePaddingTop+a.timeLinePaddingBottom));for(s in a.rows){var r=a.rows[s];let e=0;if("object"==typeof r.schedule&&(e=r.schedule.length),t&&t.timeline&&e++,(l+=Math.max(e,1))>=d)break;n++}return n},_addScheduleBgData:function(l){return this.each(function(){let e=f(this);var t=_._loadSettingData.apply(e),i=_._loadData.apply(e),a=Math.ceil((l.startTime-i.tableStartTime)/t.widthTime),i=Math.floor((l.endTime-i.tableStartTime)/t.widthTime);let n=f('<div class="sc_bgBar"><span class="text"></span></div>');n.css({left:a*t.widthTimeX,top:0,width:(i-a)*t.widthTimeX,height:e.find(".sc_main .timeline").eq(l.timeline).height()}),l.text&&n.find(".text").text(l.text),l.class&&n.addClass(l.class),e.find(".sc_main .timeline").eq(l.timeline).append(n)})},_addScheduleData:function(h,e){let m=e;return m.startTime=m.startTime||_.calcStringTime(m.start),m.endTime=m.endTime||_.calcStringTime(m.end),this.each(function(){let l=f(this),s=_._loadSettingData.apply(l),d=_._loadData.apply(l);var e=Math.ceil((m.startTime-d.tableStartTime)/s.widthTime),t=Math.floor((m.endTime-d.tableStartTime)/s.widthTime);let i=f('<div class="sc_bar"><span class="head"><span class="time"></span></span><span class="text"></span></div>');var a=_.formatTime(m.startTime),n=_.formatTime(m.endTime),r=_._getScheduleCount.apply(l,[m.timeline]);i.css({left:e*s.widthTimeX,top:r*s.timeLineY+s.timeLinePaddingTop,width:(t-e)*s.widthTimeX,height:s.timeLineY}),i.find(".time").text(a+"-"+n),m.text&&i.find(".text").text(m.text),m.class&&i.addClass(m.class);l.find(".sc_main .timeline").eq(h).append(i),d.schedule.push(m),_._saveData.apply(l,[d]),s.onAppendSchedule&&s.onAppendSchedule.apply(l,[i,m]);r=d.schedule.length-1,i.data("sc_key",r),i.on("mouseup",function(){if(s.onClick&&!0!==f(this).data("dragCheck")&&!0!==f(this).data("resizeCheck")){let e=f(this);var t=e.data("sc_key");s.onClick.apply(l,[e,d.schedule[t]])}}),t=l.find(".sc_bar");let o=null,c=(t.draggable({grid:[s.widthTimeX,1],containment:l.find(".sc_main"),helper:"original",start:function(e,t){let i={};i.node=this,i.offsetTop=t.position.top,i.offsetLeft=t.position.left,i.currentTop=t.position.top,i.currentLeft=t.position.left,i.timeline=_._getTimeLineNumber.apply(l,[o,t.position.top]),i.nowTimeline=i.timeline,o=i},drag:function(e,t){if(f(this).data("dragCheck",!0),!o)return!1;let i=f(this);var a=i.data("sc_key"),n=_._getTimeLineNumber.apply(l,[o,t.position.top]);return t.position.left=Math.floor(t.position.left/s.widthTimeX)*s.widthTimeX,o.nowTimeline!==n&&(o.nowTimeline=n),o.currentTop=t.position.top,o.currentLeft=t.position.left,_._rewriteBarText.apply(l,[i,d.schedule[a]]),!0},stop:function(){f(this).data("dragCheck",!1),o=null;let e=f(this);var t=e.data("sc_key"),i=e.position().left,i=d.tableStartTime+Math.floor(i/s.widthTimeX)*s.widthTime,a=i+(d.schedule[t].endTime-d.schedule[t].startTime);d.schedule[t].start=_.formatTime(i),d.schedule[t].end=_.formatTime(a),d.schedule[t].startTime=i,d.schedule[t].endTime=a,s.onChange&&s.onChange.apply(l,[e,d.schedule[t]])}}),["e"]);return s.resizableLeft&&c.push("w"),t.resizable({handles:c.join(","),grid:[s.widthTimeX,s.timeLineY-s.timeBorder],minWidth:s.widthTimeX,containment:l.find(".sc_main_scroll"),start:function(){let e=f(this);e.data("resizeCheck",!0)},resize:function(e,t){t.element.height(t.size.height),t.element.width(t.size.width)},stop:function(){let e=f(this);var t=e.data("sc_key"),i=e.position().left,a=e.outerWidth(),n=d.tableStartTime+Math.floor(i/s.widthTimeX)*s.widthTime,i=d.tableStartTime+Math.floor((i+a)/s.widthTimeX)*s.widthTime,a=d.schedule[t].timeline;d.schedule[t].start=_.formatTime(n),d.schedule[t].end=_.formatTime(i),d.schedule[t].startTime=n,d.schedule[t].endTime=i,_._resetBarPosition.apply(l,[a]),_._rewriteBarText.apply(l,[e,d.schedule[t]]),e.data("resizeCheck",!1),s.onChange&&s.onChange.apply(l,[e,d.schedule[t]])}}),!1===s.draggable&&t.draggable("disable"),!1===s.resizable&&t.resizable("disable"),r})},_getScheduleCount:function(e){var t,i=f(this),a=_._loadData.apply(i);let n=0;for(t in a.schedule)a.schedule[t].timeline===e&&n++;return n},_addRow:function(p,u){return this.each(function(){let l=f(this),i=_._loadSettingData.apply(l),s=_._loadData.apply(l);var e=l.find(".sc_main .timeline").length,t="";let a=f(t+='<div class="timeline"></div>'),n=(u.title&&a.append('<span class="timeline-title">'+u.title+"</span>"),u.subtitle&&a.append('<span class="timeline-subtitle">'+u.subtitle+"</span>"),i.onInitRow&&i.onInitRow.apply(l,[a,u]),l.find(".sc_data_scroll").append(a),t="",f(t+='<div class="timeline"></div>'));for(var d=s.tableStartTime;d<s.tableEndTime;d+=i.widthTime){var r=f('<div class="tl"></div>');r.outerWidth(i.widthTimeX),r.data("time",_.formatTime(d)),r.data("timeline",p),n.append(r)}if(n.find(".tl").on("click",function(){i.onScheduleClick&&i.onScheduleClick.apply(l,[this,f(this).data("time"),f(this).data("timeline"),s.timeline[f(this).data("timeline")]])}),n.find(".tl").on("contextmenu",function(){return i.onScheduleClick&&i.onScheduleClick.apply(l,[this,f(this).data("time"),f(this).data("timeline"),s.timeline[f(this).data("timeline")]]),!1}),l.find(".sc_main").append(n),s.timeline[p]=u,_._saveData.apply(l,[s]),u.class&&""!==u.class&&(l.find(".sc_data .timeline").eq(e).addClass(u.class),l.find(".sc_main .timeline").eq(e).addClass(u.class)),u.schedule)for(var o in u.schedule){var o=u.schedule[o],c=o.start||_.calcStringTime(o.startTime),h=o.end||_.calcStringTime(o.endTime),m={};m.start=c,m.end=h,o.text&&(m.text=o.text),m.timeline=p,m.data={},o.data&&(m.data=o.data),_._addScheduleData.apply(l,[e,m])}_._resetBarPosition.apply(l,[e]),l.find(".sc_main .timeline").eq(e).droppable({accept:".sc_bar",drop:function(e,t){let i=t.draggable;var t=i.data("sc_key"),a=s.schedule[t].timeline,n=l.find(".sc_main .timeline").index(this);s.schedule[t].timeline=n,i.appendTo(this),_._resetBarPosition.apply(l,[a]),_._resetBarPosition.apply(l,[n])}}),i.onAppendRow&&l.find(".sc_main .timeline").eq(e).find(".sc_bar").each(function(){let e=f(this);var t=e.data("sc_key");i.onAppendRow.apply(l,[e,s.schedule[t]])})})},_rewriteBarText:function(a,n){return this.each(function(){var e=f(this),t=_._loadSettingData.apply(e),e=_._loadData.apply(e),i=a.position().left,e=e.tableStartTime+Math.floor(i/t.widthTimeX)*t.widthTime,i=e+(n.endTime-n.startTime),t=_.formatTime(e)+"-"+_.formatTime(i);f(a).find(".time").html(t)})},_resetBarPosition:function(u){return this.each(function(){let e=f(this);var t,i,a,n,l=_._loadSettingData.apply(e),s=e.find(".sc_main .timeline").eq(u).find(".sc_bar");let d=[],r=[],o=0,c,h,m;for(m=0;m<s.length;m++)d[m]={code:m,x:f(s[m]).position().left};for(d.sort(function(e,t){return e.x<t.x?-1:e.x>t.x?1:0}),m=0;m<d.length;m++){for(t=d[m].code,c=f(s[t]),o=0;o<r.length;o++){let e=!1;for(var p=0;p<r[o].length;p++)i=r[o][p],h=f(s[i]),i=c.position().left,n=c.position().left+c.outerWidth(),a=h.position().left,i<h.position().left+h.outerWidth()&&a<n&&(e=!0);if(!e)break}r[o]||(r[o]=[]),c.css({top:o*l.timeLineY+l.timeLinePaddingTop}),r[o][r[o].length]=t}_._resizeRow.apply(e,[u,r.length])})},_resizeRow:function(a,n){return this.each(function(){let e=f(this);var t=_._loadSettingData.apply(e),i=Math.max(n,1);e.find(".sc_data .timeline").eq(a).outerHeight(i*t.timeLineY+t.timeLineBorder+t.timeLinePaddingTop+t.timeLinePaddingBottom),e.find(".sc_main .timeline").eq(a).outerHeight(i*t.timeLineY+t.timeLineBorder+t.timeLinePaddingTop+t.timeLinePaddingBottom),e.find(".sc_main .timeline").eq(a).find(".sc_bgBar").each(function(){f(this).outerHeight(f(this).closest(".timeline").outerHeight())}),e.find(".sc_data").outerHeight(e.find(".sc_main_box").outerHeight())})},_resizeWindow:function(){return this.each(function(){let e=f(this);var t=_._loadSettingData.apply(e),i=_._loadData.apply(e),a=e.width()-t.dataWidth-t.verticalScrollbar,i=Math.floor((i.tableEndTime-i.tableStartTime)/t.widthTime);e.find(".sc_header_cell").width(t.dataWidth),e.find(".sc_data,.sc_data_scroll").width(t.dataWidth),e.find(".sc_header").width(a),e.find(".sc_main_box").width(a),e.find(".sc_header_scroll").width(t.widthTimeX*i),e.find(".sc_main_scroll").width(t.widthTimeX*i)})},_moveSchedules:function(e,o,c){return this.each(function(){let i=f(this),a=_._loadSettingData.apply(i),n=_._loadData.apply(i);var l,s,d,r=i.find(".sc_main .timeline").eq(e).find(".sc_bar");for(let t=0;t<r.length;t++){let e=f(r[t]);o.position().left<=e.position().left&&(l=e.position().left+a.widthTimeX*c,s=Math.floor((n.tableEndTime-n.tableStartTime)/a.widthTime)*a.widthTimeX-e.outerWidth(),e.css({left:Math.max(0,Math.min(l,s))}),l=e.data("sc_key"),d=(s=n.tableStartTime+Math.floor(e.position().left/a.widthTimeX)*a.widthTime)+(n.schedule[l].end-n.schedule[l].start),n.schedule[l].start=_.formatTime(s),n.schedule[l].end=_.formatTime(d),n.schedule[l].startTime=s,n.schedule[l].endTime=d,_._rewriteBarText.apply(i,[e,n.schedule[l]]),a.onChange&&a.onChange.apply(i,[e,n.schedule[l]]))}_._resetBarPosition.apply(i,[e])})},init:function(t){return this.each(function(){let i=f(this);var e,a=f.extend({className:"jq-schedule",rows:{},startTime:"07:00",endTime:"19:30",widthTimeX:25,widthTime:600,timeLineY:50,timeLineBorder:1,timeBorder:1,timeLinePaddingTop:0,timeLinePaddingBottom:0,headTimeBorder:1,dataWidth:160,verticalScrollbar:0,bundleMoveWidth:1,draggable:!0,resizable:!0,resizableLeft:!1,onInitRow:null,onChange:null,onClick:null,onAppendRow:null,onAppendSchedule:null,onScheduleClick:null},t),n=(_._saveSettingData.apply(i,[a]),_.calcStringTime(a.startTime)),l=_.calcStringTime(a.endTime);n-=n%a.widthTime,l-=l%a.widthTime,_._saveData.apply(i,[{tableStartTime:n,tableEndTime:l}]);i.append('<div class="sc_menu">\n<div class="sc_header_cell"><span> </span></div>\n<div class="sc_header">\n<div class="sc_header_scroll"></div>\n</div>\n</div>\n<div class="sc_wrapper">\n<div class="sc_data">\n<div class="sc_data_scroll"></div>\n</div>\n<div class="sc_main_box">\n<div class="sc_main_scroll">\n<div class="sc_main"></div>\n</div>\n</div>\n</div>'),i.addClass(a.className),i.find(".sc_main_box").on("scroll",function(){i.find(".sc_data_scroll").css("top",-1*f(this).scrollTop()),i.find(".sc_header_scroll").css("left",-1*f(this).scrollLeft())});let s=-1;for(let t=n;t<l;t+=a.widthTime)if(s<0||Math.floor(s/3600)!==Math.floor(t/3600)){d="",d+='<div class="sc_time">'+_.formatTime(t)+"</div>";let e=f(d);var d=Number(Math.min(3600*Math.ceil((t+a.widthTime)/3600),l)-t),d=Math.floor(d/a.widthTime);e.width(d*a.widthTimeX),i.find(".sc_header_scroll").append(e),s=t}for(e in f(window).on("resize",function(){_._resizeWindow.apply(i)}).trigger("resize"),a.rows)_._addRow.apply(i,[e,a.rows[e]])})}};f.fn.timeSchedule=function(e){return _[e]?_[e].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof e&&e?(f.error("Method "+e+" does not exist on jQuery.timeSchedule"),this):_.init.apply(this,arguments)}}(jQuery);
//# sourceMappingURL=jq.schedule.min.js.map