Skip to content

Commit

Permalink
begin dcalendar
Browse files Browse the repository at this point in the history
  • Loading branch information
nowa committed Jul 18, 2008
1 parent a8a5eaa commit cdaaea0
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 46 deletions.
63 changes: 63 additions & 0 deletions examples/dcalendar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>dcalendar</title>
<meta name="generator" content="TextMate http://macromates.com/">
<meta name="author" content="nowa">
<script src="../src/core/jplus.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/core/browser.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/core/object.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/core/function.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/class/class.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/native/string.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/native/enumerable.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/native/array.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/native/number.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/native/hash.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/native/date.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/dom/element.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/event/event.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/dom/element.event.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/request/request.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/utilities/dcalendar.js" type="text/javascript" charset="utf-8"></script>
<!-- Date: 2008-07-17 -->
<style type="text/css" media="screen">
#main {
width: 950px;
margin: 0 auto;
}

h3 .author{
font-size: 12px;
font-weight: normal;
}

.dcalendar{
background-color:#CCCCCC;
text-align:center;
}

.dcalendar td{
background: #fff;
}

.dcalendar .dcal-days td{
width: 20px;
height: 30px;
line-height: 30px;
}
</style>
</head>
<body>

<div id="main">
<h3>DCalendar <span class="author">by <a href="http://nowazhu.com/blog/">nowa</a></span></h3>

<div jpc="dcalendar"></div>
</div>

</body>
</html>
6 changes: 1 addition & 5 deletions examples/dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,13 @@
$('foo').observe('click', showfoo);
i.addEvent('onA', function() {$('bar').innerHTML += '<br/>custom event fired.'});
i.fireEvent('onA');
(3).times(function() {
alert('uid:' + $('foo').uid);
});

var ajax = new Request();
ajax.addEvents({
onCreate: function() {alert('create')},
onRequest: function() {alert('request')},
onSuccess: function() {alert('Success')},
onException: function() {alert('exception')},
onException: function(request, e) {alert(e.description ? e.description : e)},
on200: function() {alert('200')}
});
ajax.send({
Expand All @@ -66,7 +63,6 @@
}
// onComplete: function(request, response) {alert(response.responseText);}
});
alert(ajax.post);
});
</script>
<!-- Date: 2008-06-03 -->
Expand Down
69 changes: 32 additions & 37 deletions src/dom/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function $(element, notrash) {
if (Object.isString(element))
element = document.getElementById(element);
element.uid = element.uid || [Element.UID++];
return (!notrash && Garbage.collect(element)) ? Element.extend(element) : element;
return (!notrash) ? Element.extend(element) : element;
}

// element constructor
Expand All @@ -40,6 +40,14 @@ function $(element, notrash) {

Element.UID = 0;

Element.Storage = {

get: function(uid){
return (this[uid] = this[uid] || {});
}

};

Element.Methods = {
/*
* 元素可见状态
Expand Down Expand Up @@ -141,6 +149,28 @@ Element.Methods = {
});
return results;
}).join('&');
},

retrieve: function(element, property, dflt){
element = $(element);
var storage = Element.Storage.get(element.uid);
var prop = storage[property];
if ($defined(dflt) && !$defined(prop)) prop = storage[property] = dflt;
return $pick(prop);
},

store: function(element, property, value){
element = $(element);
var storage = Element.Storage.get(element.uid);
storage[property] = value;
return element;
},

eliminate: function(element, property){
element = $(element);
var storage = Element.Storage.get(element.uid);
delete storage[property];
return element;
}
};

Expand Down Expand Up @@ -294,39 +324,4 @@ Element.Attributes = {
}))).toObject();
delete EA.Camels;

})(Element.Attributes);

var Garbage = {

Elements: {},

ignored: {object: 1, embed: 1, OBJECT: 1, EMBED: 1},

collect: function(el){
if (el.$attributes) return true;
if (Garbage.ignored[el.tagName]) return false;
Garbage.Elements[el.uid] = el;
el.$attributes = {};
return true;
},

trash: function(elements){
for (var i = elements.length, el; i--; i) Garbage.kill(elements[i]);
},

kill: function(el){
if (!el || !el.$attributes) return;
delete Garbage.Elements[el.uid];
if (el.retrieve('events')) el.removeEvents();
for (var p in el.$attributes) el.$attributes[p] = null;
if (Browser.Engine.trident){
for (var d in Element.Prototype) el[d] = null;
}
el.$attributes = el.uid = null;
},

empty: function(){
for (var uid in Garbage.Elements) Garbage.kill(Garbage.Elements[uid]);
}

};
})(Element.Attributes);
3 changes: 2 additions & 1 deletion src/event/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ Event.Customs = Class.create({
if (!this._events || !this._events[type]) return this;
options = options || {};
options.bind = options.bind || this;
args = args || [];
args = options.event ? [options.event].concat($A(args)) : args;
this._events[type].each(function(fn) {
var _call = function() { return fn.apply(options.bind, args) };
if (options.delay)
if ($defined(options.delay))
_call.delay(options.delay);
else
_call();
Expand Down
2 changes: 1 addition & 1 deletion src/native/enumerable.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var Enumerable = {
iterator.call(context, value, index++);
});
} catch(e) {
if (e != $break) throw e;
if (e != $break) { throw e; }
}
return this;
},
Expand Down
2 changes: 1 addition & 1 deletion src/native/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Object.extend(Number.prototype, {
* @return {Number} number
*/
times: function(iterator, context) {
new Array(parseInt(this)).each(iterator, context);
new Array(parseInt(this)).map(function(value, index) {return index}).each(iterator, context);
return this;
},

Expand Down
33 changes: 32 additions & 1 deletion src/request/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,35 @@ var methods = {};
});

