Web Frontend Develope, Design, Music, Life——Stay Hungry, Stay Foolish~

基于YUI3的Dialog

先看Demo

主要用到了类的继承,YUI3的overlay、plugin组件

/**
* A YUI Dialog widget
* @class Dialog
* @namespace Y.Dialog
* @author kongyan@taobao.com
*/
YUI.add(’dialog’, function(Y){
//默认配置
var defaultConfig = {
anim: false,
width: ‘320px’,
height: ‘160px’,
duration: 0.5,
yesText: ‘确定’,
noText: ‘关闭’,
opacity: 0.5,
zIndex: 1000,
draggable: true,
modal: true
},
HEAD_STR = ‘<div class=”dialog-hd”><h3>{title}</h3><a class=”close” href=”#close”>关闭</a></div>’,
BODY_STR = ‘<div class=”dialog-bd”>{body}</div>’,
FOOTER_STR_ALERT = ‘<div class=”dialog-ft”><input class=”dialog-ok” type=”button” value=”{yesText}” /></div>’,
FOOTER_STR_CONFIRM = ‘<div class=”dialog-ft”><input class=”dialog-ok” type=”button” value=”{yesText}” /> <input class=”dialog-no” type=”button” value=”{noText}” /></div>’,
FOOTER_STR_BOX = ”;

//弹层基类
function overlayBase(title, body, cfg){
this.overlay = new Y.Overlay({
width: cfg.width,
height: cfg.height,
headerContent: Y.substitute(HEAD_STR, {title: title}),
bodyContent: Y.substitute(BODY_STR, {body: body}),
footerContent: cfg.foot,
centered: true,
shim: cfg.shim,
zIndex: cfg.zIndex
});
this.cfg = cfg;
this.render();
}
Y.mix(overlayBase.prototype, {
close: function(){
this.overlay.hide();
this.getNode(’box’).remove();
this.removeMask();
},
getNode: function(nodestr){
switch(nodestr){
case ‘box’:
return this.overlay.get(’boundingBox’);
break;
case ‘header’:
return this.overlay.getStdModNode(Y.WidgetStdMod.HEADER);
break;
case ‘body’:
return this.overlay.getStdModNode(Y.WidgetStdMod.BODY);
break;
case ‘footer’:
return this.overlay.getStdModNode(Y.WidgetStdMod.FOOTER);
break;
}
},
addMask: function(){
if(Y.one(’#dialog-mask’))return;
Y.one(’body’).append(’<div id=”dialog-mask” style=”position:absolute;top:0;left:0; background: #666; z-index: 100; width:’+Y.one(’document’).get(’docWidth’)+’px; height:’+Y.one(’document’).get(’docHeight’)+’px; opacity:’+this.cfg.opacity+’; filter:alpha(opacity=’+Math.floor(this.cfg.opacity*100)+’)”></div>’);
},
removeMask: function(){
if(Y.one(’#dialog-mask’)){
Y.one(’#dialog-mask’).remove();
}
},
render: function(){
var _s = this;
if(this.cfg.modal){
this.addMask();
}
this.overlay.render();
if(this.cfg.anim){
this.overlay.plug(AnimPlugin, {duration: this.cfg.duration});
}
if(this.cfg.draggable){
this.getNode(’header’).setStyle(’cursor’, ‘move’);
this.getNode(’box’).plug(Y.Plugin.Drag);
this.getNode(’box’).dd.addHandle(’.yui-widget-hd’);
}
this.getNode(’header’).query(’.close’).on(’click’, function(e){
e.halt();
_s.close();
});
this.addListener();
},
addListener: function(){
}
});

//alertDialog-派生自overlayBase
function alertDialog(title, body, cfg){
var cfg = Y.merge(defaultConfig, cfg||{});
cfg.foot = Y.substitute(FOOTER_STR_ALERT, {yesText: cfg.yesText});
alertDialog.superclass.constructor.apply(this, [title, body, cfg]);
}
Y.extend(alertDialog, overlayBase, {
addListener: function(){
var _s = this;
this.getNode(’footer’).query(’.dialog-ok’).on(’click’, function(e){
_s.close();
if(_s.cfg.yescallback){
_s.cfg.yescallback();
}
});
}
});

//confirmDialog-派生自overlayBase
function confirmDialog(title, body, cfg){
var cfg = Y.merge(defaultConfig, cfg||{});
cfg.foot = Y.substitute(FOOTER_STR_CONFIRM, {yesText: cfg.yesText, noText: cfg.noText});
confirmDialog.superclass.constructor.apply(this, [title, body, cfg]);
}
Y.extend(confirmDialog, overlayBase, {
addListener: function(){
var _s = this;
this.getNode(’footer’).query(’.dialog-ok’).on(’click’, function(e){
_s.close();
if(_s.cfg.yescallback){
_s.cfg.yescallback();
}
});
this.getNode(’footer’).query(’.dialog-no’).on(’click’, function(e){
_s.close();
if(_s.cfg.nocallback){
_s.cfg.nocallback();
}
});
}
});
//boxDialog-派生自overlayBase
function boxDialog(title, body, cfg){
var cfg = Y.merge(defaultConfig, cfg||{});
cfg.foot = Y.substitute(FOOTER_STR_BOX, {});
boxDialog.superclass.constructor.apply(this, [title, body, cfg]);
}
Y.extend(boxDialog, overlayBase, {
addListener: function(){

}
});
var Dialog = {
alert: alertDialog,
confirm: confirmDialog,
box: boxDialog
};
Y.Dialog = Dialog;
}, ‘3.0.0′, {requires: ['overlay', 'dd-plugin', 'plugin', 'substitute']});

2009/11/21CSS

0 Comments

那些不常用的css

虽然前端做了这么久,可是还记得当初刚开始做前端时一点一点翻着手册的日子,回头去看看,其实有一些css你可能并不知道,无意看到了一篇关于Cascading Style Sheet的博士论文,感觉发明CSS的人简直就是天才,今天又翻了一下CSS2.0的手册,整理了一些不常用但是有用的CSS(注:暂未考虑浏览器兼容性),有些已经CSS3的东西了,如果真正掌握了CSS,可以避免ie6的一些bug,可以参考前面写的一篇文章“关于IE的bug(CSS)”,接下来就是迎接CSS3吧。

1.计数器
counter-increment: none|identifier number
counter-resest: none|identifier number
content: before, content:after{content: counter(name, list-style-type);}

2.@media规则
@medea print, screen, all, aural, braille, embossed, handheld, projection, tty, tv{
body{font-size:10pt;}
}
原来有这么多媒介类型

3.border的样式
border-style : none | hidden | dotted | dashed | solid | double | groove | ridge | inset | outset

4.文本方向
direction: ltr|rtl|inherit ,和unicode-bidi:embed|bidi-override混合使用可以应用于内联文本

5.line-height和vertical-align
line-height:length|number|percentage line-height在vertical-align属性中有一定作用
vertical-align : auto | baseline | sub | super | top | text-top | middle | bottom | text-bottom | length

6.对象剪切
clip:auto|rect(number, number, number, number)

7.quotes: none|string(两个为一组)
content属性的open-quote和close-quote值不产生引号

8.字体描述
@font-face { font-family : name ; src : url ( url ) ; sRules }

9.文本阴影
text-shadow : color || length || length || opacity
可以被用于:first-letter和:first-line

10.字母间距、词间距、空白、文字换行和词内换行
letter-spacing: normal|length
word-spacing: normal|length
white-space : normal | pre | nowrap
word-wrap : normal | break-word
word-break : normal | break-all | keep-all
应用范围也是从上到下依次减少

11.转为大写
text-transform : none | capitalize | uppercase | lowercase,这在很多国外网站中会看到

12.表格标题定位、表格布局和空单元边框
caption-side : top | right | bottom | left
table-layout : auto | fixed
empty-cells : show | hide 当 border-collapse 属性等于 separate )时此属性才起作用

YUICONF 2009 PPT笔记

1.Event Delegation

<div id=“container”>
<ul>
<li id=“item-1″><em>Item Type One</em></li>
<li id=“item-2″><em>Item Type Two</em></li>
<li id=“item-3″><em>Item Type Three</em></li>
</ul>
</div>
YUI().use(“event-delegate”, function(Y) {
Y.delegate(“click”, function(e) {
Y.log(“Default scope: ” + this.get(“id”));
Y.log(“Clicked list item: ” + e.currentTarget.get(“id”));
Y.log(“Event target: ” + e.target);
Y.log(“Delegation container: ” + e.container.get(“id”));
}, “#container”, “li”);

});
只需要注册一个事件监听

2.Y.Profiler

YUI().use(‘profiler’, function(Y) {
// register function/method
Y.Profiler.registerFunction(‘Y.all’);
// register constructor
Y.Profiler.registerConstructor(‘Y.Node’);
// register object
Y.Profiler.registerObject(‘Y.Event’);

var average = Y.Profiler.getAverage(”Y.all”);

// start timer
Y.Profiler.start(‘addClass timer’);
Y.all(‘.modules’).addClass(‘.expand’);
// stop timer
Y.Profiler.stop(‘addClass timer’);

var report = Y.Profiler.getFullReport();//得到一个JSON对象
});
可以测试函数执行时间,可以对对象或原型注册方法,提供执行时间并且导出JSON格式结果

3.Flash Hybrids

Flash组件可以做什么:

  • 渲染音频、视频
  • 高级网络特性
  • 本地存储
  • 本地文件访问
  • 复杂的矢量图像渲染
  • 硬件加速渲染
  • 位图操作

<div id=”swfContainer” style=”width:500px;height:400px”></div>
var params = {version: 9.115,
useExpressInstall: false,
fixedAttributes:
{allowScriptAccess:”always”, allowNetworking:”all”, width:50},
flashVars:
{flashvar1: “One word”, flashvar2: “A word & another”, flashvar3: “Three words - 100% done & done”}
};
var newswf = new YAHOO.widget.SWF(”swfContainer”, “example.swf”, params);
newswf.addListener(”swfReady”, swfReadyHandler);

function swfReadyHandler() {
newswf.callSWF (”myMethod”,[arg1, arg2]);//通过callSWF来让swf调用myMethod函数。
}

4.YUI Doc

Comment Blocks
/**
* Description
* @tag tagValue
* @secondaryTag secondaryValue
*/

Primary Tags
‣ @module
‣ @class
‣ @property
‣ @event
‣ @method

@class
/**
* A YUI timepicker widget
*
* @class Timepicker
* @constructor
* @namespace Y.Saw
* @extends Widget
*/
YUI.add(’timepicker’, function(Y){

5.YUI Core

Y.log(message)
Y.dump(obj)
Y.later(),有一个cancel方法可以取消定时器
Y.cached(source, cache,refetch)–source是需要memoize的函数
var fnFoo = Y.cached(foo), cacheResult = fnFoo(arg1, arg2);
Y.Array
Y.Object
Y.mix
Y.merge
Y.substitue(s,o,f) s-模板,o-替代值的对象

6.OOP Modul

YUI().user(’oop’, function(Y){
Y.clone(obj);对象克隆
Y.aggregate(r,s, ov,wl);聚合对象
Y.augment(r,s,ov,wl,args);原型扩展
Y.extend(r,s,px,sx);类继承
Y.bind(f,c,args*);函数绑定一个context
Y.each(o,f,proto);遍历:o-Array/Object, f(key,value,o)
});

7.YUI3 Node

Y.Node.create();
node.append, node.prepend, node.insert, node.remove, node.replace, node.next, node.previous, node.ancestor, …

8.Y.Do 在指定的方法执行前或后加入执行一个方法
Y.Do.after(fn, obj, sFn, c)
Y.Do.before(fn, obj, sFn, c)
如果这样:FX = {wow:function(){}, wee:function(){}};
Gizmo类有 foo()
Doodad类有 bar()
则可以这样:Y.Do.before(FX.wow, myGizmo, ‘foo’);
Y.Do.after(FX.wee, myDoodad, ‘bar’);
myGizmo.foo();
myDoodad.bar();

8.YUI3 Key Event && Focus/Blur Events

Y.on(’key’, dialog.hide, ‘#dialog’, ‘down:27′, dialog);
function styleFocus(e) {
this.toggleClass(’.tb-button-focus’);
}
var node = Y.one(’#toolbar-1′);

node.delegate(’focus’, styleFocus, ‘.tb-button’);/通过委托,styleFocus里面的this就是’.tb-button’选择器选择的node节点
通过focus来进行迟绑定
function initEvents() {
this.delegate(’click’, toolbarClick);
handle.detach();
}
var handle = node.on(’focus’, initEvents);

9.Node的ARIA支持(It works != It’s usable)

node.setAttrs({role: ‘tab’, aria-disabled: true});
var tabview = Y.one(’#tabview-1′);
tabview.all(’.yui-tab>a’).set(’role’, ‘tab’);
tabview.all(’.yui-tabpanel’).set(’role’, ‘tabpanel’);

具体请参考YUI文档http://developer.yahoo.com/yui/3/api/

2009/11/05CSS

4 Comments

用Data URI Scheme嵌入内联图像

高性能网站的一个建议是减少HTTP连接数,所以使用内联图像嵌入页面可以减少HTTP连接数。目前浏览器支持Data URI的有:Opera 7.2+, Firefox, Safari, Netscape,  Mozilla, IE8

Data URI Scheme

data:[<mime type>][;charset=<charset>][;base64],<encoded data>

例如:

<img src=”data:image/gif;base64,R0lGODlhEAAOALMAAOazToeHh0tLS/7LZv/0jvb29t/f3//Ub/
/ge8WSLf/rhf/3kdbW1mxsbP//mf///yH5BAAAAAAALAAAAAAQAA4AAARe8L1Ekyky67QZ1hLnjM5UUde0ECwLJoExKcpp
V0aCcGCmTIHEIUEqjgaORCMxIC6e0CcguWw6aFjsVMkkIr7g77ZKPJjPZqIyd7sJAgVGoEGv2xsBxqNgYPj/gAwXEQA7″

对于IE8以下的版本,可以使用MHTML(Multipurpose Internet Mail Extensions HyperText Markup Language),例如:

/*
Content-Type: multipart/related; boundary=”_ANY_SEPARATOR”

–_ANY_SEPARATOR
Content-Location:somestring
Content-Transfer-Encoding:base64

iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAIAA[...snip...]SuQmCC
*/

#myid {
/* normal browsers */
background-image: url(”data:image/png;base64,iVBORw0[...snip...]“);
/* IE < 8 targeted with the star hack */
*background-image: url(mhtml:http://phpied.com/mhtml.css!somestring);
}

Data URI的好处是减少HTTP请求,坏处是浏览器对于URI的长度有限制以及增大了文档的大小。
相关链接:
生成Data URI的网址:
http://www.sveinbjorn.org/dataurlmaker
http://dancewithnet.com/lab/2009/data-uri-mhtml/create.php

phpied-http://www.phpied.com/data-urls-what-are-they-and-how-to-use/
wikipedia-http://en.wikipedia.org/wiki/Data:_URI_scheme
hedger wang-http://www.hedgerwow.com/360/dhtml/base64-image/demo.php
dancewithnet-http://dancewithnet.com/2009/08/15/data-uri-mhtml/
chencheng-http://www.chencheng.org/blog/2009/10/28/data-uri-try/

返回顶部