Object.extend(Request.prototype, methods);
})();
})();

Element.Properties.send = {

set: function(options){
var send = this.retrieve('send');
if (send) send.cancel();
return this.eliminate('send').store('send:options', Object.extend({
data: this, link: 'cancel', method: this.get('method') || 'post', url: this.get('action')
}, options));
},

get: function(options){
if (options || !this.retrieve('send')){
if (options || !this.retrieve('send:options')) this.set('send', options);
this.store('send', new Request(this.retrieve('send:options')));s
}
return this.retrieve('send');
}

};

Element.addMethods({

send: function(element, url){
element = $(element);
var sender = element.get('send');
sender.send({data: element, url: url || sender.options.url});
return element;
}

});
109 changes: 109 additions & 0 deletions src/utilities/dcalendar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//
// 支持拖动选择的横向日历
// JPlus -> dcalendar.js
//
// Created by nowa on 2008-07-17.
// Copyright 2008 jplus.nowazhu.com. All rights reserved.
//

var DCalendar = {
DCID: 0
};

DCalendar.core = Class.create({

implement: Event.Customs,

options: {
autoshow: true,
showfix: false,
wrapper: null
},

days_of_month: [31,28,31,30,31,30,31,31,30,31,30,31],

containers: [],

defaultc: null,

on_days_line: false,

onmouse_start: 0,

onmouse_end: 0,

initialize: function(options) {
this.options = Object.extend(this.options, options || {});
this.today = Date.now();
this.year = this.today.year();
this.month = this.today.month();
this.day = this.today.day();

var _wrapper = this.options.wrapper ? this.options.wrapper : document;
var divs = _wrapper.getElementsByTagName('div');
$A(divs).each(function(div) {
if (div.getAttribute('jpc') == 'dcalendar') {
div = $(div);
div.set('DCID', div.get('DCID') || DCalendar.DCID++);
div.id = 'dcid_' + div.get('DCID');
this.containers.push(div);
}
}, this);
},

show: function() {
this.containers.each(function(cal) {
this.defaultc = cal;
this.options = Object.extend(this.options, cal.get('options') || {});
this.create();
}, this);
},

before_create: function() {
this.fireEvent('beforeCreate');
},

create: function() {
this.before_create();
var _days = this.get_month_days(this.month-1);
var _html = '<table class="dcalendar" border="0" cellspacing="1" cellpadding="4"><tbody><tr class="dcal-title"><td colspan="' + _days + '">' + this.today.strftime('%Y年%m月') + '</td></tr><tr class="dcal-days" id="' + this.defaultc.id + '_days">';
_days.times(function(i) {
_html += '<td>' + (i + 1) + '</td>';
});
_html += '</tr></tbody></table>';
this.defaultc.innerHTML = _html;

this.enabled();
},

enabled: function() {
var days_line = $(this.defaultc.id + '_days');
this.on_days_line = false;
this.onmouse_start = 0;
this.onmouse_end = 0;

var _dcal = this, _tds = days_line.getElementsByTagName('td');
$A(_tds).each(function(td) {
td.observe('click', function(e) {
this.className = this.className == 'selected' ? '' : 'selected';
_dcal.refresh_selected(this.parentNode.parentNode.parentNode.parentNode);
})
});
},

refresh_selected: function(container) {

},

get_month_days: function(month) {
var _days = this.days_of_month[month];
if (month==1 && (year%4==0 && year%100!=0 || year%400==0)) _days++;
return _days;
}

});

document.observe('dom:ready', function() {
var dcal = new DCalendar.core({wrapper: $('main')});
dcal.show();
});

0 comments on commit cdaaea0

Please sign in to comment.