diff --git a/js/lib/avatar/avatar.js b/js/lib/avatar/avatar.js new file mode 100644 index 0000000..140d604 --- /dev/null +++ b/js/lib/avatar/avatar.js @@ -0,0 +1,92 @@ +/** + * + * @authors yutent (yutent@doui.cc) + * @date 2017-03-17 20:55:57 + * + */ + +"use strict"; + + +// 1216fc0c668c09ade029ceae6db87f97cf560e21 +define(function(){ + + var Avatar = function(){ + this.sum = function(arr){ + var sum = 0; + arr.forEach(function(it){ + sum -= -it + }) + } + }; + + Avatar.prototype = { + get: function(hash, size){ + if(!hash) + return this.defafultImg; + + if(!size || size < 100){ + size = 100 + } + + var cv = document.createElement('canvas'), + ct = cv.getContext('2d'), + bg = hash.slice(-3), + color = hash.slice(-9, -6), + fixColor = color, + lens = hash.slice(0, 8).match(/([\w]{1})/g), + pos1 = hash.slice(8, 16).match(/([\w]{1})/g), + pos2 = hash.slice(16, 24).match(/([\w]{1})/g), + step = size / 10; + + + + cv.width = size; + cv.height = size; + + lens = lens.map(c => { + c = parseInt(c, 16); + return c % 8 + }) + pos1 = pos1.map(c => { + c = parseInt(c, 16); + return c % 4 + }) + pos2 = pos2.map(c => { + c = parseInt(c, 16); + return c % 4 + }) + fixColor = this.sum(lens) > 32 ? bg : color; + + ct.fillStyle = '#' + bg; + ct.fillRect(0, 0, size, size) + + for(var i = 1; i < 9; i++){ + + var xl = lens[i-1], + xp1 = pos1[i-1], + xp2 = pos2[i-1]; + + if(xl + xp1 > 8){ + xl = 8 - xp1 + } + ct.fillStyle = '#' + color; + ct.fillRect((xp1 + 1) * step, i * step, xl * step, step) + + ct.fillStyle = '#' + color; + ct.fillRect((9 - xp1 - xl) * step, i * step, xl * step, step) + + ct.fillStyle = '#' + fixColor; + ct.fillRect((xp2 + 1) * step, i * step, step, step) + + ct.fillStyle = '#' + fixColor; + ct.fillRect((8 - xp2) * step, i * step, step, step) + } + + return cv.toDataURL() + } + } + + return new Avatar() + +}) \ No newline at end of file diff --git a/js/lib/avatar/def.jpg b/js/lib/avatar/def.jpg new file mode 100644 index 0000000..8a23176 Binary files /dev/null and b/js/lib/avatar/def.jpg differ diff --git a/js/lib/count/doui.count.js b/js/lib/count/doui.count.js new file mode 100644 index 0000000..1a18aea --- /dev/null +++ b/js/lib/count/doui.count.js @@ -0,0 +1,153 @@ +/** + * + * @authors yutent (yutent@doui.cc) + * @date 2016-08-19 10:38:25 + * + */ + +"use strict"; + + +define(['avalon'], function(av){ + + + + av.component('do:count', { + $replace: true, + $template: '', + maxLen: 8, + speed: 1, + update: av.noop, + list: [], + $list: [], + total: 0, + style: 2, + $construct: function(opt, a, b){ + var vm = av.mix(a, b) + document.head.insertAdjacentHTML('afterBegin', '') + + vm.total = vm.total >> 0 + vm.maxLen = vm.maxLen || 8 + + return av.mix(opt, vm) + }, + $ready: function(vm, ele){ + + function updateList(val){ + val = numberFormat(val, vm.maxLen) + + vm.$list = [] + vm.$list = val.split('') + if(vm.style === 2){ + vm.$list = vm.$list.reverse() + val = vm.$list.join('').replace(/([\d,]{3})/g, '$1,') + val = val.replace(/^,|,$/g, '') + vm.$list = val.split('').reverse() + } + + vm.$list.forEach(function(it, i){ + + if(it === ','){ + if(!vm.list[i]) + vm.list.push({opt: 0, val: [it]}) + + }else{ + if(vm.list[i]){ + if(it !== vm.list[i].last){ + vm.list[i].last = it + vm.list[i].val.push(it) + var curr = ele.querySelectorAll('.num-box')[i] + curr.querySelector('.num').style.marginTop = vm.speed * 50 + 'px' + setTimeout(function(){ + vm.list[i].val.shift() + }, 300) + } + }else{ + vm.list.push({opt: 1, last: it, val: [it]}) + } + } + + }) + } + + updateList(vm.total) + + vm.update = function(val){ + if(val < 0) //确定滚动方向 + vm.speed = 1 + else + vm.speed = -1 + + + vm.total = (vm.total - 0) + val + + } + + + vm.$watch('total', function(n, o){ + + if(n === o) + return + + updateList(n) + + }) + + + + }, + + + }) + + + + + + + + + + + + + + + + + + + + + + + + + + + + //数字长度补全(前面加0) + function numberFormat(num, len){ + num += '' + if(num.length >= len) + return num + + while(num.length < len) + num = '0' + num + return num + } + + + + return av + + + + + +}) \ No newline at end of file diff --git a/js/lib/count/doui.count.min.js b/js/lib/count/doui.count.min.js new file mode 100644 index 0000000..1804f0e --- /dev/null +++ b/js/lib/count/doui.count.min.js @@ -0,0 +1 @@ +"use strict";define(["avalon"],function(t){function e(t,e){if(t+="",t.length>=e)return t;for(;t.length
  • {{el}}
  • ',maxLen:8,speed:1,update:t.noop,list:[],$list:[],total:0,style:2,$construct:function(e,i,l){var n=t.mix(i,l);return document.head.insertAdjacentHTML("afterBegin",""),n.total=n.total>>0,n.maxLen=n.maxLen||8,t.mix(e,n)},$ready:function(t,i){function l(l){l=e(l,t.maxLen),t.$list=[],t.$list=l.split(""),2===t.style&&(t.$list=t.$list.reverse(),l=t.$list.join("").replace(/([\d,]{3})/g,"$1,"),l=l.replace(/^,|,$/g,""),t.$list=l.split("").reverse()),t.$list.forEach(function(e,l){if(","===e)t.list[l]||t.list.push({opt:0,val:[e]});else if(t.list[l]){if(e!==t.list[l].last){t.list[l].last=e,t.list[l].val.push(e);var n=i.querySelectorAll(".num-box")[l];n.querySelector(".num").style.marginTop=50*t.speed+"px",setTimeout(function(){t.list[l].val.shift()},300)}}else t.list.push({opt:1,last:e,val:[e]})})}l(t.total),t.update=function(e){t.speed=0>e?1:-1,t.total=t.total-0+e},t.$watch("total",function(t,e){t!==e&&l(t)})}}),t}); \ No newline at end of file diff --git a/js/lib/datepicker/Readme.md b/js/lib/datepicker/Readme.md new file mode 100644 index 0000000..e3320a1 --- /dev/null +++ b/js/lib/datepicker/Readme.md @@ -0,0 +1,46 @@ +# 日历组件文档 + +## 配置说明 + +```json + { + showTime: false, //对话框上显示时间 + showCalendar: false, //显示日历对话框 + disabled: false, //是否禁用 + exclass: '', //输入框拓展样式, 用于外部调整输入框样式以适配各种场景 + duplex: '', + format: '', // 日期显示格式 + radius: 0, //日历输入框边框圆角半径 + border: 1, //日历输入框边框大小 + btns: { //切换年份/月份的按钮上的字符 + prevYear: '<<', + nextYear: '>>', + prevMonth: '<', + nextMonth: '>' + }, + callback: function(date){/*...*/}, //日期被修改后的回调,参数是被修改后的值 + } + +``` + + +## 用法 + +```html + +
    + 示例1: + +
    + + + +``` diff --git a/js/lib/datepicker/doui.datepicker.css b/js/lib/datepicker/doui.datepicker.css new file mode 100644 index 0000000..30eeebd --- /dev/null +++ b/js/lib/datepicker/doui.datepicker.css @@ -0,0 +1,40 @@ +@charset "UTF-8"; +/** + * + * @authors yutent (yutent@doui.cc) + * @date 2016-02-14 14:00:06 + * + */ +.do-ui-datepicker {position:relative;z-index:65534;width:100%;height:100%;} +.do-ui-datepicker a {text-decoration:none;} +.do-ui-datepicker .date-input {float:left;width:100%;height:100%;border:1px solid #ddd;line-height:18px;padding:0 5px;} + +.do-ui-datepicker .calendar {position:absolute;z-index:65534;left:0;top:98%;width:230px;height:auto;min-height:60px;padding:10px;border:1px solid #ddd;background:#fff;font-size:13px;box-shadow:0 0 5px rgba(0,0,0,.1)} + +.do-ui-datepicker .calendar-hd {width:100%;height:30px;line-height:30px;background:#f3f3f3;color:#666;text-align:center;} +.do-ui-datepicker .calendar-contrl {position:relative;width:90%;height:30px;margin:0 5%;line-height:30px;border-bottom:1px solid #eee;color:#09f;text-align:center;} +.do-ui-datepicker .calendar-contrl a {position:absolute;top:0;left:0;width:30px;color:#09f;} +.do-ui-datepicker .calendar-contrl .prev-month {left:30px;} +.do-ui-datepicker .calendar-contrl .next-month {left:auto;right:30px;} +.do-ui-datepicker .calendar-contrl .next-year {left:auto;right:0;} + +.do-ui-datepicker .calendar-table {position:relative;width:90%;height:auto;margin:0 5%;line-height:25px;color:#888;text-align:center;} +.do-ui-datepicker .calendar-table .tr {width:100%;height:auto;min-height:25px;} +.do-ui-datepicker .calendar-table .tr.tr-hd {border-bottom:1px solid #eee;margin-bottom:3px;} +.do-ui-datepicker .calendar-table .tr .td {float:left;width:14.28%;height:25px;} +.do-ui-datepicker .calendar-table .tr .do-st-hand:hover {background:#b6def9;} +.do-ui-datepicker .calendar-table .tr .td.weeken {color:#f30;} +.do-ui-datepicker .calendar-table .tr .td.selected {background:#09f;color:#fff;} +.do-ui-datepicker .calendar-table .tr .td.disabled {color:#ddd;cursor:default;} + +.do-ui-datepicker .time-contrl {position:relative;width:90%;height:30px;margin:5px 5%;line-height:30px;border-top:1px solid #eee;color:#888;} +.do-ui-datepicker .time-contrl label {float:left;height:20px;margin:5px 5px 5px 0;} +.do-ui-datepicker .time-contrl input {float:left;width:25px;height:20px;padding:0 3px;line-height:17px;outline:none;} +.do-ui-datepicker .time-contrl label:after {float:right;height:20px;line-height:20px;font-size:12px;} +.do-ui-datepicker .time-contrl .hours:after {content:"时"} +.do-ui-datepicker .time-contrl .minutes:after {content:"分"} +.do-ui-datepicker .time-contrl .seconds:after {content:"秒"} +.do-ui-datepicker .time-contrl .now {float:right;width:40px;height:20px;margin:5px;border:1px solid #ddd;border-radius:3px;line-height:20px;background:#f7f7f7;color:#888;text-align:center;} +.do-ui-datepicker .time-contrl .now:hover {background:#eee;} +.do-ui-datepicker .time-contrl .now:active {background:#e7e7e7;} +.do-ui-datepicker .calendar-tips {position:absolute;z-index:65535;left:25%;top:40%;width:50%;height:30px;line-height:30px;background:rgba(0,0,0,.7);color:#fff;font-size:12px;text-align:center;} \ No newline at end of file diff --git a/js/lib/datepicker/doui.datepicker.js b/js/lib/datepicker/doui.datepicker.js new file mode 100644 index 0000000..ec6b2b6 --- /dev/null +++ b/js/lib/datepicker/doui.datepicker.js @@ -0,0 +1,375 @@ +/** + * + * @authors yutent (yutent@doui.cc) + * @date 2016-02-14 13:58:39 + * + */ +"use strict"; + +define(['avalon', + 'css!./doui.datepicker.min' + ], + function(av){ + //父级vm + var parentVm = null + + av.component('do:datepicker', { + $replace: true, + $template: '
    请选择日期
    • {{el.day}}
    {{tips}}
    ', + $construct: function(opts, b, c){ + var vm = av.mix(b, c) + /********** 获取上一级vm ***********/ + var pctrl = vm.duplex.slice(0, vm.duplex.indexOf('.')) + parentVm = av.vmodels[pctrl] + /**********************************/ + //获取初始值 + vm.duplex = vm.duplex.slice(vm.duplex.indexOf('.') + 1) + var defaultVal = (new Function('v', 'return v.' + vm.duplex))(parentVm) + + //日期格式化, 默认会调用过滤器的格式'Y-m-d H:i:s' + if(!vm.showTime && !vm.format){ + vm.format = 'Y-m-d'; + } + + if(defaultVal === undefined){ + if(vm.minDate){ + defaultVal = vm.minDate, vm.format; + }else if(opts.maxDate){ + defaultVal = vm.maxDate, vm.format; + } + } + opts.dateVal = defaultVal && av.filters.date(defaultVal, vm.format) + opts.calendar = { + list: [], + year: av.filters.date(opts.dateVal, 'Y'), + month: av.filters.date(opts.dateVal, 'm'), + day: av.filters.date(opts.dateVal, 'd') || 0, + hour: av.filters.date(opts.dateVal, 'H') || 0, + minute: av.filters.date(opts.dateVal, 'i') || 0, + second: av.filters.date(opts.dateVal, 's') || 0, + minYear: vm.minDate && av.filters.date(vm.minDate, 'Y') >> 0, + minMonth: vm.minDate && av.filters.date(vm.minDate, 'm') >> 0, + minDay: vm.minDate && av.filters.date(vm.minDate, 'd') >> 0 || 1, + maxYear: vm.maxDate && av.filters.date(vm.maxDate, 'Y') >> 0, + maxMonth: vm.maxDate && av.filters.date(vm.maxDate, 'm') >> 0, + maxDay: vm.maxDate && av.filters.date(vm.maxDate, 'd') >> 0 + } + //移除部分属性 + delete vm.minDate; + delete vm.maxDate; + + return av.mix(opts, vm) + }, + $init: function(vm, ele){ + // av.log(vm) + var lastDate = { + year: vm.calendar.year >> 0, + month: vm.calendar.month >> 0, + day: vm.calendar.day >> 0 + } + var timer + + getCalendar() //初始化日历显示 + + //日历按钮,未超出限制时切换日历 + vm.$turn = function(type, step){ + var year = vm.calendar.year >> 0, + month = vm.calendar.month >> 0; + + if(type === 1){ + year += step; + }else{ + month += step; + if(month < 1){ + month = 12; + year-- + } + if(month > 12){ + month = 1; + year++ + } + } + if(isLimited(year, month) === true){ + vm.tips = '日期超出限制'; + return; + } + vm.calendar.year = year; + vm.calendar.month = numberFormat(month, 2); + } + + //选择日期 + vm.$getDate = function(disabled, day){ + if(disabled) + return; + + vm.calendar.day = day; + + changeStyle(day); + updateTime(); + vm.showCalendar = !1; + } + + //输入框获取焦点时,显示日历 + vm.$focus = function(){ + vm.showCalendar = !0; + } + + //获取当前时间 + vm.$now = function(){ + var year = av.filters.date(null, 'Y') >> 0, + month = av.filters.date(null, 'm') >> 0, + day = av.filters.date(null, 'd') >> 0; + + var isLimitYM = isLimited(year, month), + disabled = disabledDay(day, isLimitYM); + + if(disabled){ + vm.tips = '今天超出了限制日期'; + return; + } + + vm.calendar.year = year; + vm.calendar.month = month; + vm.calendar.day = day; + vm.calendar.hour = av.filters.date(null, 'H'); + vm.calendar.minute = av.filters.date(null, 'i'); + vm.calendar.second = av.filters.date(null, 's'); + + changeStyle(day); + updateTime(); + + vm.showCalendar = !1; + } + + + + /******************************************************************************/ + //计算日历数组 + function getCalendar(){ + var year = vm.calendar.year >> 0 + var month = vm.calendar.month >> 0 + var nums = getNumsOfYearMonth(year, month) + var numsFixed = -getDayByYearMonth(year, month) + 1 + var isLimitYM = isLimited(year, month) + + vm.calendar.list.clear(); + + for(var i = numsFixed; i <= nums; i++){ + + var day = { + weeken: !1, + day: '', + selected: !1, + disable: !0 + } + if(i > 0){ + var d = getDayByYearMonth(year, month, i) + day = { + weeken: d == 0 || d == 6, + day: i, + selected: isSelected(i), + disable: disabledDay(i, isLimitYM) + } + } + vm.calendar.list.push(day) + } + } + + //判断当前年/月是否超出限制 + function isLimited(year, month){ + var limit = { + Y: vm.calendar.minYear, + M: vm.calendar.minMonth, + mY: vm.calendar.maxYear, + mM: vm.calendar.maxMonth, + } + var res = '' + + if((!limit.Y && !limit.mY) || (!limit.M && !limit.mM)) + return false + + if(year){ + if((limit.Y && year < limit.Y) || (limit.mY && year > limit.mY)) + return true + }else{ + return false + } + + if(month){ + if(year === limit.Y){ + if(limit.M && month < limit.M) + return true + + if(month == limit.M) + res += '-' + } + + if(year === limit.mY){ + if(limit.mM && month > limit.mM) + return true + + if(month == limit.mM) + res += '+' + } + } + return res + } + + //判断指定天数是否有效 + function disabledDay(day, limitedYM){ + var minD = vm.calendar.minDay + var maxD = vm.calendar.maxDay + + if(limitedYM === '-') + return day < minD + + if(limitedYM === '+') + return maxD && day > maxD + + if(limitedYM === '-+') + return day < minD || (maxD && day > maxD) + + return limitedYM + } + + //判断指定天数是否被选中 + function isSelected(day){ + var year = vm.calendar.year >> 0 + var month = vm.calendar.month >> 0 + + return !(lastDate.year !== year || lastDate.month !== month || lastDate.day !== day) + } + + //修改当前选中日期的样式 + function changeStyle(day){ + vm.calendar.list.forEach(function(item){ + if(item.day != day){ + item.selected = !1 + }else{ + item.selected = !0 + } + }) + } + + //更新时间 + function updateTime(){ + var cal = vm.calendar + var year = cal.year + var month = cal.month + var day = cal.day + var hour = cal.hour + var minute = cal.minute + var second = cal.second + var date = year + '-' + month + '-' + day + + if(vm.showTime){ + date += ' ' + hour + ':' + minute + ':' + second + } + lastDate = { + year: year >> 0, + month: month >> 0, + day: day >> 0 + } + vm.dateVal = av.filters.date(date, vm.format) + } + + /******************************************************************************/ + + vm.$watch('calendar.year', function(){ + getCalendar(); + }) + + vm.$watch('calendar.month', function(){ + getCalendar(); + }) + vm.$watch('calendar.hour', function(v){ + vm.calendar.hour = v + updateTime() + }) + vm.$watch('calendar.minute', function(v){ + vm.calendar.minute = v + updateTime() + }) + vm.$watch('calendar.second', function(v){ + vm.calendar.second = v + updateTime() + }) + + vm.$watch('showCalendar', function(v){ + if(v || !vm.duplex) + return; + + eval('parentVm.' + vm.duplex + ' = "' + vm.dateVal + '"'); + vm.callback && vm.callback(vm.dateVal); + }) + + vm.$watch('tips', function(v){ + if(!v) + return; + clearTimeout(timer); + timer = setTimeout(function(){ + vm.tips = ''; + }, 1500) + }) + + document.addEventListener('click', function(){ + vm.showCalendar = !1; + }) + + }, + showTime: false, //对话框上显示时间 + showCalendar: false, //显示日历对话框 + disabled: false, //是否禁用 + exclass: '', //输入框拓展样式, 用于外部调整输入框样式以适配各种场景 + tips: '', + duplex: '', + format: '', // 日期显示格式 + dateVal: '', + radius: 0, //日历输入框边框圆角半径 + border: 1, //日历输入框边框大小 + btns: { //切换年份/月份的按钮上的字符 + prevYear: '<<', + nextYear: '>>', + prevMonth: '<', + nextMonth: '>' + }, + $focus: av.noop, + $turn: av.noop, + $getDate: av.noop, + $now: av.noop, + $cancelBubble: function(event){ + event.stopPropagation && event.stopPropagation() || (event.cancelBubble = true); + }, + callback: null, //日期被修改后的回调 + }) + + + //获取今年的年份/月份,返回的是数组 + function getThisYearMonth(){ + var oDate = new Date() + return [oDate.getFullYear(), oDate.getMonth() + 1] + } + + //根据年份获取指定月份天数 + function getNumsOfYearMonth(year, month){ + return new Date(year, month, 0).getDate() + } + + //判断指定年月第一天是星期几 + function getDayByYearMonth(year, month, day){ + day = day || 1 + return new Date(year, month - 1, day).getDay() + } + //数字长度补全(前面加0) + function numberFormat(num, len){ + num += '' + if(num.length === len) + return num + + while(num.length < len) + num = '0' + num + return num + } + + return av +}) \ No newline at end of file diff --git a/js/lib/datepicker/doui.datepicker.min.css b/js/lib/datepicker/doui.datepicker.min.css new file mode 100644 index 0000000..99cffb9 --- /dev/null +++ b/js/lib/datepicker/doui.datepicker.min.css @@ -0,0 +1 @@ +@charset "UTF-8";.do-ui-datepicker{position:relative;z-index:65534;width:100%;height:100%}.do-ui-datepicker a{text-decoration:none}.do-ui-datepicker .date-input{float:left;width:100%;height:100%;border:1px solid #ddd;line-height:18px;padding:0 5px}.do-ui-datepicker .calendar{position:absolute;z-index:65534;left:0;top:98%;width:230px;height:auto;min-height:60px;padding:10px;border:1px solid #ddd;background:#fff;font-size:13px;box-shadow:0 0 5px rgba(0,0,0,.1)}.do-ui-datepicker .calendar-hd{width:100%;height:30px;line-height:30px;background:#f3f3f3;color:#666;text-align:center}.do-ui-datepicker .calendar-contrl{position:relative;width:90%;height:30px;margin:0 5%;line-height:30px;border-bottom:1px solid #eee;color:#09f;text-align:center}.do-ui-datepicker .calendar-contrl a{position:absolute;top:0;left:0;width:30px;color:#09f}.do-ui-datepicker .calendar-contrl .prev-month{left:30px}.do-ui-datepicker .calendar-contrl .next-month{left:auto;right:30px}.do-ui-datepicker .calendar-contrl .next-year{left:auto;right:0}.do-ui-datepicker .calendar-table{position:relative;width:90%;height:auto;margin:0 5%;line-height:25px;color:#888;text-align:center}.do-ui-datepicker .calendar-table .tr{width:100%;height:auto;min-height:25px}.do-ui-datepicker .calendar-table .tr.tr-hd{border-bottom:1px solid #eee;margin-bottom:3px}.do-ui-datepicker .calendar-table .tr .td{float:left;width:14.28%;height:25px}.do-ui-datepicker .calendar-table .tr .do-st-hand:hover{background:#b6def9}.do-ui-datepicker .calendar-table .tr .td.weeken{color:#f30}.do-ui-datepicker .calendar-table .tr .td.selected{background:#09f;color:#fff}.do-ui-datepicker .calendar-table .tr .td.disabled{color:#ddd;cursor:default}.do-ui-datepicker .time-contrl{position:relative;width:90%;height:30px;margin:5px 5%;line-height:30px;border-top:1px solid #eee;color:#888}.do-ui-datepicker .time-contrl label{float:left;height:20px;margin:5px 5px 5px 0}.do-ui-datepicker .time-contrl input{float:left;width:25px;height:20px;padding:0 3px;line-height:17px;outline:0}.do-ui-datepicker .time-contrl label:after{float:right;height:20px;line-height:20px;font-size:12px}.do-ui-datepicker .time-contrl .hours:after{content:"时"}.do-ui-datepicker .time-contrl .minutes:after{content:"分"}.do-ui-datepicker .time-contrl .seconds:after{content:"秒"}.do-ui-datepicker .time-contrl .now{float:right;width:40px;height:20px;margin:5px;border:1px solid #ddd;border-radius:3px;line-height:20px;background:#f7f7f7;color:#888;text-align:center}.do-ui-datepicker .time-contrl .now:hover{background:#eee}.do-ui-datepicker .time-contrl .now:active{background:#e7e7e7}.do-ui-datepicker .calendar-tips{position:absolute;z-index:65535;left:25%;top:40%;width:50%;height:30px;line-height:30px;background:rgba(0,0,0,.7);color:#fff;font-size:12px;text-align:center} \ No newline at end of file diff --git a/js/lib/datepicker/doui.datepicker.min.js b/js/lib/datepicker/doui.datepicker.min.js new file mode 100644 index 0000000..7380e41 --- /dev/null +++ b/js/lib/datepicker/doui.datepicker.min.js @@ -0,0 +1 @@ +"use strict";define(["avalon","css!./doui.datepicker.min"],function(av){function getThisYearMonth(){var a=new Date;return[a.getFullYear(),a.getMonth()+1]}function getNumsOfYearMonth(a,e){return new Date(a,e,0).getDate()}function getDayByYearMonth(a,e,t){return t=t||1,new Date(a,e-1,t).getDay()}function numberFormat(a,e){if(a+="",a.length===e)return a;for(;a.length
    请选择日期
    • {{el.day}}
    {{tips}}
    ',$construct:function(a,e,t){var n=av.mix(e,t),l=n.duplex.slice(0,n.duplex.indexOf("."));parentVm=av.vmodels[l],n.duplex=n.duplex.slice(n.duplex.indexOf(".")+1);var r=new Function("v","return v."+n.duplex)(parentVm);return n.showTime||n.format||(n.format="Y-m-d"),void 0===r&&(n.minDate?(r=n.minDate,n.format):a.maxDate&&(r=n.maxDate,n.format)),a.dateVal=r&&av.filters.date(r,n.format),a.calendar={list:[],year:av.filters.date(a.dateVal,"Y"),month:av.filters.date(a.dateVal,"m"),day:av.filters.date(a.dateVal,"d")||0,hour:av.filters.date(a.dateVal,"H")||0,minute:av.filters.date(a.dateVal,"i")||0,second:av.filters.date(a.dateVal,"s")||0,minYear:n.minDate&&av.filters.date(n.minDate,"Y")>>0,minMonth:n.minDate&&av.filters.date(n.minDate,"m")>>0,minDay:n.minDate&&av.filters.date(n.minDate,"d")>>0||1,maxYear:n.maxDate&&av.filters.date(n.maxDate,"Y")>>0,maxMonth:n.maxDate&&av.filters.date(n.maxDate,"m")>>0,maxDay:n.maxDate&&av.filters.date(n.maxDate,"d")>>0},delete n.minDate,delete n.maxDate,av.mix(a,n)},$init:function(vm,ele){function getCalendar(){var a=vm.calendar.year>>0,e=vm.calendar.month>>0,t=getNumsOfYearMonth(a,e),n=-getDayByYearMonth(a,e)+1,l=isLimited(a,e);vm.calendar.list.clear();for(var r=n;t>=r;r++){var s={weeken:!1,day:"",selected:!1,disable:!0};if(r>0){var d=getDayByYearMonth(a,e,r);s={weeken:0==d||6==d,day:r,selected:isSelected(r),disable:disabledDay(r,l)}}vm.calendar.list.push(s)}}function isLimited(a,e){var t={Y:vm.calendar.minYear,M:vm.calendar.minMonth,mY:vm.calendar.maxYear,mM:vm.calendar.maxMonth},n="";if(!t.Y&&!t.mY||!t.M&&!t.mM)return!1;if(!a)return!1;if(t.Y&&at.mY)return!0;if(e){if(a===t.Y){if(t.M&&et.mM)return!0;e==t.mM&&(n+="+")}}return n}function disabledDay(a,e){var t=vm.calendar.minDay,n=vm.calendar.maxDay;return"-"===e?t>a:"+"===e?n&&a>n:"-+"===e?t>a||n&&a>n:e}function isSelected(a){var e=vm.calendar.year>>0,t=vm.calendar.month>>0;return!(lastDate.year!==e||lastDate.month!==t||lastDate.day!==a)}function changeStyle(a){vm.calendar.list.forEach(function(e){e.selected=e.day!=a?!1:!0})}function updateTime(){var a=vm.calendar,e=a.year,t=a.month,n=a.day,l=a.hour,r=a.minute,s=a.second,d=e+"-"+t+"-"+n;vm.showTime&&(d+=" "+l+":"+r+":"+s),lastDate={year:e>>0,month:t>>0,day:n>>0},vm.dateVal=av.filters.date(d,vm.format)}var lastDate={year:vm.calendar.year>>0,month:vm.calendar.month>>0,day:vm.calendar.day>>0},timer;getCalendar(),vm.$turn=function(a,e){var t=vm.calendar.year>>0,n=vm.calendar.month>>0;return 1===a?t+=e:(n+=e,1>n&&(n=12,t--),n>12&&(n=1,t++)),isLimited(t,n)===!0?void(vm.tips="日期超出限制"):(vm.calendar.year=t,void(vm.calendar.month=numberFormat(n,2)))},vm.$getDate=function(a,e){a||(vm.calendar.day=e,changeStyle(e),updateTime(),vm.showCalendar=!1)},vm.$focus=function(){vm.showCalendar=!0},vm.$now=function(){var a=av.filters.date(null,"Y")>>0,e=av.filters.date(null,"m")>>0,t=av.filters.date(null,"d")>>0,n=isLimited(a,e),l=disabledDay(t,n);return l?void(vm.tips="今天超出了限制日期"):(vm.calendar.year=a,vm.calendar.month=e,vm.calendar.day=t,vm.calendar.hour=av.filters.date(null,"H"),vm.calendar.minute=av.filters.date(null,"i"),vm.calendar.second=av.filters.date(null,"s"),changeStyle(t),updateTime(),void(vm.showCalendar=!1))},vm.$watch("calendar.year",function(){getCalendar()}),vm.$watch("calendar.month",function(){getCalendar()}),vm.$watch("calendar.hour",function(a){vm.calendar.hour=a,updateTime()}),vm.$watch("calendar.minute",function(a){vm.calendar.minute=a,updateTime()}),vm.$watch("calendar.second",function(a){vm.calendar.second=a,updateTime()}),vm.$watch("showCalendar",function(v){!v&&vm.duplex&&(eval("parentVm."+vm.duplex+' = "'+vm.dateVal+'"'),vm.callback&&vm.callback(vm.dateVal))}),vm.$watch("tips",function(a){a&&(clearTimeout(timer),timer=setTimeout(function(){vm.tips=""},1500))}),document.addEventListener("click",function(){vm.showCalendar=!1})},showTime:!1,showCalendar:!1,disabled:!1,exclass:"",tips:"",duplex:"",format:"",dateVal:"",radius:0,border:1,btns:{prevYear:"<<",nextYear:">>",prevMonth:"<",nextMonth:">"},$focus:av.noop,$turn:av.noop,$getDate:av.noop,$now:av.noop,$cancelBubble:function(a){a.stopPropagation&&a.stopPropagation()||(a.cancelBubble=!0)},callback:null}),av}); \ No newline at end of file diff --git a/js/lib/datepicker/test/ex-1.html b/js/lib/datepicker/test/ex-1.html new file mode 100644 index 0000000..8b9e63c --- /dev/null +++ b/js/lib/datepicker/test/ex-1.html @@ -0,0 +1,69 @@ + + + + + +测试1 + + + + + + + + +
    + +
    + 示例1: + +
    + + +
    + 示例2: + +
    + + +
    + + + + + + \ No newline at end of file diff --git a/js/lib/drag/doc.md b/js/lib/drag/doc.md new file mode 100644 index 0000000..4a35826 --- /dev/null +++ b/js/lib/drag/doc.md @@ -0,0 +1,85 @@ +# 拖拽插件 +> 该插件可以让任意一个元素可以被拖拽,而不需要该元素是否具有定位属性。 +> 使用时,在目标元素上添加`:drag`属性即可以实现拖拽功能。 + +## 依赖 +> 依赖`yua`框架 + +## 浏览器兼容性 ++ chrome ++ firefox ++ safari ++ IE10+ + + +## 用法 +> 只需要在要拖拽的元素上添加`:drag`即可; +> 如果要拖拽的元素不是当前元素,只需要给该属性增加一个值为想要拖拽元素的类名或ID。 +> 具体请看示例: +> **注意:** `拖拽的元素不是本身时,只会往父级一级一级找相匹配的` + +```html + + + + + + + + +
    + + + + + + + +``` + + +```html + + + + + + + + +
    +
    +
    + + + + + + + +``` diff --git a/js/lib/drag/drag.js b/js/lib/drag/drag.js new file mode 100644 index 0000000..927b06c --- /dev/null +++ b/js/lib/drag/drag.js @@ -0,0 +1,217 @@ +/** + * + * @authors yutent (yutent@doui.cc) + * @date 2017-03-29 18:39:35 + * + */ + +"use strict"; + +define(['yua'], function(){ + + function getBindingCallback(elem, name, vmodels) { + var callback = elem.getAttribute(name) + if (callback) { + for (var i = 0, vm; vm = vmodels[i++]; ) { + if (vm.hasOwnProperty(callback) && typeof vm[callback] === "function") { + return vm[callback] + } + } + } + } + + // 元素拖动 + yua.directive('drag', { + priority: 1500, + init: function(binding){ + binding.expr = '"' + binding.expr + '"' + yua(binding.element).css('cursor', 'move') + + //取得拖动的3种状态回调 + //按下,且拖拽之前 + binding. beforedrag = getBindingCallback(binding.element, 'data-beforedrag', binding.vmodels) + //拖拽过程 + binding.dragging = getBindingCallback(binding.element, 'data-dragging', binding.vmodels) + // 拖拽结束,且释放鼠标 + binding.dragged = getBindingCallback(binding.element, 'data-dragged', binding.vmodels) + + //获取是否允许溢出可视区 + binding.overflow = !!binding.element.dataset.overflow + + //方向,x轴, y轴, xy轴 + binding.axis = 'xy' + if(!!binding.element.dataset.axis){ + binding.axis = binding.element.dataset.axis + delete binding.element.dataset.axis + } + + //默认不限制拖拽区域 + binding.limit = false + if(!!binding.element.dataset.limit) { + binding.limit = binding.element.dataset.limit + delete binding.element.dataset.limit + } + + // 如果不限制,则允许溢出 + if(binding.limit === false) { + binding.overflow = true + } + + delete binding.element.dataset.overflow + delete binding.element.dataset.beforedrag + delete binding.element.dataset.dragging + delete binding.element.dataset.dragged + }, + update: function(val){ + var _this = this, + target = val ? this.element.parentNode : this.element, + $drag = yua(this.element), + $doc = yua(document), + $target = null, + parent = null; + + // val值不为空时, 获取真正的拖动元素 + // 仅从父级上找 + while(val && target){ + if(target.classList.contains(val) || target.id === val){ + break + }else{ + target = target.parentNode + } + } + $target = yua(target); + // 限制范围为parent时,获取父级元素 + if(this.limit === 'parent'){ + parent = target.parentNode + } + + + var dx,dy,mx,my,ox,oy,tw,th,ww,wh,bst,bsl; + $drag.bind('mousedown', function(ev){ + var gcs = getComputedStyle(target), + cst = gcs.transform.replace(/matrix\((.*)\)/, '$1'), + offset = $target.offset(); + + cst = cst !== 'none' ? cst.split(', ') : [1,0,0,1,0,0] + cst[4] -= 0 + cst[5] -= 0 + + //记录初始的transform位移 + dx = cst[4] + dy = cst[5] + + //滚动条的偏移 + bst = document.body.scrollTop + bsl = document.body.scrollLeft + + // 计算元素的offset值, 需要修正 + ox = offset.left - dx - bsl + oy = offset.top - dy - bst + + mx = ev.pageX //按下鼠标的的坐标值 + my = ev.pageY //按下鼠标的的坐标值 + + // 在按下时才获取窗口大小, 是为了防止人为的改变窗口大小,导致计算不准备 + // 同时减少不必要的事件监听(页面上可能会很多可拖动元素) + ww = window.innerWidth; + wh = window.innerHeight; + + // 同样,在点击之后获取元素的宽高,可保证获取到的是真实的值 + tw = target.clientWidth; + th = target.clientHeight; + + //拖拽前回调 + if(_this.beforedrag){ + _this.beforedrag.call(_this.vmodels, target, ox, oy) + } + + //限制区域, 4个值依次是: 上, 下, 左, 右 + var limit = [0, wh - th, 0, ww - tw] + + if(_this.limit === 'parent') { + var pgcs = getComputedStyle(parent), + pcst = pgcs.transform.replace(/matrix\((.*)\)/, '$1'), + poffset = yua(parent).offset(); + + pcst = pcst !== 'none' ? pcst.split(', ') : [1,0,0,1,0,0] + + var pox = poffset.left - pcst[4] - bsl, + poy = poffset.top - pcst[5] - bst; + + limit = [poy, poy + parent.clientHeight - th, pox, pox + parent.clientWidth - tw] + } + + var mvfn = $doc.bind('mousemove', function(ev){ + + //坐标轴限制 + if(_this.axis !== 'y'){ + cst[4] = ev.pageX - mx + dx + } + if(_this.axis !== 'x'){ + cst[5] = ev.pageY - my + dy + } + + + var fox = ox + cst[4], //修正的offset + foy = oy + cst[5]; //修正的offset + + + //如果不允许溢出可视区 + if(!_this.overflow){ + if(_this.axis !== 'y'){ + if(fox <= limit[2]) { + fox = limit[2] + //修正矩阵 + cst[4] = fox - ox + } + if(fox >= limit[3]){ + fox = limit[3] + //修正矩阵 + cst[4] = fox - ox + } + } + + if(_this.axis !== 'x'){ + if(foy <= limit[0]) { + foy = limit[0] + //修正矩阵 + cst[5] = foy - oy + } + if(foy >= limit[1]){ + foy = limit[1] + //修正矩阵 + cst[5] = foy - oy + } + } + + } + + $target.css({ + transform: 'matrix(' + cst.join(', ') + ')' + }) + + //拖拽过程的回调 + if(_this.dragging){ + _this.dragging.call(_this.vmodels, target, fox, foy) + } + // 防止拖动到边缘时导致页面滚动 + ev.preventDefault() + }), + upfn = $doc.bind('mouseup', function(ev){ + $doc.unbind('mousemove', mvfn) + $doc.unbind('mouseup', upfn) + //结束回调 + if(_this.dragged){ + _this.dragged.call(_this.vmodels, target, fox, foy) + } + }); + + }) + + } + }) + + + + +}) diff --git a/js/lib/layer/index.html b/js/lib/layer/index.html new file mode 100644 index 0000000..a0526c2 --- /dev/null +++ b/js/lib/layer/index.html @@ -0,0 +1,50 @@ + + + + + +Examples + + + + + + + + alert层 + confirm层 + msg层 + prompt层 + loading层 + + + + + + + + + + + + + \ No newline at end of file diff --git a/js/lib/layer/layer.js b/js/lib/layer/layer.js new file mode 100644 index 0000000..d81e8b2 --- /dev/null +++ b/js/lib/layer/layer.js @@ -0,0 +1,135 @@ +/** + * + * @authors yutent (yutent@doui.cc) + * @date 2016-09-21 01:36:29 + * + */ + +"use strict"; + + +define(['avalon', 'css!./skin/def'], function(av){ + + + + var prep = { + type: { + 1: 'alert', + 2: 'confirm', + 3: 'msg', + 4: 'loading', + 5: 'iframe', + 6: 'tips', + 7: 'prompt' + }, + init: { + type: 1, + shade: true, + shadeClose: false, + move: '.do-ui-layer-move', + title: '温馨提示', + content: '', + offset: {x: '300px', y: '200px'}, + btns: ['确定', '取消'], + yes: av.noop, + no: av.noop, + success: av.noop + } + } + + function uuid(type){ + type = type || 1 + return prep.type[type] + Math.round(Date.now()/1000).toString(16) + Math.random().toString(16).slice(2, 6) + } + + + + var layer = { + alert: function(msg, conf){ + conf = conf || {} + return layer.open(av.mix({content: msg}, conf)) + }, + confirm: function(msg, conf){ + conf = conf || {} + return layer.open(av.mix({content: msg}, conf)) + }, + msg: function(msg, conf){ + av.log(msg) + }, + loading: function(msg, conf){ + av.log(msg) + }, + iframe: function(url, conf){ + av.log(url) + }, + tips: function(msg, conf){ + av.log(msg) + }, + prompt: function(callback){ + av.log('23456') + }, + use: function(conf, callback){ + require(['css!./skin/' + conf.skin], callback) + } + } + + + var FN = function(conf){ + this.ready(conf) + } + + + FN.prototype = { + init: {}, + create: function(){ + var layBox = document.createElement('div') + layBox.className = 'do-ui-layer skin-def' + layBox.style.left = this.init.offset.x + layBox.style.top = this.init.offset.y + layBox.setAttribute('ms-controller', this.init.$id) + layBox.innerHTML = '
    {{title}}
    ' + + '
    ' + + '' + + '
    ' + + '
    ' + + '
    ' + + '' + + '' + + '
    ' + return layBox + }, + show: function(){ + + var layBox = this.create() + document.body.appendChild(layBox) + + av.define(this.init) + av.scan(layBox) + }, + ready: function(conf){ + this.init = av.mix({$id: uuid()}, prep.init, conf) + this.show() + }, + close: function(){ + + } + } + + + + + layer.open = function(conf){ + av.log(conf) + return new FN(conf).init + } + + + if(!window.layer) + window.layer = layer + + + + return layer + + +}) \ No newline at end of file diff --git a/js/lib/layer/skin/def.css b/js/lib/layer/skin/def.css new file mode 100644 index 0000000..c30092c --- /dev/null +++ b/js/lib/layer/skin/def.css @@ -0,0 +1,49 @@ +@charset "UTF-8"; +/** + * + * @authors yutent (yutent@doui.cc) + * @date 2016-09-21 01:36:22 + * + */ + +.do-ui-layer, .do-ui-layer * {margin: 0;padding: 0;vertical-align: baseline;box-sizing:border-box;} +.do-ui-layer a {text-decoration:none;} + +@font-face {font-family: "deficon"; + src: url('def.eot?t=1474609176'); /* IE9*/ + src: url('def.woff?t=1474609176') format('woff'), /* chrome, firefox */ + url('def.ttf?t=1474609176') format('truetype'); /* chrome, firefox, opera, Safari, Android, iOS 4.2+*/ +} + +.do-ui-layer-shade {position:fixed;left:0;top:0;z-index:65534;width:100%;height:100%;background:rgba(255,255,255,.05);} +.do-ui-layer {position:fixed;left:50%;top:230px;z-index:65535;width:auto;height:auto;} + + +.do-ui-layer.skin-def {min-width:300px;background:#fff;color:#666;font-size:13px;box-shadow:0 0 10px rgba(0,0,0,.3);} +.do-ui-layer.skin-def .deficon {display: inline-block;font-family:"deficon" !important;font-style:normal;-webkit-font-smoothing: antialiased;-webkit-text-stroke-width: 0.2px;-moz-osx-font-smoothing: grayscale;} +.do-ui-layer.skin-def .icon-1:before {content:"\e605";color:#11b330;} +.do-ui-layer.skin-def .icon-2:before {content:"\e602";color:#f30;} +.do-ui-layer.skin-def .icon-3:before {content:"\e603";color:#f30;} +.do-ui-layer.skin-def .icon-4:before {content:"\e604";color:#f30;} +.do-ui-layer.skin-def .icon-5:before {content:"\e608";color:#09f;} +.do-ui-layer.skin-def .icon-6:before {content:"\e609";color:#ee0} +.do-ui-layer.skin-def .icon-7:before {content:"\e60a";color:#63e2c2} +.do-ui-layer.skin-def .icon-8:before {content:"\e606";color:#ee0} +.do-ui-layer.skin-def .icon-9:before {content:"\e607";color:#f30;} + + + +.do-ui-layer.skin-def .layer-title {width:100%;height:40px;padding:0 8px;line-height:40px;background:#f3f3f3;font-size:14px;color:#454545;} +.do-ui-layer.skin-def .layer-min,.do-ui-layer.skin-def .layer-close {position:absolute;display:block;top:10px;width:20px;height:20px;line-height:20px;border:0;text-align:center;cursor:pointer;} +.do-ui-layer.skin-def .layer-min {right:40px;} +.do-ui-layer.skin-def .layer-close {right:10px;} +.do-ui-layer.skin-def .layer-close:hover,.do-ui-layer.skin-def .layer-min:hover {border: 1px solid #ddd;line-height: 18px;} +.do-ui-layer.skin-def .layer-min:before {content:"\e600";} +.do-ui-layer.skin-def .layer-close:before {content:"\e601";} + +.do-ui-layer.skin-def .layer-content {width:100%;height:auto;min-height:100px;padding:10px;} +.do-ui-layer.skin-def .layer-content .deficon {float:left;width:60px;height:100%;line-height:50px;font-size:40px;text-align:center;} +.do-ui-layer.skin-def .layer-content .detail {float:left;width:auto;height:100%;margin-top:10px;padding:5px 15px;word-break:break-all;word-wrap: break-word;} + +.do-ui-layer.skin-def .layer-btns {width:100%;height:40px;padding:0 5px;line-height:28px;font-size:14px;color:#454545;text-align:right;} +.do-ui-layer.skin-def .layer-btns a {display:inline-block;width:auto;min-width:60px;height:30px;margin:0 5px;padding:0 10px;line-height:28px;border:1px solid #ddd;color:#454545;text-align:center;background:#f3f3f3;} \ No newline at end of file diff --git a/js/lib/layer/skin/def.eot b/js/lib/layer/skin/def.eot new file mode 100644 index 0000000..0e43397 Binary files /dev/null and b/js/lib/layer/skin/def.eot differ diff --git a/js/lib/layer/skin/def.ttf b/js/lib/layer/skin/def.ttf new file mode 100644 index 0000000..a8735ab Binary files /dev/null and b/js/lib/layer/skin/def.ttf differ diff --git a/js/lib/layer/skin/def.woff b/js/lib/layer/skin/def.woff new file mode 100644 index 0000000..75922e1 Binary files /dev/null and b/js/lib/layer/skin/def.woff differ diff --git a/js/lib/md5/Readme.md b/js/lib/md5/Readme.md new file mode 100644 index 0000000..092741e --- /dev/null +++ b/js/lib/md5/Readme.md @@ -0,0 +1,27 @@ +# md5 加密组件 + +可对普通字符串和文件计算其对应的md5值。 + + +组件符合AMD规范, 可使用require引入 + + +### demo: + +```javascript +require(['./md5'], function(SparkMD5){ + var Spark = new SparkMD5() + var md5 = function(cont){ + return Spark.sign.call(Spark, cont) + } + + var file = /*...*/ //文件表单获取 + var fs = new FileReader() // Chrome, IE9+, FF, Safari + fs.readAsBinaryString(file) + + fs.onload = function(){ // 计算该文件的md5签名 + var sign = md5(this.result) + } +}) +``` + diff --git a/js/lib/md5/md5.js b/js/lib/md5/md5.js new file mode 100644 index 0000000..4a376db --- /dev/null +++ b/js/lib/md5/md5.js @@ -0,0 +1 @@ +!function(t){if("object"==typeof exports)module.exports=t();else if("function"==typeof define&&define.amd)define(t);else{var r;try{r=window}catch(n){r=self}r.SparkMD5=t()}}(function(){"use strict";var t=function(t,r){return t+r&4294967295},r=function(r,n,e,i,f,o){return n=t(t(n,r),t(i,o)),t(n<>>32-f,e)},n=function(t,n,e,i,f,o,s){return r(n&e|~n&i,t,n,f,o,s)},e=function(t,n,e,i,f,o,s){return r(n&i|e&~i,t,n,f,o,s)},i=function(t,n,e,i,f,o,s){return r(n^e^i,t,n,f,o,s)},f=function(t,n,e,i,f,o,s){return r(e^(n|~i),t,n,f,o,s)},o=function(r,o){var s=r[0],u=r[1],a=r[2],h=r[3];s=n(s,u,a,h,o[0],7,-680876936),h=n(h,s,u,a,o[1],12,-389564586),a=n(a,h,s,u,o[2],17,606105819),u=n(u,a,h,s,o[3],22,-1044525330),s=n(s,u,a,h,o[4],7,-176418897),h=n(h,s,u,a,o[5],12,1200080426),a=n(a,h,s,u,o[6],17,-1473231341),u=n(u,a,h,s,o[7],22,-45705983),s=n(s,u,a,h,o[8],7,1770035416),h=n(h,s,u,a,o[9],12,-1958414417),a=n(a,h,s,u,o[10],17,-42063),u=n(u,a,h,s,o[11],22,-1990404162),s=n(s,u,a,h,o[12],7,1804603682),h=n(h,s,u,a,o[13],12,-40341101),a=n(a,h,s,u,o[14],17,-1502002290),u=n(u,a,h,s,o[15],22,1236535329),s=e(s,u,a,h,o[1],5,-165796510),h=e(h,s,u,a,o[6],9,-1069501632),a=e(a,h,s,u,o[11],14,643717713),u=e(u,a,h,s,o[0],20,-373897302),s=e(s,u,a,h,o[5],5,-701558691),h=e(h,s,u,a,o[10],9,38016083),a=e(a,h,s,u,o[15],14,-660478335),u=e(u,a,h,s,o[4],20,-405537848),s=e(s,u,a,h,o[9],5,568446438),h=e(h,s,u,a,o[14],9,-1019803690),a=e(a,h,s,u,o[3],14,-187363961),u=e(u,a,h,s,o[8],20,1163531501),s=e(s,u,a,h,o[13],5,-1444681467),h=e(h,s,u,a,o[2],9,-51403784),a=e(a,h,s,u,o[7],14,1735328473),u=e(u,a,h,s,o[12],20,-1926607734),s=i(s,u,a,h,o[5],4,-378558),h=i(h,s,u,a,o[8],11,-2022574463),a=i(a,h,s,u,o[11],16,1839030562),u=i(u,a,h,s,o[14],23,-35309556),s=i(s,u,a,h,o[1],4,-1530992060),h=i(h,s,u,a,o[4],11,1272893353),a=i(a,h,s,u,o[7],16,-155497632),u=i(u,a,h,s,o[10],23,-1094730640),s=i(s,u,a,h,o[13],4,681279174),h=i(h,s,u,a,o[0],11,-358537222),a=i(a,h,s,u,o[3],16,-722521979),u=i(u,a,h,s,o[6],23,76029189),s=i(s,u,a,h,o[9],4,-640364487),h=i(h,s,u,a,o[12],11,-421815835),a=i(a,h,s,u,o[15],16,530742520),u=i(u,a,h,s,o[2],23,-995338651),s=f(s,u,a,h,o[0],6,-198630844),h=f(h,s,u,a,o[7],10,1126891415),a=f(a,h,s,u,o[14],15,-1416354905),u=f(u,a,h,s,o[5],21,-57434055),s=f(s,u,a,h,o[12],6,1700485571),h=f(h,s,u,a,o[3],10,-1894986606),a=f(a,h,s,u,o[10],15,-1051523),u=f(u,a,h,s,o[1],21,-2054922799),s=f(s,u,a,h,o[8],6,1873313359),h=f(h,s,u,a,o[15],10,-30611744),a=f(a,h,s,u,o[6],15,-1560198380),u=f(u,a,h,s,o[13],21,1309151649),s=f(s,u,a,h,o[4],6,-145523070),h=f(h,s,u,a,o[11],10,-1120210379),a=f(a,h,s,u,o[2],15,718787259),u=f(u,a,h,s,o[9],21,-343485551),r[0]=t(s,r[0]),r[1]=t(u,r[1]),r[2]=t(a,r[2]),r[3]=t(h,r[3])},s=function(t){var r,n=[];for(r=0;64>r;r+=4)n[r>>2]=t.charCodeAt(r)+(t.charCodeAt(r+1)<<8)+(t.charCodeAt(r+2)<<16)+(t.charCodeAt(r+3)<<24);return n},u=function(t){var r,n=[];for(r=0;64>r;r+=4)n[r>>2]=t[r]+(t[r+1]<<8)+(t[r+2]<<16)+(t[r+3]<<24);return n},a=function(t){var r,n,e,i,f,u,a=t.length,h=[1732584193,-271733879,-1732584194,271733878];for(r=64;a>=r;r+=64)o(h,s(t.substring(r-64,r)));for(t=t.substring(r-64),n=t.length,e=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],r=0;n>r;r+=1)e[r>>2]|=t.charCodeAt(r)<<(r%4<<3);if(e[r>>2]|=128<<(r%4<<3),r>55)for(o(h,e),r=0;16>r;r+=1)e[r]=0;return i=8*a,i=i.toString(16).match(/(.*?)(.{0,8})$/),f=parseInt(i[2],16),u=parseInt(i[1],16)||0,e[14]=f,e[15]=u,o(h,e),h},h=function(t){var r,n,e,i,f,s,a=t.length,h=[1732584193,-271733879,-1732584194,271733878];for(r=64;a>=r;r+=64)o(h,u(t.subarray(r-64,r)));for(t=a>r-64?t.subarray(r-64):new Uint8Array(0),n=t.length,e=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],r=0;n>r;r+=1)e[r>>2]|=t[r]<<(r%4<<3);if(e[r>>2]|=128<<(r%4<<3),r>55)for(o(h,e),r=0;16>r;r+=1)e[r]=0;return i=8*a,i=i.toString(16).match(/(.*?)(.{0,8})$/),f=parseInt(i[2],16),s=parseInt(i[1],16)||0,e[14]=f,e[15]=s,o(h,e),h},c=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"],p=function(t){var r,n="";for(r=0;4>r;r+=1)n+=c[t>>8*r+4&15]+c[t>>8*r&15];return n},y=function(t){var r;for(r=0;r>16)+(r>>16)+(n>>16);return e<<16|65535&n}),d.prototype.append=function(t){return/[\u0080-\uFFFF]/.test(t)&&(t=unescape(encodeURIComponent(t))),this.appendBinary(t),this},d.prototype.appendBinary=function(t){this._buff+=t,this._length+=t.length;var r,n=this._buff.length;for(r=64;n>=r;r+=64)o(this._state,s(this._buff.substring(r-64,r)));return this._buff=this._buff.substr(r-64),this},d.prototype.end=function(t){var r,n,e=this._buff,i=e.length,f=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];for(r=0;i>r;r+=1)f[r>>2]|=e.charCodeAt(r)<<(r%4<<3);return this._finish(f,i),n=t?this._state:y(this._state),this.reset(),n},d.prototype.sign=function(t){return this.appendBinary(t),this.end()},d.prototype._finish=function(t,r){var n,e,i,f=r;if(t[f>>2]|=128<<(f%4<<3),f>55)for(o(this._state,t),f=0;16>f;f+=1)t[f]=0;n=8*this._length,n=n.toString(16).match(/(.*?)(.{0,8})$/),e=parseInt(n[2],16),i=parseInt(n[1],16)||0,t[14]=e,t[15]=i,o(this._state,t)},d.prototype.reset=function(){return this._buff="",this._length=0,this._state=[1732584193,-271733879,-1732584194,271733878],this},d.prototype.destroy=function(){delete this._state,delete this._buff,delete this._length},d.hash=function(t,r){/[\u0080-\uFFFF]/.test(t)&&(t=unescape(encodeURIComponent(t)));var n=a(t);return r?n:y(n)},d.hashBinary=function(t,r){var n=a(t);return r?n:y(n)},d.ArrayBuffer=function(){this.reset()},d.ArrayBuffer.prototype.append=function(t){var r,n=this._concatArrayBuffer(this._buff,t),e=n.length;for(this._length+=t.byteLength,r=64;e>=r;r+=64)o(this._state,u(n.subarray(r-64,r)));return this._buff=e>r-64?n.subarray(r-64):new Uint8Array(0),this},d.ArrayBuffer.prototype.end=function(t){var r,n,e=this._buff,i=e.length,f=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];for(r=0;i>r;r+=1)f[r>>2]|=e[r]<<(r%4<<3);return this._finish(f,i),n=t?this._state:y(this._state),this.reset(),n},d.ArrayBuffer.prototype._finish=d.prototype._finish,d.ArrayBuffer.prototype.reset=function(){return this._buff=new Uint8Array(0),this._length=0,this._state=[1732584193,-271733879,-1732584194,271733878],this},d.ArrayBuffer.prototype.destroy=d.prototype.destroy,d.ArrayBuffer.prototype._concatArrayBuffer=function(t,r){var n=t.length,e=new Uint8Array(n+r.byteLength);return e.set(t),e.set(new Uint8Array(r),n),e},d.ArrayBuffer.hash=function(t,r){var n=h(new Uint8Array(t));return r?n:y(n)},d}); \ No newline at end of file diff --git a/js/lib/pages/ex-1.html b/js/lib/pages/ex-1.html new file mode 100644 index 0000000..f2f5b2f --- /dev/null +++ b/js/lib/pages/ex-1.html @@ -0,0 +1,57 @@ + + + + + +Examples-1 + + + + + + + + +
    + +
    + +
    + + + + + + + + \ No newline at end of file diff --git a/js/lib/pages/pages.css b/js/lib/pages/pages.css new file mode 100644 index 0000000..eb0922b --- /dev/null +++ b/js/lib/pages/pages.css @@ -0,0 +1,34 @@ +.widget-pages .do-ui-pages {width:100%;height:30px;text-align:center;} +.widget-pages.h20 .do-ui-pages {height:20px;} +.widget-pages .do-ui-pages a {display:inline-block;width:auto;min-width:20px;height:30px;padding:0 10px;margin:0 3px;line-height:28px;} +.widget-pages .do-ui-pages .curr,.widget-pages .do-ui-pages .more {line-height:30px;padding:0;border:none!important;background:none!important;color:#666!important;cursor:default} +.widget-pages.h20 .do-ui-pages a {min-width:10px;height:20px;line-height:18px;} +.widget-pages.h20 .do-ui-pages .curr,.widget-pages.h20 .do-ui-pages .more {line-height:20px;} + +.widget-pages .do-ui-pages .page-jump {display:inline-block;} +.widget-pages .do-ui-pages .page-jump span,.widget-pages .do-ui-pages .page-jump input{display:inline-block;} +.widget-pages .do-ui-pages .page-jump input {width:25px;height:18px;padding:0 3px;background:none;border:1px solid #ddd;} + +.widget-pages.skin-default .do-ui-pages a {border:1px solid #ddd;background:#f3f3f3;color:#666;} +.widget-pages.skin-default .do-ui-pages a:hover {border:1px solid #e3e3e3;background:#e3e3e3;color:#666;} +.widget-pages.skin-default .do-ui-pages a:active {border:1px solid #ccc;background:#ccc;color:#666;} + +.widget-pages.skin-blue .do-ui-pages a {border:1px solid #1b9af7;background:#1b9af7;color:#fff;} +.widget-pages.skin-blue .do-ui-pages a:hover {border:1px solid #13b5ff;background:#13b5ff;color:#fff;} +.widget-pages.skin-blue .do-ui-pages a:active {border:1px solid #1682cf;background:#1682cf;color:#fff;} + +.widget-pages.skin-red .do-ui-pages a {border:1px solid #ff4351;background:#ff4351;color:#fff;} +.widget-pages.skin-red .do-ui-pages a:hover {border:1px solid #ff7680;background:#ff7680;color:#fff;} +.widget-pages.skin-red .do-ui-pages a:active {border:1px solid #f64c59;background:#f64c59;color:#fff;} + +.widget-pages.skin-yellow .do-ui-pages a {border:1px solid #feae1b;background:#feae1b;color:#fff;} +.widget-pages.skin-yellow .do-ui-pages a:hover {border:1px solid #fec04e;background:#fec04e;color:#fff;} +.widget-pages.skin-yellow .do-ui-pages a:active {border:1px solid #f3ab26;background:#f3ab26;color:#fff;} + +.widget-pages.skin-green .do-ui-pages a {border:1px solid #a5de37;background:#a5de37;color:#fff;} +.widget-pages.skin-green .do-ui-pages a:hover {border:1px solid #b9e563;background:#b9e563;color:#fff;} +.widget-pages.skin-green .do-ui-pages a:active {border:1px solid #a1d243;background:#a1d243;color:#fff;} + +.widget-pages.skin-purple .do-ui-pages a {border:1px solid #7b72e9;background:#7b72e9;color:#fff;} +.widget-pages.skin-purple .do-ui-pages a:hover {border:1px solid #a49ef0;background:#a49ef0;color:#fff;} +.widget-pages.skin-purple .do-ui-pages a:active {border:1px solid #827ae1;background:#827ae1;color:#fff;} diff --git a/js/lib/pages/pages.js b/js/lib/pages/pages.js new file mode 100644 index 0000000..df372f8 --- /dev/null +++ b/js/lib/pages/pages.js @@ -0,0 +1,127 @@ +define(["avalon","text!./pages.tpl","css!./pages.css"], function (av, tpl) { + + var widget = av.ui.pages = function(ele, data, vms){ + + var opts = av.mix({}, data.pagesOptions); + var height = opts.height || '', + skin = opts.skin || 'default'; + + delete opts.skin; + delete opts.height; + opts.pages = []; //无论是否定义,都会清掉,有点暴力 + + opts.$id = data.pagesId; + opts.$init = function(scan){ + ele.classList.add('widget-pages', 'skin-' + skin, 'do-fn-noselect'); + height && ele.classList.add(height);//非空时才添加,避免报错 + ele.innerHTML = tpl; + calPages(Pager); + scan() + }; + opts.$remove = function() { + ele.innerHTML = '' + }; + opts.setUrl = function(id){ + if(!Pager.url || id === '...' || Pager.curr === id || id > Pager.total || id < 1) + return 'javascript:;' + return Pager.url.replace('{id}', id) + }; + opts.onJump = function(event, id){ + event.preventDefault() + id = id >> 0; + jump(id, Pager); + }; + opts.jumpPage = function(event){ + var pid = Pager.jumpTxt; + if(pid > Pager.total) + Pager.jumpTxt = Pager.total; + + if(event.keyCode == 13) + return jump(pid, Pager); + } + + var Pager = av.define(opts); + + + Pager.$watch('total', function(v, old){ + Pager.total = v >> 0 || 1; //自动转换成数字类型,防止传入的值为字符串时报错,如 '3' + old = old >> 0; + (v !== old) && calPages(Pager); + }) + + Pager.$watch('curr', function(v, old){ + v = v >> 0 || 1;//自动转换成数字类型 + old = old >> 0; + Pager.curr = v; + (v !== old) && calPages(Pager); + }) + + return Pager; + } + + /** + * [calPages 计算要显示的页码数组,并赋值给pages] + * @param {[type]} Pager [分页vm对象] + */ + function calPages(Pager){ + if(Pager.total < 2){ + Pager.pages.clear(); + return; + } + + var pageArr = [], len = (Pager.curr < Pager.max / 2) ? Pager.max : Math.floor(Pager.max / 2); + + if(Pager.curr - len > 1) + pageArr.push('...'); + + for(var i = Pager.curr - len; i < Pager.curr + len && i <= Pager.total; i++){ + if(i > 0) + pageArr.push(i) + } + if(Pager.curr + len < Pager.total) + pageArr.push('...'); + + Pager.pages = pageArr; + } + + /** + * [jump 内部跳转函数] + * @param {[type]} id [要跳转去的页码] + * @param {[type]} Pager [分页vm对象] + */ + function jump(id, Pager){ + if(id < 1) + id = Pager.jumpTxt = 1; + if(id > Pager.total) + id = Pager.jumpTxt = Pager.total; + if(Pager.curr === id) + return; + + Pager.curr = Pager.jumpTxt = id; + Pager.callback && Pager.callback(id); + + calPages(Pager); + } + + //默认参数 + widget.defaults = { + curr: 1, //当前页 + total: 1, // 总页数默认为1,即页面上不会显示分页条 + max: 5, // 最多显示页码数 + url: 'javascript:;', //页码按钮上的url,如'#!/page-{id}.html',其中{id}会被替换成该页码 + pageJump: !1, //是否显示跳转表单 + simpleMode: !1, //简单模式,即只有上一页和下一页 + jumpTxt: 1, //跳转输入框显示的页码 + pages: [], //页码数组 + btns: { //除页码本身外的按钮上的字符 + prev: '<<', + next: '>>', + home: '首页', + end: '末页' + }, + callback: null //点击页码/上/下/首/末页的回调,页码无效或者为当前页的时候不会触发 + } + + + return av; +}) \ No newline at end of file diff --git a/js/lib/pages/pages.min.css b/js/lib/pages/pages.min.css new file mode 100644 index 0000000..584ca81 --- /dev/null +++ b/js/lib/pages/pages.min.css @@ -0,0 +1 @@ +.widget-pages .do-ui-pages{width:100%;height:30px;text-align:center}.widget-pages.h20 .do-ui-pages{height:20px}.widget-pages .do-ui-pages a{display:inline-block;width:auto;min-width:20px;height:30px;padding:0 10px;margin:0 3px;line-height:28px}.widget-pages .do-ui-pages .curr,.widget-pages .do-ui-pages .more{line-height:30px;padding:0;border:none!important;background:none!important;color:#666!important;cursor:default}.widget-pages.h20 .do-ui-pages a{min-width:10px;height:20px;line-height:18px}.widget-pages.h20 .do-ui-pages .curr,.widget-pages.h20 .do-ui-pages .more{line-height:20px}.widget-pages .do-ui-pages .page-jump,.widget-pages .do-ui-pages .page-jump input,.widget-pages .do-ui-pages .page-jump span{display:inline-block}.widget-pages .do-ui-pages .page-jump input{width:25px;height:18px;padding:0 3px;background:0 0;border:1px solid #ddd}.widget-pages.skin-default .do-ui-pages a{border:1px solid #ddd;background:#f3f3f3;color:#666}.widget-pages.skin-default .do-ui-pages a:hover{border:1px solid #e3e3e3;background:#e3e3e3;color:#666}.widget-pages.skin-default .do-ui-pages a:active{border:1px solid #ccc;background:#ccc;color:#666}.widget-pages.skin-blue .do-ui-pages a{border:1px solid #1b9af7;background:#1b9af7;color:#fff}.widget-pages.skin-blue .do-ui-pages a:hover{border:1px solid #13b5ff;background:#13b5ff;color:#fff}.widget-pages.skin-blue .do-ui-pages a:active{border:1px solid #1682cf;background:#1682cf;color:#fff}.widget-pages.skin-red .do-ui-pages a{border:1px solid #ff4351;background:#ff4351;color:#fff}.widget-pages.skin-red .do-ui-pages a:hover{border:1px solid #ff7680;background:#ff7680;color:#fff}.widget-pages.skin-red .do-ui-pages a:active{border:1px solid #f64c59;background:#f64c59;color:#fff}.widget-pages.skin-yellow .do-ui-pages a{border:1px solid #feae1b;background:#feae1b;color:#fff}.widget-pages.skin-yellow .do-ui-pages a:hover{border:1px solid #fec04e;background:#fec04e;color:#fff}.widget-pages.skin-yellow .do-ui-pages a:active{border:1px solid #f3ab26;background:#f3ab26;color:#fff}.widget-pages.skin-green .do-ui-pages a{border:1px solid #a5de37;background:#a5de37;color:#fff}.widget-pages.skin-green .do-ui-pages a:hover{border:1px solid #b9e563;background:#b9e563;color:#fff}.widget-pages.skin-green .do-ui-pages a:active{border:1px solid #a1d243;background:#a1d243;color:#fff}.widget-pages.skin-purple .do-ui-pages a{border:1px solid #7b72e9;background:#7b72e9;color:#fff}.widget-pages.skin-purple .do-ui-pages a:hover{border:1px solid #a49ef0;background:#a49ef0;color:#fff}.widget-pages.skin-purple .do-ui-pages a:active{border:1px solid #827ae1;background:#827ae1;color:#fff} \ No newline at end of file diff --git a/js/lib/pages/pages.min.js b/js/lib/pages/pages.min.js new file mode 100644 index 0000000..e935c7c --- /dev/null +++ b/js/lib/pages/pages.min.js @@ -0,0 +1 @@ +define(["avalon","text!./pages.tpl","css!./pages.css"],function(t,a){function e(t){if(t.total<2)return void t.pages.clear();var a=[],e=t.curr1&&a.push("...");for(var r=t.curr-e;r0&&a.push(r);t.curr+et&&(t=a.jumpTxt=1),t>a.total&&(t=a.jumpTxt=a.total),a.curr!==t&&(a.curr=a.jumpTxt=t,a.callback&&a.callback(t),e(a))}var n=t.ui.pages=function(n,u){var c=t.mix({},u.pagesOptions),l=c.height||"",i=c.skin||"default";delete c.skin,delete c.height,c.pages=[],c.$id=u.pagesId,c.$init=function(t){n.classList.add("widget-pages","skin-"+i,"do-fn-noselect"),l&&n.classList.add(l),n.innerHTML=a,e(o),t()},c.$remove=function(){n.innerHTML=""},c.setUrl=function(t){return!o.url||"..."===t||o.curr===t||t>o.total||1>t?"javascript:;":o.url.replace("{id}",t)},c.onJump=function(t,a){t.preventDefault(),a>>=0,r(a,o)},c.jumpPage=function(t){var a=o.jumpTxt;return a>o.total&&(o.jumpTxt=o.total),13==t.keyCode?r(a,o):void 0};var o=t.define(c);return o.$watch("total",function(t,a){o.total=t>>0||1,a>>=0,t!==a&&e(o)}),o.$watch("curr",function(t,a){t=t>>0||1,a>>=0,o.curr=t,t!==a&&e(o)}),o};return n.defaults={curr:1,total:1,max:5,url:"javascript:;",pageJump:!1,simpleMode:!1,jumpTxt:1,pages:[],btns:{prev:"<<",next:">>",home:"首页",end:"末页"},callback:null},t}); \ No newline at end of file diff --git a/js/lib/pages/pages.tpl b/js/lib/pages/pages.tpl new file mode 100644 index 0000000..c57a431 --- /dev/null +++ b/js/lib/pages/pages.tpl @@ -0,0 +1,13 @@ +
    + {{btns.home}} + {{btns.prev}} + {{el}} + {{btns.next}} + {{btns.end}} +
    + 共{{total}}页,跳转到第 + + + 确定 +
    +
    \ No newline at end of file diff --git a/js/lib/request/ajax.js b/js/lib/request/ajax.js new file mode 100644 index 0000000..4aec5d9 --- /dev/null +++ b/js/lib/request/ajax.js @@ -0,0 +1,1051 @@ +//========================================= +// 数据交互模块 by 司徒正美 +// 版本: 1.0.0 +// 最近更新: 2015/4/30 +//========================================== +avalon.Promise = Promise; +define(["avalon"], function(avalon) { + var global = window + var DOC = global.document + var encode = encodeURIComponent + var decode = decodeURIComponent + + var rlocalProtocol = /^(?:about|app|app-storage|.+-extension|file|res|widget):$/ + var rheaders = /^(.*?):[ \t]*([^\r\n]*)\r?$/mg + var rnoContent = /^(?:GET|HEAD)$/ + var rprotocol = /^\/\// + var rhash = /#.*$/ + var rquery = /\?/ + var rjsonp = /(=)\?(?=&|$)|\?\?/ + var r20 = /%20/g + var radd = /\+/g + var r5b5d = /%5B(.*?)%5D$/; + + var originAnchor = document.createElement("a") + originAnchor.href = location.href + //告诉WEB服务器自己接受什么介质类型,*/* 表示任何类型,type/* 表示该类型下的所有子类型,type/sub-type。 + var accepts = { + xml: "application/xml, text/xml", + html: "text/html", + text: "text/plain", + json: "application/json, text/javascript", + script: "text/javascript, application/javascript", + "*": ["*/"] + ["*"] //避免被压缩掉 + } + + function IE() { + if (window.VBArray) { + var mode = document.documentMode + return mode ? mode : window.XMLHttpRequest ? 7 : 6 + } else { + return 0 + } + } + var useOnload = IE() === 0 || IE() > 8 + + function parseJS(code) { + var indirect = eval + code = code.trim() + if (code) { + if (code.indexOf("use strict") === 1) { + var script = document.createElement("script") + script.text = code; + head.appendChild(script).parentNode.removeChild(script) + } else { + indirect(code) + } + } + } + + if (!String.prototype.startsWith) { + String.prototype.startsWith = function(searchString, position) { + position = position || 0; + return this.lastIndexOf(searchString, position) === position; + } + } + + var head = DOC.getElementsByTagName("head")[0] //HEAD元素 + var isLocal = false + try { + //在IE下如果重置了document.domain,直接访问window.location会抛错,但用document.URL就ok了 + //http://www.cnblogs.com/WuQiang/archive/2012/09/21/2697474.html + isLocal = rlocalProtocol.test(location.protocol) + } catch (e) { + } + + new function() { + //http://www.cnblogs.com/rubylouvre/archive/2010/04/20/1716486.html + var s = ["XMLHttpRequest", + "ActiveXObject('MSXML2.XMLHTTP.6.0')", + "ActiveXObject('MSXML2.XMLHTTP.3.0')", + "ActiveXObject('MSXML2.XMLHTTP')", + "ActiveXObject('Microsoft.XMLHTTP')" + ] + s[0] = IE() < 8 && IE() !== 0 && isLocal ? "!" : s[0] //IE下只能使用ActiveXObject + for (var i = 0, axo; axo = s[i++];) { + try { + if (eval("new " + axo)) { + avalon.xhr = new Function("return new " + axo) + break; + } + } catch (e) { + } + }} + var supportCors = "withCredentials" in avalon.xhr() + + + + + function parseXML(data, xml, tmp) { + try { + var mode = document.documentMode + if (window.DOMParser && (!mode || mode > 8)) { // Standard + tmp = new DOMParser() + xml = tmp.parseFromString(data, "text/xml") + } else { // IE + xml = new ActiveXObject("Microsoft.XMLDOM") //"Microsoft.XMLDOM" + xml.async = "false"; + xml.loadXML(data) + } + } catch (e) { + xml = void 0 + } + if (!xml || !xml.documentElement || xml.getElementsByTagName("parsererror").length) { + avalon.error("Invalid XML: " + data) + } + return xml + } + + //ajaxExtend是一个非常重要的内部方法,负责将用法参数进行规整化 + //1. data转换为字符串 + //2. type转换为大写 + //3. url正常化,加querystring, 加时间戮 + //4. 判定有没有跨域 + //5. 添加hasContent参数 + var defaults = { + type: "GET", + contentType: "application/x-www-form-urlencoded; charset=UTF-8", + async: true, + jsonp: "callback" + } + function ajaxExtend(opts) { + opts = avalon.mix({}, defaults, opts) + opts.type = opts.type.toUpperCase() + var querystring = typeof opts.data === "string" ? opts.data : avalon.param(opts.data) + opts.querystring = querystring || "" + opts.url = opts.url.replace(rhash, "").replace(rprotocol, location.protocol + "//") + + if (typeof opts.crossDomain !== "boolean") { //判定是否跨域 + var urlAnchor = document.createElement("a"); + // Support: IE6-11+ + // IE throws exception if url is malformed, e.g. http://example.com:80x/ + try { + urlAnchor.href = opts.url; + // in IE7-, get the absolute path + var absUrl = !"1"[0] ? urlAnchor.getAttribute("href", 4) : urlAnchor.href; + urlAnchor.href = absUrl + opts.crossDomain = originAnchor.protocol + "//" + originAnchor.host !== urlAnchor.protocol + "//" + urlAnchor.host; + } catch (e) { + opts.crossDomain = true; + } + } + opts.hasContent = !rnoContent.test(opts.type) //是否为post请求 + if (!opts.hasContent) { + if (querystring) { //如果为GET请求,则参数依附于url上 + opts.url += (rquery.test(opts.url) ? "&" : "?") + querystring; + } + if (opts.cache === false) { //添加时间截 + opts.url += (rquery.test(opts.url) ? "&" : "?") + "_time=" + (new Date() - 0) + } + } + return opts; + } + /** + * 伪XMLHttpRequest类,用于屏蔽浏览器差异性 + * var ajax = new(self.XMLHttpRequest||ActiveXObject)("Microsoft.XMLHTTP") + * ajax.onreadystatechange = function(){ + * if (ajax.readyState==4 && ajax.status==200){ + * alert(ajax.responseText) + * } + * } + * ajax.open("POST", url, true) + * ajax.send("key=val&key1=val2") + */ + var XHRMethods = { + setRequestHeader: function(name, value) { + this.requestHeaders[name] = value; + return this; + }, + getAllResponseHeaders: function() { + return this.readyState === 4 ? this.responseHeadersString : null; + }, + getResponseHeader: function(name, match) { + if (this.readyState === 4) { + while ((match = rheaders.exec(this.responseHeadersString))) { + this.responseHeaders[match[1]] = match[2]; + } + match = this.responseHeaders[name]; + } + return match === undefined ? null : match; + }, + overrideMimeType: function(type) { + this.mimeType = type; + return this; + }, + // 中止请求 + abort: function(statusText) { + statusText = statusText || "abort"; + if (this.transport) { + this.respond(0, statusText) + } + return this; + }, + /** + * 用于派发success,error,complete等回调 + * http://www.cnblogs.com/rubylouvre/archive/2011/05/18/2049989.html + * @param {Number} status 状态码 + * @param {String} statusText 对应的扼要描述 + */ + dispatch: function(status, nativeStatusText) { + var statusText = nativeStatusText + // 只能执行一次,防止重复执行 + if (!this.transport) { //2:已执行回调 + return + } + this.readyState = 4 + var isSuccess = status >= 200 && status < 300 || status === 304 + if (isSuccess) { + if (status === 204) { + statusText = "nocontent" + } else if (status === 304) { + statusText = "notmodified" + } else { + //如果浏览器能直接返回转换好的数据就最好不过,否则需要手动转换 + if (typeof this.response === "undefined") { + var dataType = this.options.dataType || this.options.mimeType + if (!dataType && this.responseText || this.responseXML) { //如果没有指定dataType,则根据mimeType或Content-Type进行揣测 + dataType = this.getResponseHeader("Content-Type") || "" + dataType = dataType.match(/json|xml|script|html/i) || ["text"] + dataType = dataType[0].toLowerCase() + } + var responseText = this.responseText || '', + responseXML = this.responseXML || '' + try { + this.response = avalon.ajaxConverters[dataType].call(this, responseText, responseXML) + } catch (e) { + isSuccess = false + this.error = e + statusText = "parsererror" + } + } + } + } + this.status = status; + this.statusText = statusText + "" + if (this.timeoutID) { + clearTimeout(this.timeoutID) + delete this.timeoutID + } + this._transport = this.transport + + /** + * global event handler + */ + var that = this + + // 到这要么成功,调用success, 要么失败,调用 error, 最终都会调用 complete + if (isSuccess) { + this._resolve([this.response, statusText, this]) + /** + * global event handler + */ + window.setTimeout(function() { + avalon.ajaxGlobalEvents.success(that, that.options, that.response) + }, 0) + } else { + this._reject([this, statusText, this.error]) + /** + * global event handler + */ + window.setTimeout(function() { + avalon.ajaxGlobalEvents.error(that, that.options, statusText) + }, 0) + } + delete this.transport + + /** + * global event handler + */ + ajaxActive-- + + window.setTimeout(function() { + avalon.ajaxGlobalEvents.complete(that, that.options) + }, 0) + + if (ajaxActive === 0) { + // 最后一个 + window.setTimeout(function() { + avalon.ajaxGlobalEvents.stop() + }, 0) + } + + } + } + /** + * global event handler + */ + // 记录当前活跃的 ajax 数 + var ajaxActive = 0 + + //ajax主函数 + avalon.ajax = function(opts, promise) { + if (!opts || !opts.url) { + avalon.error("参数必须为Object并且拥有url属性") + } + opts = ajaxExtend(opts) //处理用户参数,比如生成querystring, type大写化 + //创建一个伪XMLHttpRequest,能处理complete,success,error等多投事件 + var XHRProperties = { + responseHeadersString: "", + responseHeaders: {}, + requestHeaders: {}, + querystring: opts.querystring, + readyState: 0, + uniqueID: ("" + Math.random()).replace(/0\./, ""), + status: 0 + } + var _reject, _resolve + var promise = new avalon.Promise(function(resolve, reject) { + _resolve = resolve + _reject = reject + }) + + promise.options = opts + promise._reject = _reject + promise._resolve = _resolve + + var doneList = [], + failList = [] + + Array("done", "fail", "always").forEach(function(method) { + promise[method] = function(fn) { + if (typeof fn === "function") { + if (method !== "fail") + doneList.push(fn) + if (method !== "done") + failList.push(fn) + } + return this + } + }) + + var isSync = opts.async === false + if (isSync) { + avalon.log("warnning:与jquery1.8一样,async:false这配置已经被废弃") + promise.async = false + } + + + avalon.mix(promise, XHRProperties, XHRMethods) + + promise.then(function(value) { + value = Array.isArray(value) ? value : value === void 0 ? [] : [value] + for (var i = 0, fn; fn = doneList[i++];) { + fn.apply(promise, value) + } + return value + }, function(value) { + value = Array.isArray(value) ? value : value === void 0 ? [] : [value] + for (var i = 0, fn; fn = failList[i++];) { + fn.apply(promise, value) + } + return value + }) + + + promise.done(opts.success).fail(opts.error).always(opts.complete) + + var dataType = opts.dataType //目标返回数据类型 + var transports = avalon.ajaxTransports + + if ((opts.crossDomain && !supportCors || rjsonp.test(opts.url)) && dataType === "json" && opts.type === "GET") { + dataType = opts.dataType = "jsonp" + } + var name = opts.form ? "upload" : dataType + var transport = transports[name] || transports.xhr + avalon.mix(promise, transport) //取得传送器的request, respond, preproccess + if (promise.preproccess) { //这用于jsonp upload传送器 + dataType = promise.preproccess() || dataType + } + //设置首部 1、Content-Type首部 + if (opts.contentType) { + promise.setRequestHeader("Content-Type", opts.contentType) + } + //2.处理Accept首部 + promise.setRequestHeader("Accept", accepts[dataType] ? accepts[dataType] + ", */*; q=0.01" : accepts["*"]) + for (var i in opts.headers) { //3. 处理headers里面的首部 + promise.setRequestHeader(i, opts.headers[i]) + } + // 4.处理超时 + if (opts.async && opts.timeout > 0) { + promise.timeoutID = setTimeout(function() { + promise.abort("timeout") + promise.dispatch(0, "timeout") + }, opts.timeout) + } + + /** + * global event handler + */ + if (ajaxActive === 0) { + // 第一个 + avalon.ajaxGlobalEvents.start() + } + avalon.ajaxGlobalEvents.send(promise, opts) + ajaxActive++ + + + + promise.request() + return promise + }; + "get,post".replace(avalon.rword, function(method) { + avalon[method] = function(url, data, callback, type) { + if (typeof data === "function") { + type = type || callback + callback = data + data = void 0 + } + return avalon.ajax({ + type: method, + url: url, + data: data, + success: callback, + dataType: type + }) + }; + }) + function ok(val) { + return val + } + function ng(e) { + throw e + } + avalon.getScript = function(url, callback) { + return avalon.get(url, null, callback, "script") + } + avalon.getJSON = function(url, data, callback) { + return avalon.get(url, data, callback, "json") + } + avalon.upload = function(url, form, data, callback, dataType) { + if (typeof data === "function") { + dataType = callback; + callback = data; + data = void 0; + } + return avalon.ajax({ + url: url, + type: "post", + dataType: dataType, + form: form, + data: data, + success: callback + }); + } + + + /** + * global event handler + */ + avalon.ajaxGlobalEvents = {}; + + ["start", "stop", "complete", "error", "success", "send"].forEach(function(method) { + avalon.ajaxGlobalEvents[method] = avalon.noop + }) + + avalon.ajaxConverters = { //转换器,返回用户想要做的数据 + text: function(text) { + // return text || ""; + return text; + }, + xml: function(text, xml) { + return xml !== void 0 ? xml : parseXML(text) + }, + html: function(text) { + return avalon.parseHTML(text) //一个文档碎片,方便直接插入DOM树 + }, + json: function(text) { + if (!avalon.parseJSON) { + avalon.log("avalon.parseJSON不存在,请升级到最新版") + } + return avalon.parseJSON(text) + }, + script: function(text) { + parseJS(text) + return text; + }, + jsonp: function() { + var json, callbackName; + if (this.jsonpCallback.startsWith('avalon.')) { + callbackName = this.jsonpCallback.replace(/avalon\./, '') + json = avalon[callbackName] + delete avalon[callbackName] + } else { + json = window[this.jsonpCallback] + } + return json; + } + } + + var rbracket = /\[\]$/ + avalon.param = function(obj) { + var prefix, + s = [], + add = function(key, value) { + // If value is a function, invoke it and return its value + value = typeof value === "function" ? value() : (value == null ? "" : value); + s[s.length] = encodeURIComponent(key) + "=" + encodeURIComponent(value); + } + // 处理数组与类数组的jquery对象 + if (Array.isArray(obj)) { + // Serialize the form elements + avalon.each(obj, add) + + } else { + for (prefix in obj) { + paramInner(prefix, obj[prefix], add); + } + } + + // Return the resulting serialization + return s.join("&").replace(r20, "+"); + } + + function paramInner(prefix, obj, add) { + var name; + if (Array.isArray(obj)) { + // Serialize array item. + avalon.each(obj, function(i, v) { + if (rbracket.test(prefix)) { + // Treat each array item as a scalar. + add(prefix, v); + } else { + // Item is non-scalar (array or object), encode its numeric index. + paramInner( + prefix + "[" + (typeof v === "object" ? i : "") + "]", + v, + add); + } + }); + } else if (avalon.isPlainObject(obj)) { + // Serialize object item. + for (name in obj) { + paramInner(prefix + "[" + name + "]", obj[name], add); + } + + } else { + // Serialize scalar item. + add(prefix, obj); + } + } + //将一个字符串转换为对象 + function tryDecodeURIComponent(value) { + try { + return decodeURIComponent(value); + } catch (e) { + return value + } + } + + + //a%5B0%5D%5Bvalue%5D a%5B1%5D%5B%5D + function addSubObject(host, text, value) { + var match = text.match(r5b5d) + if (!match) { + return true + } + + var steps = [] + var first = true + var step, index, key + while (index = text.lastIndexOf("%5B")) { + if (index === -1) { + break + } + key = text.slice(index).slice(3, -3) + text = text.slice(0, index) + if (key === "") { + steps.unshift({ + action: "pushArrayElement" + }) + } else if ((key >>> 0) + "" === key) { + steps.unshift({ + action: "setSubArray", + value: key + }) + } else { + if (first) { + steps.unshift({ + action: "setObjectProperty", + value: tryDecodeURIComponent(key) + }) + } else { + steps.unshift({ + action: "setSubObjet", + value: tryDecodeURIComponent(key) + }) + } + } + first = false + } + first = true + while (step = steps.shift()) { + var isObject = /Object/.test(step.action) + if (first) { + if (!(text in host)) { + host[text] = isObject ? {} : [] + } + first = false + host = host[text] + } + switch (step.action) { + case "pushArrayElement": + host.push(value) + break + case "setObjectProperty": + host[step.value] = value + break + case "setSubObjet": + if (!(step.value in host)) { + host[step.value] = {} + } + host = host[step.value] + break + case "setSubArray": + if (!(step.value in host)) { + host[step.value] = [] + } + host = host[step.value] + break + } + } + } + // function add + avalon.unparam = function(qs, sep, eq) { + sep = sep || '&'; + eq = eq || '='; + var obj = {}; + if ((typeof qs !== "string") || qs.length === 0) { + return obj; + } + if (qs.indexOf("?") !== -1) { + qs = qs.split("?").pop() + } + var array = qs.split(sep); + for (var i = 0, el; el = array[i++];) { + var arr = el.split("=") + if (arr.length === 1) { //处理只有键名没键值的情况 + obj[arr[0]] = "" + } else { + var key = arr[0].replace(radd, '%20') + var v = tryDecodeURIComponent(arr.slice(1).join("=").replace(radd, ' ')); + if (addSubObject(obj, key, v)) { //处理存在中括号的情况 + var k = tryDecodeURIComponent(key) //处理不存在中括号的简单的情况 + if (!Object.prototype.hasOwnProperty.call(obj, k)) { + obj[k] = v; + } else if (Array.isArray(obj[k])) { + obj[k].push(v); + } else { + obj[k] = [obj[k], v]; + } + } + } + } + + return obj + } + var rinput = /select|input|button|textarea/i + var rcheckbox = /radio|checkbox/ + var rline = /\r?\n/g + function trimLine(val) { + return val.replace(rline, "\r\n") + } + //表单元素变字符串, form为一个元素节点 + avalon.serialize = function(form) { + var json = {}; + // 不直接转换form.elements,防止以下情况:
    + Array.prototype.filter.call(form.getElementsByTagName("*"), function(el) { + if (rinput.test(el.nodeName) && el.name && !el.disabled) { + return rcheckbox.test(el.type) ? el.checked : true //只处理拥有name并且没有disabled的表单元素 + } + }).forEach(function(el) { + var val = avalon(el).val() + val = Array.isArray(val) ? val.map(trimLine) : trimLine(val) + var name = el.name + if (name in json) { + if (Array.isArray(val)) { + json[name].push(val) + } else { + json[name] = [json[name], val] + } + } else { + json[name] = val + } + }) + return avalon.param(json, false) // 名值键值对序列化,数组元素名字前不加 [] + } + + var transports = avalon.ajaxTransports = { + xhr: { + //发送请求 + request: function() { + var self = this; + var opts = this.options; + var transport = this.transport = new avalon.xhr; + transport.open(opts.type, opts.url, opts.async, opts.username, opts.password) + if (this.mimeType && transport.overrideMimeType) { + transport.overrideMimeType(this.mimeType) + } + //IE6下,如果transport中没有withCredentials,直接设置会报错 + if (opts.crossDomain && "withCredentials" in transport) { + transport.withCredentials = true + } + + /* + * header 中设置 X-Requested-With 用来给后端做标示: + * 这是一个 ajax 请求。 + * + * 在 Chrome、Firefox 3.5+ 和 Safari 4+ 下, + * 在进行跨域请求时设置自定义 header,会触发 preflighted requests, + * 会预先发送 method 为 OPTIONS 的请求。 + * + * 于是,如果跨域,禁用此功能。 + */ + if (!opts.crossDomain) { + this.requestHeaders["X-Requested-With"] = "XMLHttpRequest" + } + + for (var i in this.requestHeaders) { + transport.setRequestHeader(i, this.requestHeaders[i] + "") + } + + /* + * progress + */ + if (opts.progressCallback) { + // 判断是否 ie6-9 + var isOldIE = document.all && !window.atob; + if (!isOldIE) { + transport.upload.onprogress = opts.progressCallback + } + } + + var dataType = opts.dataType + if ("responseType" in transport && /^(blob|arraybuffer|text)$/.test(dataType)) { + transport.responseType = dataType + this.useResponseType = true + } + //必须要支持 FormData 和 file.fileList 的浏览器 才能用 xhr 发送 + //标准规定的 multipart/form-data 发送必须用 utf-8 格式, 记得 ie 会受到 document.charset 的影响 + transport.send(opts.hasContent && (this.formdata || this.querystring) || null) + //在同步模式中,IE6,7可能会直接从缓存中读取数据而不会发出请求,因此我们需要手动发出请求 + + if (!opts.async || transport.readyState === 4) { + this.respond() + } else { + if (useOnload) { //如果支持onerror, onload新API + transport.onload = transport.onerror = function(e) { + this.readyState = 4 //IE9+ + this.status = e.type === "load" ? 200 : 500 + self.respond() + } + } else { + transport.onreadystatechange = function() { + self.respond() + } + } + } + }, + //用于获取原始的responseXMLresponseText 修正status statusText + //第二个参数为1时中止清求 + respond: function(event, forceAbort) { + var transport = this.transport + if (!transport) { + return + } + // by zilong:避免abort后还继续派发onerror等事件 + if (forceAbort && this.timeoutID) { + clearTimeout(this.timeoutID); + delete this.timeoutID + } + try { + var completed = transport.readyState === 4 + if (forceAbort || completed) { + transport.onreadystatechange = avalon.noop + if (useOnload) { //IE6下对XHR对象设置onerror属性可能报错 + transport.onerror = transport.onload = null + } + if (forceAbort) { + if (!completed && typeof transport.abort === "function") { // 完成以后 abort 不要调用 + transport.abort() + } + } else { + var status = transport.status + //设置responseText + var text = transport.responseText + + this.responseText = typeof text === "string" ? text : void 0 + //设置responseXML + try { + //当responseXML为[Exception: DOMException]时, + //访问它会抛“An attempt was made to use an object that is not, or is no longer, usable”异常 + var xml = transport.responseXML + this.responseXML = xml.documentElement + } catch (e) { + } + //设置response + if (this.useResponseType) { + this.response = transport.response + } + //设置responseHeadersString + this.responseHeadersString = transport.getAllResponseHeaders() + + try { //火狐在跨城请求时访问statusText值会抛出异常 + var statusText = transport.statusText + } catch (e) { + this.error = e + statusText = "firefoxAccessError" + } + //用于处理特殊情况,如果是一个本地请求,只要我们能获取数据就假当它是成功的 + if (!status && isLocal && !this.options.crossDomain) { + status = this.responseText ? 200 : 404 + //IE有时会把204当作为1223 + } else if (status === 1223) { + status = 204 + } + this.dispatch(status, statusText) + } + } + } catch (err) { + // 如果网络问题时访问XHR的属性,在FF会抛异常 + // http://helpful.knobs-dials.com/index.php/Component_returned_failure_code:_0x80040111_(NS_ERROR_NOT_AVAILABLE) + if (!forceAbort) { + this.dispatch(500, err) + } + } + } + }, + jsonp: { + preproccess: function() { + var opts = this.options; + var name = this.jsonpCallback = opts.jsonpCallback || "avalon.jsonp" + setTimeout("1") + if (rjsonp.test(opts.url)) { + opts.url = opts.url.replace(rjsonp, "$1" + name) + } else { + opts.url = opts.url + (rquery.test(opts.url) ? "&" : "?") + opts.jsonp + "=" + name + } + //将后台返回的json保存在惰性函数中 + if (name.startsWith('avalon.')) { + name = name.replace(/avalon\./, '') + avalon[name] = function(json) { + avalon[name] = json + } + } else { + window[name] = function(json) { + window[name] = json + } + } + return "script" + } + }, + script: { + request: function() { + var opts = this.options; + var node = this.transport = DOC.createElement("script") + if (opts.charset) { + node.charset = opts.charset + } + var self = this; + node.onerror = node[useOnload ? "onload" : "onreadystatechange"] = function() { + self.respond() + }; + node.src = opts.url + head.insertBefore(node, head.firstChild) + }, + respond: function(event, forceAbort) { + var node = this.transport + if (!node) { + return + } + // by zilong:避免abort后还继续派发onerror等事件 + if (forceAbort && this.timeoutID) { + clearTimeout(this.timeoutID); + delete this.timeoutID + } + var execute = /loaded|complete|undefined/i.test(node.readyState) + if (forceAbort || execute) { + node.onerror = node.onload = node.onreadystatechange = null + var parent = node.parentNode; + if (parent) { + parent.removeChild(node) + } + if (!forceAbort) { + var args; + if (this.jsonpCallback) { + var jsonpCallback = this.jsonpCallback.startsWith('avalon.') ? avalon[this.jsonpCallback.replace(/avalon\./, '')] : window[this.jsonpCallback] + args = typeof jsonpCallback === "function" ? [500, "error"] : [200, "success"] + } else { + args = [200, "success"] + } + + this.dispatch.apply(this, args) + } + } + } + }, + upload: { + preproccess: function() { + var opts = this.options, formdata + if (typeof opts.form.append === "function") { //简单判断opts.form是否为FormData + formdata = opts.form; + opts.contentType = ''; + } else { + formdata = new FormData(opts.form) //将二进制什么一下子打包到formdata + } + avalon.each(opts.data, function(key, val) { + formdata.append(key, val) //添加客外数据 + }) + this.formdata = formdata + } + } + } + + + avalon.mix(transports.jsonp, transports.script) + avalon.mix(transports.upload, transports.xhr) + + if (!window.FormData) { + var str = 'Function BinaryToArray(binary)\r\n\ + Dim oDic\r\n\ + Set oDic = CreateObject("scripting.dictionary")\r\n\ + length = LenB(binary) - 1\r\n\ + For i = 1 To length\r\n\ + oDic.add i, AscB(MidB(binary, i, 1))\r\n\ + Next\r\n\ + BinaryToArray = oDic.Items\r\n\ + End Function' + execScript(str, "VBScript"); + avalon.fixAjax = function() { + avalon.ajaxConverters.arraybuffer = function() { + var body = this.tranport && this.tranport.responseBody + if (body) { + return new VBArray(BinaryToArray(body)).toArray(); + } + }; + function createIframe(ID) { + var iframe = avalon.parseHTML("').firstChild + return (doc.body || doc.documentElement).insertBefore(iframe, null) + }, + mkForm: function(form){ + if(!form) + form = doc.createElement('form') + + form.target = this.pool.uuid + form.action = this.pool.url + form.method = 'POST' + form.enctype = 'multipart/form-data' + + return form + }, + dispatch: function(self, status){ + var _this = this + + if(!this.transport) + return + + //状态为4,既已成功, 则清除超时 + clearTimeout(_this.timeoutID) + + if(status === 4) + self.status = status + + //成功的回调 + var isSucc = (self.status >= 200 && self.status < 300) || self.status === 304 + + var result = { + response: { + url: self.responseURL || self.URL, + headers: {'content-type': ''} + }, + request: { + url: self.responseURL || self.URL, + headers: _this.pool.headers + }, + status: self.status, + statusText: self.statusText || 'OK', + text: '', + body: '', + error: null + } + if(typeof _this.transport !== 'object'){ + delete _this.transport + delete _this.pool + } + // 非iframe方式 + if(!status){ + var headers = self.getAllResponseHeaders() + headers = headers.split('\n') + headers.forEach(function(it, i){ + it = it.trim() + if(it){ + it = it.split(':') + result.response.headers[it.shift().toLowerCase()] = it.join(':').trim() + } + + }) + } + + if(isSucc){ + + if(status === 204){ + result.statusText = 'no content' + }else if(status === 304){ + result.statusText = 'not modified' + }else{ + //处理返回的数据 + + var dataType = result.response.headers['content-type'].match(/json|xml|script|html/i) || ['text'] + + dataType = dataType[0].toLowerCase() + + var responseTXT = self.responseText || '' + var responseXML = self.responseXML || '' + try{ + result.text = responseTXT || responseXML + result.body = requestConvert[dataType](responseTXT, responseXML) + }catch(e){ + isSucc = false + result.error = e + result.statusText = 'parse error' + } + } + }else{ + if(status){ + result.status = 200 + + if(self.body){ + result.text = self.body.innerHTML + result.body = result.text + var child = self.body.firstChild + if(child && child.nodeName.toUpperCase() === 'PRE'){ + result.text = child.innerHTML + result.body = requestConvert.json(result.text) + } + }else{ + result.text = result.body = self + } + + //删除iframe + _this.transport.parentNode.removeChild(_this.transport) + //还原表单, 避免参数重复提交 + if(_this.pool.field && _this.pool.field.length){ + _this.pool.form.target = '' + _this.pool.form.action = '' + for(var i = 0,el; el = _this.pool.field[i++];){ + _this.pool.form.removeChild(el) + } + } + + }else{ + result.status = result.status || 504 + result.statusText = result.statusText || 'Connected timeout' + result.error = F.merge(new Error(result.statusText), {status: result.status}) + } + } + + _this.callback(result.error, result) + delete _this.transport + delete _this.pool + delete _this.xhr + + } + } + + // 设置表单类型, 支持2种, form/json + _requestp.type = function(t){ + if(this.pool.formType === 'form-data') + return this + + this.pool.formType = t || 'form' + if(t === 'form' || this.pool.type === 'GET') + this.set('content-type', 'application/x-www-form-urlencoded; charset=UTF-8') + else + this.set('content-type', 'application/json; charset=UTF-8') + + return this + } + + //设置头信息 + _requestp.set = function(k, val){ + if(!this.transport) + return + + if(typeof k === 'object'){ + for(var i in k){ + i = i.toLowerCase() + this.pool.headers[i] = k[i] + } + + }else if(typeof k === 'string'){ + if(arguments.length < 2) + throw new Error('2 arguments required') + + // 全转小写,避免重复写入 + k = k.toLowerCase() + + if(val === undefined) + delete this.pool.headers[k] + else + this.pool.headers[k] = val + }else{ + throw new Error('arguments must be string/object, but [' + (typeof k) + '] given') + } + return this + } + + //设置请求参数 + _requestp.send = function(k, val){ + + if(!this.transport) + return + + // 1. send方法可以多次调用, 但必须保证格式一致 + // 2. 2次圴提交纯字符串也会抛出异常 + if(typeof k === 'object'){ + if(this.pool.data && (typeof this.pool.data === 'string')) + throw new Error('param can not be string and object at the same time') + if(!this.pool.data) + this.pool.data = {} + + F.merge(this.pool.data, k) + }else{ + if(typeof k === 'string'){ + if(arguments.length === 1){ + if(this.pool.data) + throw new Error('invalid param in function send') + + this.pool.data = k + }else{ + if(this.pool.data && (typeof this.pool.data === 'string')) + throw new Error('param can not be string and object at the same time') + + if(!this.pool.data) + this.pool.data = {} + + this.pool.data[k] = val + } + + }else{ + throw new Error('argument of send must be string/object, but [' + (typeof k) + '] given') + } + + } + + return this + } + + //该方法用于 form-data类型的post请求的参数设置 + _requestp.field = function(k, val){ + + if(!this.transport) + return + + // 此类型优先级最高 + this.pool.formType = 'form-data' + if(!this.pool.data || (this.pool.data && typeof this.pool.data !== 'object')) + this.pool.data = {} + + if(arguments.length === 1 && typeof k === 'object'){ + F.merge(this.pool.data, k) + }else if(arguments.length === 2){ + this.pool.data[k] = val + }else{ + throw new TypeError('argument must be an object, but ' + (typeof k) + ' given') + } + return this + } + + + //设置缓存 + _requestp.cache = function(t){ + if(!this.transport) + return + + if(this.pool.type === 'GET') + this.pool.cache = !!t + + return this + } + + + //取消网络请求 + _requestp.abort = function(){ + delete this.transport + if(!this.pool.form) + this.xhr.abort() + + return this + } + + //超时设置, 单位毫秒 + _requestp.timeout = function(time){ + if(typeof time !== 'number' || time < 1) + return this + + this.pool.timeout = time + return this + } + + + _requestp.form = function(form){ + if(typeof form === 'object' && form.nodeName === 'FORM'){ + this.pool.type = 'POST' + this.pool.form = form + } + + return this + } + + + var originAnchor = doc.createElement('a') + originAnchor.href = location.href + _requestp.end = function(callback){ + var _this = this; + // 回调已执行, 或已取消, 则直接返回, 防止重复执行 + if(!this.transport) + return + + if(!this.pool.url) + throw new Error('Invalid request url') + + F.merge(this, requestExtend) + + this.callback = callback || noop + + // 1. url规范化 + this.pool.url = this.pool.url.replace(/#.*$/, '').replace(/^\/\//, location.protocol + '//') + + // 2. data转字符串 + this.pool.param = F.param(this.pool.data) + + // 3. 处理跨域 + if(typeof this.pool.crossDomain !== 'boolean'){ + var anchor = doc.createElement('a') + try{ + anchor.href = this.pool.url + // IE7及以下浏览器 '1'[0]的结果是 undefined + // IE7下需要获取绝对路径 + var absUrl = !'1'[0] ? anchor.getAttribute('href', 4) : anchor.href + anchor.href = absUrl + this.pool.crossDomain = (originAnchor.protocol !== anchor.protocol) || (originAnchor.host !== anchor.host) + }catch(e){ + this.pool.crossDomain = true + } + } + + // 4. 设置Content-Type类型, 默认x-www-form-urlencoded + if(!this.pool.formType) + this.type('form') + + // 5.处理GET请求 + this.pool.hasContent = this.pool.type !== 'GET' //是否为post请求 + if(!this.pool.hasContent){ + + //GET请求直接把参数拼接到url上 + if(this.pool.param){ + this.pool.url += (/\?/.test(this.pool.url) ? '&' : '?') + this.pool.param + } + //加随机值,避免缓存 + if(this.pool.cache === false) + this.pool.url += (/\?/.test(this.pool.url) ? '&' : '?') + '_=' + Math.random() + }else{ + if(this.pool.formType === 'form-data'){ + delete this.pool.headers['content-type'] + this.pool.param = this.formData() + + }else if(this.pool.formType !== 'form'){ + this.pool.param = JSON.stringify(this.pool.data) + } + } + + // transport不恒为 true, 则说明是走iframe路线的 + if(this.transport !== true){ + this.transport.onload = function(ev){ + _this.dispatch(this.contentWindow.document, 4) + } + this.pool.form.submit() + }else{ + this.xhr.onreadystatechange = function(statusTxt){ + + if(this.readyState !== 4) + return + + _this.dispatch(this) + } + + + // 6. 初始化xhr提交 + this.xhr.open(this.pool.type, this.pool.url, true) + + // 7. 设置头信息 + for(var i in this.pool.headers){ + if(this.pool.headers[i]) + this.xhr.setRequestHeader(i, this.pool.headers[i]) + } + + // 8. 发起网络请求 + this.xhr.send(this.pool.param) + + + } + + //超时处理 + //IE8- 不支持timeout属性的设置, + if(this.pool.timeout && this.pool.timeout > 0){ + this.timeoutID = setTimeout(function(){ + _this.abort() + }, this.pool.timeout) + } + + } + + + + + + + + + + + + + // ---------------------- end ------------------------ + + + if(!win.request){ + win.request = { + get: function(url){ + if(!url) + throw new Error('argument url is required') + + return new _request(url, 'GET') + }, + post: function(url){ + if(!url) + throw new Error('argument url is required') + + return new _request(url, 'POST') + }, + version: '1.0.0', + release: 'request full version/1.0.0' + } + } + + return request +}) diff --git a/js/lib/request/request.min.js b/js/lib/request/request.min.js new file mode 100644 index 0000000..b994faf --- /dev/null +++ b/js/lib/request/request.min.js @@ -0,0 +1 @@ +"use strict";var r=[];window.JSON||(r=["./json"]),define(r,function(){function serialize(t,e,o){var r;if(Array.isArray(e))e.forEach(function(e,i){r=t?t+"["+(Array.isArray(e)?i:"")+"]":i,"object"==typeof e?serialize(r,e,o):o(r,e)});else for(var i in e)r=t?t+"["+i+"]":i,"object"==typeof e[i]?serialize(r,e[i],o):o(r,e[i])}var _request=function(t,e){this.transport=!0,e=(e+"").trim().toUpperCase(),this.xhr=Xhr(),this.pool={url:(t+"").trim(),type:e||"GET",form:"",headers:{},uuid:Math.random().toString(16).substr(2)}},_requestp=_request.prototype,toS=Object.prototype.toString,win=window,doc=win.document,encode=encodeURIComponent,decode=decodeURIComponent,noop=function(t){if(t)throw new Error(t+"")},rlocalProtocol=/^(?:about|app|app-storage|.+-extension|file|res|widget):$/,isLocal=!1;try{isLocal=rlocalProtocol.test(location.protocol)}catch(e){}var rheaders=/^(.*?):[ \t]*([^\r\n]*)\r?$/gm;!win.FormData,String.prototype.trim&&(String.prototype.trim=function(){return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,"")}),Array.prototype.forEach||(Array.prototype.forEach=function(t,e){var o,r;if(null===this)throw new TypeError("forEach is not a function of null");var i=Object(this),s=i.length>>>0;if("function"!=typeof t)throw new TypeError("callback is not a function");for(arguments.length>1&&(o=e),r=0;s>r;){var a;r in i&&(a=i[r],t.call(o,a,r,i)),r++}}),Array.isArray||(Array.isArray=function(t){return"[object Array]"===toS.call(t)});var IE=function(){if(window.VBArray){var t=document.documentMode;return t?t:window.XMLHttpRequest?7:6}return 0}();win.Xhr=function(){var obj=["XMLHttpRequest","ActiveXObject('MSXML2.XMLHTTP.6.0')","ActiveXObject('MSXML2.XMLHTTP.3.0')","ActiveXObject('MSXML2.XMLHTTP')","ActiveXObject('Microsoft.XMLHTTP')"];obj[0]=8>IE&&0!==IE&&isLocal?"!":obj[0];for(var i=0,a;a=obj[i++];)try{if(eval("new "+a))return new Function("return new "+a)}catch(e){}}();var supportCors="withCredentials"in Xhr(),Format=function(){this.tagHooks=new function(){this.option=doc.createElement("select"),this.thead=doc.createElement("table"),this.td=doc.createElement("tr"),this.area=doc.createElement("map"),this.tr=doc.createElement("tbody"),this.col=doc.createElement("colgroup"),this.legend=doc.createElement("fieldset"),this._default=doc.createElement("div"),this.g=doc.createElementNS("http://www.w3.org/2000/svg","svg"),this.optgroup=this.option,this.tbody=this.tfoot=this.colgroup=this.caption=this.thead,this.th=this.td};var t=this;"circle,defs,ellipse,image,line,path,polygon,polyline,rect,symbol,text,use".replace(/,/g,function(e){t.tagHooks[e]=t.tagHooks.g}),this.rtagName=/<([\w:]+)/,this.rxhtml=/<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/gi,this.scriptTypes={"text/javascript":1,"text/ecmascript":1,"application/ecmascript":1,"application/javascript":1},this.rhtml=/<|&#?\w+;/};Format.prototype={parseJS:function(code){if(code=(code+"").trim())if(1===code.indexOf("use strict")){var script=doc.createElement("script");script.text=code,doc.head.appendChild(script).parentNode.removeChild(script)}else eval(code)},parseXML:function(t,e,o){try{var r=doc.documentMode;win.DOMParser&&(!r||r>8)?(o=new DOMParser,e=o.parseFromString(t,"text/xml")):(e=new ActiveXObject("Microsoft.XMLDOM"),e.async="false",e.loadXML(t))}catch(i){e=void 0}return e&&e.documentElement&&!e.getElementsByTagName("parsererror").length||console.error("Invalid XML: "+t),e},parseHTML:function(t){var e=doc.createDocumentFragment().cloneNode(!1);if("string"!=typeof t)return e;if(!this.rhtml.test(t))return e.appendChild(document.createTextNode(t)),e;t=t.replace(this.rxhtml,"<$1>").trim();var o=(this.rtagName.exec(t)||["",""])[1].toLowerCase(),r=this.tagHooks[o]||this.tagHooks._default,i=null;r.innerHTML=t;var s=r.getElementsByTagName("script");if(s.length)for(var a,n=0;a=s[n++];)if(this.scriptTypes[a.type]){var p=doc.createElement("script").cloneNode(!1);a.attributes.forEach(function(t){p.setAttribute(t.name,t.value)}),p.text=a.text,a.parentNode.replaceChild(p,a)}for(;i=r.firstChild;)e.appendChild(i);return e},param:function(t){if(!t||"string"==typeof t||"number"==typeof t)return t;var e=[],o=function(t,o){/native code/.test(o)||(o="function"==typeof o?o():o,o="[object File]"!==toS.call(o)?encode(o):o,e.push(encode(t)+"="+o))};return"object"==typeof t&&serialize("",t,o),e.join("&")},parseForm:function(t){for(var e,o={},r=0;e=t.elements[r++];)switch(e.type){case"select-one":case"select-multiple":if(e.name.length&&!e.disabled)for(var i,s=0;i=e.options[s++];)i.selected&&(o[e.name]=i.value||i.text);break;case"file":e.name.length&&!e.disabled&&(o[e.name]=e.files[0]);break;case void 0:case"submit":case"reset":case"button":break;case"radio":case"checkbox":if(!e.checked)break;default:e.name.length&&!e.disabled&&(o[e.name]=e.value)}return o},merge:function(t,e){if("object"!=typeof t||"object"!=typeof e)throw new TypeError("argument must be an object");if(Object.assign)return Object.assign(t,e);for(var o in e)t[o]=e[o];return t}};var F=new Format,requestConvert={text:function(t){return t},xml:function(t,e){return void 0!==e?e:F.parseXML(t)},html:function(t){return F.parseHTML(t)},json:function(t){return JSON.parse(t)},script:function(t){return F.parseJS(t)},jsonp:function(){return window[this.jsonpCallback]}},requestExtend={formData:function(){if(win.FormData){if(this.pool.form){console.log(this.pool.form.elements);var t=F.parseForm(this.pool.form);console.log(t),F.merge(this.pool.data,t)}var e=new FormData;for(var o in this.pool.data){var r=this.pool.data[o];Array.isArray(r)?r.forEach(function(t){e.append(o+"[]",t)}):e.append(o,this.pool.data[o])}return e}this.transport=this.mkIframe(this.pool.uuid),this.pool.form=this.mkForm(this.pool.form),this.pool.field=[];for(var o in this.pool.data){var i=this.pool.data[o];if(Array.isArray(i))for(var s,a=0;s=i[a++];){var r=doc.createElement("input");r.type="hidden",r.name=o+"[]",r.value=s,this.pool.field.push(r),this.pool.form.appendChild(r)}else{var r=doc.createElement("input");r.type="hidden",r.name=o,r.value=i,this.pool.field.push(r),this.pool.form.appendChild(r)}}},mkIframe:function(t){var e=F.parseHTML('').firstChild;return(doc.body||doc.documentElement).insertBefore(e,null)},mkForm:function(t){return t||(t=doc.createElement("form")),t.target=this.pool.uuid,t.action=this.pool.url,t.method="POST",t.enctype="multipart/form-data",t},dispatch:function(t,e){var o=this;if(this.transport){clearTimeout(o.timeoutID),4===e&&(t.status=e);var r=t.status>=200&&t.status<300||304===t.status,i={response:{url:t.responseURL||t.URL,headers:{"content-type":""}},request:{url:t.responseURL||t.URL,headers:o.pool.headers},status:t.status,statusText:t.statusText||"OK",text:"",body:"",error:null};if("object"!=typeof o.transport&&(delete o.transport,delete o.pool),!e){var s=t.getAllResponseHeaders();s=s.split("\n"),s.forEach(function(t){t=t.trim(),t&&(t=t.split(":"),i.response.headers[t.shift().toLowerCase()]=t.join(":").trim())})}if(r)if(204===e)i.statusText="no content";else if(304===e)i.statusText="not modified";else{var a=i.response.headers["content-type"].match(/json|xml|script|html/i)||["text"];a=a[0].toLowerCase();var n=t.responseText||"",p=t.responseXML||"";try{i.text=n||p,i.body=requestConvert[a](n,p)}catch(l){r=!1,i.error=l,i.statusText="parse error"}}else if(e){if(i.status=200,t.body){i.text=t.body.innerHTML,i.body=i.text;var c=t.body.firstChild;c&&"PRE"===c.nodeName.toUpperCase()&&(i.text=c.innerHTML,i.body=requestConvert.json(i.text))}else i.text=i.body=t;if(o.transport.parentNode.removeChild(o.transport),o.pool.field&&o.pool.field.length){o.pool.form.target="",o.pool.form.action="";for(var h,u=0;h=o.pool.field[u++];)o.pool.form.removeChild(h)}}else i.status=i.status||504,i.statusText=i.statusText||"Connected timeout",i.error=F.merge(new Error(i.statusText),{status:i.status});o.callback(i.error,i),delete o.transport,delete o.pool,delete o.xhr}}};_requestp.type=function(t){return"form-data"===this.pool.formType?this:(this.pool.formType=t||"form","form"===t||"GET"===this.pool.type?this.set("content-type","application/x-www-form-urlencoded; charset=UTF-8"):this.set("content-type","application/json; charset=UTF-8"),this)},_requestp.set=function(t,e){if(this.transport){if("object"==typeof t)for(var o in t)o=o.toLowerCase(),this.pool.headers[o]=t[o];else{if("string"!=typeof t)throw new Error("arguments must be string/object, but ["+typeof t+"] given");if(arguments.length<2)throw new Error("2 arguments required");t=t.toLowerCase(),void 0===e?delete this.pool.headers[t]:this.pool.headers[t]=e}return this}},_requestp.send=function(t,e){if(this.transport){if("object"==typeof t){if(this.pool.data&&"string"==typeof this.pool.data)throw new Error("param can not be string and object at the same time");this.pool.data||(this.pool.data={}),F.merge(this.pool.data,t)}else{if("string"!=typeof t)throw new Error("argument of send must be string/object, but ["+typeof t+"] given");if(1===arguments.length){if(this.pool.data)throw new Error("invalid param in function send");this.pool.data=t}else{if(this.pool.data&&"string"==typeof this.pool.data)throw new Error("param can not be string and object at the same time");this.pool.data||(this.pool.data={}),this.pool.data[t]=e}}return this}},_requestp.field=function(t,e){if(this.transport){if(this.pool.formType="form-data",(!this.pool.data||this.pool.data&&"object"!=typeof this.pool.data)&&(this.pool.data={}),1===arguments.length&&"object"==typeof t)F.merge(this.pool.data,t);else{if(2!==arguments.length)throw new TypeError("argument must be an object, but "+typeof t+" given");this.pool.data[t]=e}return this}},_requestp.cache=function(t){return this.transport?("GET"===this.pool.type&&(this.pool.cache=!!t),this):void 0},_requestp.abort=function(){return delete this.transport,this.pool.form||this.xhr.abort(),this},_requestp.timeout=function(t){return"number"!=typeof t||1>t?this:(this.pool.timeout=t,this)},_requestp.form=function(t){return"object"==typeof t&&"FORM"===t.nodeName&&(this.pool.type="POST",this.pool.form=t),this};var originAnchor=doc.createElement("a");return originAnchor.href=location.href,_requestp.end=function(t){var e=this;if(this.transport){if(!this.pool.url)throw new Error("Invalid request url");if(F.merge(this,requestExtend),this.callback=t||noop,this.pool.url=this.pool.url.replace(/#.*$/,"").replace(/^\/\//,location.protocol+"//"),this.pool.param=F.param(this.pool.data),"boolean"!=typeof this.pool.crossDomain){var o=doc.createElement("a");try{o.href=this.pool.url;var r="1"[0]?o.href:o.getAttribute("href",4);o.href=r,this.pool.crossDomain=originAnchor.protocol!==o.protocol||originAnchor.host!==o.host}catch(i){this.pool.crossDomain=!0}}if(this.pool.formType||this.type("form"),this.pool.hasContent="GET"!==this.pool.type,this.pool.hasContent?"form-data"===this.pool.formType?(delete this.pool.headers["content-type"],this.pool.param=this.formData()):"form"!==this.pool.formType&&(this.pool.param=JSON.stringify(this.pool.data)):(this.pool.param&&(this.pool.url+=(/\?/.test(this.pool.url)?"&":"?")+this.pool.param),this.pool.cache===!1&&(this.pool.url+=(/\?/.test(this.pool.url)?"&":"?")+"_="+Math.random())),this.transport!==!0)this.transport.onload=function(){e.dispatch(this.contentWindow.document,4)},this.pool.form.submit();else{this.xhr.onreadystatechange=function(){4===this.readyState&&e.dispatch(this)},this.xhr.open(this.pool.type,this.pool.url,!0);for(var s in this.pool.headers)this.pool.headers[s]&&this.xhr.setRequestHeader(s,this.pool.headers[s]);this.xhr.send(this.pool.param)}this.pool.timeout&&this.pool.timeout>0&&(this.timeoutID=setTimeout(function(){e.abort()},this.pool.timeout))}},win.request||(win.request={get:function(t){if(!t)throw new Error("argument url is required");return new _request(t,"GET")},post:function(t){if(!t)throw new Error("argument url is required");return new _request(t,"POST")},version:"1.0.0",release:"request full version/1.0.0"}),request}); \ No newline at end of file diff --git a/js/lib/router/router.js b/js/lib/router/router.js new file mode 100644 index 0000000..1f775ee --- /dev/null +++ b/js/lib/router/router.js @@ -0,0 +1,163 @@ +define(['yua'], function(){ + + function Router(){ + this.table = {get: []}; + this.errorFn = null; + this.history = null; + this.hash = ''; + this.started = false; + this.init = {}; + } + + var defaultOptions = { + prefix: /^(#!|#)[\/]?/, //hash前缀正则 + historyOpen: true, //是否开启hash历史 + allowReload: true //连续点击同一个链接是否重新加载 + }; + var isMouseUp = true; + + var ruleRegExp = /(:id)|(\{id\})|(\{id:([A-z\d\,\[\]\{\}\-\+\*\?\!:\^\$]*)\})/g; + + Router.prototype = { + error: function(callback){ + this.errorFn = callback; + }, + config: function(opts){ + if(this.started) + return console.error('Router config has been set'); + + this.started = true; + if(!opts.allowReload) + opts.historyOpen = true; + this.init = yua.mix({}, defaultOptions, opts); + }, + _getRegExp: function(rule, opts){ + var re = rule.replace(ruleRegExp, function(m, p1, p2, p3, p4){ + var w = '([\\w.-]'; + if(p1 || p2){ + return w + '+)'; + }else{ + if(!/^\{[\d\,]+\}$/.test(p4)){ + w = '('; + } + return w + p4 + ')'; + } + }) + re = re.replace(/(([^\\])([\/]+))/g, '$2\\/').replace(/(([^\\])([\.]+))/g, '$2\\.').replace(/(([^\\])([\-]+))/g, '$2\\-').replace(/(\(.*)(\\[\-]+)(.*\))/g, '$1-$3'); + re = '^' + re + '$'; + opts.regexp = new RegExp(re) + return opts; + }, + _add: function(method, rule, callback){ + if(!this.started) + this.config({}); + + var table = this.table[method.toLowerCase()]; + if (rule.charAt(0) !== "/") { + console.error('char "/" must be in front of router rule'); + return; + } + rule = rule.replace(/^[\/]+|[\/]+$|\s+/g, ''); + var opts = {}; + opts.rule = rule; + opts.callback = callback; + yua.Array.ensure(table, this._getRegExp(rule, opts)); + }, + _route: function(method, hash){ + var hash = hash.trim(); + var table = this.table[method]; + var init = this.init; + + if(!init.allowReload && hash === this.history) + return; + + if(init.historyOpen){ + this.history = hash; + if(yua.ls) + yua.ls('lastHash', hash); + } + + for(var i = 0, obj; obj = table[i++];){ + var args = hash.match(obj.regexp); + if(args){ + args.shift(); + return obj.callback.apply(obj, args) + } + } + this.errorFn && this.errorFn(hash); + }, + on: function(rule, callback){ + this._add('get', rule, callback); + } + } + + yua.bind(window, 'load', function(){ + if(!yua.router.started) + return; + var prefix = yua.router.init.prefix; + var hash = location.hash; + hash = hash.replace(prefix, "").trim(); + yua.router._route('get', hash); + }); + + + if('onhashchange' in window){ + window.addEventListener('hashchange', function(event){ + if(!isMouseUp) + return; + var prefix = yua.router.init.prefix; + var hash = location.hash.replace(prefix, "").trim(); + yua.router._route('get', hash) + }) + } + + //劫持页面上所有点击事件,如果事件源来自链接或其内部, + //并且它不会跳出本页,并且以"#/"或"#!/"开头,那么触发updateLocation方法 + yua.bind(document, "mousedown", function(event){ + var defaultPrevented = "defaultPrevented" in event ? event['defaultPrevented'] : event.returnValue === false + if (defaultPrevented || event.ctrlKey || event.metaKey || event.which === 2) + return + var target = event.target + while (target.nodeName !== "A") { + target = target.parentNode + if (!target || target.tagName === "BODY") { + return + } + } + + if (targetIsThisWindow(target.target)){ + if(!yua.router.started) + return; + var href = target.getAttribute("href") || target.getAttribute("xlink:href"), + prefix = yua.router.init.prefix; + + if (href === null || !prefix.test(href)) + return + + yua.router.hash = href.replace(prefix, "").trim(); + event.preventDefault(); + location.hash = href; + isMouseUp = false; + } + }) + + yua.bind(document, "mouseup", function(){ + if(!isMouseUp){ + yua.router._route('get', yua.router.hash); + isMouseUp = true; + } + + }) + + + //判定A标签的target属性是否指向自身 + //thanks https://github.com/quirkey/sammy/blob/master/lib/sammy.js#L219 + function targetIsThisWindow(targetWindow) { + if (!targetWindow || targetWindow === window.name || targetWindow === '_self' || (targetWindow === 'top' && window == window.top)) { + return true + } + return false + } + + return yua.router = new Router; +}) \ No newline at end of file diff --git a/js/lib/router/router.min.js b/js/lib/router/router.min.js new file mode 100644 index 0000000..9b21962 --- /dev/null +++ b/js/lib/router/router.min.js @@ -0,0 +1 @@ +define(["yua"],function(){function t(){this.table={get:[]},this.errorFn=null,this.history=null,this.hash="",this.started=!1,this.init={}}function e(t){return!t||t===window.name||"_self"===t||"top"===t&&window==window.top?!0:!1}var r={prefix:/^(#!|#)[\/]?/,historyOpen:!0,allowReload:!0},i=!0,a=/(:id)|(\{id\})|(\{id:([A-z\d\,\[\]\{\}\-\+\*\?\!:\^\$]*)\})/g;return t.prototype={error:function(t){this.errorFn=t},config:function(t){return this.started?console.error("Router config has been set"):(this.started=!0,t.allowReload||(t.historyOpen=!0),void(this.init=yua.mix({},r,t)))},_getRegExp:function(t,e){var r=t.replace(a,function(t,e,r,i,a){var n="([\\w.-]";return e||r?n+"+)":(/^\{[\d\,]+\}$/.test(a)||(n="("),n+a+")")});return r=r.replace(/(([^\\])([\/]+))/g,"$2\\/").replace(/(([^\\])([\.]+))/g,"$2\\.").replace(/(([^\\])([\-]+))/g,"$2\\-").replace(/(\(.*)(\\[\-]+)(.*\))/g,"$1-$3"),r="^"+r+"$",e.regexp=new RegExp(r),e},_add:function(t,e,r){this.started||this.config({});var i=this.table[t.toLowerCase()];if("/"!==e.charAt(0))return void console.error('char "/" must be in front of router rule');e=e.replace(/^[\/]+|[\/]+$|\s+/g,"");var a={};a.rule=e,a.callback=r,yua.Array.ensure(i,this._getRegExp(e,a))},_route:function(t,e){var e=e.trim(),r=this.table[t],i=this.init;if(i.allowReload||e!==this.history){i.historyOpen&&(this.history=e,yua.ls&&yua.ls("lastHash",e));for(var a,n=0;a=r[n++];){var o=e.match(a.regexp);if(o)return o.shift(),a.callback.apply(a,o)}this.errorFn&&this.errorFn(e)}},on:function(t,e){this._add("get",t,e)}},yua.bind(window,"load",function(){if(yua.router.started){var t=yua.router.init.prefix,e=location.hash;e=e.replace(t,"").trim(),yua.router._route("get",e)}}),"onhashchange"in window&&window.addEventListener("hashchange",function(){if(i){var t=yua.router.init.prefix,e=location.hash.replace(t,"").trim();yua.router._route("get",e)}}),yua.bind(document,"mousedown",function(t){var r="defaultPrevented"in t?t.defaultPrevented:t.returnValue===!1;if(!(r||t.ctrlKey||t.metaKey||2===t.which)){for(var a=t.target;"A"!==a.nodeName;)if(a=a.parentNode,!a||"BODY"===a.tagName)return;if(e(a.target)){if(!yua.router.started)return;var n=a.getAttribute("href")||a.getAttribute("xlink:href"),o=yua.router.init.prefix;if(null===n||!o.test(n))return;yua.router.hash=n.replace(o,"").trim(),t.preventDefault(),location.hash=n,i=!1}}}),yua.bind(document,"mouseup",function(){i||(yua.router._route("get",yua.router.hash),i=!0)}),yua.router=new t}); \ No newline at end of file diff --git a/js/lib/uploader/index.html b/js/lib/uploader/index.html new file mode 100644 index 0000000..73e51f9 --- /dev/null +++ b/js/lib/uploader/index.html @@ -0,0 +1,62 @@ + + + + + +Examples + + + + + + + +
    + +
    + + + + 提交 + + + + + + + + \ No newline at end of file diff --git a/js/lib/uploader/uploader.js b/js/lib/uploader/uploader.js new file mode 100644 index 0000000..ea4a301 --- /dev/null +++ b/js/lib/uploader/uploader.js @@ -0,0 +1,137 @@ +/** + * Uploader 无刷新上传文件(next版) + * 只支持chrome/firefox/IE10+/opera12+ + * @authors yutent (yutent@doui.cc) + * @date 2016-09-05 19:33:23 + * + */ + +"use strict"; + +define(function(){ + +/* var isIE = !!window.VBArray + + function IEVersion(){ + if(document.documentMode) + return document.documentMode + + return window.XMLHttpRequest ? 7 : 6 + }*/ + + var Uploader = function(){ + this.init() + } + + Uploader.prototype = { + init: function(){ + this.xhr = new XMLHttpRequest() + this.form = new FormData() + this.field = {} + this.header = {} + return this + }, + setUrl: function(url){ + if(!url || typeof url !== 'string') + return console.error('url不能为空且必须是字符串') + + this.url = url + return this + }, + setField: function(key, val){ + if(typeof key === 'object'){ + for(var i in key){ + this.field[i] = key[i] + } + }else{ + this.field[key] = val + } + return this + }, + setHeader: function(key, val){ + if(typeof key === 'object'){ + for(var i in key){ + this.header[i] = key[i] + } + }else{ + this.header[key] = val + } + return this + }, + start: function(){ + var _this = this + if(!this.url) + return console.error('请先设置url') + + this.xhr.open('POST', this.url, true) + + for(var i in this.field){ + this.form.append(i, this.field[i]) + } + + var startTime = Date.now() + + this.xhr.upload.addEventListener('progress', function(evt){ + + if(evt.lengthComputable && _this.progress){ + var now = Date.now() + var upSize = (evt.loaded * 1000) / 1024 + var res = {loaded: evt.loaded, time: now - startTime} + res.speed = upSize / res.time + + if(res.speed < 10){ + res.speed = Math.floor(res.speed * 1024) + ' B/s' + }else{ + res.speed = res.speed.toFixed(2) + ' KB/s' + } + + res.progress = Math.round(evt.loaded * 100 / evt.total) + _this.progress(res) + } + + }, false) + + this.xhr.onreadystatechange = function(){ + if(_this.xhr.readyState === 4 && + _this.xhr.status === 200 && + _this.xhr.responseText !== ''){ + var res = _this.xhr.responseText + try{ + res = JSON.parse(res) + }catch(err){} + _this.end && _this.end(res) + }else{ + if(_this.xhr.status !== 200 && _this.xhr.responseText) + _this.error && _this.error(_this.xhr.responseText) + } + } + + this.xhr.send(this.form) + + }, + onProgress: function(fn){ + this.progress = fn + return this + }, + onEnd: function(fn){ + this.end = fn + return this + }, + onError: function(fn){ + this.error = fn + return this + } + + + } + + + + + if(!window.Uploader) + window.Uploader = Uploader + + + return Uploader + +}) \ No newline at end of file diff --git a/js/lib/uploader/uploader.min.js b/js/lib/uploader/uploader.min.js new file mode 100644 index 0000000..e69de29 diff --git a/js/touch.js b/js/touch.js new file mode 100644 index 0000000..431d29f --- /dev/null +++ b/js/touch.js @@ -0,0 +1,544 @@ +var ua = navigator.userAgent.toLowerCase() +//http://stackoverflow.com/questions/9038625/detect-if-device-is-ios +function iOSversion() { + //https://developer.apple.com/library/prerelease/mac/releasenotes/General/WhatsNewInSafari/Articles/Safari_9.html + //http://mp.weixin.qq.com/s?__biz=MzA3MDQ4MzQzMg==&mid=256900619&idx=1&sn=b29f84cff0b8d7b9742e5d8b3cd8f218&scene=1&srcid=1009F9l4gh9nZ7rcQJEhmf7Q#rd + if (/iPad|iPhone|iPod/i.test(ua) && !window.MSStream) { + if ("backdropFilter" in document.documentElement.style) { + return 9 + } + if (!!window.indexedDB) { + return 8 + } + if (!!window.SpeechSynthesisUtterance) { + return 7 + } + if (!!window.webkitAudioContext) { + return 6 + } + if (!!window.matchMedia) { + return 5 + } + if (!!window.history && 'pushState' in window.history) { + return 4 + } + return 3 + } + return NaN +} + +var deviceIsAndroid = ua.indexOf('android') > 0 +var deviceIsIOS = iOSversion() + +var Recognizer = yua.gestureHooks = { + pointers: {}, + //以AOP切入touchstart, touchmove, touchend, touchcancel回调 + start: function (event, callback) { + + //touches是当前屏幕上所有触摸点的列表; + //targetTouches是当前对象上所有触摸点的列表; + //changedTouches是涉及当前事件的触摸点的列表。 + for (var i = 0; i < event.changedTouches.length; i++) { + var touch = event.changedTouches[i], + id = touch.identifier, + pointer = { + startTouch: mixLocations({}, touch), + startTime: Date.now(), + status: 'tapping', + element: event.target, + pressingHandler: Recognizer.pointers[id] && Recognizer.pointers[id].pressingHandler + } + Recognizer.pointers[id] = pointer; + callback(pointer, touch) + + } + }, + move: function (event, callback) { + for (var i = 0; i < event.changedTouches.length; i++) { + var touch = event.changedTouches[i] + var pointer = Recognizer.pointers[touch.identifier] + if (!pointer) { + return + } + + if (!("lastTouch" in pointer)) { + pointer.lastTouch = pointer.startTouch + pointer.lastTime = pointer.startTime + pointer.deltaX = pointer.deltaY = pointer.duration = pointer.distance = 0 + } + + var time = Date.now() - pointer.lastTime + + if (time > 0) { + + var RECORD_DURATION = 70 + if (time > RECORD_DURATION) { + time = RECORD_DURATION + } + if (pointer.duration + time > RECORD_DURATION) { + pointer.duration = RECORD_DURATION - time + } + + pointer.duration += time; + pointer.lastTouch = mixLocations({}, touch) + + pointer.lastTime = Date.now() + + pointer.deltaX = touch.clientX - pointer.startTouch.clientX + pointer.deltaY = touch.clientY - pointer.startTouch.clientY + var x = pointer.deltaX * pointer.deltaX + var y = pointer.deltaY * pointer.deltaY + pointer.distance = Math.sqrt(x + y) + pointer.isVertical = x < y + + callback(pointer, touch) + } + } + }, + end: function (event, callback) { + for (var i = 0; i < event.changedTouches.length; i++) { + var touch = event.changedTouches[i], + id = touch.identifier, + pointer = Recognizer.pointers[id] + + if (!pointer) + continue + + callback(pointer, touch) + + delete Recognizer.pointers[id] + } + }, + //人工触发合成事件 + fire: function (elem, type, props) { + if (elem) { + var event = document.createEvent('Events') + event.initEvent(type, true, true) + yua.mix(event, props) + elem.dispatchEvent(event) + } + }, + //添加各种识别器 + add: function (name, recognizer) { + function move(event) { + recognizer.touchmove(event) + } + + function end(event) { + recognizer.touchend(event) + + document.removeEventListener('touchmove', move) + + document.removeEventListener('touchend', end) + + document.removeEventListener('touchcancel', cancel) + + } + + function cancel(event) { + recognizer.touchcancel(event) + + document.removeEventListener('touchmove', move) + + document.removeEventListener('touchend', end) + + document.removeEventListener('touchcancel', cancel) + + } + + recognizer.events.forEach(function (eventName) { + yua.eventHooks[eventName] = { + fix: function (el, fn) { + if (!el['touch-' + name]) { + el['touch-' + name] = '1' + el.addEventListener('touchstart', function (event) { + recognizer.touchstart(event) + + document.addEventListener('touchmove', move) + + document.addEventListener('touchend', end) + + document.addEventListener('touchcancel', cancel) + + }) + } + return fn + } + } + }) + } +} + +var locations = ['screenX', 'screenY', 'clientX', 'clientY', 'pageX', 'pageY'] + +// 复制 touch 对象上的有用属性到固定对象上 +function mixLocations(target, source) { + if (source) { + locations.forEach(function (key) { + target[key] = source[key] + }) + } + return target +} + +var supportPointer = !!navigator.pointerEnabled || !!navigator.msPointerEnabled + +if (supportPointer) { // 支持pointer的设备可用样式来取消click事件的300毫秒延迟 + root.style.msTouchAction = root.style.touchAction = 'none' +} +var tapRecognizer = { + events: ['tap'], + touchBoundary: 10, + tapDelay: 200, + needClick: function(target) { + //判定是否使用原生的点击事件, 否则使用sendClick方法手动触发一个人工的点击事件 + switch (target.nodeName.toLowerCase()) { + case 'button': + case 'select': + case 'textarea': + if (target.disabled) { + return true + } + + break; + case 'input': + // IOS6 pad 上选择文件,如果不是原生的click,弹出的选择界面尺寸错误 + if ((deviceIsIOS && target.type === 'file') || target.disabled) { + return true + } + + break; + case 'label': + case 'iframe': + case 'video': + return true + } + + return false + }, + needFocus: function(target) { + switch (target.nodeName.toLowerCase()) { + case 'textarea': + case 'select': //实测android下select也需要 + return true; + case 'input': + switch (target.type) { + case 'button': + case 'checkbox': + case 'file': + case 'image': + case 'radio': + case 'submit': + return false + } + //如果是只读或disabled状态,就无须获得焦点了 + return !target.disabled && !target.readOnly + default: + return false + } + }, + focus: function(targetElement) { + var length; + //在iOS7下, 对一些新表单元素(如date, datetime, time, month)调用focus方法会抛错, + //幸好的是,我们可以改用setSelectionRange获取焦点, 将光标挪到文字的最后 + var type = targetElement.type + if (deviceIsIOS && targetElement.setSelectionRange && + type.indexOf('date') !== 0 && type !== 'time' && type !== 'month') { + length = targetElement.value.length + targetElement.setSelectionRange(length, length) + } else { + targetElement.focus() + } + }, + findControl: function(labelElement) { + // 获取label元素所对应的表单元素 + // 可以能过control属性, getElementById, 或用querySelector直接找其内部第一表单元素实现 + if (labelElement.control !== undefined) { + return labelElement.control + } + + if (labelElement.htmlFor) { + return document.getElementById(labelElement.htmlFor) + } + + return labelElement.querySelector('button, input:not([type=hidden]), keygen, meter, output, progress, select, textarea') + }, + fixTarget: function(target) { + if (target.nodeType === 3) { + return target.parentNode + } + if (window.SVGElementInstance && (target instanceof SVGElementInstance)) { + return target.correspondingUseElement; + } + + return target + }, + updateScrollParent: function(targetElement) { + //如果事件源元素位于某一个有滚动条的祖父元素中,那么保持其scrollParent与scrollTop值 + var scrollParent = targetElement.tapScrollParent + + if (!scrollParent || !scrollParent.contains(targetElement)) { + var parentElement = targetElement + do { + if (parentElement.scrollHeight > parentElement.offsetHeight) { + scrollParent = parentElement + targetElement.tapScrollParent = parentElement + break + } + + parentElement = parentElement.parentElement + } while (parentElement) + } + + if (scrollParent) { + scrollParent.lastScrollTop = scrollParent.scrollTop + } + }, + touchHasMoved: function(event) { + //判定是否发生移动,其阀值是10px + var touch = event.changedTouches[0], + boundary = tapRecognizer.touchBoundary + return Math.abs(touch.pageX - tapRecognizer.pageX) > boundary || + Math.abs(touch.pageY - tapRecognizer.pageY) > boundary + + }, + + findType: function(targetElement) { + // 安卓chrome浏览器上,模拟的 click 事件不能让 select 打开,故使用 mousedown 事件 + return deviceIsAndroid && targetElement.tagName.toLowerCase() === 'select' ? + 'mousedown' : 'click' + }, + sendClick: function(targetElement, event) { + // 在click之前触发tap事件 + Recognizer.fire(targetElement, 'tap', { + touchEvent: event + }) + var clickEvent, touch + //某些安卓设备必须先移除焦点,之后模拟的click事件才能让新元素获取焦点 + if (document.activeElement && document.activeElement !== targetElement) { + document.activeElement.blur() + } + + touch = event.changedTouches[0] + // 手动触发点击事件,此时必须使用document.createEvent('MouseEvents')来创建事件 + // 及使用initMouseEvent来初始化它 + clickEvent = document.createEvent('MouseEvents') + clickEvent.initMouseEvent(tapRecognizer.findType(targetElement), true, true, window, 1, touch.screenX, + touch.screenY, touch.clientX, touch.clientY, false, false, false, false, 0, null) + clickEvent.touchEvent = event + targetElement.dispatchEvent(clickEvent) + }, + touchstart: function(event) { + //忽略多点触摸 + if (event.targetTouches.length !== 1) { + return true + } + //修正事件源对象 + var targetElement = tapRecognizer.fixTarget(event.target) + var touch = event.targetTouches[0] + if (deviceIsIOS) { + // 判断是否是点击文字,进行选择等操作,如果是,不需要模拟click + var selection = window.getSelection(); + if (selection.rangeCount && !selection.isCollapsed) { + return true + } + var id = touch.identifier + //当 alert 或 confirm 时,点击其他地方,会触发touch事件,identifier相同,此事件应该被忽略 + if (id && isFinite(tapRecognizer.lastTouchIdentifier) && tapRecognizer.lastTouchIdentifier === id) { + event.preventDefault() + return false + } + + tapRecognizer.lastTouchIdentifier = id + + tapRecognizer.updateScrollParent(targetElement) + } + //收集触摸点的信息 + tapRecognizer.status = "tapping" + tapRecognizer.startTime = Date.now() + tapRecognizer.element = targetElement + tapRecognizer.pageX = touch.pageX + tapRecognizer.pageY = touch.pageY + // 如果点击太快,阻止双击带来的放大收缩行为 + if ((tapRecognizer.startTime - tapRecognizer.lastTime) < tapRecognizer.tapDelay) { + event.preventDefault() + } + }, + touchmove: function(event) { + if (tapRecognizer.status !== "tapping") { + return true + } + // 如果事件源元素发生改变,或者发生了移动,那么就取消触发点击事件 + if (tapRecognizer.element !== tapRecognizer.fixTarget(event.target) || + tapRecognizer.touchHasMoved(event)) { + tapRecognizer.status = tapRecognizer.element = 0 + } + + }, + touchend: function(event) { + var targetElement = tapRecognizer.element + var now = Date.now() + //如果是touchstart与touchend相隔太久,可以认为是长按,那么就直接返回 + //或者是在touchstart, touchmove阶段,判定其不该触发点击事件,也直接返回 + if (!targetElement || now - tapRecognizer.startTime > tapRecognizer.tapDelay) { + return true + } + + + tapRecognizer.lastTime = now + + var startTime = tapRecognizer.startTime + tapRecognizer.status = tapRecognizer.startTime = 0 + + targetTagName = targetElement.tagName.toLowerCase() + if (targetTagName === 'label') { + //尝试触发label上可能绑定的tap事件 + Recognizer.fire(targetElement, 'tap', { + touchEvent: event + }) + var forElement = tapRecognizer.findControl(targetElement) + if (forElement) { + tapRecognizer.focus(targetElement) + targetElement = forElement + } + } else if (tapRecognizer.needFocus(targetElement)) { + // 如果元素从touchstart到touchend经历时间过长,那么不应该触发点击事 + // 或者此元素是iframe中的input元素,那么它也无法获点焦点 + if ((now - startTime) > 100 || (deviceIsIOS && window.top !== window && targetTagName === 'input')) { + tapRecognizer.element = 0 + return false + } + + tapRecognizer.focus(targetElement) + deviceIsAndroid && tapRecognizer.sendClick(targetElement, event) + + return false + } + + if (deviceIsIOS) { + //如果它的父容器的滚动条发生改变,那么应该识别为划动或拖动事件,不应该触发点击事件 + var scrollParent = targetElement.tapScrollParent; + if (scrollParent && scrollParent.lastScrollTop !== scrollParent.scrollTop) { + return true + } + } + //如果这不是一个需要使用原生click的元素,则屏蔽原生事件,避免触发两次click + if (!tapRecognizer.needClick(targetElement)) { + event.preventDefault() + // 触发一次模拟的click + tapRecognizer.sendClick(targetElement, event) + } + }, + touchcancel: function() { + tapRecognizer.startTime = tapRecognizer.element = 0 + } +} + +Recognizer.add("tap", tapRecognizer) + +var pressRecognizer = { + events: ['longtap', 'doubletap'], + cancelPress: function (pointer) { + clearTimeout(pointer.pressingHandler) + pointer.pressingHandler = null + }, + touchstart: function (event) { + Recognizer.start(event, function (pointer, touch) { + pointer.pressingHandler = setTimeout(function () { + if (pointer.status === 'tapping') { + Recognizer.fire(event.target, 'longtap', { + touch: touch, + touchEvent: event + }) + } + pressRecognizer.cancelPress(pointer) + }, 800) + if (event.changedTouches.length !== 1) { + pointer.status = 0 + } + }) + + }, + touchmove: function (event) { + Recognizer.move(event, function (pointer) { + if (pointer.distance > 10 && pointer.pressingHandler) { + pressRecognizer.cancelPress(pointer) + if (pointer.status === 'tapping') { + pointer.status = 'panning' + } + } + }) + }, + touchend: function (event) { + Recognizer.end(event, function (pointer, touch) { + pressRecognizer.cancelPress(pointer) + if (pointer.status === 'tapping') { + pointer.lastTime = Date.now() + if (pressRecognizer.lastTap && pointer.lastTime - pressRecognizer.lastTap.lastTime < 300) { + Recognizer.fire(pointer.element, 'doubletap', { + touch: touch, + touchEvent: event + }) + } + + pressRecognizer.lastTap = pointer + } + }) + + }, + touchcancel: function (event) { + Recognizer.end(event, function (pointer) { + pressRecognizer.cancelPress(pointer) + }) + } +} +Recognizer.add('press', pressRecognizer) + +var swipeRecognizer = { + events: ['swipe', 'swipeleft', 'swiperight', 'swipeup', 'swipedown'], + getAngle: function (x, y ) { + return Math.atan2(y, x) * 180 / Math.PI + }, + getDirection: function (x, y) { + var angle = swipeRecognizer.getAngle(x, y) + if ((angle < -45) && (angle > -135)) { + return "up" + } else if ((angle >= 45) && (angle < 315)) { + return "down" + } else if ((angle > -45) && (angle <= 45)) { + return "right" + } else{ + return "left" + } + }, + touchstart: function (event) { + Recognizer.start(event, noop) + }, + touchmove: function (event) { + Recognizer.move(event, noop) + }, + touchend: function (event) { + if(event.changedTouches.length !== 1){ + return + } + Recognizer.end(event, function (pointer, touch) { + var isflick = (pointer.distance > 30 && pointer.distance / pointer.duration > 0.65) + if (isflick) { + var extra = { + deltaX : pointer.deltaX, + deltaY: pointer.deltaY, + touch: touch, + touchEvent: event, + direction: swipeRecognizer.getDirection(pointer.deltaX, pointer.deltaY), + isVertical: pointer.isVertical + } + var target = pointer.element + Recognizer.fire(target, 'swipe', extra) + Recognizer.fire(target, 'swipe' + extra.direction, extra) + } + }) + } +} + +swipeRecognizer.touchcancel = swipeRecognizer.touchend +Recognizer.add('swipe', swipeRecognizer) \ No newline at end of file diff --git a/js/yua-touch.js b/js/yua-touch.js new file mode 100644 index 0000000..6af1b7f --- /dev/null +++ b/js/yua-touch.js @@ -0,0 +1,7026 @@ +/*================================================== + * + * @authors yutent (yutent@doui.cc) + * @date 2017-03-21 21:05:57 + * support IE10+ and other browsers + * + ==================================================*/ +(function(global, factory) { + + if (typeof module === "object" && typeof module.exports === "object") { + // For CommonJS and CommonJS-like environments where a proper `window` + // is present, execute the factory and get yua. + // For environments that do not have a `window` with a `document` + // (such as Node.js), expose a factory as module.exports. + // This accentuates the need for the creation of a real `window`. + // e.g. var yua = require("yua")(window); + module.exports = global.document ? factory(global, true) : function(w) { + if (!w.document) { + throw new Error("Yua只能运行在浏览器环境") + } + return factory(w) + } + } else { + factory(global) + } + +// Pass this if window is not defined yet +}(typeof window !== "undefined" ? window : this, function(window, noGlobal){ + +/********************************************************************* + * 全局变量及方法 * + **********************************************************************/ + +var expose = Date.now() +//http://stackoverflow.com/questions/7290086/javascript-use-strict-and-nicks-find-global-function +var DOC = window.document +var head = DOC.head //HEAD元素 +head.insertAdjacentHTML("afterBegin", '') +var ifGroup = head.firstChild + +function log() { + if (yua.config.debug) { +// http://stackoverflow.com/questions/8785624/how-to-safely-wrap-console-log + console.log.apply(console, arguments) + } +} + +/** + * Creates a new object without a prototype. This object is useful for lookup without having to + * guard against prototypically inherited properties via hasOwnProperty. + * + * Related micro-benchmarks: + * - http://jsperf.com/object-create2 + * - http://jsperf.com/proto-map-lookup/2 + * - http://jsperf.com/for-in-vs-object-keys2 + */ +function createMap() { + return Object.create(null) +} + +var subscribers = "$" + expose + +var nullObject = {} //作用类似于noop,只用于代码防御,千万不要在它上面添加属性 +var rword = /[^, ]+/g //切割字符串为一个个小块,以空格或豆号分开它们,结合replace实现字符串的forEach +var rw20g = /\w+/g +var rsvg = /^\[object SVG\w*Element\]$/ +var rwindow = /^\[object (?:Window|DOMWindow|global)\]$/ +var oproto = Object.prototype +var ohasOwn = oproto.hasOwnProperty +var serialize = oproto.toString +var ap = Array.prototype +var aslice = ap.slice +var W3C = window.dispatchEvent +var root = DOC.documentElement +var yuaFragment = DOC.createDocumentFragment() +var cinerator = DOC.createElement("div") +var class2type = {} +"Boolean Number String Function Array Date RegExp Object Error".replace(rword, function (name) { + class2type["[object " + name + "]"] = name.toLowerCase() +}) +var bindingID = 1024 +var IEVersion = NaN +if (window.VBArray) { + IEVersion = document.documentMode || (window.XMLHttpRequest ? 7 : 6) +} + +function noop(){} +function scpCompile(array){ + return Function.apply(noop, array) +} + +function oneObject(array, val) { + if (typeof array === "string") { + array = array.match(rword) || [] + } + var result = {}, + value = val !== void 0 ? val : 1 + for (var i = 0, n = array.length; i < n; i++) { + result[array[i]] = value + } + return result +} + +var generateID = function (mark) { + mark = mark && (mark + '-') || 'yua-' + return mark + Math.floor(Date.now() / 1000).toString(16) + '-' + Math.random().toString(16).slice(2, 6) + '-' + Math.random().toString(16).slice(6, 10) +} + +yua = function (el) { //创建jQuery式的无new 实例化结构 + return new yua.init(el) +} + +/*视浏览器情况采用最快的异步回调*/ +yua.nextTick = new function () {// jshint ignore:line + var tickImmediate = window.setImmediate + var tickObserver = window.MutationObserver + if (tickImmediate) { + return tickImmediate.bind(window) + } + + var queue = [] + function callback() { + var n = queue.length + for (var i = 0; i < n; i++) { + queue[i]() + } + queue = queue.slice(n) + } + + if (tickObserver) { + var node = document.createTextNode("yua") + new tickObserver(callback).observe(node, {characterData: true})// jshint ignore:line + var bool = false + return function (fn) { + queue.push(fn) + bool = !bool + node.data = bool + } + } + + + return function (fn) { + setTimeout(fn, 4) + } +}// jshint ignore:line + +/********************************************************************* + * yua的静态方法定义区 * + **********************************************************************/ + +yua.init = function (el) { + this[0] = this.element = el +} +yua.fn = yua.prototype = yua.init.prototype + +yua.type = function (obj) { //取得目标的类型 + if (obj == null) { + return String(obj) + } + // 早期的webkit内核浏览器实现了已废弃的ecma262v4标准,可以将正则字面量当作函数使用,因此typeof在判定正则时会返回function + return typeof obj === "object" || typeof obj === "function" ? + class2type[serialize.call(obj)] || "object" : + typeof obj +} + +yua.isFunction = function (fn) { + return serialize.call(fn) === "[object Function]" +} + +yua.isWindow = function (obj) { + return rwindow.test(serialize.call(obj)) +} + +/*判定是否是一个朴素的javascript对象(Object),不是DOM对象,不是BOM对象,不是自定义类的实例*/ +yua.isPlainObject = function (obj) { + // 简单的 typeof obj === "object"检测,会致使用isPlainObject(window)在opera下通不过 + return serialize.call(obj) === "[object Object]" && Object.getPrototypeOf(obj) === oproto +} + +//与jQuery.extend方法,可用于浅拷贝,深拷贝 +yua.mix = yua.fn.mix = function () { + var options, name, src, copy, copyIsArray, clone, + target = arguments[0] || {}, + i = 1, + length = arguments.length, + deep = false + + // 如果第一个参数为布尔,判定是否深拷贝 + if (typeof target === "boolean") { + deep = target + target = arguments[1] || {} + i++ + } + + //确保接受方为一个复杂的数据类型 + if (typeof target !== "object" && !yua.isFunction(target)) { + target = {} + } + + //如果只有一个参数,那么新成员添加于mix所在的对象上 + if (i === length) { + target = this + i-- + } + + for (; i < length; i++) { + //只处理非空参数 + if ((options = arguments[i]) != null) { + for (name in options) { + src = target[name] + copy = options[name] + // 防止环引用 + if (target === copy) { + continue + } + if (deep && copy && (yua.isPlainObject(copy) || (copyIsArray = Array.isArray(copy)))) { + + if (copyIsArray) { + copyIsArray = false + clone = src && Array.isArray(src) ? src : [] + + } else { + clone = src && yua.isPlainObject(src) ? src : {} + } + + target[name] = yua.mix(deep, clone, copy) + } else if (copy !== void 0) { + target[name] = copy + } + } + } + } + return target +} + + + + +/*-----------------部分ES6的JS实现 start---------------*/ + +if(!Object.assign){ + Object.defineProperty(Object, 'assign', { + enumerable: false, + value: function(target, first){ + "use strict"; + if(target === undefined || target === null) + throw new TypeError('Can not convert first argument to object') + + var to = Object(target) + for(var i = 0, len = arguments.length; i < len; i++){ + var next = arguments[i] + if(next === undefined || next === null) + continue + + var keys = Object.keys(Object(next)) + for(var j = 0, n = keys.length; j < n; j++){ + var key = keys[j] + var desc = Object.getOwnPropertyDescriptor(next, key) + if(desc !== undefined && desc.enumerable) + to[key] = next[key] + } + } + return to + } + }) +} + +if(!Array.from){ + Object.defineProperty(Array, 'from', { + enumerable: false, + value: (function(){ + var toStr = Object.prototype.toString + var isCallable = function(fn){ + return typeof fn === 'function' || toStr.call(fn) === '[object Function]' + } + + var toInt = function(val){ + var num = val - 0 + if(isNaN(num)) + return 0 + + if(num === 0 || isFinite(num)) + return num + + return (num > 0 ? 1 : -1) * Math.floor(Math.abs(num)) + } + var maxInt = Math.pow(2, 53) - 1 + var toLen = function(val){ + var len = toInt(val) + return Math.min(Math.max(len, 0), maxInt) + } + + return function(arrLike){ + var _this = this + var items = Object(arrLike) + if(arrLike === null) + throw new TypeError('Array.from requires an array-like object - not null or undefined') + + var mapFn = arguments.length > 1 ? arguments[1] : undefined + var other + if(mapFn !== undefined){ + if(!isCallable(mapFn)) + throw new TypeError('Array.from: when provided, the second argument must be a function') + + if(arguments.length > 2) + other = arguments[2] + } + + var len = toLen(items.length) + var arr = isCallable(_this) ? Object(new _this(len)) : new Array(len) + var k = 0 + var kVal + while(k < len){ + kVal = items[k] + if(mapFn) + arr[k] = other === 'undefined' ? mapFn(kVal, k) : mapFn.call(other, kVal, k) + else + arr[k] = kVal + + k++ + } + arr.length = len + return arr + } + })() + }) +} + + + +// 判断数组是否包含指定元素 +if(!Array.prototype.includes){ + Object.defineProperty(Array.prototype, + 'includes', + { + value: function(val){ + for(var i in this){ + if(this[i] === val) + return true + } + return false + }, + enumerable: false + }) +} + +//类似于Array 的splice方法 +if(!String.prototype.splice){ + Object.defineProperty(String.prototype, + 'splice', + { + value: function(start, len, sub){ + var length = this.length, + argLen = arguments.length; + + fill = fill === undefined ? '' : fill + + if(argLen < 1){ + return this + } + + //处理负数 + if(start < 0){ + if(Math.abs(start) >= length) + start = 0 + else + start = length + start + } + + if(argLen === 1){ + return this.slice(0, start) + }else{ + len -= 0; + + var strl = this.slice(0, start), + strr = this.slice(start + len); + + return strl + fill + strr + } + }, + enumerable: false + }) +} + +if(!Date.prototype.getFullWeek){ + //获取当天是本年度第几周 + Object.defineProperty(Date.prototype, + 'getFullWeek', + { + value: function(){ + var thisYear = this.getFullYear(), + that = new Date(thisYear, 0, 1), + firstDay = that.getDay(), + numsOfToday = (this - that) / 86400000; + return Math.ceil((numsOfToday + firstDay) / 7) + }, + enumerable: false + }) + + //获取当天是本月第几周 + Object.defineProperty(Date.prototype, + 'getWeek', + { + value: function(){ + var today = this.getDate(), + thisMonth = this.getMonth(), + thisYear = this.getFullYear(), + firstDay = new Date(thisYear, thisMonth, 1).getDay(); + return Math.ceil((today + firstDay) / 7) + }, + enumerable: false + }) +} + +if(!Date.isDate){ + Object.defineProperty(Date, + 'isDate', + { + value: function(obj){ + return (typeof obj === 'object') && obj.getTime ? true : false + }, + enumerable: false + }) +} + +//时间格式化 +if(!Date.prototype.format){ + Object.defineProperty(Date.prototype, + 'format', + { + value: function(str){ + str = str || 'Y-m-d H:i:s' + var week = ['Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun'], + dt = { + 'fullyear': this.getFullYear(), + 'year': this.getYear(), + 'fullweek': this.getFullWeek(), + 'week': this.getWeek(), + 'month': this.getMonth() + 1, + 'date': this.getDate(), + 'day': week[this.getDay()], + 'hours': this.getHours(), + 'minutes': this.getMinutes(), + 'seconds': this.getSeconds() + }, + re; + + dt.g = dt.hours > 12 ? dt.hours - 12 : dt.hours + + re = { + 'Y': dt.fullyear, + 'y': dt.year, + 'm': dt.month < 10 ? '0' + dt.month : dt.month, + 'n': dt.month, + 'd': dt.date < 10 ? '0' + dt.date : dt.date, + 'j': dt.date, + 'H': dt.hours < 10 ? '0' + dt.hours : dt.hours, + 'h': dt.g < 10 ? '0' + dt.g : dt.g, + 'G': dt.hours, + 'g': dt.g, + 'i': dt.minutes < 10 ? '0' + dt.minutes : dt.minutes, + 's': dt.seconds < 10 ? '0' + dt.seconds : dt.seconds, + 'W': dt.fullweek, + 'w': dt.week, + 'D': dt.day + } + + for(var i in re){ + str = str.replace(new RegExp(i, 'g'), re[i]) + } + return str + }, + enumerable: false + }) +} + + +/*-----------------部分ES6的JS实现 ending---------------*/ + + + + + + + + + + + + +yua.mix({ + rword: rword, + subscribers: subscribers, + version: '1.0.0-touch', + ui: {}, + log: log, + slice: function (nodes, start, end) { + return aslice.call(nodes, start, end) + }, + noop: noop, + /*如果不用Error对象封装一下,str在控制台下可能会乱码*/ + error: function (str, e) { + throw new (e || Error)(str)// jshint ignore:line + }, + /*将一个以空格或逗号隔开的字符串或数组,转换成一个键值都为1的对象*/ + oneObject: oneObject, + /* yua.range(10) + => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + yua.range(1, 11) + => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + yua.range(0, 30, 5) + => [0, 5, 10, 15, 20, 25] + yua.range(0, -10, -1) + => [0, -1, -2, -3, -4, -5, -6, -7, -8, -9] + yua.range(0) + => []*/ + range: function (start, end, step) { // 用于生成整数数组 + step || (step = 1) + if (end == null) { + end = start || 0 + start = 0 + } + var index = -1, + length = Math.max(0, Math.ceil((end - start) / step)), + result = new Array(length) + while (++index < length) { + result[index] = start + start += step + } + return result + }, + eventHooks: {}, + /*绑定事件*/ + bind: function (el, type, fn, phase) { + var hooks = yua.eventHooks; + type = type.split(','); + yua.each(type, function(i, t){ + t = t.trim() + var hook = hooks[t]; + if (typeof hook === "object") { + type = hook.type || type + phase = hook.phase || !!phase + fn = hook.fix ? hook.fix(el, fn) : fn + } + el.addEventListener(t, fn, phase) + }) + return fn + }, + /*卸载事件*/ + unbind: function (el, type, fn, phase) { + var hooks = yua.eventHooks; + type = type.split(','); + fn = fn || noop + yua.each(type, function(i, t){ + t = t.trim() + var hook = hooks[t]; + if (typeof hook === "object") { + type = hook.type || type + phase = hook.phase || !!phase + } + el.removeEventListener(t, fn, phase) + }) + }, + /*读写删除元素节点的样式*/ + css: function (node, name, value) { + if (node instanceof yua) { + node = node[0] + } + var prop = /[_-]/.test(name) ? camelize(name) : name, fn + name = yua.cssName(prop) || prop + if (value === void 0 || typeof value === "boolean") { //获取样式 + fn = cssHooks[prop + ":get"] || cssHooks["@:get"] + if (name === "background") { + name = "backgroundColor" + } + var val = fn(node, name) + return value === true ? parseFloat(val) || 0 : val + } else if (value === "") { //请除样式 + node.style[name] = "" + } else { //设置样式 + if (value == null || value !== value) { + return + } + if (isFinite(value) && !yua.cssNumber[prop]) { + value += "px" + } + fn = cssHooks[prop + ":set"] || cssHooks["@:set"] + fn(node, name, value) + } + }, + /*遍历数组与对象,回调的第一个参数为索引或键名,第二个或元素或键值*/ + each: function (obj, fn) { + if (obj) { //排除null, undefined + var i = 0 + if (isArrayLike(obj)) { + for (var n = obj.length; i < n; i++) { + if (fn(i, obj[i]) === false) + break + } + } else { + for (i in obj) { + if (obj.hasOwnProperty(i) && fn(i, obj[i]) === false) { + break + } + } + } + } + }, + Array: { + /*只有当前数组不存在此元素时只添加它*/ + ensure: function (target, item) { + if (target.indexOf(item) === -1) { + return target.push(item) + } + }, + /*移除数组中指定位置的元素,返回布尔表示成功与否*/ + removeAt: function (target, index) { + return !!target.splice(index, 1).length + }, + /*移除数组中第一个匹配传参的那个元素,返回布尔表示成功与否*/ + remove: function (target, item) { + var index = target.indexOf(item) + if (~index) + return yua.Array.removeAt(target, index) + return false + } + }, + /** + * [ls localStorage操作] + * @param {[type]} name [键名] + * @param {[type]} val [键值,为空时删除] + * @return + */ + ls: function(name, val){ + if(!window.localStorage) + return log('该浏览器不支持本地储存localStorage') + + if(this.type(name) === 'object'){ + for(var i in name){ + localStorage.setItem(i, name[i]); + } + return; + } + switch(arguments.length){ + case 1: + return localStorage.getItem(name); + default: + if((this.type(val) == 'string' && val.trim() === '') || val === null){ + localStorage.removeItem(name); + return; + } + if(this.type(val) !== 'object' && this.type(val) !== 'array'){ + localStorage.setItem(name, val.toString()); + }else{ + localStorage.setItem(name, JSON.stringify(val)); + } + } + }, + /** + * [cookie cookie 操作 ] + * @param name [cookie名] + * @param value [cookie值] + * @param {[json]} opt [有效期,域名,路径等] + * @return {[boolean]} [读取时返回对应的值,写入时返回true] + */ + cookie: function(name, value, opt){ + if(arguments.length > 1){ + if(!name) return; + + //设置默认的参数 + opt = opt || {}; + opt = this.mix({expires: '', path: '/', domain: document.domain, secure: ''}, opt); + + if(!value){ + document.cookie = encodeURIComponent(name) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=" + opt.domain + "; path=" + opt.path; + return true; + } + if (opt.expires) { + switch (opt.expires.constructor) { + case Number: + opt.expires = (opt.expires === Infinity) ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + opt.expires; + break; + case String: + opt.expires = "; expires=" + opt.expires; + break; + case Date: + opt.expires = "; expires=" + opt.expires.toUTCString(); + break; + } + } + document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) + opt.expires + "; domain=" + opt.domain + "; path=" + opt.path + "; " + opt.secure; + return true; + }else{ + if (!name){ + var keys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/); + for (var i=0, len=keys.length; i>> 0)) { + return true //由于ecma262v5能修改对象属性的enumerable,因此不能用propertyIsEnumerable来判定了 + } + } + return false +} + +// https://github.com/rsms/js-lru +var Cache = new function() {// jshint ignore:line + function LRU(maxLength) { + this.size = 0 + this.limit = maxLength + this.head = this.tail = void 0 + this._keymap = {} + } + + var p = LRU.prototype + + p.put = function(key, value) { + var entry = { + key: key, + value: value + } + this._keymap[key] = entry + if (this.tail) { + this.tail.newer = entry + entry.older = this.tail + } else { + this.head = entry + } + this.tail = entry + if (this.size === this.limit) { + this.shift() + } else { + this.size++ + } + return value + } + + p.shift = function() { + var entry = this.head + if (entry) { + this.head = this.head.newer + this.head.older = + entry.newer = + entry.older = + this._keymap[entry.key] = void 0 + delete this._keymap[entry.key] //#1029 + } + } + p.get = function(key) { + var entry = this._keymap[key] + if (entry === void 0) + return + if (entry === this.tail) { + return entry.value + } + // HEAD--------------TAIL + // <.older .newer> + // <--- add direction -- + // A B C E + if (entry.newer) { + if (entry === this.head) { + this.head = entry.newer + } + entry.newer.older = entry.older // C <-- E. + } + if (entry.older) { + entry.older.newer = entry.newer // C. --> E + } + entry.newer = void 0 // D --x + entry.older = this.tail // D. --> E + if (this.tail) { + this.tail.newer = entry // E. <-- D + } + this.tail = entry + return entry.value + } + return LRU +}// jshint ignore:line + + + + + + + + + + + +/********************************************************************* + * DOM 底层补丁 * + **********************************************************************/ + +//safari5+是把contains方法放在Element.prototype上而不是Node.prototype +if (!DOC.contains) { + Node.prototype.contains = function (arg) { + return !!(this.compareDocumentPosition(arg) & 16) + } +} +yua.contains = function(root, el) { + try { + while ((el = el.parentNode)) + if (el === root) + return true + return false + } catch (e) { + return false + } +} + +if (window.SVGElement) { + var svgns = "http://www.w3.org/2000/svg" + var svg = DOC.createElementNS(svgns, "svg") + svg.innerHTML = '' + if (!rsvg.test(svg.firstChild)) {// #409 + /* jshint ignore:start */ + function enumerateNode(node, targetNode) { + if (node && node.childNodes) { + var nodes = node.childNodes + for (var i = 0, el; el = nodes[i++]; ) { + if (el.tagName) { + var svg = DOC.createElementNS(svgns, + el.tagName.toLowerCase()) + // copy attrs + ap.forEach.call(el.attributes, function (attr) { + svg.setAttribute(attr.name, attr.value) + }) + // 递归处理子节点 + enumerateNode(el, svg) + targetNode.appendChild(svg) + } + } + } + } + /* jshint ignore:end */ + Object.defineProperties(SVGElement.prototype, { + "outerHTML": {//IE9-11,firefox不支持SVG元素的innerHTML,outerHTML属性 + enumerable: true, + configurable: true, + get: function () { + return new XMLSerializer().serializeToString(this) + }, + set: function (html) { + var tagName = this.tagName.toLowerCase(), + par = this.parentNode, + frag = yua.parseHTML(html) + // 操作的svg,直接插入 + if (tagName === "svg") { + par.insertBefore(frag, this) + // svg节点的子节点类似 + } else { + var newFrag = DOC.createDocumentFragment() + enumerateNode(frag, newFrag) + par.insertBefore(newFrag, this) + } + par.removeChild(this) + } + }, + "innerHTML": { + enumerable: true, + configurable: true, + get: function () { + var s = this.outerHTML + var ropen = new RegExp("<" + this.nodeName + '\\b(?:(["\'])[^"]*?(\\1)|[^>])*>', "i") + var rclose = new RegExp("<\/" + this.nodeName + ">$", "i") + return s.replace(ropen, "").replace(rclose, "") + }, + set: function (html) { + if (yua.clearHTML) { + yua.clearHTML(this) + var frag = yua.parseHTML(html) + enumerateNode(frag, this) + } + } + } + }) + } +} + +//========================= event binding ==================== + +var eventHooks = yua.eventHooks + +//针对firefox, chrome修正mouseenter, mouseleave(chrome30+) +if (!("onmouseenter" in root)) { + yua.each({ + mouseenter: "mouseover", + mouseleave: "mouseout" + }, function (origType, fixType) { + eventHooks[origType] = { + type: fixType, + fix: function (elem, fn) { + return function (e) { + var t = e.relatedTarget + if (!t || (t !== elem && !(elem.compareDocumentPosition(t) & 16))) { + delete e.type + e.type = origType + return fn.call(elem, e) + } + } + } + } + }) +} + +//针对IE9+, w3c修正animationend +yua.each({ + AnimationEvent: "animationend", + WebKitAnimationEvent: "webkitAnimationEnd" +}, function (construct, fixType) { + if (window[construct] && !eventHooks.animationend) { + eventHooks.animationend = { + type: fixType + } + } +}) + +if (DOC.onmousewheel === void 0) { + /* IE6-11 chrome mousewheel wheelDetla 下 -120 上 120 + firefox DOMMouseScroll detail 下3 上-3 + firefox wheel detlaY 下3 上-3 + IE9-11 wheel deltaY 下40 上-40 + chrome wheel deltaY 下100 上-100 */ + eventHooks.mousewheel = { + type: "wheel", + fix: function (elem, fn) { + return function (e) { + e.wheelDeltaY = e.wheelDelta = e.deltaY > 0 ? -120 : 120 + e.wheelDeltaX = 0 + Object.defineProperty(e, "type", { + value: "mousewheel" + }) + fn.call(elem, e) + } + } + } +} + + + + + + + + + + + + + + + + +/********************************************************************* + * 配置系统 * + **********************************************************************/ + +function kernel(settings) { + for (var p in settings) { + if (!ohasOwn.call(settings, p)) + continue + var val = settings[p] + if (typeof kernel.plugins[p] === "function") { + kernel.plugins[p](val) + } else if (typeof kernel[p] === "object") { + yua.mix(kernel[p], val) + } else { + kernel[p] = val + } + } + return this +} +yua.config = kernel + +var openTag, closeTag, rexpr, rexprg, rbind, rregexp = /[-.*+?^${}()|[\]\/\\]/g + +function escapeRegExp(target) { + //http://stevenlevithan.com/regex/xregexp/ + //将字符串安全格式化为正则表达式的源码 + return (target + "").replace(rregexp, "\\$&") +} + +var plugins = { + interpolate: function (array) { + openTag = array[0] + closeTag = array[1] + if (openTag === closeTag) { + throw new SyntaxError("openTag!==closeTag") + var test = openTag + "test" + closeTag + cinerator.innerHTML = test + if (cinerator.innerHTML !== test && cinerator.innerHTML.indexOf("<") > -1) { + throw new SyntaxError("此定界符不合法") + } + cinerator.innerHTML = "" + } + kernel.openTag = openTag + kernel.closeTag = closeTag + var o = escapeRegExp(openTag), + c = escapeRegExp(closeTag) + rexpr = new RegExp(o + "([\\s\\S]*)" + c) + rexprg = new RegExp(o + "([\\s\\S]*)" + c, "g") + rbind = new RegExp(o + "[\\s\\S]*" + c + "|\\s:") //此处有疑问 + } +} +kernel.plugins = plugins +kernel.plugins['interpolate'](["{{", "}}"]) + +kernel.async = true +kernel.debug = true +kernel.paths = {} +kernel.shim = {} +kernel.maxRepeatSize = 100 + +function $watch(expr, binding) { + var $events = this.$events || (this.$events = {}), + queue = $events[expr] || ($events[expr] = []) + + if (typeof binding === "function") { + var backup = binding + backup.uuid = "_"+ (++bindingID) + binding = { + element: root, + type: "user-watcher", + handler: noop, + vmodels: [this], + expr: expr, + uuid: backup.uuid + } + binding.wildcard = /\*/.test(expr) + } + + if (!binding.update) { + if (/\w\.*\B/.test(expr) || expr === "*") { + binding.getter = noop + var host = this + binding.update = function () { + var args = this.fireArgs || [] + if (args[2]) + binding.handler.apply(host, args) + delete this.fireArgs + } + queue.sync = true + yua.Array.ensure(queue, binding) + } else { + yua.injectBinding(binding) + } + if (backup) { + binding.handler = backup + } + } else if (!binding.oneTime) { + yua.Array.ensure(queue, binding) + } + + return function () { + binding.update = binding.getter = binding.handler = noop + binding.element = DOC.createElement("a") + } +} + +function $emit(key, args) { + var event = this.$events + if (event && event[key]) { + if (args) { + args[2] = key + } + var arr = event[key] + notifySubscribers(arr, args) + if (args && event['*'] && !/\./.test(key)) { + for (var sub, k = 0; sub = event["*"][k++]; ) { + try { + sub.handler.apply(this, args) + } catch (e) { + } + } + } + var parent = this.$up + if (parent) { + if (this.$pathname) { + $emit.call(parent, this.$pathname + "." + key, args)//以确切的值往上冒泡 + } + $emit.call(parent, "*." + key, args)//以模糊的值往上冒泡 + } + } else { + parent = this.$up + if (this.$ups) { + for (var i in this.$ups) { + + $emit.call(this.$ups[i], i + "." + key, args)//以确切的值往上冒泡 + } + return + } + if (parent) { + var p = this.$pathname + if (p === "") + p = "*" + var path = p + "." + key; + arr = path.split("."); + + args = args.concat([path, key]) + + if (arr.indexOf("*") === -1) { + $emit.call(parent, path, args)//以确切的值往上冒泡 + arr[1] = "*" + $emit.call(parent, arr.join("."), args)//以模糊的值往上冒泡 + } else { + $emit.call(parent, path, args)//以确切的值往上冒泡 + } + } + } +} + +function collectDependency(el, key) { + do { + if (el.$watch) { + var e = el.$events || (el.$events = {}) + var array = e[key] || (e[key] = []) + dependencyDetection.collectDependency(array) + return + } + el = el.$up + if (el) { + key = el.$pathname + "." + key + } else { + break + } + } while (true) +} + +function notifySubscribers(subs, args) { + if (!subs) + return + if (new Date() - beginTime > 444 && typeof subs[0] === "object") { + rejectDisposeQueue() + } + var users = [], renders = [] + for (var i = 0, sub; sub = subs[i++]; ) { + if (sub.type === "user-watcher") { + users.push(sub) + } else { + renders.push(sub) + } + } + if (kernel.async) { + buffer.render()//1 + for (i = 0; sub = renders[i++]; ) { + if (sub.update) { + sub.uuid = sub.uuid || "_"+(++bindingID) + var uuid = sub.uuid + if (!buffer.queue[uuid]) { + buffer.queue[uuid] = "__" + buffer.queue.push(sub) + } + } + } + } else { + for (i = 0; sub = renders[i++]; ) { + if (sub.update) { + sub.update()//最小化刷新DOM树 + } + } + } + for (i = 0; sub = users[i++]; ) { + if (args && args[2] === sub.expr || sub.wildcard) { + sub.fireArgs = args + } + sub.update() + } +} + + + + + + + + + + +//yua最核心的方法的两个方法之一(另一个是yua.scan),返回一个ViewModel(VM) +var VMODELS = yua.vmodels = {} //所有vmodel都储存在这里 +yua.define = function (source) { + var $id = source.$id + if (!$id) { + log("warning: vm必须指定$id") + } + var vmodel = modelFactory(source) + vmodel.$id = $id + return VMODELS[$id] = vmodel +} + +//一些不需要被监听的属性 +var $$skipArray = oneObject("$id,$watch,$fire,$events,$model,$skipArray,$active,$pathname,$up,$ups,$track,$accessors") + +//如果浏览器不支持ecma262v5的Object.defineProperties或者存在BUG,比如IE8 +//标准浏览器使用__defineGetter__, __defineSetter__实现 + +function modelFactory(source, options) { + options = options || {} + options.watch = true + return observeObject(source, options) +} + +//监听对象属性值的变化(注意,数组元素不是数组的属性),通过对劫持当前对象的访问器实现 +//监听对象或数组的结构变化, 对对象的键值对进行增删重排, 或对数组的进行增删重排,都属于这范畴 +// 通过比较前后代理VM顺序实现 +function Component() { +} + +function observeObject(source, options) { + if (!source || (source.$id && source.$accessors) || (source.nodeName && source.nodeType > 0)) { + return source + } + //source为原对象,不能是元素节点或null + //options,可选,配置对象,里面有old, force, watch这三个属性 + options = options || nullObject + var force = options.force || nullObject + var old = options.old + var oldAccessors = old && old.$accessors || nullObject + var $vmodel = new Component() //要返回的对象, 它在IE6-8下可能被偷龙转凤 + var accessors = {} //监控属性 + var hasOwn = {} + var skip = [] + var simple = [] + var $skipArray = {} + if (source.$skipArray) { + $skipArray = oneObject(source.$skipArray) + delete source.$skipArray + } + //处理计算属性 + var computed = source.$computed + if (computed) { + delete source.$computed + for (var name in computed) { + hasOwn[name] = true; + (function (key, value) { + var old; + if(typeof value === 'function'){ + value = {get: value, set: noop} + } + if(typeof value.set !== 'function'){ + value.set = noop + } + accessors[key] = { + get: function () { + return old = value.get.call(this) + }, + set: function (x) { + var older = old,newer; + value.set.call(this, x) + newer = this[key] + if (this.$fire && (newer !== older)) { + this.$fire(key, newer, older) + } + }, + enumerable: true, + configurable: true + } + })(name, computed[name])// jshint ignore:line + } + } + + for (name in source) { + var value = source[name] + if (!$$skipArray[name]) + hasOwn[name] = true + if (typeof value === "function" || (value && value.nodeName && value.nodeType > 0) || + (!force[name] && (name.charAt(0) === "$" || $$skipArray[name] || $skipArray[name]))) { + skip.push(name) + } else if (isComputed(value)) { + log("warning:计算属性建议放在$computed对象中统一定义"); + (function (key, value) { + var old; + if(typeof value === 'function'){ + value = {get: value, set: noop} + } + if(typeof value.set !== 'function'){ + value.set = noop + } + accessors[key] = { + get: function () { + return old = value.get.call(this) + }, + set: function (x) { + var older = old,newer; + value.set.call(this, x) + newer = this[key] + if (this.$fire && (newer !== older)) { + this.$fire(key, newer, older) + } + }, + enumerable: true, + configurable: true + } + })(name, value)// jshint ignore:line + } else { + simple.push(name) + if (oldAccessors[name]) { + accessors[name] = oldAccessors[name] + } else { + accessors[name] = makeGetSet(name, value) + } + } + } + + accessors["$model"] = $modelDescriptor + $vmodel = Object.defineProperties($vmodel, accessors, source) + function trackBy(name) { + return hasOwn[name] === true + } + skip.forEach(function (name) { + $vmodel[name] = source[name] + }) + + + /* jshint ignore:start */ + hideProperty($vmodel, "$ups", null) + hideProperty($vmodel, "$id", "anonymous") + hideProperty($vmodel, "$up", old ? old.$up : null) + hideProperty($vmodel, "$track", Object.keys(hasOwn)) + hideProperty($vmodel, "$active", false) + hideProperty($vmodel, "$pathname", old ? old.$pathname : "") + hideProperty($vmodel, "$accessors", accessors) + hideProperty($vmodel, "hasOwnProperty", trackBy) + if (options.watch) { + hideProperty($vmodel, "$watch", function () { + return $watch.apply($vmodel, arguments) + }) + hideProperty($vmodel, "$fire", function (path, a) { + if (path.indexOf("all!") === 0) { + var ee = path.slice(4) + for (var i in yua.vmodels) { + var v = yua.vmodels[i] + v.$fire && v.$fire.apply(v, [ee, a]) + } + } else { + $emit.call($vmodel, path, [a]) + } + }) + } + /* jshint ignore:end */ + + //必须设置了$active,$events + simple.forEach(function (name) { + var oldVal = old && old[name] + var val = $vmodel[name] = source[name] + if (val && typeof val === "object") { + val.$up = $vmodel + val.$pathname = name + } + $emit.call($vmodel, name,[val,oldVal]) + }) + for (name in computed) { + value = $vmodel[name] + } + $vmodel.$active = true + + return $vmodel +} + + + + + + + + + + + + + + + + + + + + + + + + + + + +/* + 新的VM拥有如下私有属性 + $id: vm.id + $events: 放置$watch回调与绑定对象 + $watch: 增强版$watch + $fire: 触发$watch回调 + $track:一个数组,里面包含用户定义的所有键名 + $active:boolean,false时防止依赖收集 + $model:返回一个纯净的JS对象 + $accessors:放置所有读写器的数据描述对象 + $up:返回其上级对象 + $pathname:返回此对象在上级对象的名字,注意,数组元素的$pathname为空字符串 + ============================= + $skipArray:用于指定不可监听的属性,但VM生成是没有此属性的 + */ +function isComputed(val) {//speed up! + if (val && typeof val === "object") { + for (var i in val) { + if (i !== "get" && i !== "set") { + return false + } + } + return typeof val.get === "function" + } +} +function makeGetSet(key, value) { + var childVm, value = NaN + return { + get: function () { + if (this.$active) { + collectDependency(this, key) + } + return value + }, + set: function (newVal) { + if (value === newVal) + return + var oldValue = value + childVm = observe(newVal, value) + if (childVm) { + value = childVm + } else { + childVm = void 0 + value = newVal + } + + if (Object(childVm) === childVm) { + childVm.$pathname = key + childVm.$up = this + } + if (this.$active) { + $emit.call(this, key, [value, oldValue]) + } + }, + enumerable: true, + configurable: true + } +} + +function observe(obj, old, hasReturn, watch) { + if (Array.isArray(obj)) { + return observeArray(obj, old, watch) + } else if (yua.isPlainObject(obj)) { + if (old && typeof old === 'object') { + var keys = Object.keys(obj) + var keys2 = Object.keys(old) + if (keys.join(";") === keys2.join(";")) { + for (var i in obj) { + if (obj.hasOwnProperty(i)) { + old[i] = obj[i] + } + } + return old + } + old.$active = false + } + return observeObject(obj, { + old: old, + watch: watch + }) + } + if (hasReturn) { + return obj + } +} + +function observeArray(array, old, watch) { + if (old && old.splice) { + var args = [0, old.length].concat(array) + old.splice.apply(old, args) + return old + } else { + for (var i in newProto) { + array[i] = newProto[i] + } + hideProperty(array, "$up", null) + hideProperty(array, "$pathname", "") + hideProperty(array, "$track", createTrack(array.length)) + + array._ = observeObject({ + length: NaN + }, { + watch: true + }) + array._.length = array.length + array._.$watch("length", function (a, b) { + $emit.call(array.$up, array.$pathname + ".length", [a, b]) + }) + if (watch) { + hideProperty(array, "$watch", function () { + return $watch.apply(array, arguments) + }) + } + + Object.defineProperty(array, "$model", $modelDescriptor) + + for (var j = 0, n = array.length; j < n; j++) { + var el = array[j] = observe(array[j], 0, 1, 1) + if (Object(el) === el) {//#1077 + el.$up = array + } + } + + return array + } +} + +function hideProperty(host, name, value) { + + Object.defineProperty(host, name, { + value: value, + writable: true, + enumerable: false, + configurable: true + }) + +} + +function toJson(val) { + var xtype = yua.type(val) + if (xtype === "array") { + var array = [] + for (var i = 0; i < val.length; i++) { + array[i] = toJson(val[i]) + } + return array + } else if (xtype === "object") { + var obj = {} + for (i in val) { + if (val.hasOwnProperty(i)) { + var value = val[i] + obj[i] = value && value.nodeType ? value : toJson(value) + } + } + return obj + } + return val +} + +var $modelDescriptor = { + get: function () { + return toJson(this) + }, + set: noop, + enumerable: false, + configurable: true + } + + + + + +/********************************************************************* + * 监控数组(与:each, :repeat配合使用) * + **********************************************************************/ + +var arrayMethods = ['push', 'pop', 'shift', 'unshift', 'splice'] +var arrayProto = Array.prototype +var newProto = { + notify: function () { + $emit.call(this.$up, this.$pathname) + }, + set: function (index, val) { + if (((index >>> 0) === index) && this[index] !== val) { + if (index > this.length) { + throw Error(index + "set方法的第一个参数不能大于原数组长度") + } + $emit.call(this.$up, this.$pathname + ".*", [val, this[index]]) + this.splice(index, 1, val) + } + }, + contains: function (el) { //判定是否包含 + return this.indexOf(el) !== -1 + }, + ensure: function (el) { + if (!this.contains(el)) { //只有不存在才push + this.push(el) + } + return this + }, + pushArray: function (arr) { + return this.push.apply(this, arr) + }, + remove: function (el) { //移除第一个等于给定值的元素 + return this.removeAt(this.indexOf(el)) + }, + removeAt: function (index) { //移除指定索引上的元素 + if ((index >>> 0) === index) { + return this.splice(index, 1) + } + return [] + }, + size: function () { //取得数组长度,这个函数可以同步视图,length不能 + return this._.length + }, + removeAll: function (all) { //移除N个元素 + if (Array.isArray(all)) { + for (var i = this.length - 1; i >= 0; i--) { + if (all.indexOf(this[i]) !== -1) { + _splice.call(this.$track, i, 1) + _splice.call(this, i, 1) + + } + } + } else if (typeof all === "function") { + for (i = this.length - 1; i >= 0; i--) { + var el = this[i] + if (all(el, i)) { + _splice.call(this.$track, i, 1) + _splice.call(this, i, 1) + + } + } + } else { + _splice.call(this.$track, 0, this.length) + _splice.call(this, 0, this.length) + + } + if (!W3C) { + this.$model = toJson(this) + } + this.notify() + this._.length = this.length + }, + clear: function () { + this.removeAll() + return this + } +} + +var _splice = arrayProto.splice +arrayMethods.forEach(function (method) { + var original = arrayProto[method] + newProto[method] = function () { + // 继续尝试劫持数组元素的属性 + var args = [] + for (var i = 0, n = arguments.length; i < n; i++) { + args[i] = observe(arguments[i], 0, 1, 1) + } + var result = original.apply(this, args) + addTrack(this.$track, method, args) + if (!W3C) { + this.$model = toJson(this) + } + this.notify() + this._.length = this.length + return result + } +}) + +"sort,reverse".replace(rword, function (method) { + newProto[method] = function () { + var oldArray = this.concat() //保持原来状态的旧数组 + var newArray = this + var mask = Math.random() + var indexes = [] + var hasSort = false + arrayProto[method].apply(newArray, arguments) //排序 + for (var i = 0, n = oldArray.length; i < n; i++) { + var neo = newArray[i] + var old = oldArray[i] + if (neo === old) { + indexes.push(i) + } else { + var index = oldArray.indexOf(neo) + indexes.push(index)//得到新数组的每个元素在旧数组对应的位置 + oldArray[index] = mask //屏蔽已经找过的元素 + hasSort = true + } + } + if (hasSort) { + sortByIndex(this.$track, indexes) + if (!W3C) { + this.$model = toJson(this) + } + this.notify() + } + return this + } +}) + +function sortByIndex(array, indexes) { + var map = {}; + for (var i = 0, n = indexes.length; i < n; i++) { + map[i] = array[i] + var j = indexes[i] + if (j in map) { + array[i] = map[j] + delete map[j] + } else { + array[i] = array[j] + } + } +} + +function createTrack(n) { + var ret = [] + for (var i = 0; i < n; i++) { + ret[i] = generateID("$proxy$each") + } + return ret +} + +function addTrack(track, method, args) { + switch (method) { + case 'push': + case 'unshift': + args = createTrack(args.length) + break + case 'splice': + if (args.length > 2) { + // 0, 5, a, b, c --> 0, 2, 0 + // 0, 5, a, b, c, d, e, f, g--> 0, 0, 3 + var del = args[1] + var add = args.length - 2 + // args = [args[0], Math.max(del - add, 0)].concat(createTrack(Math.max(add - del, 0))) + args = [args[0], args[1]].concat(createTrack(args.length - 2)) + } + break + } + Array.prototype[method].apply(track, args) +} + + + + + + + + + + + + + + + +/********************************************************************* + * 依赖调度系统 * + **********************************************************************/ + +//检测两个对象间的依赖关系 +var dependencyDetection = (function () { + var outerFrames = [] + var currentFrame + return { + begin: function (binding) { + //accessorObject为一个拥有callback的对象 + outerFrames.push(currentFrame) + currentFrame = binding + }, + end: function () { + currentFrame = outerFrames.pop() + }, + collectDependency: function (array) { + if (currentFrame) { + //被dependencyDetection.begin调用 + currentFrame.callback(array) + } + } + }; +})() + +//将绑定对象注入到其依赖项的订阅数组中 +var roneval = /^on$/ + +function returnRandom() { + return new Date() - 0 +} + +yua.injectBinding = function (binding) { + + binding.handler = binding.handler || directives[binding.type].update || noop + binding.update = function () { + var begin = false + if (!binding.getter) { + begin = true + dependencyDetection.begin({ + callback: function (array) { + injectDependency(array, binding) + } + }) + binding.getter = parseExpr(binding.expr, binding.vmodels, binding) + binding.observers.forEach(function (a) { + a.v.$watch(a.p, binding) + }) + delete binding.observers + } + try { + var args = binding.fireArgs, a, b + delete binding.fireArgs + if (!args) { + if (binding.type === "on") { + a = binding.getter + "" + } else { + try { + a = binding.getter.apply(0, binding.args) + } catch(e) { + a = null + } + } + } else { + a = args[0] + b = args[1] + } + b = typeof b === "undefined" ? binding.oldValue : b + if (binding._filters) { + a = filters.$filter.apply(0, [a].concat(binding._filters)) + } + if (binding.signature) { + var xtype = yua.type(a) + if (xtype !== "array" && xtype !== "object") { + throw Error("warning:" + binding.expr + "只能是对象或数组") + } + binding.xtype = xtype + var vtrack = getProxyIds(binding.proxies || [], xtype) + var mtrack = a.$track || (xtype === "array" ? createTrack(a.length) : + Object.keys(a)) + binding.track = mtrack + if (vtrack !== mtrack.join(";")) { + binding.handler(a, b) + binding.oldValue = 1 + } + } else if (Array.isArray(a) ? a.length !== (b && b.length) : false) { + binding.handler(a, b) + binding.oldValue = a.concat() + } else if (!("oldValue" in binding) || a !== b) { + binding.handler(a, b) + binding.oldValue = Array.isArray(a) ? a.concat() : a + } + } catch (e) { + delete binding.getter + log("warning:exception throwed in [yua.injectBinding] ", e) + var node = binding.element + if (node && node.nodeType === 3) { + node.nodeValue = openTag + (binding.oneTime ? "::" : "") + binding.expr + closeTag + } + } finally { + begin && dependencyDetection.end() + + } + } + binding.update() +} + +//将依赖项(比它高层的访问器或构建视图刷新函数的绑定对象)注入到订阅者数组 +function injectDependency(list, binding) { + if (binding.oneTime) + return + if (list && yua.Array.ensure(list, binding) && binding.element) { + injectDisposeQueue(binding, list) + if (new Date() - beginTime > 444) { + rejectDisposeQueue() + } + } +} + +function getProxyIds(a, isArray) { + var ret = [] + for (var i = 0, el; el = a[i++]; ) { + ret.push(isArray ? el.$id : el.$key) + } + return ret.join(";") +} + +/********************************************************************* + * 定时GC回收机制 (基于1.6基于频率的GC) * + **********************************************************************/ + +var disposeQueue = yua.$$subscribers = [] +var beginTime = new Date() + +//添加到回收列队中 +function injectDisposeQueue(data, list) { + data.list = list + data.i = ~~data.i + if (!data.uuid) { + data.uuid = "_" + (++bindingID) + } + if (!disposeQueue[data.uuid]) { + disposeQueue[data.uuid] = "__" + disposeQueue.push(data) + } +} + +var lastGCIndex = 0 +function rejectDisposeQueue(data) { + var i = lastGCIndex || disposeQueue.length + var threshold = 0 + while (data = disposeQueue[--i]) { + if (data.i < 7) { + if (data.element === null) { + disposeQueue.splice(i, 1) + if (data.list) { + yua.Array.remove(data.list, data) + delete disposeQueue[data.uuid] + } + continue + } + if (shouldDispose(data.element)) { //如果它的虚拟DOM不在VTree上或其属性不在VM上 + disposeQueue.splice(i, 1) + yua.Array.remove(data.list, data) + disposeData(data) + //yua会在每次全量更新时,比较上次执行时间, + //假若距离上次有半秒,就会发起一次GC,并且只检测当中的500个绑定 + //而一个正常的页面不会超过2000个绑定(500即取其4分之一) + //用户频繁操作页面,那么2,3秒内就把所有绑定检测一遍,将无效的绑定移除 + if (threshold++ > 500) { + lastGCIndex = i + break + } + continue + } + data.i++ + //基于检测频率,如果检测过7次,可以认为其是长久存在的节点,那么以后每7次才检测一次 + if (data.i === 7) { + data.i = 14 + } + } else { + data.i-- + } + } + beginTime = new Date() +} + +function disposeData(data) { + delete disposeQueue[data.uuid] // 先清除,不然无法回收了 + data.element = null + data.rollback && data.rollback() + for (var key in data) { + data[key] = null + } +} + +function shouldDispose(el) { + try {//IE下,如果文本节点脱离DOM树,访问parentNode会报错 + var fireError = el.parentNode.nodeType + } catch (e) { + return true + } + if (el.ifRemove) { + // 如果节点被放到ifGroup,才移除 + if (!root.contains(el.ifRemove) && (ifGroup === el.parentNode)) { + el.parentNode && el.parentNode.removeChild(el) + return true + } + } + return el.msRetain ? 0 : (el.nodeType === 1 ? !root.contains(el) : !yua.contains(root, el)) +} + + + + + + + + + + + + + + + + + + + +/************************************************************************ + * HTML处理(parseHTML, innerHTML, clearHTML) * + *************************************************************************/ + +//parseHTML的辅助变量 +var tagHooks = new function() {// jshint ignore:line + yua.mix(this, { + option: DOC.createElement("select"), + thead: DOC.createElement("table"), + td: DOC.createElement("tr"), + area: DOC.createElement("map"), + tr: DOC.createElement("tbody"), + col: DOC.createElement("colgroup"), + legend: DOC.createElement("fieldset"), + _default: DOC.createElement("div"), + "g": DOC.createElementNS("http://www.w3.org/2000/svg", "svg") + }) + this.optgroup = this.option + this.tbody = this.tfoot = this.colgroup = this.caption = this.thead + this.th = this.td +}// jshint ignore:line +String("circle,defs,ellipse,image,line,path,polygon,polyline,rect,symbol,text,use").replace(rword, function(tag) { + tagHooks[tag] = tagHooks.g //处理SVG +}) + +var rtagName = /<([\w:]+)/ +var rxhtml = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/ig +var scriptTypes = oneObject(["", "text/javascript", "text/ecmascript", "application/ecmascript", "application/javascript"]) +var script = DOC.createElement("script") +var rhtml = /<|&#?\w+;/ + +yua.parseHTML = function(html) { + var fragment = yuaFragment.cloneNode(false) + if (typeof html !== "string" ) { + return fragment + } + if (!rhtml.test(html)) { + fragment.appendChild(DOC.createTextNode(html)) + return fragment + } + html = html.replace(rxhtml, "<$1>").trim() + var tag = (rtagName.exec(html) || ["", ""])[1].toLowerCase(), + //取得其标签名 + wrapper = tagHooks[tag] || tagHooks._default, + firstChild + wrapper.innerHTML = html + var els = wrapper.getElementsByTagName("script") + if (els.length) { //使用innerHTML生成的script节点不会发出请求与执行text属性 + for (var i = 0, el; el = els[i++]; ) { + if (scriptTypes[el.type]) { + var neo = script.cloneNode(false) //FF不能省略参数 + ap.forEach.call(el.attributes, function(attr) { + neo.setAttribute(attr.name, attr.value) + })// jshint ignore:line + neo.text = el.text + el.parentNode.replaceChild(neo, el) + } + } + } + + while (firstChild = wrapper.firstChild) { // 将wrapper上的节点转移到文档碎片上! + fragment.appendChild(firstChild) + } + return fragment +} + +yua.innerHTML = function(node, html) { + var a = this.parseHTML(html) + this.clearHTML(node).appendChild(a) +} + +yua.clearHTML = function(node) { + node.textContent = "" + while (node.firstChild) { + node.removeChild(node.firstChild) + } + return node +} + +/********************************************************************* + * yua的原型方法定义区 * + **********************************************************************/ + +function hyphen(target) { + //转换为连字符线风格 + return target.replace(/([a-z\d])([A-Z]+)/g, "$1-$2").toLowerCase() +} + +function camelize(target) { + //转换为驼峰风格 + if (target.indexOf("-") < 0 && target.indexOf("_") < 0) { + return target //提前判断,提高getStyle等的效率 + } + return target.replace(/[-_][^-_]/g, function (match) { + return match.charAt(1).toUpperCase() + }) +} + +"add,remove".replace(rword, function (method) { + yua.fn[method + "Class"] = function (cls) { + var el = this[0] + //https://developer.mozilla.org/zh-CN/docs/Mozilla/Firefox/Releases/26 + if (cls && typeof cls === "string" && el && el.nodeType === 1) { + cls.replace(/\S+/g, function (c) { + el.classList[method](c) + }) + } + return this + } +}) + +yua.fn.mix({ + hasClass: function (cls) { + var el = this[0] || {} //IE10+, chrome8+, firefox3.6+, safari5.1+,opera11.5+支持classList,chrome24+,firefox26+支持classList2.0 + return el.nodeType === 1 && el.classList.contains(cls) + }, + toggleClass: function (value, stateVal) { + var className, i = 0 + var classNames = String(value).match(/\S+/g) || [] + var isBool = typeof stateVal === "boolean" + while ((className = classNames[i++])) { + var state = isBool ? stateVal : !this.hasClass(className) + this[state ? "addClass" : "removeClass"](className) + } + return this + }, + attr: function (name, value) { + if (arguments.length === 2) { + this[0].setAttribute(name, value) + return this + } else { + return this[0].getAttribute(name) + } + }, + data: function (name, value) { + name = "data-" + hyphen(name || "") + switch (arguments.length) { + case 2: + this.attr(name, value) + return this + case 1: + var val = this.attr(name) + return parseData(val) + case 0: + var ret = {} + ap.forEach.call(this[0].attributes, function (attr) { + if (attr) { + name = attr.name + if (!name.indexOf("data-")) { + name = camelize(name.slice(5)) + ret[name] = parseData(attr.value) + } + } + }) + return ret + } + }, + removeData: function (name) { + name = "data-" + hyphen(name) + this[0].removeAttribute(name) + return this + }, + css: function (name, value) { + if (yua.isPlainObject(name)) { + for (var i in name) { + yua.css(this, i, name[i]) + } + } else { + var ret = yua.css(this, name, value) + } + return ret !== void 0 ? ret : this + }, + position: function () { + var offsetParent, offset, + elem = this[0], + parentOffset = { + top: 0, + left: 0 + }; + if (!elem) { + return + } + if (this.css("position") === "fixed") { + offset = elem.getBoundingClientRect() + } else { + offsetParent = this.offsetParent() //得到真正的offsetParent + offset = this.offset() // 得到正确的offsetParent + if (offsetParent[0].tagName !== "HTML") { + parentOffset = offsetParent.offset() + } + parentOffset.top += yua.css(offsetParent[0], "borderTopWidth", true) + parentOffset.left += yua.css(offsetParent[0], "borderLeftWidth", true) + // Subtract offsetParent scroll positions + parentOffset.top -= offsetParent.scrollTop() + parentOffset.left -= offsetParent.scrollLeft() + } + return { + top: offset.top - parentOffset.top - yua.css(elem, "marginTop", true), + left: offset.left - parentOffset.left - yua.css(elem, "marginLeft", true) + } + }, + offsetParent: function () { + var offsetParent = this[0].offsetParent + while (offsetParent && yua.css(offsetParent, "position") === "static") { + offsetParent = offsetParent.offsetParent; + } + return yua(offsetParent || root) + }, + bind: function (type, fn, phase) { + if (this[0]) { //此方法不会链 + return yua.bind(this[0], type, fn, phase) + } + }, + unbind: function (type, fn, phase) { + if (this[0]) { + yua.unbind(this[0], type, fn, phase) + } + return this + }, + val: function (value) { + var node = this[0] + if (node && node.nodeType === 1) { + var get = arguments.length === 0 + var access = get ? ":get" : ":set" + var fn = valHooks[getValType(node) + access] + if (fn) { + var val = fn(node, value) + } else if (get) { + return (node.value || "").replace(/\r/g, "") + } else { + node.value = value + } + } + return get ? val : this + } +}) + +if (root.dataset) { + yua.fn.data = function (name, val) { + name = name && camelize(name) + var dataset = this[0].dataset + switch (arguments.length) { + case 2: + dataset[name] = val + return this + case 1: + val = dataset[name] + return parseData(val) + case 0: + var ret = createMap() + for (name in dataset) { + ret[name] = parseData(dataset[name]) + } + return ret + } + } +} + +yua.parseJSON = JSON.parse + +var rbrace = /(?:\{[\s\S]*\}|\[[\s\S]*\])$/ +function parseData(data) { + try { + if (typeof data === "object") + return data + data = data === "true" ? true : + data === "false" ? false : + data === "null" ? null : +data + "" === data ? +data : rbrace.test(data) ? JSON.parse(data) : data + } catch (e) { + } + return data +} + +yua.fireDom = function (elem, type, opts) { + var hackEvent = DOC.createEvent("Events"); + hackEvent.initEvent(type, true, true) + yua.mix(hackEvent, opts) + elem.dispatchEvent(hackEvent) +} + +yua.each({ + scrollLeft: "pageXOffset", + scrollTop: "pageYOffset" +}, function (method, prop) { + yua.fn[method] = function (val) { + var node = this[0] || {}, + win = getWindow(node), + top = method === "scrollTop" + if (!arguments.length) { + return win ? win[prop] : node[method] + } else { + if (win) { + win.scrollTo(!top ? val : win[prop], top ? val : win[prop]) + } else { + node[method] = val + } + } + } +}) + +function getWindow(node) { + return node.window && node.document ? node : node.nodeType === 9 ? node.defaultView : false +} + + + + + + +//=============================css相关================================== + +var cssHooks = yua.cssHooks = createMap() +var prefixes = ["", "-webkit-", "-moz-", "-ms-"] //去掉opera-15的支持 +var cssMap = { + "float": "cssFloat" +} + +yua.cssNumber = oneObject("animationIterationCount,animationIterationCount,columnCount,order,flex,flexGrow,flexShrink,fillOpacity,fontWeight,lineHeight,opacity,orphans,widows,zIndex,zoom") + +yua.cssName = function (name, host, camelCase) { + if (cssMap[name]) { + return cssMap[name] + } + host = host || root.style + for (var i = 0, n = prefixes.length; i < n; i++) { + camelCase = camelize(prefixes[i] + name) + if (camelCase in host) { + return (cssMap[name] = camelCase) + } + } + return null +} + +cssHooks["@:set"] = function (node, name, value) { + node.style[name] = value +} + +cssHooks["@:get"] = function (node, name) { + if (!node || !node.style) { + throw new Error("getComputedStyle要求传入一个节点 " + node) + } + var ret, computed = getComputedStyle(node) + if (computed) { + ret = name === "filter" ? computed.getPropertyValue(name) : computed[name] + if (ret === "") { + ret = node.style[name] //其他浏览器需要我们手动取内联样式 + } + } + return ret +} +cssHooks["opacity:get"] = function (node) { + var ret = cssHooks["@:get"](node, "opacity") + return ret === "" ? "1" : ret +} + +"top,left".replace(rword, function (name) { + cssHooks[name + ":get"] = function (node) { + var computed = cssHooks["@:get"](node, name) + return /px$/.test(computed) ? computed : + yua(node).position()[name] + "px" + } +}) + +var cssShow = { + position: "absolute", + visibility: "hidden", + display: "block" +} +var rdisplayswap = /^(none|table(?!-c[ea]).+)/ +function showHidden(node, array) { + //http://www.cnblogs.com/rubylouvre/archive/2012/10/27/2742529.html + if (node.offsetWidth <= 0) { //opera.offsetWidth可能小于0 + var styles = getComputedStyle(node, null) + if (rdisplayswap.test(styles["display"])) { + var obj = { + node: node + } + for (var name in cssShow) { + obj[name] = styles[name] + node.style[name] = cssShow[name] + } + array.push(obj) + } + var parent = node.parentNode + if (parent && parent.nodeType === 1) { + showHidden(parent, array) + } + } +} + +"Width,Height".replace(rword, function (name) { //fix 481 + var method = name.toLowerCase(), + clientProp = "client" + name, + scrollProp = "scroll" + name, + offsetProp = "offset" + name + cssHooks[method + ":get"] = function (node, which, override) { + var boxSizing = -4 + if (typeof override === "number") { + boxSizing = override + } + which = name === "Width" ? ["Left", "Right"] : ["Top", "Bottom"] + var ret = node[offsetProp] // border-box 0 + if (boxSizing === 2) { // margin-box 2 + return ret + yua.css(node, "margin" + which[0], true) + yua.css(node, "margin" + which[1], true) + } + if (boxSizing < 0) { // padding-box -2 + ret = ret - yua.css(node, "border" + which[0] + "Width", true) - yua.css(node, "border" + which[1] + "Width", true) + } + if (boxSizing === -4) { // content-box -4 + ret = ret - yua.css(node, "padding" + which[0], true) - yua.css(node, "padding" + which[1], true) + } + return ret + } + cssHooks[method + "&get"] = function (node) { + var hidden = []; + showHidden(node, hidden); + var val = cssHooks[method + ":get"](node) + for (var i = 0, obj; obj = hidden[i++]; ) { + node = obj.node + for (var n in obj) { + if (typeof obj[n] === "string") { + node.style[n] = obj[n] + } + } + } + return val; + } + yua.fn[method] = function (value) { //会忽视其display + var node = this[0] + if (arguments.length === 0) { + if (node.setTimeout) { //取得窗口尺寸,IE9后可以用node.innerWidth /innerHeight代替 + return node["inner" + name] + } + if (node.nodeType === 9) { //取得页面尺寸 + var doc = node.documentElement + //FF chrome html.scrollHeight< body.scrollHeight + //IE 标准模式 : html.scrollHeight> body.scrollHeight + //IE 怪异模式 : html.scrollHeight 最大等于可视窗口多一点? + return Math.max(node.body[scrollProp], doc[scrollProp], node.body[offsetProp], doc[offsetProp], doc[clientProp]) + } + return cssHooks[method + "&get"](node) + } else { + return this.css(method, value) + } + } + yua.fn["inner" + name] = function () { + return cssHooks[method + ":get"](this[0], void 0, -2) + } + yua.fn["outer" + name] = function (includeMargin) { + return cssHooks[method + ":get"](this[0], void 0, includeMargin === true ? 2 : 0) + } +}) + +yua.fn.offset = function () { //取得距离页面左右角的坐标 + var node = this[0] + try { + var rect = node.getBoundingClientRect() + // Make sure element is not hidden (display: none) or disconnected + // https://github.com/jquery/jquery/pull/2043/files#r23981494 + if (rect.width || rect.height || node.getClientRects().length) { + var doc = node.ownerDocument + var root = doc.documentElement + var win = doc.defaultView + return { + top: rect.top + win.pageYOffset - root.clientTop, + left: rect.left + win.pageXOffset - root.clientLeft + } + } + } catch (e) { + return { + left: 0, + top: 0 + } + } +} + + + + + + + + +//=============================val相关======================= + +function getValType(elem) { + var ret = elem.tagName.toLowerCase() + return ret === "input" && /checkbox|radio/.test(elem.type) ? "checked" : ret +} + +var valHooks = { + "select:get": function (node, value) { + var option, options = node.options, + index = node.selectedIndex, + one = node.type === "select-one" || index < 0, + values = one ? null : [], + max = one ? index + 1 : options.length, + i = index < 0 ? max : one ? index : 0 + for (; i < max; i++) { + option = options[i] + //旧式IE在reset后不会改变selected,需要改用i === index判定 + //我们过滤所有disabled的option元素,但在safari5下,如果设置select为disable,那么其所有孩子都disable + //因此当一个元素为disable,需要检测其是否显式设置了disable及其父节点的disable情况 + if ((option.selected || i === index) && !option.disabled) { + value = option.value + if (one) { + return value + } + //收集所有selected值组成数组返回 + values.push(value) + } + } + return values + }, + "select:set": function (node, values, optionSet) { + values = [].concat(values) //强制转换为数组 + for (var i = 0, el; el = node.options[i++]; ) { + if ((el.selected = values.indexOf(el.value) > -1)) { + optionSet = true + } + } + if (!optionSet) { + node.selectedIndex = -1 + } + } +} + +var keyMap = {} +var keys = ["break,case,catch,continue,debugger,default,delete,do,else,false", + "finally,for,function,if,in,instanceof,new,null,return,switch,this", + "throw,true,try,typeof,var,void,while,with", /* 关键字*/ + "abstract,boolean,byte,char,class,const,double,enum,export,extends", + "final,float,goto,implements,import,int,interface,long,native", + "package,private,protected,public,short,static,super,synchronized", + "throws,transient,volatile", /*保留字*/ + "arguments,let,yield,undefined"].join(",") +keys.replace(/\w+/g, function (a) { + keyMap[a] = true +}) + +var ridentStart = /[a-z_$]/i +var rwhiteSpace = /[\s\uFEFF\xA0]/ +function getIdent(input, lastIndex) { + var result = [] + var subroutine = !!lastIndex + lastIndex = lastIndex || 0 + //将表达式中的标识符抽取出来 + var state = "unknown" + var variable = "" + for (var i = 0; i < input.length; i++) { + var c = input.charAt(i) + if (c === "'" || c === '"') {//字符串开始 + if (state === "unknown") { + state = c + } else if (state === c) {//字符串结束 + state = "unknown" + } + } else if (c === "\\") { + if (state === "'" || state === '"') { + i++ + } + } else if (ridentStart.test(c)) {//碰到标识符 + if (state === "unknown") { + state = "variable" + variable = c + } else if (state === "maybePath") { + variable = result.pop() + variable += "." + c + state = "variable" + } else if (state === "variable") { + variable += c + } + } else if (/\w/.test(c)) { + if (state === "variable") { + variable += c + } + } else if (c === ".") { + if (state === "variable") { + if (variable) { + result.push(variable) + variable = "" + state = "maybePath" + } + } + } else if (c === "[") { + if (state === "variable" || state === "maybePath") { + if (variable) {//如果前面存在变量,收集它 + result.push(variable) + variable = "" + } + var lastLength = result.length + var last = result[lastLength - 1] + var innerResult = getIdent(input.slice(i), i) + if (innerResult.length) {//如果括号中存在变量,那么这里添加通配符 + result[lastLength - 1] = last + ".*" + result = innerResult.concat(result) + } else { //如果括号中的东西是确定的,直接转换为其子属性 + var content = input.slice(i + 1, innerResult.i) + try { + var text = (scpCompile(["return " + content]))() + result[lastLength - 1] = last + "." + text + } catch (e) { + } + } + state = "maybePath"//]后面可能还接东西 + i = innerResult.i + } + } else if (c === "]") { + if (subroutine) { + result.i = i + lastIndex + addVar(result, variable) + return result + } + } else if (rwhiteSpace.test(c) && c !== "\r" && c !== "\n") { + if (state === "variable") { + if (addVar(result, variable)) { + state = "maybePath" // aaa . bbb 这样的情况 + } + variable = "" + } + } else { + addVar(result, variable) + state = "unknown" + variable = "" + } + } + addVar(result, variable) + return result +} + +function addVar(array, element) { + if (element && !keyMap[element]) { + array.push(element) + return true + } +} + +function addAssign(vars, vmodel, name, binding) { + var ret = [], + prefix = " = " + name + "." + for (var i = vars.length, prop; prop = vars[--i]; ) { + var arr = prop.split("."), a + var first = arr[0] + while (a = arr.shift()) { + if (vmodel.hasOwnProperty(a)) { + ret.push(first + prefix + first) + binding.observers.push({ + v: vmodel, + p: prop + }) + vars.splice(i, 1) + } else { + break + } + } + } + return ret +} + +var rproxy = /(\$proxy\$[a-z]+)\-[\-0-9a-f]+$/ +var variablePool = new Cache(218) +//缓存求值函数,以便多次利用 +var evaluatorPool = new Cache(128) + +function getVars(expr) { + expr = expr.trim() + var ret = variablePool.get(expr) + if (ret) { + return ret.concat() + } + var array = getIdent(expr) + var uniq = {} + var result = [] + for (var i = 0, el; el = array[i++]; ) { + if (!uniq[el]) { + uniq[el] = 1 + result.push(el) + } + } + return variablePool.put(expr, result).concat() +} + +function parseExpr(expr, vmodels, binding) { + var filters = binding.filters + if (typeof filters === "string" && filters.trim() && !binding._filters) { + binding._filters = parseFilter(filters.trim()) + } + + var vars = getVars(expr) + var expose = new Date() - 0 + var assigns = [] + var names = [] + var args = [] + binding.observers = [] + for (var i = 0, sn = vmodels.length; i < sn; i++) { + if (vars.length) { + var name = "vm" + expose + "_" + i + names.push(name) + args.push(vmodels[i]) + assigns.push.apply(assigns, addAssign(vars, vmodels[i], name, binding)) + } + } + binding.args = args + var dataType = binding.type + var exprId = vmodels.map(function (el) { + return String(el.$id).replace(rproxy, "$1") + }) + expr + dataType + var getter = evaluatorPool.get(exprId) //直接从缓存,免得重复生成 + if (getter) { + if (dataType === "duplex") { + var setter = evaluatorPool.get(exprId + "setter") + binding.setter = setter.apply(setter, binding.args) + } + return binding.getter = getter + } + + if (!assigns.length) { + assigns.push("fix" + expose) + } + + if (dataType === "duplex") { + var nameOne = {} + assigns.forEach(function (a) { + var arr = a.split("=") + nameOne[arr[0].trim()] = arr[1].trim() + }) + expr = expr.replace(/[\$\w]+/, function (a) { + return nameOne[a] ? nameOne[a] : a + }) + /* jshint ignore:start */ + var fn2 = scpCompile(names.concat("'use strict';" + + "return function(vvv){" + expr + " = vvv\n}\n")) + /* jshint ignore:end */ + evaluatorPool.put(exprId + "setter", fn2) + binding.setter = fn2.apply(fn2, binding.args) + } + + if (dataType === "on") { //事件绑定 + if (expr.indexOf("(") === -1) { + expr += ".call(this, $event)" + } else { + expr = expr.replace("(", ".call(this,") + } + names.push("$event") + expr = "\nreturn " + expr + ";" //IE全家 Function("return ")出错,需要Function("return ;") + var lastIndex = expr.lastIndexOf("\nreturn") + var header = expr.slice(0, lastIndex) + var footer = expr.slice(lastIndex) + expr = header + "\n" + footer + } else { + expr = "\nreturn " + expr + ";" //IE全家 Function("return ")出错,需要Function("return ;") + } + /* jshint ignore:start */ + getter = scpCompile(names.concat("'use strict';\nvar " + + assigns.join(",\n") + expr)) + /* jshint ignore:end */ + + return evaluatorPool.put(exprId, getter) +} + +function normalizeExpr(code) { + var hasExpr = rexpr.test(code) //比如:class="width{{w}}"的情况 + if (hasExpr) { + var array = scanExpr(code) + if (array.length === 1) { + return array[0].expr + } + return array.map(function (el) { + return el.type ? "(" + el.expr + ")" : quote(el.expr) + }).join(" + ") + } else { + return code + } +} + +yua.normalizeExpr = normalizeExpr +yua.parseExprProxy = parseExpr + +var rthimRightParentheses = /\)\s*$/ +var rthimOtherParentheses = /\)\s*\|/g +var rquoteFilterName = /\|\s*([$\w]+)/g +var rpatchBracket = /"\s*\["/g +var rthimLeftParentheses = /"\s*\(/g +function parseFilter(filters) { + filters = filters + .replace(rthimRightParentheses, "")//处理最后的小括号 + .replace(rthimOtherParentheses, function () {//处理其他小括号 + return "],|" + }) + .replace(rquoteFilterName, function (a, b) { //处理|及它后面的过滤器的名字 + return "[" + quote(b) + }) + .replace(rpatchBracket, function () { + return '"],["' + }) + .replace(rthimLeftParentheses, function () { + return '",' + }) + "]" + /* jshint ignore:start */ + return scpCompile(["return [" + filters + "]"])() + /* jshint ignore:end */ +} + + + + + + +/********************************************************************* + * 编译系统 * + **********************************************************************/ + +var quote = JSON.stringify + + + + + + + + + + + + +/********************************************************************* + * 扫描系统 * + **********************************************************************/ + +yua.scan = function (elem, vmodel) { + elem = elem || root + var vmodels = vmodel ? [].concat(vmodel) : [] + scanTag(elem, vmodels) +} + +//http://www.w3.org/TR/html5/syntax.html#void-elements +var stopScan = oneObject("area,base,basefont,br,col,command,embed,hr,img,input,link,meta,param,source,track,wbr,noscript,script,style,textarea".toUpperCase()) + +function checkScan(elem, callback, innerHTML) { + var id = setTimeout(function () { + var currHTML = elem.innerHTML + clearTimeout(id) + if (currHTML === innerHTML) { + callback() + } else { + checkScan(elem, callback, currHTML) + } + }) +} + +function createSignalTower(elem, vmodel) { + var id = elem.getAttribute("yuactrl") || vmodel.$id + elem.setAttribute("yuactrl", id) + if (vmodel.$events) { + vmodel.$events.expr = elem.tagName + '[yuactrl="' + id + '"]' + } +} + +function getBindingCallback(elem, name, vmodels) { + var callback = elem.getAttribute(name) + if (callback) { + for (var i = 0, vm; vm = vmodels[i++]; ) { + if (vm.hasOwnProperty(callback) && typeof vm[callback] === "function") { + return vm[callback] + } + } + } +} + +function executeBindings(bindings, vmodels) { + for (var i = 0, binding; binding = bindings[i++]; ) { + binding.vmodels = vmodels + directives[binding.type].init(binding) + + yua.injectBinding(binding) + if (binding.getter && binding.element.nodeType === 1) { //移除数据绑定,防止被二次解析 + //chrome使用removeAttributeNode移除不存在的特性节点时会报错 https://github.com/RubyLouvre/yua/issues/99 + binding.element.removeAttribute(binding.name) + } + } + bindings.length = 0 +} + +//https://github.com/RubyLouvre/yua/issues/636 +var mergeTextNodes = IEVersion && window.MutationObserver ? function (elem) { + var node = elem.firstChild, text + while (node) { + var aaa = node.nextSibling + if (node.nodeType === 3) { + if (text) { + text.nodeValue += node.nodeValue + elem.removeChild(node) + } else { + text = node + } + } else { + text = null + } + node = aaa + } +} : 0 +var roneTime = /^\s*::/ +var rmsAttr = /:(\w+)-?(.*)/ + +var events = oneObject("animationend,blur,change,input,click,dblclick,focus,keydown,keypress,keyup,mousedown,mouseenter,mouseleave,mousemove,mouseout,mouseover,mouseup,scan,scroll,submit") +var obsoleteAttrs = oneObject("value,title,alt,checked,selected,disabled,readonly,enabled,href,src") +function bindingSorter(a, b) { + return a.priority - b.priority +} + +var rnoCollect = /^(:\S+|data-\S+|on[a-z]+|id|style|class)$/ +var ronattr = /^on\-[\w-]+$/ +function getOptionsFromTag(elem, vmodels) { + var attributes = elem.attributes + var ret = {} + for (var i = 0, attr; attr = attributes[i++]; ) { + var name = attr.name + if (attr.specified && !rnoCollect.test(name)) { + var camelizeName = camelize(attr.name) + if (/^on\-[\w-]+$/.test(name)) { + ret[camelizeName] = getBindingCallback(elem, name, vmodels) + } else { + ret[camelizeName] = parseData(attr.value) + } + } + + } + return ret +} + +function scanAttr(elem, vmodels, match) { + var scanNode = true + if (vmodels.length) { + var attributes = elem.attributes + var bindings = [] + var uniq = {} + for (var i = 0, attr; attr = attributes[i++]; ) { + var name = attr.name + if (uniq[name]) {//IE8下:repeat,:with BUG + continue + } + uniq[name] = 1 + if (attr.specified) { + if (match = name.match(rmsAttr)) { + //如果是以指定前缀命名的 + var type = match[1] + var param = match[2] || "" + var value = attr.value + if (events[type]) { + param = type + type = "on" + } else if (obsoleteAttrs[type]) { + param = type + type = "attr" + name = ":" + type + "-" + param + log("warning!请改用" + name + "代替" + attr.name + "!") + } + if (directives[type]) { + var newValue = value.replace(roneTime, "") + var oneTime = value !== newValue + var binding = { + type: type, + param: param, + element: elem, + name: name, + expr: newValue, + oneTime: oneTime, + uuid: "_" + (++bindingID), + priority: (directives[type].priority || type.charCodeAt(0) * 10) + (Number(param.replace(/\D/g, "")) || 0) + } + if (type === "html" || type === "text") { + + var filters = getToken(value).filters + binding.expr = binding.expr.replace(filters, "") + binding.filters = filters.replace(rhasHtml, function () { + binding.type = "html" + binding.group = 1 + return "" + }).trim() // jshint ignore:line + } else if (type === "duplex") { + var hasDuplex = name + } else if (name === ":if-loop") { + binding.priority += 100 + } else if (name === ":attr-value") { + var hasAttrValue = name + } + bindings.push(binding) + } + } + } + } + if (bindings.length) { + bindings.sort(bindingSorter) + + if (hasDuplex && hasAttrValue && elem.type === "text") { + log("warning!一个控件不能同时定义:attr-value与" + hasDuplex) + } + + for (i = 0; binding = bindings[i]; i++) { + type = binding.type + if (rnoscanAttrBinding.test(type)) { + return executeBindings(bindings.slice(0, i + 1), vmodels) + } else if (scanNode) { + scanNode = !rnoscanNodeBinding.test(type) + } + } + executeBindings(bindings, vmodels) + } + } + if (scanNode && !stopScan[elem.tagName] && (isWidget(elem) ? elem.msResolved : 1)) { + mergeTextNodes && mergeTextNodes(elem) + scanNodeList(elem, vmodels) //扫描子孙元素 + } +} + +var rnoscanAttrBinding = /^if|widget|repeat$/ +var rnoscanNodeBinding = /^html|include$/ + +function scanNodeList(parent, vmodels) { + var nodes = yua.slice(parent.childNodes) + scanNodeArray(nodes, vmodels) +} + +function scanNodeArray(nodes, vmodels) { + function _delay_component(name) { + setTimeout(function () { + yua.component(name) + }) + } + for (var i = 0, node; node = nodes[i++]; ) { + switch (node.nodeType) { + case 1: + var elem = node + if (!elem.msResolved && elem.parentNode && elem.parentNode.nodeType === 1) { + var library = isWidget(elem) + if (library) { + var widget = elem.localName ? elem.localName.replace(library + ":", "") : elem.nodeName + var fullName = library + ":" + camelize(widget) + componentQueue.push({ + library: library, + element: elem, + fullName: fullName, + widget: widget, + vmodels: vmodels, + name: "widget" + }) + if (yua.components[fullName]) { + //确保所有:attr-name扫描完再处理 + _delay_component(fullName) + } + } + } + + scanTag(node, vmodels) //扫描元素节点 + + if (node.msHasEvent) { + yua.fireDom(node, "datasetchanged", { + bubble: node.msHasEvent + }) + } + + break + case 3: + if (rexpr.test(node.nodeValue)) { + scanText(node, vmodels, i) //扫描文本节点 + } + break + } + + } +} + +function scanTag(elem, vmodels, node) { + //扫描顺序 :skip(0) --> :important(1) --> :controller(2) --> :if(10) --> :repeat(100) + //--> :if-loop(110) --> :attr(970) ...--> :each(1400)-->:with(1500)--〉:duplex(2000)垫后 + var a = elem.getAttribute(":skip") + var b = elem.getAttributeNode(":important") + var c = elem.getAttributeNode(":controller") + if (typeof a === "string") { + return + } else if (node = b || c) { + + var newVmodel = yua.vmodels[node.value] + + if (!newVmodel) { + return + } + + //把父级VM补上 + newVmodel.$up = vmodels[0] + //:important不包含父VM,:controller相反 + vmodels = node === b ? [newVmodel] : [newVmodel].concat(vmodels) + + elem.removeAttribute(node.name) //removeAttributeNode不会刷新[:controller]样式规则 + elem.classList.remove(node.name) + createSignalTower(elem, newVmodel) + } + scanAttr(elem, vmodels) //扫描特性节点 + + if (newVmodel) { + setTimeout(function () { + newVmodel.$fire(":scan-end", elem) + }) + } +} +var rhasHtml = /\|\s*html(?:\b|$)/, + r11a = /\|\|/g, + rlt = /</g, + rgt = />/g, + rstringLiteral = /(['"])(\\\1|.)+?\1/g, + rline = /\r?\n/g +function getToken(value) { + if (value.indexOf("|") > 0) { + var scapegoat = value.replace(rstringLiteral, function (_) { + return Array(_.length + 1).join("1") // jshint ignore:line + }) + var index = scapegoat.replace(r11a, "\u1122\u3344").indexOf("|") //干掉所有短路或 + if (index > -1) { + return { + type: "text", + filters: value.slice(index).trim(), + expr: value.slice(0, index) + } + } + } + return { + type: "text", + expr: value, + filters: "" + } +} + +function scanExpr(str) { + var tokens = [], + value, start = 0, + stop + do { + stop = str.indexOf(openTag, start) + if (stop === -1) { + break + } + value = str.slice(start, stop) + if (value) { // {{ 左边的文本 + tokens.push({ + expr: value + }) + } + start = stop + openTag.length + stop = str.indexOf(closeTag, start) + if (stop === -1) { + break + } + value = str.slice(start, stop) + if (value) { //处理{{ }}插值表达式 + tokens.push(getToken(value.replace(rline,""))) + } + start = stop + closeTag.length + } while (1) + value = str.slice(start) + if (value) { //}} 右边的文本 + tokens.push({ + expr: value + }) + } + return tokens +} + +function scanText(textNode, vmodels, index) { + var bindings = [], + tokens = scanExpr(textNode.data) + if (tokens.length) { + for (var i = 0, token; token = tokens[i++];) { + var node = DOC.createTextNode(token.expr) //将文本转换为文本节点,并替换原来的文本节点 + if (token.type) { + token.expr = token.expr.replace(roneTime, function () { + token.oneTime = true + return "" + }) // jshint ignore:line + token.element = node + token.filters = token.filters.replace(rhasHtml, function () { + token.type = "html" + return "" + }) // jshint ignore:line + token.pos = index * 1000 + i + bindings.push(token) //收集带有插值表达式的文本 + } + yuaFragment.appendChild(node) + } + textNode.parentNode.replaceChild(yuaFragment, textNode) + if (bindings.length) + executeBindings(bindings, vmodels) + } +} + +//使用来自游戏界的双缓冲技术,减少对视图的冗余刷新 +var Buffer = function () { + this.queue = [] +} +Buffer.prototype = { + render: function (isAnimate) { + if (!this.locked) { + this.locked = isAnimate ? root.offsetHeight + 10 : 1 + var me = this + yua.nextTick(function () { + me.flush() + }) + } + }, + flush: function () { + for (var i = 0, sub; sub = this.queue[i++]; ) { + sub.update && sub.update() + } + this.locked = 0 + this.queue = [] + } +} + +var buffer = new Buffer() + +var componentQueue = [] +var widgetList = [] +var componentHooks = { + $construct: function () { + return yua.mix.apply(null, arguments) + }, + $ready: noop, + $init: noop, + $dispose: noop, + $container: null, + $childReady: noop, + $replace: false, + $extend: null, + $$template: function (str) { + return str + } +} + +yua.components = {} +yua.component = function (name, opts) { + if (opts) { + yua.components[name] = yua.mix({}, componentHooks, opts) + } + for (var i = 0, obj; obj = componentQueue[i]; i++) { + if (name === obj.fullName) { + componentQueue.splice(i, 1) + i--; + + (function (host, hooks, elem, widget) { + //如果elem已从Document里移除,直接返回 + //issuse : https://github.com/RubyLouvre/yua2/issues/40 + if (!yua.contains(DOC, elem) || elem.msResolved) { + yua.Array.remove(componentQueue, host) + return + } + + var dependencies = 1 + var library = host.library + var global = yua.libraries[library] || componentHooks + + //===========收集各种配置======= + if (elem.getAttribute(":attr-identifier")) { + //如果还没有解析完,就延迟一下 #1155 + return + } + var elemOpts = getOptionsFromTag(elem, host.vmodels) + var vmOpts = getOptionsFromVM(host.vmodels, elemOpts.config || host.fullName) + var $id = elemOpts.$id || elemOpts.identifier || generateID(widget) + delete elemOpts.config + delete elemOpts.$id + delete elemOpts.identifier + var componentDefinition = {} + + var parentHooks = yua.components[hooks.$extend] + if (parentHooks) { + yua.mix(true, componentDefinition, parentHooks) + componentDefinition = parentHooks.$construct.call(elem, componentDefinition, {}, {}) + } else { + yua.mix(true, componentDefinition, hooks) + } + componentDefinition = yua.components[name].$construct.call(elem, componentDefinition, vmOpts, elemOpts) + + componentDefinition.$refs = {} + componentDefinition.$id = $id + + //==========构建VM========= + var keepSlot = componentDefinition.$slot + var keepReplace = componentDefinition.$replace + var keepContainer = componentDefinition.$container + var keepTemplate = componentDefinition.$template + delete componentDefinition.$slot + delete componentDefinition.$replace + delete componentDefinition.$container + delete componentDefinition.$construct + + var vmodel = yua.define(componentDefinition) || {} + elem.msResolved = 1 //防止二进扫描此元素 + vmodel.$init(vmodel, elem) + global.$init(vmodel, elem) + var nodes = elem.childNodes + //收集插入点 + var slots = {}, snode + for (var s = 0, el; el = nodes[s++]; ) { + var type = el.nodeType === 1 && el.getAttribute("slot") || keepSlot + if (type) { + if (slots[type]) { + slots[type].push(el) + } else { + slots[type] = [el] + } + } + } + + if (vmodel.$$template) { + yua.clearHTML(elem) + elem.innerHTML = vmodel.$$template(keepTemplate) + } + for (s in slots) { + if (vmodel.hasOwnProperty(s)) { + var ss = slots[s] + if (ss.length) { + var fragment = yuaFragment.cloneNode(true) + for (var ns = 0; snode = ss[ns++]; ) { + fragment.appendChild(snode) + } + vmodel[s] = fragment + } + slots[s] = null + } + } + slots = null + var child = elem.children[0] || elem.firstChild + if (keepReplace) { + elem.parentNode.replaceChild(child, elem) + child.msResolved = 1 + var cssText = elem.style.cssText + var className = elem.className + elem = host.element = child + elem.style.cssText += ";"+ cssText + if (className) { + yua(elem).addClass(className) + } + } + if (keepContainer) { + keepContainer.appendChild(elem) + } + yua.fireDom(elem, "datasetchanged", + {library: library, vm: vmodel, childReady: 1}) + var children = 0 + var removeFn = yua.bind(elem, "datasetchanged", function (e) { + if (e.childReady && e.library === library) { + dependencies += e.childReady + if (vmodel !== e.vm) { + vmodel.$refs[e.vm.$id] = e.vm + if (e.childReady === -1) { + children++ + vmodel.$childReady(vmodel, elem, e) + } + e.stopPropagation() + } + } + if (dependencies === 0) { + var id1 = setTimeout(function () { + clearTimeout(id1) + + vmodel.$ready(vmodel, elem, host.vmodels) + global.$ready(vmodel, elem, host.vmodels) + }, children ? Math.max(children * 17, 100) : 17) + yua.unbind(elem, "datasetchanged", removeFn) + //================== + host.rollback = function () { + try { + vmodel.$dispose(vmodel, elem) + global.$dispose(vmodel, elem) + } catch (e) { + } + delete yua.vmodels[vmodel.$id] + } + injectDisposeQueue(host, widgetList) + if (window.chrome) { + elem.addEventListener("DOMNodeRemovedFromDocument", function () { + setTimeout(rejectDisposeQueue) + }) + } + + } + }) + scanTag(elem, [vmodel].concat(host.vmodels)) + yua.vmodels[vmodel.$id] = vmodel + if (!elem.childNodes.length) { + yua.fireDom(elem, "datasetchanged", {library: library, vm: vmodel, childReady: -1}) + } else { + var id2 = setTimeout(function () { + clearTimeout(id2) + yua.fireDom(elem, "datasetchanged", {library: library, vm: vmodel, childReady: -1}) + }, 17) + } + + })(obj, yua.components[name], obj.element, obj.widget)// jshint ignore:line + + } + } +} + + +function getOptionsFromVM(vmodels, pre) { + if (pre) { + for (var i = 0, v; v = vmodels[i++]; ) { + if (v.hasOwnProperty(pre) && typeof v[pre] === "object") { + var vmOptions = v[pre] + return vmOptions.$model || vmOptions + break + } + } + } + return {} +} + +yua.libraries = [] +yua.library = function (name, opts) { + if (DOC.namespaces) { + DOC.namespaces.add(name, 'http://www.w3.org/1999/xhtml'); + } + yua.libraries[name] = yua.mix({ + $init: noop, + $ready: noop, + $dispose: noop + }, opts || {}) +} + +yua.library("ms") +yua.library("do") + + + + + + + + + + + +/* + broswer nodeName scopeName localName + IE9 ONI:BUTTON oni button + IE10 ONI:BUTTON undefined oni:button + IE8 button oni undefined + chrome ONI:BUTTON undefined oni:button + + */ +function isWidget(el) { //如果为自定义标签,返回UI库的名字 + if (el.scopeName && el.scopeName !== "HTML") { + return el.scopeName + } + var fullName = el.nodeName.toLowerCase() + var index = fullName.indexOf(":") + if (index > 0) { + return fullName.slice(0, index) + } +} +//各种MVVM框架在大型表格下的性能测试 +// https://github.com/RubyLouvre/yua/issues/859 + +var bools = ["autofocus,autoplay,async,allowTransparency,checked,controls", + "declare,disabled,defer,defaultChecked,defaultSelected", + "contentEditable,isMap,loop,multiple,noHref,noResize,noShade", + "open,readOnly,selected" +].join(",") +var boolMap = {} +bools.replace(rword, function (name) { + boolMap[name.toLowerCase()] = name +}) + +var propMap = {//属性名映射 + "accept-charset": "acceptCharset", + "char": "ch", + "charoff": "chOff", + "class": "className", + "for": "htmlFor", + "http-equiv": "httpEquiv" +} + +var anomaly = ["accessKey,bgColor,cellPadding,cellSpacing,codeBase,codeType,colSpan", + "dateTime,defaultValue,frameBorder,longDesc,maxLength,marginWidth,marginHeight", + "rowSpan,tabIndex,useMap,vSpace,valueType,vAlign" +].join(",") +anomaly.replace(rword, function (name) { + propMap[name.toLowerCase()] = name +}) + + + + + + + + + + + + + + + + + + + + + + + + + + + +var attrDir = yua.directive("attr", { + init: function (binding) { + //{{aaa}} --> aaa + //{{aaa}}/bbb.html --> (aaa) + "/bbb.html" + binding.expr = normalizeExpr(binding.expr.trim()) + if (binding.type === "include") { + var elem = binding.element + effectBinding(elem, binding) + binding.includeRendered = getBindingCallback(elem, "data-include-rendered", binding.vmodels) + binding.includeLoaded = getBindingCallback(elem, "data-include-loaded", binding.vmodels) + var outer = binding.includeReplace = !!yua(elem).data("includeReplace") + if (yua(elem).data("includeCache")) { + binding.templateCache = {} + } + binding.start = DOC.createComment(":include") + binding.end = DOC.createComment(":include-end") + if (outer) { + binding.element = binding.end + binding._element = elem + elem.parentNode.insertBefore(binding.start, elem) + elem.parentNode.insertBefore(binding.end, elem.nextSibling) + } else { + elem.insertBefore(binding.start, elem.firstChild) + elem.appendChild(binding.end) + } + } + }, + update: function (val) { + + var elem = this.element + var obj = val + + if(typeof obj === 'object' && obj !== null){ + if(!yua.isPlainObject(obj)) + obj = obj.$model + }else{ + if(!this.param) + return + + obj = {} + obj[this.param] = val + } + + for(var i in obj){ + if(i === 'href' || i === 'src'){ + //处理IE67自动转义的问题 + if(!root.hasAttribute) + obj[i] = obj[i].replace(/&/g, '&') + + elem[i] = obj[i] + + //chrome v37- 下embed标签动态设置的src,无法发起请求 + if(window.chrome && elem.tagName === 'EMBED'){ + var parent = elem.parentNode + var com = document.createComment(':src') + parent.replaceChild(com, elem) + parent.replaceChild(elem, com) + } + }else{ + var k = i + //古董IE下,部分属性名字要进行映射 + if(!W3C && propMap[k]) + k = propMap[k] + + if(typeof elem[boolMap[k]] === 'boolean'){ + //布尔属性必须使用el.xxx = true|false方式设值 + elem[boolMap[k]] = !!obj[i] + + //如果为false, IE全系列下相当于setAttribute(xxx, ''),会影响到样式,需要进一步处理 + if(!obj[i]) + obj[i] = !!obj[i] + } + if(obj[i] === false || obj[i] === null || obj[i] === undefined) + return elem.removeAttribute(k) + + //SVG只能使用setAttribute(xxx, yyy), VML只能使用elem.xxx = yyy ,HTML的固有属性必须elem.xxx = yyy + var isInnate = rsvg.test(elem) ? false : (DOC.namespaces && isVML(elem)) ? true : k in elem.cloneNode(false) + if (isInnate) { + elem[k] = obj[i] + } else { + elem.setAttribute(k, obj[i]) + } + } + } + } +}) + +//这几个指令都可以使用插值表达式,如:src="aaa/{{b}}/{{c}}.html" +"title,alt,src,value,css,include,href,data".replace(rword, function (name) { + directives[name] = attrDir +}) + +//类名定义, :class="xx:yy" :class="{xx: yy}" :class="xx" :class="{{xx}}" +yua.directive("class", { + init: function (binding) { + if(!/^\{.*\}$/.test(binding.expr)){ + + var expr = binding.expr.split(':') + expr[1] = expr[1] && expr[1].trim() || 'true' + var arr = expr[0].split(/\s+/) + binding.expr = '{' + arr.map(function(it){ + return it + ': ' + expr[1] + }).join(', ') + '}' + + }else if(/^\{\{.*\}\}$/.test(binding.expr)){ + + binding.expr = binding.expr.slice(2, -2) + } + + if (binding.type === "hover" || binding.type === "active") { //确保只绑定一次 + if (!binding.hasBindEvent) { + var elem = binding.element + var $elem = yua(elem) + var activate = "mouseenter" //在移出移入时切换类名 + var abandon = "mouseleave" + if (binding.type === "active") { //在聚焦失焦中切换类名 + elem.tabIndex = elem.tabIndex || -1 + activate = "mousedown" + abandon = "mouseup" + var fn0 = $elem.bind("mouseleave", function () { + binding.toggleClass && $elem.removeClass(binding.newClass) + }) + } + } + + var fn1 = $elem.bind(activate, function () { + binding.toggleClass && $elem.addClass(binding.newClass) + }) + var fn2 = $elem.bind(abandon, function () { + binding.toggleClass && $elem.removeClass(binding.newClass) + }) + binding.rollback = function () { + $elem.unbind("mouseleave", fn0) + $elem.unbind(activate, fn1) + $elem.unbind(abandon, fn2) + } + binding.hasBindEvent = true + } + + }, + update: function (val) { + var obj = val + if(!obj) + return log('class绑定错误') + + if(typeof obj === 'string'){ + obj = {} + obj[val] = true + } + + if(!yua.isPlainObject(obj)){ + obj = obj.$model + } + + if(this.param) + return log('不再支持:class-xx="yy"的写法', this.name) + + var $elem = yua(this.element) + for(var i in obj){ + $elem.toggleClass(i, !!obj[i]) + } + } +}) + +"hover,active".replace(rword, function (name) { + directives[name] = directives["class"] +}) + +//样式定义 :css-width="200" +//:css="{width: 200}" +yua.directive("css", { + init: directives.attr.init, + update: function (val) { + var $elem = yua(this.element) + if(!this.param){ + var obj = val + try{ + if(typeof val === 'object'){ + if(!yua.isPlainObject(val)) + obj = val.$model + }else{ + obj = new Function('return ' + val)() + } + for(var i in obj){ + $elem.css(i, obj[i]) + } + }catch(err){ + log('样式格式错误', val) + } + }else{ + $elem.css(this.param, val) + } + } +}) + +//兼容2种写法 :data-xx="yy", :data="{xx: yy}" +yua.directive("data", { + priority: 100, + update: function (val) { + var obj = val + if(typeof obj === 'object' && obj !== null){ + if(!yua.isPlainObject(obj)) + obj = val.$model + + for(var i in obj){ + this.element.setAttribute('data-' + i, obj[i]) + } + }else{ + if(!this.param) + return + + this.element.setAttribute('data-' + this.param, obj) + } + } +}) + + + +/*------ 表单验证 -------*/ +var __rules = {}; +yua.validate = function(key){ + return !__rules[key] || __rules[key].every(function(it){ return it.checked}) +}; +yua.directive('rule', { + priority: 2010, + init: function(binding){ + if(binding.param && !__rules[binding.param]){ + __rules[binding.param] = []; + binding.target = __rules[binding.param] + } + }, + update: function(obj){ + var _this = this, + elem = this.element, + ruleID = -1; + + if(!['INPUT', 'TEXTAREA'].includes(elem.nodeName)) + return + + if(this.target){ + ruleID = this.target.length; + this.target.push({checked: true}) + } + + //如果父级元素没有定位属性,则加上相对定位 + if(getComputedStyle(elem.parentNode).position === 'static'){ + elem.parentNode.style.position = 'relative' + } + + var $elem = yua(elem), + ol = elem.offsetLeft + elem.offsetWidth - 50, + ot = elem.offsetTop + elem.offsetHeight + 8, + tips = document.createElement('div'); + + tips.className = 'do-rule-tips' + tips.style.left = ol + 'px' + tips.style.bottom = ot + 'px' + + + function checked(ev){ + var txt = '', + val = elem.value; + + if(obj.require && (val === '' || val === null)) + txt = '必填项' + + if(!txt && obj.isNumeric) + txt = !isFinite(val) ? '必须为合法数字' : '' + + if(!txt && obj.isEmail) + txt = !/^[\w\.\-]+@\w+([\.\-]\w+)*\.\w+$/.test(val) ? 'Email格式错误' : '' + + if(!txt && obj.isPhone) + txt = !/^1[34578]\d{9}$/.test(val) ? '手机格式错误' : '' + + if(!txt && obj.isCN) + txt = !/^[\u4e00-\u9fa5]+$/.test(val) ? '必须为纯中文' : '' + + if(!txt && obj.exp) + txt = !obj.exp.test(val) ? (obj.msg || '格式错误') : '' + + if(!txt && obj.maxLen) + txt = val.length > obj.maxLen ? ('长度不得超过' + obj.maxLen + '位') : '' + + if(!txt && obj.minLen) + txt = val.length < obj.minLen ? ('长度不得小于' + obj.minLen + '位') : '' + + if(!txt && obj.hasOwnProperty('max')) + txt = val > obj.max ? ('输入值不能大于' + obj.max) : '' + + if(!txt && obj.hasOwnProperty('min')) + txt = val < obj.min ? ('输入值不能小于' + obj.min) : '' + + if(!txt && obj.eq){ + var eqEl = document.querySelector('#' + obj.eq) + txt = val !== eqEl.value ? (obj.msg || '2次值不一致') : '' + } + + + if(txt){ + if(ev){ + tips.textContent = txt + elem.parentNode.appendChild(tips) + } + //必须是"必填项"才会更新验证状态 + if(_this.target && obj.require){ + _this.target[ruleID].checked = false + } + }else{ + if(_this.target){ + _this.target[ruleID].checked = true + } + try{ + elem.parentNode.removeChild(tips) + }catch(err){} + } + } + + + $elem.bind('change,blur', checked) + $elem.bind('focus', function(ev){ + try{ + elem.parentNode.removeChild(tips) + }catch(err){} + + }) + + checked() + } +}) + + +//双工绑定 +var rduplexType = /^(?:checkbox|radio)$/ +var rduplexParam = /^(?:radio|checked)$/ +var rnoduplexInput = /^(file|button|reset|submit|checkbox|radio|range)$/ +var duplexBinding = yua.directive("duplex", { + priority: 2000, + init: function (binding, hasCast) { + var elem = binding.element + var vmodels = binding.vmodels + binding.changed = getBindingCallback(elem, "data-duplex-changed", vmodels) || noop + var params = [] + var casting = oneObject("string,number,boolean,checked") + if (elem.type === "radio" && binding.param === "") { + binding.param = "checked" + } + + binding.param.replace(rw20g, function (name) { + if (rduplexType.test(elem.type) && rduplexParam.test(name)) { + if (name === "radio") + log(":duplex-radio已经更名为:duplex-checked") + name = "checked" + binding.isChecked = true + binding.xtype = "radio" + } + if (name === "bool") { + name = "boolean" + log(":duplex-bool已经更名为:duplex-boolean") + } else if (name === "text") { + name = "string" + log(":duplex-text已经更名为:duplex-string") + } + if (casting[name]) { + hasCast = true + } + yua.Array.ensure(params, name) + }) + if (!hasCast) { + params.push("string") + } + binding.param = params.join("-") + if (!binding.xtype) { + binding.xtype = elem.tagName === "SELECT" ? "select" : + elem.type === "checkbox" ? "checkbox" : + elem.type === "radio" ? "radio" : + /^change/.test(elem.getAttribute("data-duplex-event")) ? "change" : + "input" + } + //===================绑定事件====================== + var bound = binding.bound = function (type, callback) { + elem.addEventListener(type, callback, false) + var old = binding.rollback + binding.rollback = function () { + elem.yuaSetter = null + yua.unbind(elem, type, callback) + old && old() + } + } + function callback(value) { + binding.changed.call(this, value, binding) + } + var composing = false + function compositionStart() { + composing = true + } + function compositionEnd() { + composing = false + setTimeout(updateVModel) + } + var updateVModel = function (e) { + + var val = elem.value; + //防止递归调用形成死循环 + //处理中文输入法在minlengh下引发的BUG + if (composing || val === binding.oldValue || binding.pipe === null){ + return + } + + var lastValue = binding.pipe(val, binding, "get") + binding.oldValue = val + binding.setter(lastValue) + + callback.call(elem, lastValue) + yua.fireDom(elem, 'change') + + } + switch (binding.xtype) { + case "radio": + bound("click", function () { + var lastValue = binding.pipe(elem.value, binding, "get") + binding.setter(lastValue) + callback.call(elem, lastValue) + }) + break + case "checkbox": + bound("change", function () { + var method = elem.checked ? "ensure" : "remove" + var array = binding.getter.apply(0, binding.vmodels) + if (!Array.isArray(array)) { + log(":duplex应用于checkbox上要对应一个数组") + array = [array] + } + var val = binding.pipe(elem.value, binding, "get") + yua.Array[method](array, val) + callback.call(elem, array) + }) + break + case "change": + bound("change", updateVModel) + break + case "input": + bound("input", updateVModel) + bound("keyup", updateVModel) + if (!IEVersion) { + bound("compositionstart", compositionStart) + bound("compositionend", compositionEnd) + bound("DOMAutoComplete", updateVModel) + } + break + case "select": + bound("change", function () { + var val = yua(elem).val() //字符串或字符串数组 + if (Array.isArray(val)) { + val = val.map(function (v) { + return binding.pipe(v, binding, "get") + }) + } else { + val = binding.pipe(val, binding, "get") + } + if (val + "" !== binding.oldValue) { + try { + binding.setter(val) + } catch (ex) { + log(ex) + } + } + }) + bound("datasetchanged", function (e) { + if (e.bubble === "selectDuplex") { + var value = binding._value + var curValue = Array.isArray(value) ? value.map(String) : value + "" + yua(elem).val(curValue) + elem.oldValue = curValue + "" + callback.call(elem, curValue) + } + }) + break + } + if (binding.xtype === "input" && !rnoduplexInput.test(elem.type)) { + if (elem.type !== "hidden") { + bound("focus", function () { + elem.msFocus = true + }) + bound("blur", function () { + elem.msFocus = false + }) + } + elem.yuaSetter = updateVModel //#765 + watchValueInTimer(function () { + if (root.contains(elem)) { + if (!elem.msFocus) { + updateVModel() + } + } else if (!elem.msRetain) { + return false + } + }) + } + + }, + update: function (value) { + var elem = this.element, binding = this, curValue + if (!this.init) { + for (var i in yua.vmodels) { + var v = yua.vmodels[i] + v.$fire("yua-duplex-init", binding) + } + var cpipe = binding.pipe || (binding.pipe = pipe) + cpipe(null, binding, "init") + this.init = 1 + } + switch (this.xtype) { + case "input": + case "change": + curValue = this.pipe(value, this, "set") //fix #673 + if (curValue !== this.oldValue) { + var fixCaret = false + if (elem.msFocus) { + try { + var start = elem.selectionStart + var end = elem.selectionEnd + if (start === end) { + var pos = start + fixCaret = true + } + } catch (e) { + } + } + elem.value = this.oldValue = curValue + if (fixCaret && !elem.readOnly) { + elem.selectionStart = elem.selectionEnd = pos + } + } + break + case "radio": + curValue = binding.isChecked ? !!value : value + "" === elem.value + elem.checked = curValue + break + case "checkbox": + var array = [].concat(value) //强制转换为数组 + curValue = this.pipe(elem.value, this, "get") + elem.checked = array.indexOf(curValue) > -1 + break + case "select": + //必须变成字符串后才能比较 + binding._value = value + if (!elem.msHasEvent) { + elem.msHasEvent = "selectDuplex" + //必须等到其孩子准备好才触发 + } else { + yua.fireDom(elem, "datasetchanged", { + bubble: elem.msHasEvent + }) + } + break + } + } +}) + + +function fixNull(val) { + return val == null ? "" : val +} +yua.duplexHooks = { + checked: { + get: function (val, binding) { + return !binding.oldValue + } + }, + string: { + get: function (val) { //同步到VM + return val + }, + set: fixNull + }, + "boolean": { + get: function (val) { + return val === "true" + }, + set: fixNull + }, + number: { + get: function (val, binding) { + var number = val - 0 + if (-val === -number) { + return number + } + var arr = /strong|medium|weak/.exec(binding.element.getAttribute("data-duplex-number")) || ["medium"] + switch (arr[0]) { + case "strong": + return 0 + case "medium": + return val === "" ? "" : 0 + case "weak": + return val + } + }, + set: fixNull + } +} + +function pipe(val, binding, action, e) { + binding.param.replace(rw20g, function (name) { + var hook = yua.duplexHooks[name] + if (hook && typeof hook[action] === "function") { + val = hook[action](val, binding) + } + }) + return val +} + +var TimerID, ribbon = [] + +yua.tick = function (fn) { + if (ribbon.push(fn) === 1) { + TimerID = setInterval(ticker, 60) + } +} + +function ticker() { + for (var n = ribbon.length - 1; n >= 0; n--) { + var el = ribbon[n] + if (el() === false) { + ribbon.splice(n, 1) + } + } + if (!ribbon.length) { + clearInterval(TimerID) + } +} + +var watchValueInTimer = noop +new function () { // jshint ignore:line + try { //#272 IE9-IE11, firefox + var setters = {} + var aproto = HTMLInputElement.prototype + var bproto = HTMLTextAreaElement.prototype + function newSetter(value) { // jshint ignore:line + setters[this.tagName].call(this, value) + if (!this.msFocus && this.yuaSetter) { + this.yuaSetter() + } + } + var inputProto = HTMLInputElement.prototype + Object.getOwnPropertyNames(inputProto) //故意引发IE6-8等浏览器报错 + setters["INPUT"] = Object.getOwnPropertyDescriptor(aproto, "value").set + + Object.defineProperty(aproto, "value", { + set: newSetter + }) + setters["TEXTAREA"] = Object.getOwnPropertyDescriptor(bproto, "value").set + Object.defineProperty(bproto, "value", { + set: newSetter + }) + } catch (e) { + //在chrome 43中 :duplex终于不需要使用定时器实现双向绑定了 + // http://updates.html5rocks.com/2015/04/DOM-attributes-now-on-the-prototype + // https://docs.google.com/document/d/1jwA8mtClwxI-QJuHT7872Z0pxpZz8PBkf2bGAbsUtqs/edit?pli=1 + watchValueInTimer = yua.tick + } +} // jshint ignore:line + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +/*-------------动画------------*/ + +yua.directive("effect", { + priority: 5, + init: function (binding) { + var text = binding.expr, + className, + rightExpr + var colonIndex = text.replace(rexprg, function (a) { + return a.replace(/./g, "0") + }).indexOf(":") //取得第一个冒号的位置 + if (colonIndex === -1) { // 比如 :class/effect="aaa bbb ccc" 的情况 + className = text + rightExpr = true + } else { // 比如 :class/effect-1="ui-state-active:checked" 的情况 + className = text.slice(0, colonIndex) + rightExpr = text.slice(colonIndex + 1) + } + if (!rexpr.test(text)) { + className = quote(className) + } else { + className = normalizeExpr(className) + } + binding.expr = "[" + className + "," + rightExpr + "]" + }, + update: function (arr) { + var name = arr[0] + var elem = this.element + if (elem.getAttribute("data-effect-name") === name) { + return + } else { + elem.removeAttribute("data-effect-driver") + } + var inlineStyles = elem.style + var computedStyles = window.getComputedStyle ? window.getComputedStyle(elem) : null + var useAni = false + if (computedStyles && (supportTransition || supportAnimation)) { + + //如果支持CSS动画 + var duration = inlineStyles[transitionDuration] || computedStyles[transitionDuration] + if (duration && duration !== '0s') { + elem.setAttribute("data-effect-driver", "t") + useAni = true + } + + if (!useAni) { + + duration = inlineStyles[animationDuration] || computedStyles[animationDuration] + if (duration && duration !== '0s') { + elem.setAttribute("data-effect-driver", "a") + useAni = true + } + + } + } + + if (!useAni) { + if (yua.effects[name]) { + elem.setAttribute("data-effect-driver", "j") + useAni = true + } + } + if (useAni) { + elem.setAttribute("data-effect-name", name) + } + } +}) + +yua.effects = {} +yua.effect = function (name, callbacks) { + yua.effects[name] = callbacks +} + + + +var supportTransition = false +var supportAnimation = false + +var transitionEndEvent +var animationEndEvent +var transitionDuration = yua.cssName("transition-duration") +var animationDuration = yua.cssName("animation-duration") +new function () {// jshint ignore:line + var checker = { + 'TransitionEvent': 'transitionend', + 'WebKitTransitionEvent': 'webkitTransitionEnd', + 'OTransitionEvent': 'oTransitionEnd', + 'otransitionEvent': 'otransitionEnd' + } + var tran + //有的浏览器同时支持私有实现与标准写法,比如webkit支持前两种,Opera支持1、3、4 + for (var name in checker) { + if (window[name]) { + tran = checker[name] + break; + } + try { + var a = document.createEvent(name); + tran = checker[name] + break; + } catch (e) { + } + } + if (typeof tran === "string") { + supportTransition = true + transitionEndEvent = tran + } + + //大致上有两种选择 + //IE10+, Firefox 16+ & Opera 12.1+: animationend + //Chrome/Safari: webkitAnimationEnd + //http://blogs.msdn.com/b/davrous/archive/2011/12/06/introduction-to-css3-animat ions.aspx + //IE10也可以使用MSAnimationEnd监听,但是回调里的事件 type依然为animationend + // el.addEventListener("MSAnimationEnd", function(e) { + // alert(e.type)// animationend!!! + // }) + checker = { + 'AnimationEvent': 'animationend', + 'WebKitAnimationEvent': 'webkitAnimationEnd' + } + var ani; + for (name in checker) { + if (window[name]) { + ani = checker[name]; + break; + } + } + if (typeof ani === "string") { + supportTransition = true + animationEndEvent = ani + } + +}() + +var effectPool = []//重复利用动画实例 +function effectFactory(el, opts) { + if (!el || el.nodeType !== 1) { + return null + } + if (opts) { + var name = opts.effectName + var driver = opts.effectDriver + } else { + name = el.getAttribute("data-effect-name") + driver = el.getAttribute("data-effect-driver") + } + if (!name || !driver) { + return null + } + + var instance = effectPool.pop() || new Effect() + instance.el = el + instance.driver = driver + instance.useCss = driver !== "j" + if (instance.useCss) { + opts && yua(el).addClass(opts.effectClass) + instance.cssEvent = driver === "t" ? transitionEndEvent : animationEndEvent + } + instance.name = name + instance.callbacks = yua.effects[name] || {} + + return instance + + +} + +function effectBinding(elem, binding) { + var name = elem.getAttribute("data-effect-name") + if (name) { + binding.effectName = name + binding.effectDriver = elem.getAttribute("data-effect-driver") + var stagger = +elem.getAttribute("data-effect-stagger") + binding.effectLeaveStagger = +elem.getAttribute("data-effect-leave-stagger") || stagger + binding.effectEnterStagger = +elem.getAttribute("data-effect-enter-stagger") || stagger + binding.effectClass = elem.className || NaN + } +} +function upperFirstChar(str) { + return str.replace(/^[\S]/g, function (m) { + return m.toUpperCase() + }) +} +var effectBuffer = new Buffer() +function Effect() { +}//动画实例,做成类的形式,是为了共用所有原型方法 + +Effect.prototype = { + contrustor: Effect, + enterClass: function () { + return getEffectClass(this, "enter") + }, + leaveClass: function () { + return getEffectClass(this, "leave") + }, + // 共享一个函数 + actionFun: function (name, before, after) { + if (document.hidden) { + return + } + var me = this + var el = me.el + var isLeave = name === "leave" + name = isLeave ? "leave" : "enter" + var oppositeName = isLeave ? "enter" : "leave" + callEffectHook(me, "abort" + upperFirstChar(oppositeName)) + callEffectHook(me, "before" + upperFirstChar(name)) + if (!isLeave) + before(el) //这里可能做插入DOM树的操作,因此必须在修改类名前执行 + var cssCallback = function (cancel) { + el.removeEventListener(me.cssEvent, me.cssCallback) + if (isLeave) { + before(el) //这里可能做移出DOM树操作,因此必须位于动画之后 + yua(el).removeClass(me.cssClass) + } else { + if (me.driver === "a") { + yua(el).removeClass(me.cssClass) + } + } + if (cancel !== true) { + callEffectHook(me, "after" + upperFirstChar(name)) + after && after(el) + } + me.dispose() + } + if (me.useCss) { + if (me.cssCallback) { //如果leave动画还没有完成,立即完成 + me.cssCallback(true) + } + + me.cssClass = getEffectClass(me, name) + me.cssCallback = cssCallback + + me.update = function () { + el.addEventListener(me.cssEvent, me.cssCallback) + if (!isLeave && me.driver === "t") {//transtion延迟触发 + yua(el).removeClass(me.cssClass) + } + } + yua(el).addClass(me.cssClass)//animation会立即触发 + + effectBuffer.render(true) + effectBuffer.queue.push(me) + + } else { + callEffectHook(me, name, cssCallback) + + } + }, + enter: function (before, after) { + this.actionFun.apply(this, ["enter"].concat(yua.slice(arguments))) + + }, + leave: function (before, after) { + this.actionFun.apply(this, ["leave"].concat(yua.slice(arguments))) + + }, + dispose: function () {//销毁与回收到池子中 + this.update = this.cssCallback = null + if (effectPool.unshift(this) > 100) { + effectPool.pop() + } + } + + +} + + +function getEffectClass(instance, type) { + var a = instance.callbacks[type + "Class"] + if (typeof a === "string") + return a + if (typeof a === "function") + return a() + return instance.name + "-" + type +} + + +function callEffectHook(effect, name, cb) { + var hook = effect.callbacks[name] + if (hook) { + hook.call(effect, effect.el, cb) + } +} + +var applyEffect = function (el, dir/*[before, [after, [opts]]]*/) { + var args = aslice.call(arguments, 0) + if (typeof args[2] !== "function") { + args.splice(2, 0, noop) + } + if (typeof args[3] !== "function") { + args.splice(3, 0, noop) + } + var before = args[2] + var after = args[3] + var opts = args[4] + var effect = effectFactory(el, opts) + if (!effect) { + before() + after() + return false + } else { + var method = dir ? 'enter' : 'leave' + effect[method](before, after) + } +} + +yua.mix(yua.effect, { + apply: applyEffect, + append: function (el, parent, after, opts) { + return applyEffect(el, 1, function () { + parent.appendChild(el) + }, after, opts) + }, + before: function (el, target, after, opts) { + return applyEffect(el, 1, function () { + target.parentNode.insertBefore(el, target) + }, after, opts) + }, + remove: function (el, parent, after, opts) { + return applyEffect(el, 0, function () { + if (el.parentNode === parent) + parent.removeChild(el) + }, after, opts) + } +}) + + + + + + + + + + + + + + + + + + + + + + +yua.directive("html", { + update: function (val) { + var binding = this + var elem = this.element + var isHtmlFilter = elem.nodeType !== 1 + var parent = isHtmlFilter ? elem.parentNode : elem + if (!parent) + return + val = val == null ? "" : val + + if (elem.nodeType === 3) { + var signature = generateID("html") + parent.insertBefore(DOC.createComment(signature), elem) + binding.element = DOC.createComment(signature + ":end") + parent.replaceChild(binding.element, elem) + elem = binding.element + } + if (typeof val !== "object") {//string, number, boolean + var fragment = yua.parseHTML(String(val)) + } else if (val.nodeType === 11) { //将val转换为文档碎片 + fragment = val + } else if (val.nodeType === 1 || val.item) { + var nodes = val.nodeType === 1 ? val.childNodes : val.item + fragment = yuaFragment.cloneNode(true) + while (nodes[0]) { + fragment.appendChild(nodes[0]) + } + } + + nodes = yua.slice(fragment.childNodes) + //插入占位符, 如果是过滤器,需要有节制地移除指定的数量,如果是html指令,直接清空 + if (isHtmlFilter) { + var endValue = elem.nodeValue.slice(0, -4) + while (true) { + var node = elem.previousSibling + if (!node || node.nodeType === 8 && node.nodeValue === endValue) { + break + } else { + parent.removeChild(node) + } + } + parent.insertBefore(fragment, elem) + } else { + yua.clearHTML(elem).appendChild(fragment) + } + scanNodeArray(nodes, binding.vmodels) + } +}) + +yua.directive("if", { + priority: 10, + update: function (val) { + var binding = this + var elem = this.element + var stamp = binding.stamp = +(new Date()) + var par + var after = function () { + if (stamp !== binding.stamp) + return + binding.recoverNode = null + } + if (binding.recoverNode) + binding.recoverNode() // 还原现场,有移动节点的都需要还原现场 + try { + if (!elem.parentNode) + return + par = elem.parentNode + } catch (e) { + return + } + if (val) { //插回DOM树 + function alway() {// jshint ignore:line + if (elem.getAttribute(binding.name)) { + elem.removeAttribute(binding.name) + scanAttr(elem, binding.vmodels) + } + binding.rollback = null + } + if (elem.nodeType === 8) { + var keep = binding.keep + var hasEffect = yua.effect.apply(keep, 1, function () { + if (stamp !== binding.stamp) + return + elem.parentNode.replaceChild(keep, elem) + elem = binding.element = keep //这时可能为null + if (keep.getAttribute("_required")) {//#1044 + elem.required = true + elem.removeAttribute("_required") + } + if (elem.querySelectorAll) { + yua.each(elem.querySelectorAll("[_required=true]"), function (el) { + el.required = true + el.removeAttribute("_required") + }) + } + alway() + }, after) + hasEffect = hasEffect === false + } + if (!hasEffect) + alway() + } else { //移出DOM树,并用注释节点占据原位置 + if (elem.nodeType === 1) { + if (elem.required === true) { + elem.required = false + elem.setAttribute("_required", "true") + } + try {//如果不支持querySelectorAll或:required,可以直接无视 + yua.each(elem.querySelectorAll(":required"), function (el) { + elem.required = false + el.setAttribute("_required", "true") + }) + } catch (e) { + } + + var node = binding.element = DOC.createComment(":if"), + pos = elem.nextSibling + binding.recoverNode = function () { + binding.recoverNode = null + if (node.parentNode !== par) { + par.insertBefore(node, pos) + binding.keep = elem + } + } + + yua.effect.apply(elem, 0, function () { + binding.recoverNode = null + if (stamp !== binding.stamp) + return + elem.parentNode.replaceChild(node, elem) + binding.keep = elem //元素节点 + ifGroup.appendChild(elem) + binding.rollback = function () { + if (elem.parentNode === ifGroup) { + ifGroup.removeChild(elem) + } + } + }, after) + } + } + } +}) + +//:important绑定已经在scanTag 方法中实现 +var rnoscripts = /(?:[\s\S]+?)<\/noscript>/img +var rnoscriptText = /([\s\S]+?)<\/noscript>/im + +var getXHR = function () { + return new window.XMLHttpRequest() // jshint ignore:line +} +//将所有远程加载的模板,以字符串形式存放到这里 +var templatePool = yua.templateCache = {} + +function getTemplateContainer(binding, id, text) { + var div = binding.templateCache && binding.templateCache[id] + if (div) { + var dom = DOC.createDocumentFragment(), + firstChild + while (firstChild = div.firstChild) { + dom.appendChild(firstChild) + } + return dom + } + return yua.parseHTML(text) + +} +function nodesToFrag(nodes) { + var frag = DOC.createDocumentFragment() + for (var i = 0, len = nodes.length; i < len; i++) { + frag.appendChild(nodes[i]) + } + return frag +} +yua.directive("include", { + init: directives.attr.init, + update: function (val) { + var binding = this + var elem = this.element + var vmodels = binding.vmodels + var rendered = binding.includeRendered + var effectClass = binding.effectName && binding.effectClass // 是否开启动画 + var templateCache = binding.templateCache // 是否data-include-cache + var outer = binding.includeReplace // 是否data-include-replace + var loaded = binding.includeLoaded + var target = outer ? elem.parentNode : elem + var _ele = binding._element // data-include-replace binding.element === binding.end + + binding.recoverNodes = binding.recoverNodes || yua.noop + + var scanTemplate = function (text) { + var _stamp = binding._stamp = +(new Date()) // 过滤掉频繁操作 + if (loaded) { + var newText = loaded.apply(target, [text].concat(vmodels)) + if (typeof newText === "string") + text = newText + } + if (rendered) { + checkScan(target, function () { + rendered.call(target) + }, NaN) + } + var lastID = binding.includeLastID || "_default" // 默认 + + binding.includeLastID = val + var leaveEl = templateCache && templateCache[lastID] || DOC.createElement(elem.tagName || binding._element.tagName) // 创建一个离场元素 + + if (effectClass) { + leaveEl.className = effectClass + target.insertBefore(leaveEl, binding.start) // 插入到start之前,防止被错误的移动 + } + + // cache or animate,移动节点 + (templateCache || {})[lastID] = leaveEl + var fragOnDom = binding.recoverNodes() // 恢复动画中的节点 + if (fragOnDom) { + target.insertBefore(fragOnDom, binding.end) + } + while (true) { + var node = binding.start.nextSibling + if (node && node !== leaveEl && node !== binding.end) { + leaveEl.appendChild(node) + } else { + break + } + } + + // 元素退场 + yua.effect.remove(leaveEl, target, function () { + if (templateCache) { // write cache + if (_stamp === binding._stamp) + ifGroup.appendChild(leaveEl) + } + }, binding) + + + var enterEl = target, + before = yua.noop, + after = yua.noop; + + var fragment = getTemplateContainer(binding, val, text) + var nodes = yua.slice(fragment.childNodes) + + if (outer && effectClass) { + enterEl = _ele + enterEl.innerHTML = "" // 清空 + enterEl.setAttribute(":skip", "true") + target.insertBefore(enterEl, binding.end.nextSibling) // 插入到bingding.end之后避免被错误的移动 + before = function () { + enterEl.insertBefore(fragment, null) // 插入节点 + } + after = function () { + binding.recoverNodes = yua.noop + if (_stamp === binding._stamp) { + fragment = nodesToFrag(nodes) + target.insertBefore(fragment, binding.end) // 插入真实element + scanNodeArray(nodes, vmodels) + } + if (enterEl.parentNode === target) + target.removeChild(enterEl) // 移除入场动画元素 + } + binding.recoverNodes = function () { + binding.recoverNodes = yua.noop + return nodesToFrag(nodes) + } + } else { + before = function () {//新添加元素的动画 + target.insertBefore(fragment, binding.end) + scanNodeArray(nodes, vmodels) + } + } + + yua.effect.apply(enterEl, "enter", before, after) + + } + + if(!val) + return + + var el = val + + + if(typeof el === 'object'){ + if(el.nodeType !== 1) + return log('include 不支持非DOM对象') + }else{ + el = DOC.getElementById(val) + if(!el){ + if (typeof templatePool[val] === "string") { + yua.nextTick(function () { + scanTemplate(templatePool[val]) + }) + } else if (Array.isArray(templatePool[val])) { //#805 防止在循环绑定中发出许多相同的请求 + templatePool[val].push(scanTemplate) + } else { + var xhr = getXHR() + xhr.onload = function () { + if(xhr.status !== 200) + return log('获取网络资源出错, httpError[' + xhr.status + ']') + + var text = xhr.responseText + for (var f = 0, fn; fn = templatePool[val][f++]; ) { + fn(text) + } + templatePool[val] = text + } + xhr.onerror = function () { + log(":include load [" + val + "] error") + } + templatePool[val] = [scanTemplate] + xhr.open("GET", val, true) + if ("withCredentials" in xhr) { + xhr.withCredentials = true + } + xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest") + xhr.send(null) + } + return + } + } + + yua.nextTick(function () { + scanTemplate(el.value || el.innerText || el.innerHTML) + }) + + } +}) + +var rdash = /\(([^)]*)\)/ +var onDir = yua.directive("on", { + priority: 3000, + init: function (binding) { + var value = binding.expr + binding.type = "on" + var eventType = binding.param.replace(/-\d+$/, "") // :on-mousemove-10 + if (typeof onDir[eventType + "Hook"] === "function") { + onDir[eventType + "Hook"](binding) + } + if (value.indexOf("(") > 0 && value.indexOf(")") > -1) { + var matched = (value.match(rdash) || ["", ""])[1].trim() + if (matched === "" || matched === "$event") { // aaa() aaa($event)当成aaa处理 + value = value.replace(rdash, "") + } + } + binding.expr = value + }, + update: function (callback) { + var binding = this + var elem = this.element + callback = function (e) { + var fn = binding.getter || noop + return fn.apply(this, binding.args.concat(e)) + } + + var eventType = binding.param.replace(/-\d+$/, "") // :on-mousemove-10 + if (eventType === "scan") { + callback.call(elem, { + type: eventType + }) + } else if (typeof binding.specialBind === "function") { + binding.specialBind(elem, callback) + } else { + var removeFn = yua.bind(elem, eventType, callback) + } + binding.rollback = function () { + if (typeof binding.specialUnbind === "function") { + binding.specialUnbind() + } else { + yua.unbind(elem, eventType, removeFn) + } + } + } +}) + +yua.directive("repeat", { + priority: 90, + init: function (binding) { + var type = binding.type + binding.cache = {} //用于存放代理VM + binding.enterCount = 0 + + var elem = binding.element + if (elem.nodeType === 1) { + elem.removeAttribute(binding.name) + effectBinding(elem, binding) + binding.param = binding.param || "el" + binding.sortedCallback = getBindingCallback(elem, "data-with-sorted", binding.vmodels) + var rendered = getBindingCallback(elem, "data-" + type + "-rendered", binding.vmodels) + + var signature = generateID(type) + var start = DOC.createComment(signature + ":start") + var end = binding.element = DOC.createComment(signature + ":end") + binding.signature = signature + binding.start = start + binding.template = yuaFragment.cloneNode(false) + if (type === "repeat") { + var parent = elem.parentNode + parent.replaceChild(end, elem) + parent.insertBefore(start, end) + binding.template.appendChild(elem) + } else { + while (elem.firstChild) { + binding.template.appendChild(elem.firstChild) + } + elem.appendChild(start) + elem.appendChild(end) + parent = elem + } + binding.element = end + + if (rendered) { + var removeFn = yua.bind(parent, "datasetchanged", function () { + rendered.apply(parent, parent.args) + yua.unbind(parent, "datasetchanged", removeFn) + parent.msRendered = rendered + }) + } + } + }, + update: function (value, oldValue) { + var binding = this + var xtype = this.xtype + + this.enterCount += 1 + var init = !oldValue + if (init) { + binding.$outer = {} + var check0 = "$key" + var check1 = "$val" + if (xtype === "array") { + check0 = "$first" + check1 = "$last" + } + for (var i = 0, v; v = binding.vmodels[i++]; ) { + if (v.hasOwnProperty(check0) && v.hasOwnProperty(check1)) { + binding.$outer = v + break + } + } + } + var track = this.track + if (binding.sortedCallback) { //如果有回调,则让它们排序 + var keys2 = binding.sortedCallback.call(parent, track) + if (keys2 && Array.isArray(keys2)) { + track = keys2 + } + } + + var action = "move" + binding.$repeat = value + var fragments = [] + var transation = init && yuaFragment.cloneNode(false) + var proxies = [] + var param = this.param + var retain = yua.mix({}, this.cache) + var elem = this.element + var length = track.length + + var parent = elem.parentNode + + //检查新元素数量 + var newCount = 0 + for (i = 0; i < length; i++) { + var keyOrId = track[i] + if (!retain[keyOrId]) + newCount++ + } + var oldCount = 0 + for (i in retain){ + oldCount++ + } + var clear = (!length || newCount === length) && oldCount > 10 //当全部是新元素,且移除元素较多(10)时使用clear + + var kill = elem.previousSibling + var start = binding.start + + /*log(kill === start, kill) + while(kill !== start && kill.nodeName !== '#comment'){ + parent.removeChild(kill) + kill = elem.previousSibling + }*/ + if (clear){ + while(kill !== start){ + parent.removeChild(kill) + kill = elem.previousSibling + } + } + + + for (i = 0; i < length; i++) { + + keyOrId = track[i] //array为随机数, object 为keyName + var proxy = retain[keyOrId] + if (!proxy) { + + proxy = getProxyVM(this) + proxy.$up = null + if (xtype === "array") { + action = "add" + proxy.$id = keyOrId + var valueItem = value[i] + proxy[param] = valueItem //index + if (Object(valueItem) === valueItem) { + valueItem.$ups = valueItem.$ups || {} + valueItem.$ups[param] = proxy + } + + } else { + action = "append" + proxy.$key = keyOrId + proxy.$val = value[keyOrId] //key + proxy[param] = { $key: proxy.$key, $val: proxy.$val } + } + this.cache[keyOrId] = proxy + var node = proxy.$anchor || (proxy.$anchor = elem.cloneNode(false)) + node.nodeValue = this.signature + shimController(binding, transation, proxy, fragments, init && !binding.effectDriver) + decorateProxy(proxy, binding, xtype) + } else { + // if (xtype === "array") { + // proxy[param] = value[i] + // } + fragments.push({}) + retain[keyOrId] = true + } + + //重写proxy + if (this.enterCount === 1) {//防止多次进入,导致位置不对 + proxy.$active = false + proxy.$oldIndex = proxy.$index + proxy.$active = true + proxy.$index = i + + } + + if (xtype === "array") { + proxy.$first = i === 0 + proxy.$last = i === length - 1 + // proxy[param] = value[i] + } else { + proxy.$val = toJson(value[keyOrId]) //这里是处理vm.object = newObject的情况 + } + proxies.push(proxy) + } + this.proxies = proxies + if (init && !binding.effectDriver) { + parent.insertBefore(transation, elem) + fragments.forEach(function (fragment) { + scanNodeArray(fragment.nodes || [], fragment.vmodels) + //if(fragment.vmodels.length > 2) + fragment.nodes = fragment.vmodels = null + })// jshint ignore:line + } else { + + var staggerIndex = binding.staggerIndex = 0 + for (keyOrId in retain) { + if (retain[keyOrId] !== true) { + + action = "del" + !clear && removeItem(retain[keyOrId].$anchor, binding,true) + // 相当于delete binding.cache[key] + proxyRecycler(this.cache, keyOrId, param) + retain[keyOrId] = null + } + } + + for (i = 0; i < length; i++) { + proxy = proxies[i] + keyOrId = xtype === "array" ? proxy.$id : proxy.$key + var pre = proxies[i - 1] + var preEl = pre ? pre.$anchor : binding.start + if (!retain[keyOrId]) {//如果还没有插入到DOM树,进行插入动画 + (function (fragment, preElement) { + var nodes = fragment.nodes + var vmodels = fragment.vmodels + if (nodes) { + staggerIndex = mayStaggerAnimate(binding.effectEnterStagger, function () { + parent.insertBefore(fragment.content, preElement.nextSibling) + scanNodeArray(nodes, vmodels) + !init && animateRepeat(nodes, 1, binding) + }, staggerIndex) + } + fragment.nodes = fragment.vmodels = null + })(fragments[i], preEl)// jshint ignore:line + + } else if (proxy.$index !== proxy.$oldIndex) {//进行移动动画 + (function (proxy2, preElement) { + staggerIndex = mayStaggerAnimate(binding.effectEnterStagger, function () { + var curNode = removeItem(proxy2.$anchor) + var inserted = yua.slice(curNode.childNodes) + parent.insertBefore(curNode, preElement.nextSibling) + animateRepeat(inserted, 1, binding) + }, staggerIndex) + })(proxy, preEl)// jshint ignore:line + + } + } + + } + if (!value.$track) {//如果是非监控对象,那么就将其$events清空,阻止其持续监听 + for (keyOrId in this.cache) { + proxyRecycler(this.cache, keyOrId, param) + } + + } + + //repeat --> duplex + (function (args) { + parent.args = args + if (parent.msRendered) {//第一次事件触发,以后直接调用 + parent.msRendered.apply(parent, args) + } + })(kernel.newWatch ? arguments : [action]); + var id = setTimeout(function () { + clearTimeout(id) + //触发上层的select回调及自己的rendered回调 + yua.fireDom(parent, "datasetchanged", { + bubble: parent.msHasEvent + }) + }) + this.enterCount -= 1 + + } + +}) + + + + +function animateRepeat(nodes, isEnter, binding) { + for (var i = 0, node; node = nodes[i++]; ) { + if (node.className === binding.effectClass) { + yua.effect.apply(node, isEnter, noop, noop, binding) + } + } +} + +function mayStaggerAnimate(staggerTime, callback, index) { + if (staggerTime) { + setTimeout(callback, (++index) * staggerTime) + } else { + callback() + } + return index +} + +function removeItem(node, binding, flagRemove) { + var fragment = yuaFragment.cloneNode(false) + var last = node + var breakText = last.nodeValue + var staggerIndex = binding && Math.max(+binding.staggerIndex, 0) + var nodes = yua.slice(last.parentNode.childNodes) + var index = nodes.indexOf(last) + while (true) { + var pre = nodes[--index] //node.previousSibling + if (!pre || String(pre.nodeValue).indexOf(breakText) === 0) { + break + } + if (!flagRemove && binding && (pre.className === binding.effectClass)) { + node = pre; + (function (cur) { + binding.staggerIndex = mayStaggerAnimate(binding.effectLeaveStagger, function () { + yua.effect.apply(cur, 0, noop, function () { + fragment.appendChild(cur) + }, binding) + }, staggerIndex) + })(pre);// jshint ignore:line + } else { + fragment.insertBefore(pre, fragment.firstChild) + } + } + fragment.appendChild(last) + return fragment +} + +function shimController(data, transation, proxy, fragments, init) { + var content = data.template.cloneNode(true) + var nodes = yua.slice(content.childNodes) + content.appendChild(proxy.$anchor) + init && transation.appendChild(content) + var itemName = data.param || "el" + var valueItem = proxy[itemName], nv + + nv = [proxy].concat(data.vmodels) + + + var fragment = { + nodes: nodes, + vmodels: nv, + content: content + } + fragments.push(fragment) +} +// {} --> {xx: 0, yy: 1, zz: 2} add +// {xx: 0, yy: 1, zz: 2} --> {xx: 0, yy: 1, zz: 2, uu: 3} +// [xx: 0, yy: 1, zz: 2} --> {xx: 0, zz: 1, yy: 2} + +function getProxyVM(binding) { + var agent = binding.xtype === "object" ? withProxyAgent : eachProxyAgent + var proxy = agent(binding) + var node = proxy.$anchor || (proxy.$anchor = binding.element.cloneNode(false)) + node.nodeValue = binding.signature + proxy.$outer = binding.$outer + return proxy +} + +function decorateProxy(proxy, binding, type) { + if (type === "array") { + proxy.$remove = function () { + binding.$repeat.removeAt(proxy.$index) + } + var param = binding.param + proxy.$watch(param, function (a) { + var index = proxy.$index + binding.$repeat[index] = a + }) + } else { + proxy.$watch("$val", function fn(a) { + binding.$repeat[proxy.$key] = a + }) + } +} + + +var eachProxyPool = [] + +function eachProxyAgent(data, proxy) { + var itemName = data.param || "el" + for (var i = 0, n = eachProxyPool.length; i < n; i++) { + var candidate = eachProxyPool[i] + if (candidate && candidate.hasOwnProperty(itemName)) { + eachProxyPool.splice(i, 1) + proxy = candidate + break + } + } + if (!proxy) { + proxy = eachProxyFactory(itemName) + } + return proxy +} + +function eachProxyFactory(itemName) { + var source = { + $outer: {}, + $index: 0, + $oldIndex: 0, + $anchor: null, + //----- + $first: false, + $last: false, + $remove: yua.noop + } + source[itemName] = NaN + var force = { + $last: 1, + $first: 1, + $index: 1 + } + force[itemName] = 1 + var proxy = modelFactory(source, { + force: force + }) + proxy.$id = generateID("$proxy$each") + return proxy +} + +var withProxyPool = [] + +function withProxyAgent(data) { + var itemName = data.param || "el" + return withProxyPool.pop() || withProxyFactory(itemName) +} + +function withProxyFactory(itemName) { + var source = { + $key: "", + $val: NaN, + $index: 0, + $oldIndex: 0, + $outer: {}, + $anchor: null + } + source[itemName] = NaN + var force = { + $key: 1, + $val: 1, + $index: 1 + } + force[itemName] = 1 + var proxy = modelFactory(source, { + force: force + }) + proxy.$id = generateID("$proxy$with") + return proxy +} + + +function proxyRecycler(cache, key, param) { + var proxy = cache[key] + if (proxy) { + var proxyPool = proxy.$id.indexOf("$proxy$each") === 0 ? eachProxyPool : withProxyPool + proxy.$outer = {} + + for (var i in proxy.$events) { + var a = proxy.$events[i] + if (Array.isArray(a)) { + a.length = 0 + if (i === param) { + proxy[param] = NaN + } else if (i === "$val") { + proxy.$val = NaN + } + } + } + + if (proxyPool.unshift(proxy) > kernel.maxRepeatSize) { + proxyPool.pop() + } + delete cache[key] + } +} + + + + + + +/********************************************************************* + * 各种指令 * + **********************************************************************/ + +//:skip绑定已经在scanTag 方法中实现 +yua.directive("text", { + update: function (val) { + var elem = this.element + val = val == null ? "" : val //不在页面上显示undefined null + if (elem.nodeType === 3) { //绑定在文本节点上 + try { //IE对游离于DOM树外的节点赋值会报错 + elem.data = val + } catch (e) { + } + } else { //绑定在特性节点上 + elem.textContent = val + } + } +}) +function parseDisplay(nodeName, val) { + //用于取得此类标签的默认display值 + var key = "_" + nodeName + if (!parseDisplay[key]) { + var node = DOC.createElement(nodeName) + root.appendChild(node) + if (W3C) { + val = getComputedStyle(node, null).display + } else { + val = node.currentStyle.display + } + root.removeChild(node) + parseDisplay[key] = val + } + return parseDisplay[key] +} + +yua.parseDisplay = parseDisplay + +yua.directive("visible", { + init: function (binding) { + effectBinding(binding.element, binding) + }, + update: function (val) { + var binding = this, elem = this.element, stamp + var noEffect = !this.effectName + if (!this.stamp) { + stamp = this.stamp = +new Date() + if (val) { + elem.style.display = binding.display || "" + if (yua(elem).css("display") === "none") { + elem.style.display = binding.display = parseDisplay(elem.nodeName) + } + } else { + elem.style.display = "none" + } + return + } + stamp = this.stamp = +new Date() + if (val) { + yua.effect.apply(elem, 1, function () { + if (stamp !== binding.stamp) + return + var driver = elem.getAttribute("data-effect-driver") || "a" + + if (noEffect) {//不用动画时走这里 + elem.style.display = binding.display || "" + } + // "a", "t" + if (driver === "a" || driver === "t") { + if (yua(elem).css("display") === "none") { + elem.style.display = binding.display || parseDisplay(elem.nodeName) + } + } + }) + } else { + yua.effect.apply(elem, 0, function () { + if (stamp !== binding.stamp) + return + elem.style.display = "none" + }) + } + } +}) + + + + + + + + + + + + + + +/********************************************************************* + * 自带过滤器 * + **********************************************************************/ + +var rscripts = /]*>([\S\s]*?)<\/script\s*>/gim +var ron = /\s+(on[^=\s]+)(?:=("[^"]*"|'[^']*'|[^\s>]+))?/g +var ropen = /<\w+\b(?:(["'])[^"]*?(\1)|[^>])*>/ig +var rsanitize = { + a: /\b(href)\=("javascript[^"]*"|'javascript[^']*')/ig, + img: /\b(src)\=("javascript[^"]*"|'javascript[^']*')/ig, + form: /\b(action)\=("javascript[^"]*"|'javascript[^']*')/ig +} +var rsurrogate = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g +var rnoalphanumeric = /([^\#-~| |!])/g; + +function numberFormat(number, decimals, point, thousands) { + //form http://phpjs.org/functions/number_format/ + //number 必需,要格式化的数字 + //decimals 可选,规定多少个小数位。 + //point 可选,规定用作小数点的字符串(默认为 . )。 + //thousands 可选,规定用作千位分隔符的字符串(默认为 , ),如果设置了该参数,那么所有其他参数都是必需的。 + number = (number + '') + .replace(/[^0-9+\-Ee.]/g, '') + var n = !isFinite(+number) ? 0 : +number, + prec = !isFinite(+decimals) ? 3 : Math.abs(decimals), + sep = thousands || ",", + dec = point || ".", + s = '', + toFixedFix = function(n, prec) { + var k = Math.pow(10, prec) + return '' + (Math.round(n * k) / k) + .toFixed(prec) + } + // Fix for IE parseFloat(0.55).toFixed(0) = 0; + s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)) + .split('.') + if (s[0].length > 3) { + s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep) + } + if ((s[1] || '') + .length < prec) { + s[1] = s[1] || '' + s[1] += new Array(prec - s[1].length + 1) + .join('0') + } + return s.join(dec) +} + +var filters = yua.filters = { + uppercase: function(str) { + return str.toUpperCase() + }, + lowercase: function(str) { + return str.toLowerCase() + }, + //字符串截取,超过指定长度以mark标识接上 + truncate: function(str, len, mark) { + len = len || 30 + mark = typeof mark === 'string' ? mark : '...' + return str.slice(0, len) + (str.length <= len ? '' : mark) + }, + //小值秒数转化为 时间格式 + time: function(str){ + str = str>>0; + var s = str % 60; + m = Math.floor(str / 60), + h = Math.floor(m / 60), + + m = m % 60; + m = m < 10 ? '0' + m : m; + s = s < 10 ? '0' + s : s; + + if(h > 0){ + h = h < 10 ? '0' + h : h; + return h + ':' + m + ':' + s; + } + return m + ':' + s; + }, + $filter: function(val) { + for (var i = 1, n = arguments.length; i < n; i++) { + var array = arguments[i] + var fn = yua.filters[array[0]] + if (typeof fn === "function") { + var arr = [val].concat(array.slice(1)) + val = fn.apply(null, arr) + } + } + return val + }, + camelize: camelize, + //https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet + // chrome + // chrome + // IE67chrome + // IE67chrome + // IE67chrome + sanitize: function(str) { + return str.replace(rscripts, "").replace(ropen, function(a, b) { + var match = a.toLowerCase().match(/<(\w+)\s/) + if (match) { //处理a标签的href属性,img标签的src属性,form标签的action属性 + var reg = rsanitize[match[1]] + if (reg) { + a = a.replace(reg, function(s, name, value) { + var quote = value.charAt(0) + return name + "=" + quote + "javascript:void(0)" + quote// jshint ignore:line + }) + } + } + return a.replace(ron, " ").replace(/\s+/g, " ") //移除onXXX事件 + }) + }, + escape: function(str) { + //将字符串经过 str 转义得到适合在页面中显示的内容, 例如替换 < 为 < + return String(str). + replace(/&/g, '&'). + replace(rsurrogate, function(value) { + var hi = value.charCodeAt(0) + var low = value.charCodeAt(1) + return '&#' + (((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000) + ';' + }). + replace(rnoalphanumeric, function(value) { + return '&#' + value.charCodeAt(0) + ';' + }). + replace(//g, '>') + }, + currency: function(amount, symbol, fractionSize) { + return (symbol || "\u00a5") + numberFormat(amount, isFinite(fractionSize) ? fractionSize : 2) + }, + number: numberFormat, + //日期格式化,类似php的date函数, + date: function(stamp, str, second){ + second = (second === undefined) ? false : true + var oDate; + if(!Date.isDate(stamp)){ + + if(!/[^\d]/.test(stamp)){ + stamp -= 0 + if(second){ + stamp *= 1000 + } + } + + oDate = new Date(stamp); + if((oDate + '') === 'Invalid Date') + return 'Invalid Date' + + }else{ + oDate = stamp + } + return oDate.format(str) + + } +} + + + + +/********************************************************************* + * AMD加载器 * + **********************************************************************/ + +//https://www.devbridge.com/articles/understanding-amd-requirejs/ +//http://maxogden.com/nested-dependencies.html +var modules = yua.modules = { + "domReady!": { + exports: yua, + state: 3 + }, + "yua": { + exports: yua, + state: 4 + } +} +//Object(modules[id]).state拥有如下值 +// undefined 没有定义 +// 1(send) 已经发出请求 +// 2(loading) 已经被执行但还没有执行完成,在这个阶段define方法会被执行 +// 3(loaded) 执行完毕,通过onload/onreadystatechange回调判定,在这个阶段checkDeps方法会执行 +// 4(execute) 其依赖也执行完毕, 值放到exports对象上,在这个阶段fireFactory方法会执行 +modules.exports = modules.yua +var otherRequire = window.require +var otherDefine = window.define +var innerRequire +plugins.loader = function (builtin) { + var flag = innerRequire && builtin + window.require = flag ? innerRequire : otherRequire + window.define = flag ? innerRequire.define : otherDefine +} +new function () {// jshint ignore:line + var loadings = [] //正在加载中的模块列表 + var factorys = [] //放置define方法的factory函数 + var rjsext = /\.js$/i + function makeRequest(name, config) { + //1. 去掉资源前缀 + var res = "js" + name = name.replace(/^(\w+)\!/, function (a, b) { + res = b + return "" + }) + if (res === "ready") { + log("debug: ready!已经被废弃,请使用domReady!") + res = "domReady" + } + //2. 去掉querystring, hash + var query = "" + name = name.replace(rquery, function (a) { + query = a + return "" + }) + //3. 去掉扩展名 + var suffix = "." + res + var ext = /js|css/.test(suffix) ? suffix : "" + name = name.replace(/\.[a-z0-9]+$/g, function (a) { + if (a === suffix) { + ext = a + return "" + } else { + return a + } + }) + var req = yua.mix({ + query: query, + ext: ext, + res: res, + name: name, + toUrl: toUrl + }, config) + req.toUrl(name) + return req + } + + function fireRequest(req) { + var name = req.name + var res = req.res + //1. 如果该模块已经发出请求,直接返回 + var module = modules[name] + var urlNoQuery = name && req.urlNoQuery + if (module && module.state >= 1) { + return name + } + module = modules[urlNoQuery] + if (module && module.state >= 3) { + innerRequire(module.deps || [], module.factory, urlNoQuery) + return urlNoQuery + } + if (name && !module) { + module = modules[urlNoQuery] = { + id: urlNoQuery, + state: 1 //send + } + var wrap = function (obj) { + resources[res] = obj + obj.load(name, req, function (a) { + if (arguments.length && a !== void 0) { + module.exports = a + } + module.state = 4 + checkDeps() + }) + } + + if (!resources[res]) { + innerRequire([res], wrap) + } else { + wrap(resources[res]) + } + } + return name ? urlNoQuery : res + "!" + } + + //核心API之一 require + var requireQueue = [] + var isUserFirstRequire = false + innerRequire = yua.require = function (array, factory, parentUrl, defineConfig) { + if (!isUserFirstRequire) { + requireQueue.push(yua.slice(arguments)) + if (arguments.length <= 2) { + isUserFirstRequire = true + var queue = requireQueue.splice(0, requireQueue.length), args + while (args = queue.shift()) { + innerRequire.apply(null, args) + } + } + return + } + + if (!Array.isArray(array)) { + yua.error("require方法的第一个参数应为数组 " + array) + } + var deps = [] // 放置所有依赖项的完整路径 + var uniq = createMap() + var id = parentUrl || "callback" + setTimeout("1")// jshint ignore:line + defineConfig = defineConfig || createMap() + defineConfig.baseUrl = kernel.baseUrl + var isBuilt = !!defineConfig.built + if (parentUrl) { + defineConfig.parentUrl = parentUrl.substr(0, parentUrl.lastIndexOf("/")) + defineConfig.mapUrl = parentUrl.replace(rjsext, "") + } + if (isBuilt) { + var req = makeRequest(defineConfig.defineName, defineConfig) + id = req.urlNoQuery + } else { + array.forEach(function (name) { + if(!name){ + return + } + var req = makeRequest(name, defineConfig) + var url = fireRequest(req) //加载资源,并返回该资源的完整地址 + if (url) { + if (!uniq[url]) { + deps.push(url) + uniq[url] = "yua" //去重 + } + } + }) + } + + var module = modules[id] + if (!module || module.state !== 4) { + modules[id] = { + id: id, + deps: isBuilt ? array.concat() : deps, + factory: factory || noop, + state: 3 + } + } + if (!module) { + //如果此模块是定义在另一个JS文件中, 那必须等该文件加载完毕, 才能放到检测列队中 + loadings.push(id) + } + checkDeps() + } + + //核心API之二 require + innerRequire.define = function (name, deps, factory) { //模块名,依赖列表,模块本身 + if (typeof name !== "string") { + factory = deps + deps = name + name = "anonymous" + } + if (!Array.isArray(deps)) { + factory = deps + deps = [] + } + var config = { + built: !isUserFirstRequire, //用r.js打包后,所有define会放到requirejs之前 + defineName: name + } + var args = [deps, factory, config] + factory.require = function (url) { + args.splice(2, 0, url) + if (modules[url]) { + modules[url].state = 3 //loaded + var isCycle = false + try { + isCycle = checkCycle(modules[url].deps, url) + } catch (e) { + } + if (isCycle) { + yua.error(url + "模块与之前的模块存在循环依赖,请不要直接用script标签引入" + url + "模块") + } + } + delete factory.require //释放内存 + innerRequire.apply(null, args) //0,1,2 --> 1,2,0 + } + //根据标准,所有遵循W3C标准的浏览器,script标签会按标签的出现顺序执行。 + //老的浏览器中,加载也是按顺序的:一个文件下载完成后,才开始下载下一个文件。 + //较新的浏览器中(IE8+ 、FireFox3.5+ 、Chrome4+ 、Safari4+),为了减小请求时间以优化体验, + //下载可以是并行的,但是执行顺序还是按照标签出现的顺序。 + //但如果script标签是动态插入的, 就未必按照先请求先执行的原则了,目测只有firefox遵守 + //唯一比较一致的是,IE10+及其他标准浏览器,一旦开始解析脚本, 就会一直堵在那里,直接脚本解析完毕 + //亦即,先进入loading阶段的script标签(模块)必然会先进入loaded阶段 + var url = config.built ? "unknown" : getCurrentScript() + if (url) { + var module = modules[url] + if (module) { + module.state = 2 + } + factory.require(url) + } else {//合并前后的safari,合并后的IE6-9走此分支 + factorys.push(factory) + } + } + //核心API之三 require.config(settings) + innerRequire.config = kernel + //核心API之四 define.amd 标识其符合AMD规范 + innerRequire.define.amd = modules + + //==========================对用户配置项进行再加工========================== + var allpaths = kernel["orig.paths"] = createMap() + var allmaps = kernel["orig.map"] = createMap() + var allpackages = kernel["packages"] = [] + var allargs = kernel["orig.args"] = createMap() + yua.mix(plugins, { + paths: function (hash) { + yua.mix(allpaths, hash) + kernel.paths = makeIndexArray(allpaths) + }, + map: function (hash) { + yua.mix(allmaps, hash) + var list = makeIndexArray(allmaps, 1, 1) + yua.each(list, function (_, item) { + item.val = makeIndexArray(item.val) + }) + kernel.map = list + }, + packages: function (array) { + array = array.concat(allpackages) + var uniq = createMap() + var ret = [] + for (var i = 0, pkg; pkg = array[i++]; ) { + pkg = typeof pkg === "string" ? {name: pkg} : pkg + var name = pkg.name + if (!uniq[name]) { + var url = joinPath(pkg.location || name, pkg.main || "main") + url = url.replace(rjsext, "") + ret.push(pkg) + uniq[name] = pkg.location = url + pkg.reg = makeMatcher(name) + } + } + kernel.packages = ret.sort() + }, + urlArgs: function (hash) { + if (typeof hash === "string") { + hash = {"*": hash} + } + yua.mix(allargs, hash) + kernel.urlArgs = makeIndexArray(allargs, 1) + }, + baseUrl: function (url) { + if (!isAbsUrl(url)) { + var baseElement = head.getElementsByTagName("base")[0] + if (baseElement) { + head.removeChild(baseElement) + } + var node = DOC.createElement("a") + node.href = url + url = node.href + if (baseElement) { + head.insertBefore(baseElement, head.firstChild) + } + } + if (url.length > 3) + kernel.baseUrl = url + }, + shim: function (obj) { + for (var i in obj) { + var value = obj[i] + if (Array.isArray(value)) { + value = obj[i] = { + deps: value + } + } + if (!value.exportsFn && (value.exports || value.init)) { + value.exportsFn = makeExports(value) + } + } + kernel.shim = obj + } + + }) + + + //==============================内部方法================================= + function checkCycle(deps, nick) { + //检测是否存在循环依赖 + for (var i = 0, id; id = deps[i++]; ) { + if (modules[id].state !== 4 && + (id === nick || checkCycle(modules[id].deps, nick))) { + return true + } + } + } + + function checkFail(node, onError) { + var id = trimQuery(node.src) //检测是否死链 + node.onload = node.onerror = null + if (onError) { + setTimeout(function () { + head.removeChild(node) + node = null // 处理旧式IE下的循环引用问题 + }) + log("debug: 加载 " + id + " 失败" + onError + " " + (!modules[id].state)) + } else { + return true + } + } + + function checkDeps() { + //检测此JS模块的依赖是否都已安装完毕,是则安装自身 + loop: for (var i = loadings.length, id; id = loadings[--i]; ) { + var obj = modules[id], + deps = obj.deps + if (!deps) + continue + for (var j = 0, key; key = deps[j]; j++) { + if (Object(modules[key]).state !== 4) { + continue loop + } + } + //如果deps是空对象或者其依赖的模块的状态都是4 + if (obj.state !== 4) { + loadings.splice(i, 1) //必须先移除再安装,防止在IE下DOM树建完后手动刷新页面,会多次执行它 + fireFactory(obj.id, obj.deps, obj.factory) + checkDeps() //如果成功,则再执行一次,以防有些模块就差本模块没有安装好 + } + } + } + + function loadJS(url, id, callback) { + //通过script节点加载目标模块 + var node = DOC.createElement("script") + node.className = subscribers //让getCurrentScript只处理类名为subscribers的script节点 + node.onload = function () { + var factory = factorys.pop() + factory && factory.require(id) + if (callback) { + callback() + } + id && loadings.push(id) + checkDeps() + } + node.onerror = function () { + checkFail(node, true) + } + + head.insertBefore(node, head.firstChild) //chrome下第二个参数不能为null + node.src = url //插入到head的第一个节点前,防止IE6下head标签没闭合前使用appendChild抛错,更重要的是IE6下可以收窄getCurrentScript的寻找范围 + } + + var resources = innerRequire.plugins = { + //三大常用资源插件 js!, css!, text!, domReady! + domReady: { + load: noop + }, + js: { + load: function (name, req, onLoad) { + var url = req.url + var id = req.urlNoQuery + var shim = kernel.shim[name.replace(rjsext, "")] + if (shim) { //shim机制 + innerRequire(shim.deps || [], function () { + var args = yua.slice(arguments) + loadJS(url, id, function () { + onLoad(shim.exportsFn ? shim.exportsFn.apply(0, args) : void 0) + }) + }) + } else { + loadJS(url, id) + } + } + }, + css: { + load: function (name, req, onLoad) { + var url = req.url + head.insertAdjacentHTML("beforeend", '') + onLoad() + } + }, + text: { + load: function (name, req, onLoad) { + var url = req.url + var xhr = getXHR() + xhr.onload = function () { + var status = xhr.status; + if (status > 399 && status < 600) { + yua.error(url + " 对应资源不存在或没有开启 CORS") + } else { + onLoad(xhr.responseText) + } + } + var time = "_=" + (new Date() - 0) + var _url = url.indexOf("?") === -1 ? url + "?" + time : url + "&" + time + xhr.open("GET", _url, true) + if ("withCredentials" in xhr) {//这是处理跨域 + xhr.withCredentials = true + } + xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest")//告诉后端这是AJAX请求 + xhr.send() + } + } + } + innerRequire.checkDeps = checkDeps + + var rquery = /(\?[^#]*)$/ + function trimQuery(url) { + return (url || "").replace(rquery, "") + } + + function isAbsUrl(path) { + //http://stackoverflow.com/questions/10687099/how-to-test-if-a-url-string-is-absolute-or-relative + return /^(?:[a-z\-]+:)?\/\//i.test(String(path)) + } + + + function getCurrentScript() { + // inspireb by https://github.com/samyk/jiagra/blob/master/jiagra.js + var stack + try { + a.b.c() //强制报错,以便捕获e.stack + } catch (e) { //safari5的sourceURL,firefox的fileName,它们的效果与e.stack不一样 + stack = e.stack + } + if (stack) { + /**e.stack最后一行在所有支持的浏览器大致如下: + *chrome23: + * at http://113.93.50.63/data.js:4:1 + *firefox17: + *@http://113.93.50.63/query.js:4 + *opera12:http://www.oldapps.com/opera.php?system=Windows_XP + *@http://113.93.50.63/data.js:4 + *IE10: + * at Global code (http://113.93.50.63/data.js:4:1) + * //firefox4+ 可以用document.currentScript + */ + stack = stack.split(/[@ ]/g).pop() //取得最后一行,最后一个空格或@之后的部分 + stack = stack[0] === "(" ? stack.slice(1, -1) : stack.replace(/\s/, "") //去掉换行符 + return trimQuery(stack.replace(/(:\d+)?:\d+$/i, "")) //去掉行号与或许存在的出错字符起始位置 + } + var nodes = head.getElementsByTagName("script") //只在head标签中寻找 + for (var i = nodes.length, node; node = nodes[--i]; ) { + if (node.className === subscribers && node.readyState === "interactive") { + var url = node.src + return node.className = trimQuery(url) + } + } + } + + var rcallback = /^callback\d+$/ + function fireFactory(id, deps, factory) { + var module = Object(modules[id]) + module.state = 4 + for (var i = 0, array = [], d; d = deps[i++]; ) { + if (d === "exports") { + var obj = module.exports || (module.exports = createMap()) + array.push(obj) + } else { + array.push(modules[d].exports) + } + } + try { + var ret = factory.apply(window, array) + } catch (e) { + log("执行[" + id + "]模块的factory抛错: ", e) + } + if (ret !== void 0) { + module.exports = ret + } + if (rcallback.test(id)) { + delete modules[id] + } + delete module.factory + return ret + } + function toUrl(id) { + if (id.indexOf(this.res + "!") === 0) { + id = id.slice(this.res.length + 1) //处理define("css!style",[], function(){})的情况 + } + var url = id + //1. 是否命中paths配置项 + var usePath = 0 + var baseUrl = this.baseUrl + var rootUrl = this.parentUrl || baseUrl + eachIndexArray(id, kernel.paths, function (value, key) { + url = url.replace(key, value) + usePath = 1 + }) + //2. 是否命中packages配置项 + if (!usePath) { + eachIndexArray(id, kernel.packages, function (value, key, item) { + url = url.replace(item.name, item.location) + }) + } + //3. 是否命中map配置项 + if (this.mapUrl) { + eachIndexArray(this.mapUrl, kernel.map, function (array) { + eachIndexArray(url, array, function (mdValue, mdKey) { + url = url.replace(mdKey, mdValue) + rootUrl = baseUrl + }) + }) + } + var ext = this.ext + if (ext && usePath && url.slice(-ext.length) === ext) { + url = url.slice(0, -ext.length) + } + //4. 转换为绝对路径 + if (!isAbsUrl(url)) { + rootUrl = this.built || /^\w/.test(url) ? baseUrl : rootUrl + url = joinPath(rootUrl, url) + } + //5. 还原扩展名,query + var urlNoQuery = url + ext + url = urlNoQuery + this.query + urlNoQuery = url.replace(rquery, function (a) { + this.query = a + return "" + }) + //6. 处理urlArgs + eachIndexArray(id, kernel.urlArgs, function (value) { + url += (url.indexOf("?") === -1 ? "?" : "&") + value; + }) + this.url = url + return this.urlNoQuery = urlNoQuery + } + + function makeIndexArray(hash, useStar, part) { + //创建一个经过特殊算法排好序的数组 + var index = hash2array(hash, useStar, part) + index.sort(descSorterByName) + return index + } + + function makeMatcher(prefix) { + return new RegExp('^' + prefix + '(/|$)') + } + + function makeExports(value) { + return function () { + var ret + if (value.init) { + ret = value.init.apply(window, arguments) + } + return ret || (value.exports && getGlobal(value.exports)) + } + } + + + function hash2array(hash, useStar, part) { + var array = []; + for (var key in hash) { + // if (hash.hasOwnProperty(key)) {//hash是由createMap创建没有hasOwnProperty + var item = { + name: key, + val: hash[key] + } + array.push(item) + item.reg = key === "*" && useStar ? /^/ : makeMatcher(key) + if (part && key !== "*") { + item.reg = new RegExp('\/' + key.replace(/^\//, "") + '(/|$)') + } + // } + } + return array + } + + function eachIndexArray(moduleID, array, matcher) { + array = array || [] + for (var i = 0, el; el = array[i++]; ) { + if (el.reg.test(moduleID)) { + matcher(el.val, el.name, el) + return false + } + } + } + // 根据元素的name项进行数组字符数逆序的排序函数 + function descSorterByName(a, b) { + var aaa = a.name + var bbb = b.name + if (bbb === "*") { + return -1 + } + if (aaa === "*") { + return 1 + } + return bbb.length - aaa.length + } + + var rdeuce = /\/\w+\/\.\./ + function joinPath(a, b) { + if (a.charAt(a.length - 1) !== "/") { + a += "/" + } + if (b.slice(0, 2) === "./") { //相对于兄弟路径 + return a + b.slice(2) + } + if (b.slice(0, 2) === "..") { //相对于父路径 + a += b + while (rdeuce.test(a)) { + a = a.replace(rdeuce, "") + } + return a + } + if (b.slice(0, 1) === "/") { + return a + b.slice(1) + } + return a + b + } + + function getGlobal(value) { + if (!value) { + return value + } + var g = window + value.split(".").forEach(function (part) { + g = g[part] + }) + return g + } + + var mainNode = DOC.scripts[DOC.scripts.length - 1] + var dataMain = mainNode.getAttribute("data-main") + if (dataMain) { + plugins.baseUrl(dataMain) + var href = kernel.baseUrl + kernel.baseUrl = href.slice(0, href.lastIndexOf("/") + 1) + loadJS(href.replace(rjsext, "") + ".js") + } else { + var loaderUrl = trimQuery(mainNode.src) + kernel.baseUrl = loaderUrl.slice(0, loaderUrl.lastIndexOf("/") + 1) + } +}// jshint ignore:line + + + + + + + + + + + + + + + + + + + + +/********************************************************************* + * DOMReady * + **********************************************************************/ + +var readyList = [], + isReady +var fireReady = function (fn) { + isReady = true + var require = yua.require + if (require && require.checkDeps) { + modules["domReady!"].state = 4 + require.checkDeps() + } + while (fn = readyList.shift()) { + fn(yua) + } +} + +if (DOC.readyState === "complete") { + setTimeout(fireReady) //如果在domReady之外加载 +} else { + DOC.addEventListener("DOMContentLoaded", fireReady) +} +window.addEventListener("load", fireReady) +yua.ready = function (fn) { + if (!isReady) { + readyList.push(fn) + } else { + fn(yua) + } +} + +yua.config({ + loader: true +}) +yua.ready(function () { + yua.scan(DOC.body) +}) + + + + + + + + + + + + + + + + + + + + + + + + + + + +var ua = navigator.userAgent.toLowerCase() +//http://stackoverflow.com/questions/9038625/detect-if-device-is-ios +function iOSversion() { + //https://developer.apple.com/library/prerelease/mac/releasenotes/General/WhatsNewInSafari/Articles/Safari_9.html + //http://mp.weixin.qq.com/s?__biz=MzA3MDQ4MzQzMg==&mid=256900619&idx=1&sn=b29f84cff0b8d7b9742e5d8b3cd8f218&scene=1&srcid=1009F9l4gh9nZ7rcQJEhmf7Q#rd + if (/iPad|iPhone|iPod/i.test(ua) && !window.MSStream) { + if ("backdropFilter" in document.documentElement.style) { + return 9 + } + if (!!window.indexedDB) { + return 8 + } + if (!!window.SpeechSynthesisUtterance) { + return 7 + } + if (!!window.webkitAudioContext) { + return 6 + } + if (!!window.matchMedia) { + return 5 + } + if (!!window.history && 'pushState' in window.history) { + return 4 + } + return 3 + } + return NaN +} + +var deviceIsAndroid = ua.indexOf('android') > 0 +var deviceIsIOS = iOSversion() + +var Recognizer = yua.gestureHooks = { + pointers: {}, + //以AOP切入touchstart, touchmove, touchend, touchcancel回调 + start: function (event, callback) { + + //touches是当前屏幕上所有触摸点的列表; + //targetTouches是当前对象上所有触摸点的列表; + //changedTouches是涉及当前事件的触摸点的列表。 + for (var i = 0; i < event.changedTouches.length; i++) { + var touch = event.changedTouches[i], + id = touch.identifier, + pointer = { + startTouch: mixLocations({}, touch), + startTime: Date.now(), + status: 'tapping', + element: event.target, + pressingHandler: Recognizer.pointers[id] && Recognizer.pointers[id].pressingHandler + } + Recognizer.pointers[id] = pointer; + callback(pointer, touch) + + } + }, + move: function (event, callback) { + for (var i = 0; i < event.changedTouches.length; i++) { + var touch = event.changedTouches[i] + var pointer = Recognizer.pointers[touch.identifier] + if (!pointer) { + return + } + + if (!("lastTouch" in pointer)) { + pointer.lastTouch = pointer.startTouch + pointer.lastTime = pointer.startTime + pointer.deltaX = pointer.deltaY = pointer.duration = pointer.distance = 0 + } + + var time = Date.now() - pointer.lastTime + + if (time > 0) { + + var RECORD_DURATION = 70 + if (time > RECORD_DURATION) { + time = RECORD_DURATION + } + if (pointer.duration + time > RECORD_DURATION) { + pointer.duration = RECORD_DURATION - time + } + + pointer.duration += time; + pointer.lastTouch = mixLocations({}, touch) + + pointer.lastTime = Date.now() + + pointer.deltaX = touch.clientX - pointer.startTouch.clientX + pointer.deltaY = touch.clientY - pointer.startTouch.clientY + var x = pointer.deltaX * pointer.deltaX + var y = pointer.deltaY * pointer.deltaY + pointer.distance = Math.sqrt(x + y) + pointer.isVertical = x < y + + callback(pointer, touch) + } + } + }, + end: function (event, callback) { + for (var i = 0; i < event.changedTouches.length; i++) { + var touch = event.changedTouches[i], + id = touch.identifier, + pointer = Recognizer.pointers[id] + + if (!pointer) + continue + + callback(pointer, touch) + + delete Recognizer.pointers[id] + } + }, + //人工触发合成事件 + fire: function (elem, type, props) { + if (elem) { + var event = document.createEvent('Events') + event.initEvent(type, true, true) + yua.mix(event, props) + elem.dispatchEvent(event) + } + }, + //添加各种识别器 + add: function (name, recognizer) { + function move(event) { + recognizer.touchmove(event) + } + + function end(event) { + recognizer.touchend(event) + + document.removeEventListener('touchmove', move) + + document.removeEventListener('touchend', end) + + document.removeEventListener('touchcancel', cancel) + + } + + function cancel(event) { + recognizer.touchcancel(event) + + document.removeEventListener('touchmove', move) + + document.removeEventListener('touchend', end) + + document.removeEventListener('touchcancel', cancel) + + } + + recognizer.events.forEach(function (eventName) { + yua.eventHooks[eventName] = { + fix: function (el, fn) { + if (!el['touch-' + name]) { + el['touch-' + name] = '1' + el.addEventListener('touchstart', function (event) { + recognizer.touchstart(event) + + document.addEventListener('touchmove', move) + + document.addEventListener('touchend', end) + + document.addEventListener('touchcancel', cancel) + + }) + } + return fn + } + } + }) + } +} + +var locations = ['screenX', 'screenY', 'clientX', 'clientY', 'pageX', 'pageY'] + +// 复制 touch 对象上的有用属性到固定对象上 +function mixLocations(target, source) { + if (source) { + locations.forEach(function (key) { + target[key] = source[key] + }) + } + return target +} + +var supportPointer = !!navigator.pointerEnabled || !!navigator.msPointerEnabled + +if (supportPointer) { // 支持pointer的设备可用样式来取消click事件的300毫秒延迟 + root.style.msTouchAction = root.style.touchAction = 'none' +} +var tapRecognizer = { + events: ['tap'], + touchBoundary: 10, + tapDelay: 200, + needClick: function(target) { + //判定是否使用原生的点击事件, 否则使用sendClick方法手动触发一个人工的点击事件 + switch (target.nodeName.toLowerCase()) { + case 'button': + case 'select': + case 'textarea': + if (target.disabled) { + return true + } + + break; + case 'input': + // IOS6 pad 上选择文件,如果不是原生的click,弹出的选择界面尺寸错误 + if ((deviceIsIOS && target.type === 'file') || target.disabled) { + return true + } + + break; + case 'label': + case 'iframe': + case 'video': + return true + } + + return false + }, + needFocus: function(target) { + switch (target.nodeName.toLowerCase()) { + case 'textarea': + case 'select': //实测android下select也需要 + return true; + case 'input': + switch (target.type) { + case 'button': + case 'checkbox': + case 'file': + case 'image': + case 'radio': + case 'submit': + return false + } + //如果是只读或disabled状态,就无须获得焦点了 + return !target.disabled && !target.readOnly + default: + return false + } + }, + focus: function(targetElement) { + var length; + //在iOS7下, 对一些新表单元素(如date, datetime, time, month)调用focus方法会抛错, + //幸好的是,我们可以改用setSelectionRange获取焦点, 将光标挪到文字的最后 + var type = targetElement.type + if (deviceIsIOS && targetElement.setSelectionRange && + type.indexOf('date') !== 0 && type !== 'time' && type !== 'month') { + length = targetElement.value.length + targetElement.setSelectionRange(length, length) + } else { + targetElement.focus() + } + }, + findControl: function(labelElement) { + // 获取label元素所对应的表单元素 + // 可以能过control属性, getElementById, 或用querySelector直接找其内部第一表单元素实现 + if (labelElement.control !== undefined) { + return labelElement.control + } + + if (labelElement.htmlFor) { + return document.getElementById(labelElement.htmlFor) + } + + return labelElement.querySelector('button, input:not([type=hidden]), keygen, meter, output, progress, select, textarea') + }, + fixTarget: function(target) { + if (target.nodeType === 3) { + return target.parentNode + } + if (window.SVGElementInstance && (target instanceof SVGElementInstance)) { + return target.correspondingUseElement; + } + + return target + }, + updateScrollParent: function(targetElement) { + //如果事件源元素位于某一个有滚动条的祖父元素中,那么保持其scrollParent与scrollTop值 + var scrollParent = targetElement.tapScrollParent + + if (!scrollParent || !scrollParent.contains(targetElement)) { + var parentElement = targetElement + do { + if (parentElement.scrollHeight > parentElement.offsetHeight) { + scrollParent = parentElement + targetElement.tapScrollParent = parentElement + break + } + + parentElement = parentElement.parentElement + } while (parentElement) + } + + if (scrollParent) { + scrollParent.lastScrollTop = scrollParent.scrollTop + } + }, + touchHasMoved: function(event) { + //判定是否发生移动,其阀值是10px + var touch = event.changedTouches[0], + boundary = tapRecognizer.touchBoundary + return Math.abs(touch.pageX - tapRecognizer.pageX) > boundary || + Math.abs(touch.pageY - tapRecognizer.pageY) > boundary + + }, + + findType: function(targetElement) { + // 安卓chrome浏览器上,模拟的 click 事件不能让 select 打开,故使用 mousedown 事件 + return deviceIsAndroid && targetElement.tagName.toLowerCase() === 'select' ? + 'mousedown' : 'click' + }, + sendClick: function(targetElement, event) { + // 在click之前触发tap事件 + Recognizer.fire(targetElement, 'tap', { + touchEvent: event + }) + var clickEvent, touch + //某些安卓设备必须先移除焦点,之后模拟的click事件才能让新元素获取焦点 + if (document.activeElement && document.activeElement !== targetElement) { + document.activeElement.blur() + } + + touch = event.changedTouches[0] + // 手动触发点击事件,此时必须使用document.createEvent('MouseEvents')来创建事件 + // 及使用initMouseEvent来初始化它 + clickEvent = document.createEvent('MouseEvents') + clickEvent.initMouseEvent(tapRecognizer.findType(targetElement), true, true, window, 1, touch.screenX, + touch.screenY, touch.clientX, touch.clientY, false, false, false, false, 0, null) + clickEvent.touchEvent = event + targetElement.dispatchEvent(clickEvent) + }, + touchstart: function(event) { + //忽略多点触摸 + if (event.targetTouches.length !== 1) { + return true + } + //修正事件源对象 + var targetElement = tapRecognizer.fixTarget(event.target) + var touch = event.targetTouches[0] + if (deviceIsIOS) { + // 判断是否是点击文字,进行选择等操作,如果是,不需要模拟click + var selection = window.getSelection(); + if (selection.rangeCount && !selection.isCollapsed) { + return true + } + var id = touch.identifier + //当 alert 或 confirm 时,点击其他地方,会触发touch事件,identifier相同,此事件应该被忽略 + if (id && isFinite(tapRecognizer.lastTouchIdentifier) && tapRecognizer.lastTouchIdentifier === id) { + event.preventDefault() + return false + } + + tapRecognizer.lastTouchIdentifier = id + + tapRecognizer.updateScrollParent(targetElement) + } + //收集触摸点的信息 + tapRecognizer.status = "tapping" + tapRecognizer.startTime = Date.now() + tapRecognizer.element = targetElement + tapRecognizer.pageX = touch.pageX + tapRecognizer.pageY = touch.pageY + // 如果点击太快,阻止双击带来的放大收缩行为 + if ((tapRecognizer.startTime - tapRecognizer.lastTime) < tapRecognizer.tapDelay) { + event.preventDefault() + } + }, + touchmove: function(event) { + if (tapRecognizer.status !== "tapping") { + return true + } + // 如果事件源元素发生改变,或者发生了移动,那么就取消触发点击事件 + if (tapRecognizer.element !== tapRecognizer.fixTarget(event.target) || + tapRecognizer.touchHasMoved(event)) { + tapRecognizer.status = tapRecognizer.element = 0 + } + + }, + touchend: function(event) { + var targetElement = tapRecognizer.element + var now = Date.now() + //如果是touchstart与touchend相隔太久,可以认为是长按,那么就直接返回 + //或者是在touchstart, touchmove阶段,判定其不该触发点击事件,也直接返回 + if (!targetElement || now - tapRecognizer.startTime > tapRecognizer.tapDelay) { + return true + } + + + tapRecognizer.lastTime = now + + var startTime = tapRecognizer.startTime + tapRecognizer.status = tapRecognizer.startTime = 0 + + targetTagName = targetElement.tagName.toLowerCase() + if (targetTagName === 'label') { + //尝试触发label上可能绑定的tap事件 + Recognizer.fire(targetElement, 'tap', { + touchEvent: event + }) + var forElement = tapRecognizer.findControl(targetElement) + if (forElement) { + tapRecognizer.focus(targetElement) + targetElement = forElement + } + } else if (tapRecognizer.needFocus(targetElement)) { + // 如果元素从touchstart到touchend经历时间过长,那么不应该触发点击事 + // 或者此元素是iframe中的input元素,那么它也无法获点焦点 + if ((now - startTime) > 100 || (deviceIsIOS && window.top !== window && targetTagName === 'input')) { + tapRecognizer.element = 0 + return false + } + + tapRecognizer.focus(targetElement) + deviceIsAndroid && tapRecognizer.sendClick(targetElement, event) + + return false + } + + if (deviceIsIOS) { + //如果它的父容器的滚动条发生改变,那么应该识别为划动或拖动事件,不应该触发点击事件 + var scrollParent = targetElement.tapScrollParent; + if (scrollParent && scrollParent.lastScrollTop !== scrollParent.scrollTop) { + return true + } + } + //如果这不是一个需要使用原生click的元素,则屏蔽原生事件,避免触发两次click + if (!tapRecognizer.needClick(targetElement)) { + event.preventDefault() + // 触发一次模拟的click + tapRecognizer.sendClick(targetElement, event) + } + }, + touchcancel: function() { + tapRecognizer.startTime = tapRecognizer.element = 0 + } +} + +Recognizer.add("tap", tapRecognizer) + +var pressRecognizer = { + events: ['longtap', 'doubletap'], + cancelPress: function (pointer) { + clearTimeout(pointer.pressingHandler) + pointer.pressingHandler = null + }, + touchstart: function (event) { + Recognizer.start(event, function (pointer, touch) { + pointer.pressingHandler = setTimeout(function () { + if (pointer.status === 'tapping') { + Recognizer.fire(event.target, 'longtap', { + touch: touch, + touchEvent: event + }) + } + pressRecognizer.cancelPress(pointer) + }, 800) + if (event.changedTouches.length !== 1) { + pointer.status = 0 + } + }) + + }, + touchmove: function (event) { + Recognizer.move(event, function (pointer) { + if (pointer.distance > 10 && pointer.pressingHandler) { + pressRecognizer.cancelPress(pointer) + if (pointer.status === 'tapping') { + pointer.status = 'panning' + } + } + }) + }, + touchend: function (event) { + Recognizer.end(event, function (pointer, touch) { + pressRecognizer.cancelPress(pointer) + if (pointer.status === 'tapping') { + pointer.lastTime = Date.now() + if (pressRecognizer.lastTap && pointer.lastTime - pressRecognizer.lastTap.lastTime < 300) { + Recognizer.fire(pointer.element, 'doubletap', { + touch: touch, + touchEvent: event + }) + } + + pressRecognizer.lastTap = pointer + } + }) + + }, + touchcancel: function (event) { + Recognizer.end(event, function (pointer) { + pressRecognizer.cancelPress(pointer) + }) + } +} +Recognizer.add('press', pressRecognizer) + +var swipeRecognizer = { + events: ['swipe', 'swipeleft', 'swiperight', 'swipeup', 'swipedown'], + getAngle: function (x, y ) { + return Math.atan2(y, x) * 180 / Math.PI + }, + getDirection: function (x, y) { + var angle = swipeRecognizer.getAngle(x, y) + if ((angle < -45) && (angle > -135)) { + return "up" + } else if ((angle >= 45) && (angle < 315)) { + return "down" + } else if ((angle > -45) && (angle <= 45)) { + return "right" + } else{ + return "left" + } + }, + touchstart: function (event) { + Recognizer.start(event, noop) + }, + touchmove: function (event) { + Recognizer.move(event, noop) + }, + touchend: function (event) { + if(event.changedTouches.length !== 1){ + return + } + Recognizer.end(event, function (pointer, touch) { + var isflick = (pointer.distance > 30 && pointer.distance / pointer.duration > 0.65) + if (isflick) { + var extra = { + deltaX : pointer.deltaX, + deltaY: pointer.deltaY, + touch: touch, + touchEvent: event, + direction: swipeRecognizer.getDirection(pointer.deltaX, pointer.deltaY), + isVertical: pointer.isVertical + } + var target = pointer.element + Recognizer.fire(target, 'swipe', extra) + Recognizer.fire(target, 'swipe' + extra.direction, extra) + } + }) + } +} + +swipeRecognizer.touchcancel = swipeRecognizer.touchend +Recognizer.add('swipe', swipeRecognizer) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + if (typeof define === "function" && define.amd) { + define("yua", [], function() { + return yua + }) + } +// Map over yua in case of overwrite + var _yua = window.yua + yua.noConflict = function(deep) { + if (deep && window.yua === yua) { + window.yua = _yua + } + return yua + } +// Expose yua identifiers, even in AMD +// and CommonJS for browser emulators + if (noGlobal === void 0) { + window.yua = yua + } + return yua + +})); \ No newline at end of file diff --git a/js/yua.js b/js/yua.js new file mode 100644 index 0000000..95c0f46 --- /dev/null +++ b/js/yua.js @@ -0,0 +1,6475 @@ +/*================================================== + * + * @authors yutent (yutent@doui.cc) + * @date 2017-03-21 21:05:57 + * support IE10+ and other browsers + * + ==================================================*/ +(function(global, factory) { + + if (typeof module === "object" && typeof module.exports === "object") { + // For CommonJS and CommonJS-like environments where a proper `window` + // is present, execute the factory and get yua. + // For environments that do not have a `window` with a `document` + // (such as Node.js), expose a factory as module.exports. + // This accentuates the need for the creation of a real `window`. + // e.g. var yua = require("yua")(window); + module.exports = global.document ? factory(global, true) : function(w) { + if (!w.document) { + throw new Error("Yua只能运行在浏览器环境") + } + return factory(w) + } + } else { + factory(global) + } + +// Pass this if window is not defined yet +}(typeof window !== "undefined" ? window : this, function(window, noGlobal){ + +/********************************************************************* + * 全局变量及方法 * + **********************************************************************/ + +var expose = Date.now() +//http://stackoverflow.com/questions/7290086/javascript-use-strict-and-nicks-find-global-function +var DOC = window.document +var head = DOC.head //HEAD元素 +head.insertAdjacentHTML("afterBegin", '') +var ifGroup = head.firstChild + +function log() { + if (yua.config.debug) { +// http://stackoverflow.com/questions/8785624/how-to-safely-wrap-console-log + console.log.apply(console, arguments) + } +} + +/** + * Creates a new object without a prototype. This object is useful for lookup without having to + * guard against prototypically inherited properties via hasOwnProperty. + * + * Related micro-benchmarks: + * - http://jsperf.com/object-create2 + * - http://jsperf.com/proto-map-lookup/2 + * - http://jsperf.com/for-in-vs-object-keys2 + */ +function createMap() { + return Object.create(null) +} + +var subscribers = "$" + expose + +var nullObject = {} //作用类似于noop,只用于代码防御,千万不要在它上面添加属性 +var rword = /[^, ]+/g //切割字符串为一个个小块,以空格或豆号分开它们,结合replace实现字符串的forEach +var rw20g = /\w+/g +var rsvg = /^\[object SVG\w*Element\]$/ +var rwindow = /^\[object (?:Window|DOMWindow|global)\]$/ +var oproto = Object.prototype +var ohasOwn = oproto.hasOwnProperty +var serialize = oproto.toString +var ap = Array.prototype +var aslice = ap.slice +var W3C = window.dispatchEvent +var root = DOC.documentElement +var yuaFragment = DOC.createDocumentFragment() +var cinerator = DOC.createElement("div") +var class2type = {} +"Boolean Number String Function Array Date RegExp Object Error".replace(rword, function (name) { + class2type["[object " + name + "]"] = name.toLowerCase() +}) +var bindingID = 1024 +var IEVersion = NaN +if (window.VBArray) { + IEVersion = document.documentMode || (window.XMLHttpRequest ? 7 : 6) +} + +function noop(){} +function scpCompile(array){ + return Function.apply(noop, array) +} + +function oneObject(array, val) { + if (typeof array === "string") { + array = array.match(rword) || [] + } + var result = {}, + value = val !== void 0 ? val : 1 + for (var i = 0, n = array.length; i < n; i++) { + result[array[i]] = value + } + return result +} + +var generateID = function (mark) { + mark = mark && (mark + '-') || 'yua-' + return mark + Math.floor(Date.now() / 1000).toString(16) + '-' + Math.random().toString(16).slice(2, 6) + '-' + Math.random().toString(16).slice(6, 10) +} + +yua = function (el) { //创建jQuery式的无new 实例化结构 + return new yua.init(el) +} + +/*视浏览器情况采用最快的异步回调*/ +yua.nextTick = new function () {// jshint ignore:line + var tickImmediate = window.setImmediate + var tickObserver = window.MutationObserver + if (tickImmediate) { + return tickImmediate.bind(window) + } + + var queue = [] + function callback() { + var n = queue.length + for (var i = 0; i < n; i++) { + queue[i]() + } + queue = queue.slice(n) + } + + if (tickObserver) { + var node = document.createTextNode("yua") + new tickObserver(callback).observe(node, {characterData: true})// jshint ignore:line + var bool = false + return function (fn) { + queue.push(fn) + bool = !bool + node.data = bool + } + } + + + return function (fn) { + setTimeout(fn, 4) + } +}// jshint ignore:line + +/********************************************************************* + * yua的静态方法定义区 * + **********************************************************************/ + +yua.init = function (el) { + this[0] = this.element = el +} +yua.fn = yua.prototype = yua.init.prototype + +yua.type = function (obj) { //取得目标的类型 + if (obj == null) { + return String(obj) + } + // 早期的webkit内核浏览器实现了已废弃的ecma262v4标准,可以将正则字面量当作函数使用,因此typeof在判定正则时会返回function + return typeof obj === "object" || typeof obj === "function" ? + class2type[serialize.call(obj)] || "object" : + typeof obj +} + +yua.isFunction = function (fn) { + return serialize.call(fn) === "[object Function]" +} + +yua.isWindow = function (obj) { + return rwindow.test(serialize.call(obj)) +} + +/*判定是否是一个朴素的javascript对象(Object),不是DOM对象,不是BOM对象,不是自定义类的实例*/ +yua.isPlainObject = function (obj) { + // 简单的 typeof obj === "object"检测,会致使用isPlainObject(window)在opera下通不过 + return serialize.call(obj) === "[object Object]" && Object.getPrototypeOf(obj) === oproto +} + +//与jQuery.extend方法,可用于浅拷贝,深拷贝 +yua.mix = yua.fn.mix = function () { + var options, name, src, copy, copyIsArray, clone, + target = arguments[0] || {}, + i = 1, + length = arguments.length, + deep = false + + // 如果第一个参数为布尔,判定是否深拷贝 + if (typeof target === "boolean") { + deep = target + target = arguments[1] || {} + i++ + } + + //确保接受方为一个复杂的数据类型 + if (typeof target !== "object" && !yua.isFunction(target)) { + target = {} + } + + //如果只有一个参数,那么新成员添加于mix所在的对象上 + if (i === length) { + target = this + i-- + } + + for (; i < length; i++) { + //只处理非空参数 + if ((options = arguments[i]) != null) { + for (name in options) { + src = target[name] + copy = options[name] + // 防止环引用 + if (target === copy) { + continue + } + if (deep && copy && (yua.isPlainObject(copy) || (copyIsArray = Array.isArray(copy)))) { + + if (copyIsArray) { + copyIsArray = false + clone = src && Array.isArray(src) ? src : [] + + } else { + clone = src && yua.isPlainObject(src) ? src : {} + } + + target[name] = yua.mix(deep, clone, copy) + } else if (copy !== void 0) { + target[name] = copy + } + } + } + } + return target +} + + + + +/*-----------------部分ES6的JS实现 start---------------*/ + +if(!Object.assign){ + Object.defineProperty(Object, 'assign', { + enumerable: false, + value: function(target, first){ + "use strict"; + if(target === undefined || target === null) + throw new TypeError('Can not convert first argument to object') + + var to = Object(target) + for(var i = 0, len = arguments.length; i < len; i++){ + var next = arguments[i] + if(next === undefined || next === null) + continue + + var keys = Object.keys(Object(next)) + for(var j = 0, n = keys.length; j < n; j++){ + var key = keys[j] + var desc = Object.getOwnPropertyDescriptor(next, key) + if(desc !== undefined && desc.enumerable) + to[key] = next[key] + } + } + return to + } + }) +} + +if(!Array.from){ + Object.defineProperty(Array, 'from', { + enumerable: false, + value: (function(){ + var toStr = Object.prototype.toString + var isCallable = function(fn){ + return typeof fn === 'function' || toStr.call(fn) === '[object Function]' + } + + var toInt = function(val){ + var num = val - 0 + if(isNaN(num)) + return 0 + + if(num === 0 || isFinite(num)) + return num + + return (num > 0 ? 1 : -1) * Math.floor(Math.abs(num)) + } + var maxInt = Math.pow(2, 53) - 1 + var toLen = function(val){ + var len = toInt(val) + return Math.min(Math.max(len, 0), maxInt) + } + + return function(arrLike){ + var _this = this + var items = Object(arrLike) + if(arrLike === null) + throw new TypeError('Array.from requires an array-like object - not null or undefined') + + var mapFn = arguments.length > 1 ? arguments[1] : undefined + var other + if(mapFn !== undefined){ + if(!isCallable(mapFn)) + throw new TypeError('Array.from: when provided, the second argument must be a function') + + if(arguments.length > 2) + other = arguments[2] + } + + var len = toLen(items.length) + var arr = isCallable(_this) ? Object(new _this(len)) : new Array(len) + var k = 0 + var kVal + while(k < len){ + kVal = items[k] + if(mapFn) + arr[k] = other === 'undefined' ? mapFn(kVal, k) : mapFn.call(other, kVal, k) + else + arr[k] = kVal + + k++ + } + arr.length = len + return arr + } + })() + }) +} + + + +// 判断数组是否包含指定元素 +if(!Array.prototype.includes){ + Object.defineProperty(Array.prototype, + 'includes', + { + value: function(val){ + for(var i in this){ + if(this[i] === val) + return true + } + return false + }, + enumerable: false + }) +} + +//类似于Array 的splice方法 +if(!String.prototype.splice){ + Object.defineProperty(String.prototype, + 'splice', + { + value: function(start, len, sub){ + var length = this.length, + argLen = arguments.length; + + fill = fill === undefined ? '' : fill + + if(argLen < 1){ + return this + } + + //处理负数 + if(start < 0){ + if(Math.abs(start) >= length) + start = 0 + else + start = length + start + } + + if(argLen === 1){ + return this.slice(0, start) + }else{ + len -= 0; + + var strl = this.slice(0, start), + strr = this.slice(start + len); + + return strl + fill + strr + } + }, + enumerable: false + }) +} + +if(!Date.prototype.getFullWeek){ + //获取当天是本年度第几周 + Object.defineProperty(Date.prototype, + 'getFullWeek', + { + value: function(){ + var thisYear = this.getFullYear(), + that = new Date(thisYear, 0, 1), + firstDay = that.getDay(), + numsOfToday = (this - that) / 86400000; + return Math.ceil((numsOfToday + firstDay) / 7) + }, + enumerable: false + }) + + //获取当天是本月第几周 + Object.defineProperty(Date.prototype, + 'getWeek', + { + value: function(){ + var today = this.getDate(), + thisMonth = this.getMonth(), + thisYear = this.getFullYear(), + firstDay = new Date(thisYear, thisMonth, 1).getDay(); + return Math.ceil((today + firstDay) / 7) + }, + enumerable: false + }) +} + +if(!Date.isDate){ + Object.defineProperty(Date, + 'isDate', + { + value: function(obj){ + return (typeof obj === 'object') && obj.getTime ? true : false + }, + enumerable: false + }) +} + +//时间格式化 +if(!Date.prototype.format){ + Object.defineProperty(Date.prototype, + 'format', + { + value: function(str){ + str = str || 'Y-m-d H:i:s' + var week = ['Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun'], + dt = { + 'fullyear': this.getFullYear(), + 'year': this.getYear(), + 'fullweek': this.getFullWeek(), + 'week': this.getWeek(), + 'month': this.getMonth() + 1, + 'date': this.getDate(), + 'day': week[this.getDay()], + 'hours': this.getHours(), + 'minutes': this.getMinutes(), + 'seconds': this.getSeconds() + }, + re; + + dt.g = dt.hours > 12 ? dt.hours - 12 : dt.hours + + re = { + 'Y': dt.fullyear, + 'y': dt.year, + 'm': dt.month < 10 ? '0' + dt.month : dt.month, + 'n': dt.month, + 'd': dt.date < 10 ? '0' + dt.date : dt.date, + 'j': dt.date, + 'H': dt.hours < 10 ? '0' + dt.hours : dt.hours, + 'h': dt.g < 10 ? '0' + dt.g : dt.g, + 'G': dt.hours, + 'g': dt.g, + 'i': dt.minutes < 10 ? '0' + dt.minutes : dt.minutes, + 's': dt.seconds < 10 ? '0' + dt.seconds : dt.seconds, + 'W': dt.fullweek, + 'w': dt.week, + 'D': dt.day + } + + for(var i in re){ + str = str.replace(new RegExp(i, 'g'), re[i]) + } + return str + }, + enumerable: false + }) +} + + +/*-----------------部分ES6的JS实现 ending---------------*/ + + + + + + + + + + + + +yua.mix({ + rword: rword, + subscribers: subscribers, + version: '1.0.0-touch', + ui: {}, + log: log, + slice: function (nodes, start, end) { + return aslice.call(nodes, start, end) + }, + noop: noop, + /*如果不用Error对象封装一下,str在控制台下可能会乱码*/ + error: function (str, e) { + throw new (e || Error)(str)// jshint ignore:line + }, + /*将一个以空格或逗号隔开的字符串或数组,转换成一个键值都为1的对象*/ + oneObject: oneObject, + /* yua.range(10) + => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + yua.range(1, 11) + => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + yua.range(0, 30, 5) + => [0, 5, 10, 15, 20, 25] + yua.range(0, -10, -1) + => [0, -1, -2, -3, -4, -5, -6, -7, -8, -9] + yua.range(0) + => []*/ + range: function (start, end, step) { // 用于生成整数数组 + step || (step = 1) + if (end == null) { + end = start || 0 + start = 0 + } + var index = -1, + length = Math.max(0, Math.ceil((end - start) / step)), + result = new Array(length) + while (++index < length) { + result[index] = start + start += step + } + return result + }, + eventHooks: {}, + /*绑定事件*/ + bind: function (el, type, fn, phase) { + var hooks = yua.eventHooks; + type = type.split(','); + yua.each(type, function(i, t){ + t = t.trim() + var hook = hooks[t]; + if (typeof hook === "object") { + type = hook.type || type + phase = hook.phase || !!phase + fn = hook.fix ? hook.fix(el, fn) : fn + } + el.addEventListener(t, fn, phase) + }) + return fn + }, + /*卸载事件*/ + unbind: function (el, type, fn, phase) { + var hooks = yua.eventHooks; + type = type.split(','); + fn = fn || noop + yua.each(type, function(i, t){ + t = t.trim() + var hook = hooks[t]; + if (typeof hook === "object") { + type = hook.type || type + phase = hook.phase || !!phase + } + el.removeEventListener(t, fn, phase) + }) + }, + /*读写删除元素节点的样式*/ + css: function (node, name, value) { + if (node instanceof yua) { + node = node[0] + } + var prop = /[_-]/.test(name) ? camelize(name) : name, fn + name = yua.cssName(prop) || prop + if (value === void 0 || typeof value === "boolean") { //获取样式 + fn = cssHooks[prop + ":get"] || cssHooks["@:get"] + if (name === "background") { + name = "backgroundColor" + } + var val = fn(node, name) + return value === true ? parseFloat(val) || 0 : val + } else if (value === "") { //请除样式 + node.style[name] = "" + } else { //设置样式 + if (value == null || value !== value) { + return + } + if (isFinite(value) && !yua.cssNumber[prop]) { + value += "px" + } + fn = cssHooks[prop + ":set"] || cssHooks["@:set"] + fn(node, name, value) + } + }, + /*遍历数组与对象,回调的第一个参数为索引或键名,第二个或元素或键值*/ + each: function (obj, fn) { + if (obj) { //排除null, undefined + var i = 0 + if (isArrayLike(obj)) { + for (var n = obj.length; i < n; i++) { + if (fn(i, obj[i]) === false) + break + } + } else { + for (i in obj) { + if (obj.hasOwnProperty(i) && fn(i, obj[i]) === false) { + break + } + } + } + } + }, + Array: { + /*只有当前数组不存在此元素时只添加它*/ + ensure: function (target, item) { + if (target.indexOf(item) === -1) { + return target.push(item) + } + }, + /*移除数组中指定位置的元素,返回布尔表示成功与否*/ + removeAt: function (target, index) { + return !!target.splice(index, 1).length + }, + /*移除数组中第一个匹配传参的那个元素,返回布尔表示成功与否*/ + remove: function (target, item) { + var index = target.indexOf(item) + if (~index) + return yua.Array.removeAt(target, index) + return false + } + }, + /** + * [ls localStorage操作] + * @param {[type]} name [键名] + * @param {[type]} val [键值,为空时删除] + * @return + */ + ls: function(name, val){ + if(!window.localStorage) + return log('该浏览器不支持本地储存localStorage') + + if(this.type(name) === 'object'){ + for(var i in name){ + localStorage.setItem(i, name[i]); + } + return; + } + switch(arguments.length){ + case 1: + return localStorage.getItem(name); + default: + if((this.type(val) == 'string' && val.trim() === '') || val === null){ + localStorage.removeItem(name); + return; + } + if(this.type(val) !== 'object' && this.type(val) !== 'array'){ + localStorage.setItem(name, val.toString()); + }else{ + localStorage.setItem(name, JSON.stringify(val)); + } + } + }, + /** + * [cookie cookie 操作 ] + * @param name [cookie名] + * @param value [cookie值] + * @param {[json]} opt [有效期,域名,路径等] + * @return {[boolean]} [读取时返回对应的值,写入时返回true] + */ + cookie: function(name, value, opt){ + if(arguments.length > 1){ + if(!name) return; + + //设置默认的参数 + opt = opt || {}; + opt = this.mix({expires: '', path: '/', domain: document.domain, secure: ''}, opt); + + if(!value){ + document.cookie = encodeURIComponent(name) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=" + opt.domain + "; path=" + opt.path; + return true; + } + if (opt.expires) { + switch (opt.expires.constructor) { + case Number: + opt.expires = (opt.expires === Infinity) ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + opt.expires; + break; + case String: + opt.expires = "; expires=" + opt.expires; + break; + case Date: + opt.expires = "; expires=" + opt.expires.toUTCString(); + break; + } + } + document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) + opt.expires + "; domain=" + opt.domain + "; path=" + opt.path + "; " + opt.secure; + return true; + }else{ + if (!name){ + var keys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/); + for (var i=0, len=keys.length; i>> 0)) { + return true //由于ecma262v5能修改对象属性的enumerable,因此不能用propertyIsEnumerable来判定了 + } + } + return false +} + +// https://github.com/rsms/js-lru +var Cache = new function() {// jshint ignore:line + function LRU(maxLength) { + this.size = 0 + this.limit = maxLength + this.head = this.tail = void 0 + this._keymap = {} + } + + var p = LRU.prototype + + p.put = function(key, value) { + var entry = { + key: key, + value: value + } + this._keymap[key] = entry + if (this.tail) { + this.tail.newer = entry + entry.older = this.tail + } else { + this.head = entry + } + this.tail = entry + if (this.size === this.limit) { + this.shift() + } else { + this.size++ + } + return value + } + + p.shift = function() { + var entry = this.head + if (entry) { + this.head = this.head.newer + this.head.older = + entry.newer = + entry.older = + this._keymap[entry.key] = void 0 + delete this._keymap[entry.key] //#1029 + } + } + p.get = function(key) { + var entry = this._keymap[key] + if (entry === void 0) + return + if (entry === this.tail) { + return entry.value + } + // HEAD--------------TAIL + // <.older .newer> + // <--- add direction -- + // A B C E + if (entry.newer) { + if (entry === this.head) { + this.head = entry.newer + } + entry.newer.older = entry.older // C <-- E. + } + if (entry.older) { + entry.older.newer = entry.newer // C. --> E + } + entry.newer = void 0 // D --x + entry.older = this.tail // D. --> E + if (this.tail) { + this.tail.newer = entry // E. <-- D + } + this.tail = entry + return entry.value + } + return LRU +}// jshint ignore:line + + + + + + + + + + + +/********************************************************************* + * DOM 底层补丁 * + **********************************************************************/ + +//safari5+是把contains方法放在Element.prototype上而不是Node.prototype +if (!DOC.contains) { + Node.prototype.contains = function (arg) { + return !!(this.compareDocumentPosition(arg) & 16) + } +} +yua.contains = function(root, el) { + try { + while ((el = el.parentNode)) + if (el === root) + return true + return false + } catch (e) { + return false + } +} + +if (window.SVGElement) { + var svgns = "http://www.w3.org/2000/svg" + var svg = DOC.createElementNS(svgns, "svg") + svg.innerHTML = '' + if (!rsvg.test(svg.firstChild)) {// #409 + /* jshint ignore:start */ + function enumerateNode(node, targetNode) { + if (node && node.childNodes) { + var nodes = node.childNodes + for (var i = 0, el; el = nodes[i++]; ) { + if (el.tagName) { + var svg = DOC.createElementNS(svgns, + el.tagName.toLowerCase()) + // copy attrs + ap.forEach.call(el.attributes, function (attr) { + svg.setAttribute(attr.name, attr.value) + }) + // 递归处理子节点 + enumerateNode(el, svg) + targetNode.appendChild(svg) + } + } + } + } + /* jshint ignore:end */ + Object.defineProperties(SVGElement.prototype, { + "outerHTML": {//IE9-11,firefox不支持SVG元素的innerHTML,outerHTML属性 + enumerable: true, + configurable: true, + get: function () { + return new XMLSerializer().serializeToString(this) + }, + set: function (html) { + var tagName = this.tagName.toLowerCase(), + par = this.parentNode, + frag = yua.parseHTML(html) + // 操作的svg,直接插入 + if (tagName === "svg") { + par.insertBefore(frag, this) + // svg节点的子节点类似 + } else { + var newFrag = DOC.createDocumentFragment() + enumerateNode(frag, newFrag) + par.insertBefore(newFrag, this) + } + par.removeChild(this) + } + }, + "innerHTML": { + enumerable: true, + configurable: true, + get: function () { + var s = this.outerHTML + var ropen = new RegExp("<" + this.nodeName + '\\b(?:(["\'])[^"]*?(\\1)|[^>])*>', "i") + var rclose = new RegExp("<\/" + this.nodeName + ">$", "i") + return s.replace(ropen, "").replace(rclose, "") + }, + set: function (html) { + if (yua.clearHTML) { + yua.clearHTML(this) + var frag = yua.parseHTML(html) + enumerateNode(frag, this) + } + } + } + }) + } +} + +//========================= event binding ==================== + +var eventHooks = yua.eventHooks + +//针对firefox, chrome修正mouseenter, mouseleave(chrome30+) +if (!("onmouseenter" in root)) { + yua.each({ + mouseenter: "mouseover", + mouseleave: "mouseout" + }, function (origType, fixType) { + eventHooks[origType] = { + type: fixType, + fix: function (elem, fn) { + return function (e) { + var t = e.relatedTarget + if (!t || (t !== elem && !(elem.compareDocumentPosition(t) & 16))) { + delete e.type + e.type = origType + return fn.call(elem, e) + } + } + } + } + }) +} + +//针对IE9+, w3c修正animationend +yua.each({ + AnimationEvent: "animationend", + WebKitAnimationEvent: "webkitAnimationEnd" +}, function (construct, fixType) { + if (window[construct] && !eventHooks.animationend) { + eventHooks.animationend = { + type: fixType + } + } +}) + +if (DOC.onmousewheel === void 0) { + /* IE6-11 chrome mousewheel wheelDetla 下 -120 上 120 + firefox DOMMouseScroll detail 下3 上-3 + firefox wheel detlaY 下3 上-3 + IE9-11 wheel deltaY 下40 上-40 + chrome wheel deltaY 下100 上-100 */ + eventHooks.mousewheel = { + type: "wheel", + fix: function (elem, fn) { + return function (e) { + e.wheelDeltaY = e.wheelDelta = e.deltaY > 0 ? -120 : 120 + e.wheelDeltaX = 0 + Object.defineProperty(e, "type", { + value: "mousewheel" + }) + fn.call(elem, e) + } + } + } +} + + + + + + + + + + + + + + + + +/********************************************************************* + * 配置系统 * + **********************************************************************/ + +function kernel(settings) { + for (var p in settings) { + if (!ohasOwn.call(settings, p)) + continue + var val = settings[p] + if (typeof kernel.plugins[p] === "function") { + kernel.plugins[p](val) + } else if (typeof kernel[p] === "object") { + yua.mix(kernel[p], val) + } else { + kernel[p] = val + } + } + return this +} +yua.config = kernel + +var openTag, closeTag, rexpr, rexprg, rbind, rregexp = /[-.*+?^${}()|[\]\/\\]/g + +function escapeRegExp(target) { + //http://stevenlevithan.com/regex/xregexp/ + //将字符串安全格式化为正则表达式的源码 + return (target + "").replace(rregexp, "\\$&") +} + +var plugins = { + interpolate: function (array) { + openTag = array[0] + closeTag = array[1] + if (openTag === closeTag) { + throw new SyntaxError("openTag!==closeTag") + var test = openTag + "test" + closeTag + cinerator.innerHTML = test + if (cinerator.innerHTML !== test && cinerator.innerHTML.indexOf("<") > -1) { + throw new SyntaxError("此定界符不合法") + } + cinerator.innerHTML = "" + } + kernel.openTag = openTag + kernel.closeTag = closeTag + var o = escapeRegExp(openTag), + c = escapeRegExp(closeTag) + rexpr = new RegExp(o + "([\\s\\S]*)" + c) + rexprg = new RegExp(o + "([\\s\\S]*)" + c, "g") + rbind = new RegExp(o + "[\\s\\S]*" + c + "|\\s:") //此处有疑问 + } +} +kernel.plugins = plugins +kernel.plugins['interpolate'](["{{", "}}"]) + +kernel.async = true +kernel.debug = true +kernel.paths = {} +kernel.shim = {} +kernel.maxRepeatSize = 100 + +function $watch(expr, binding) { + var $events = this.$events || (this.$events = {}), + queue = $events[expr] || ($events[expr] = []) + + if (typeof binding === "function") { + var backup = binding + backup.uuid = "_"+ (++bindingID) + binding = { + element: root, + type: "user-watcher", + handler: noop, + vmodels: [this], + expr: expr, + uuid: backup.uuid + } + binding.wildcard = /\*/.test(expr) + } + + if (!binding.update) { + if (/\w\.*\B/.test(expr) || expr === "*") { + binding.getter = noop + var host = this + binding.update = function () { + var args = this.fireArgs || [] + if (args[2]) + binding.handler.apply(host, args) + delete this.fireArgs + } + queue.sync = true + yua.Array.ensure(queue, binding) + } else { + yua.injectBinding(binding) + } + if (backup) { + binding.handler = backup + } + } else if (!binding.oneTime) { + yua.Array.ensure(queue, binding) + } + + return function () { + binding.update = binding.getter = binding.handler = noop + binding.element = DOC.createElement("a") + } +} + +function $emit(key, args) { + var event = this.$events + if (event && event[key]) { + if (args) { + args[2] = key + } + var arr = event[key] + notifySubscribers(arr, args) + if (args && event['*'] && !/\./.test(key)) { + for (var sub, k = 0; sub = event["*"][k++]; ) { + try { + sub.handler.apply(this, args) + } catch (e) { + } + } + } + var parent = this.$up + if (parent) { + if (this.$pathname) { + $emit.call(parent, this.$pathname + "." + key, args)//以确切的值往上冒泡 + } + $emit.call(parent, "*." + key, args)//以模糊的值往上冒泡 + } + } else { + parent = this.$up + if (this.$ups) { + for (var i in this.$ups) { + + $emit.call(this.$ups[i], i + "." + key, args)//以确切的值往上冒泡 + } + return + } + if (parent) { + var p = this.$pathname + if (p === "") + p = "*" + var path = p + "." + key; + arr = path.split("."); + + args = args.concat([path, key]) + + if (arr.indexOf("*") === -1) { + $emit.call(parent, path, args)//以确切的值往上冒泡 + arr[1] = "*" + $emit.call(parent, arr.join("."), args)//以模糊的值往上冒泡 + } else { + $emit.call(parent, path, args)//以确切的值往上冒泡 + } + } + } +} + +function collectDependency(el, key) { + do { + if (el.$watch) { + var e = el.$events || (el.$events = {}) + var array = e[key] || (e[key] = []) + dependencyDetection.collectDependency(array) + return + } + el = el.$up + if (el) { + key = el.$pathname + "." + key + } else { + break + } + } while (true) +} + +function notifySubscribers(subs, args) { + if (!subs) + return + if (new Date() - beginTime > 444 && typeof subs[0] === "object") { + rejectDisposeQueue() + } + var users = [], renders = [] + for (var i = 0, sub; sub = subs[i++]; ) { + if (sub.type === "user-watcher") { + users.push(sub) + } else { + renders.push(sub) + } + } + if (kernel.async) { + buffer.render()//1 + for (i = 0; sub = renders[i++]; ) { + if (sub.update) { + sub.uuid = sub.uuid || "_"+(++bindingID) + var uuid = sub.uuid + if (!buffer.queue[uuid]) { + buffer.queue[uuid] = "__" + buffer.queue.push(sub) + } + } + } + } else { + for (i = 0; sub = renders[i++]; ) { + if (sub.update) { + sub.update()//最小化刷新DOM树 + } + } + } + for (i = 0; sub = users[i++]; ) { + if (args && args[2] === sub.expr || sub.wildcard) { + sub.fireArgs = args + } + sub.update() + } +} + + + + + + + + + + +//yua最核心的方法的两个方法之一(另一个是yua.scan),返回一个ViewModel(VM) +var VMODELS = yua.vmodels = {} //所有vmodel都储存在这里 +yua.define = function (source) { + var $id = source.$id + if (!$id) { + log("warning: vm必须指定$id") + } + var vmodel = modelFactory(source) + vmodel.$id = $id + return VMODELS[$id] = vmodel +} + +//一些不需要被监听的属性 +var $$skipArray = oneObject("$id,$watch,$fire,$events,$model,$skipArray,$active,$pathname,$up,$ups,$track,$accessors") + +//如果浏览器不支持ecma262v5的Object.defineProperties或者存在BUG,比如IE8 +//标准浏览器使用__defineGetter__, __defineSetter__实现 + +function modelFactory(source, options) { + options = options || {} + options.watch = true + return observeObject(source, options) +} + +//监听对象属性值的变化(注意,数组元素不是数组的属性),通过对劫持当前对象的访问器实现 +//监听对象或数组的结构变化, 对对象的键值对进行增删重排, 或对数组的进行增删重排,都属于这范畴 +// 通过比较前后代理VM顺序实现 +function Component() { +} + +function observeObject(source, options) { + if (!source || (source.$id && source.$accessors) || (source.nodeName && source.nodeType > 0)) { + return source + } + //source为原对象,不能是元素节点或null + //options,可选,配置对象,里面有old, force, watch这三个属性 + options = options || nullObject + var force = options.force || nullObject + var old = options.old + var oldAccessors = old && old.$accessors || nullObject + var $vmodel = new Component() //要返回的对象, 它在IE6-8下可能被偷龙转凤 + var accessors = {} //监控属性 + var hasOwn = {} + var skip = [] + var simple = [] + var $skipArray = {} + if (source.$skipArray) { + $skipArray = oneObject(source.$skipArray) + delete source.$skipArray + } + //处理计算属性 + var computed = source.$computed + if (computed) { + delete source.$computed + for (var name in computed) { + hasOwn[name] = true; + (function (key, value) { + var old; + if(typeof value === 'function'){ + value = {get: value, set: noop} + } + if(typeof value.set !== 'function'){ + value.set = noop + } + accessors[key] = { + get: function () { + return old = value.get.call(this) + }, + set: function (x) { + var older = old,newer; + value.set.call(this, x) + newer = this[key] + if (this.$fire && (newer !== older)) { + this.$fire(key, newer, older) + } + }, + enumerable: true, + configurable: true + } + })(name, computed[name])// jshint ignore:line + } + } + + for (name in source) { + var value = source[name] + if (!$$skipArray[name]) + hasOwn[name] = true + if (typeof value === "function" || (value && value.nodeName && value.nodeType > 0) || + (!force[name] && (name.charAt(0) === "$" || $$skipArray[name] || $skipArray[name]))) { + skip.push(name) + } else if (isComputed(value)) { + log("warning:计算属性建议放在$computed对象中统一定义"); + (function (key, value) { + var old; + if(typeof value === 'function'){ + value = {get: value, set: noop} + } + if(typeof value.set !== 'function'){ + value.set = noop + } + accessors[key] = { + get: function () { + return old = value.get.call(this) + }, + set: function (x) { + var older = old,newer; + value.set.call(this, x) + newer = this[key] + if (this.$fire && (newer !== older)) { + this.$fire(key, newer, older) + } + }, + enumerable: true, + configurable: true + } + })(name, value)// jshint ignore:line + } else { + simple.push(name) + if (oldAccessors[name]) { + accessors[name] = oldAccessors[name] + } else { + accessors[name] = makeGetSet(name, value) + } + } + } + + accessors["$model"] = $modelDescriptor + $vmodel = Object.defineProperties($vmodel, accessors, source) + function trackBy(name) { + return hasOwn[name] === true + } + skip.forEach(function (name) { + $vmodel[name] = source[name] + }) + + + /* jshint ignore:start */ + hideProperty($vmodel, "$ups", null) + hideProperty($vmodel, "$id", "anonymous") + hideProperty($vmodel, "$up", old ? old.$up : null) + hideProperty($vmodel, "$track", Object.keys(hasOwn)) + hideProperty($vmodel, "$active", false) + hideProperty($vmodel, "$pathname", old ? old.$pathname : "") + hideProperty($vmodel, "$accessors", accessors) + hideProperty($vmodel, "hasOwnProperty", trackBy) + if (options.watch) { + hideProperty($vmodel, "$watch", function () { + return $watch.apply($vmodel, arguments) + }) + hideProperty($vmodel, "$fire", function (path, a) { + if (path.indexOf("all!") === 0) { + var ee = path.slice(4) + for (var i in yua.vmodels) { + var v = yua.vmodels[i] + v.$fire && v.$fire.apply(v, [ee, a]) + } + } else { + $emit.call($vmodel, path, [a]) + } + }) + } + /* jshint ignore:end */ + + //必须设置了$active,$events + simple.forEach(function (name) { + var oldVal = old && old[name] + var val = $vmodel[name] = source[name] + if (val && typeof val === "object") { + val.$up = $vmodel + val.$pathname = name + } + $emit.call($vmodel, name,[val,oldVal]) + }) + for (name in computed) { + value = $vmodel[name] + } + $vmodel.$active = true + + return $vmodel +} + + + + + + + + + + + + + + + + + + + + + + + + + + + +/* + 新的VM拥有如下私有属性 + $id: vm.id + $events: 放置$watch回调与绑定对象 + $watch: 增强版$watch + $fire: 触发$watch回调 + $track:一个数组,里面包含用户定义的所有键名 + $active:boolean,false时防止依赖收集 + $model:返回一个纯净的JS对象 + $accessors:放置所有读写器的数据描述对象 + $up:返回其上级对象 + $pathname:返回此对象在上级对象的名字,注意,数组元素的$pathname为空字符串 + ============================= + $skipArray:用于指定不可监听的属性,但VM生成是没有此属性的 + */ +function isComputed(val) {//speed up! + if (val && typeof val === "object") { + for (var i in val) { + if (i !== "get" && i !== "set") { + return false + } + } + return typeof val.get === "function" + } +} +function makeGetSet(key, value) { + var childVm, value = NaN + return { + get: function () { + if (this.$active) { + collectDependency(this, key) + } + return value + }, + set: function (newVal) { + if (value === newVal) + return + var oldValue = value + childVm = observe(newVal, value) + if (childVm) { + value = childVm + } else { + childVm = void 0 + value = newVal + } + + if (Object(childVm) === childVm) { + childVm.$pathname = key + childVm.$up = this + } + if (this.$active) { + $emit.call(this, key, [value, oldValue]) + } + }, + enumerable: true, + configurable: true + } +} + +function observe(obj, old, hasReturn, watch) { + if (Array.isArray(obj)) { + return observeArray(obj, old, watch) + } else if (yua.isPlainObject(obj)) { + if (old && typeof old === 'object') { + var keys = Object.keys(obj) + var keys2 = Object.keys(old) + if (keys.join(";") === keys2.join(";")) { + for (var i in obj) { + if (obj.hasOwnProperty(i)) { + old[i] = obj[i] + } + } + return old + } + old.$active = false + } + return observeObject(obj, { + old: old, + watch: watch + }) + } + if (hasReturn) { + return obj + } +} + +function observeArray(array, old, watch) { + if (old && old.splice) { + var args = [0, old.length].concat(array) + old.splice.apply(old, args) + return old + } else { + for (var i in newProto) { + array[i] = newProto[i] + } + hideProperty(array, "$up", null) + hideProperty(array, "$pathname", "") + hideProperty(array, "$track", createTrack(array.length)) + + array._ = observeObject({ + length: NaN + }, { + watch: true + }) + array._.length = array.length + array._.$watch("length", function (a, b) { + $emit.call(array.$up, array.$pathname + ".length", [a, b]) + }) + if (watch) { + hideProperty(array, "$watch", function () { + return $watch.apply(array, arguments) + }) + } + + Object.defineProperty(array, "$model", $modelDescriptor) + + for (var j = 0, n = array.length; j < n; j++) { + var el = array[j] = observe(array[j], 0, 1, 1) + if (Object(el) === el) {//#1077 + el.$up = array + } + } + + return array + } +} + +function hideProperty(host, name, value) { + + Object.defineProperty(host, name, { + value: value, + writable: true, + enumerable: false, + configurable: true + }) + +} + +function toJson(val) { + var xtype = yua.type(val) + if (xtype === "array") { + var array = [] + for (var i = 0; i < val.length; i++) { + array[i] = toJson(val[i]) + } + return array + } else if (xtype === "object") { + var obj = {} + for (i in val) { + if (val.hasOwnProperty(i)) { + var value = val[i] + obj[i] = value && value.nodeType ? value : toJson(value) + } + } + return obj + } + return val +} + +var $modelDescriptor = { + get: function () { + return toJson(this) + }, + set: noop, + enumerable: false, + configurable: true + } + + + + + +/********************************************************************* + * 监控数组(与:each, :repeat配合使用) * + **********************************************************************/ + +var arrayMethods = ['push', 'pop', 'shift', 'unshift', 'splice'] +var arrayProto = Array.prototype +var newProto = { + notify: function () { + $emit.call(this.$up, this.$pathname) + }, + set: function (index, val) { + if (((index >>> 0) === index) && this[index] !== val) { + if (index > this.length) { + throw Error(index + "set方法的第一个参数不能大于原数组长度") + } + $emit.call(this.$up, this.$pathname + ".*", [val, this[index]]) + this.splice(index, 1, val) + } + }, + contains: function (el) { //判定是否包含 + return this.indexOf(el) !== -1 + }, + ensure: function (el) { + if (!this.contains(el)) { //只有不存在才push + this.push(el) + } + return this + }, + pushArray: function (arr) { + return this.push.apply(this, arr) + }, + remove: function (el) { //移除第一个等于给定值的元素 + return this.removeAt(this.indexOf(el)) + }, + removeAt: function (index) { //移除指定索引上的元素 + if ((index >>> 0) === index) { + return this.splice(index, 1) + } + return [] + }, + size: function () { //取得数组长度,这个函数可以同步视图,length不能 + return this._.length + }, + removeAll: function (all) { //移除N个元素 + if (Array.isArray(all)) { + for (var i = this.length - 1; i >= 0; i--) { + if (all.indexOf(this[i]) !== -1) { + _splice.call(this.$track, i, 1) + _splice.call(this, i, 1) + + } + } + } else if (typeof all === "function") { + for (i = this.length - 1; i >= 0; i--) { + var el = this[i] + if (all(el, i)) { + _splice.call(this.$track, i, 1) + _splice.call(this, i, 1) + + } + } + } else { + _splice.call(this.$track, 0, this.length) + _splice.call(this, 0, this.length) + + } + if (!W3C) { + this.$model = toJson(this) + } + this.notify() + this._.length = this.length + }, + clear: function () { + this.removeAll() + return this + } +} + +var _splice = arrayProto.splice +arrayMethods.forEach(function (method) { + var original = arrayProto[method] + newProto[method] = function () { + // 继续尝试劫持数组元素的属性 + var args = [] + for (var i = 0, n = arguments.length; i < n; i++) { + args[i] = observe(arguments[i], 0, 1, 1) + } + var result = original.apply(this, args) + addTrack(this.$track, method, args) + if (!W3C) { + this.$model = toJson(this) + } + this.notify() + this._.length = this.length + return result + } +}) + +"sort,reverse".replace(rword, function (method) { + newProto[method] = function () { + var oldArray = this.concat() //保持原来状态的旧数组 + var newArray = this + var mask = Math.random() + var indexes = [] + var hasSort = false + arrayProto[method].apply(newArray, arguments) //排序 + for (var i = 0, n = oldArray.length; i < n; i++) { + var neo = newArray[i] + var old = oldArray[i] + if (neo === old) { + indexes.push(i) + } else { + var index = oldArray.indexOf(neo) + indexes.push(index)//得到新数组的每个元素在旧数组对应的位置 + oldArray[index] = mask //屏蔽已经找过的元素 + hasSort = true + } + } + if (hasSort) { + sortByIndex(this.$track, indexes) + if (!W3C) { + this.$model = toJson(this) + } + this.notify() + } + return this + } +}) + +function sortByIndex(array, indexes) { + var map = {}; + for (var i = 0, n = indexes.length; i < n; i++) { + map[i] = array[i] + var j = indexes[i] + if (j in map) { + array[i] = map[j] + delete map[j] + } else { + array[i] = array[j] + } + } +} + +function createTrack(n) { + var ret = [] + for (var i = 0; i < n; i++) { + ret[i] = generateID("$proxy$each") + } + return ret +} + +function addTrack(track, method, args) { + switch (method) { + case 'push': + case 'unshift': + args = createTrack(args.length) + break + case 'splice': + if (args.length > 2) { + // 0, 5, a, b, c --> 0, 2, 0 + // 0, 5, a, b, c, d, e, f, g--> 0, 0, 3 + var del = args[1] + var add = args.length - 2 + // args = [args[0], Math.max(del - add, 0)].concat(createTrack(Math.max(add - del, 0))) + args = [args[0], args[1]].concat(createTrack(args.length - 2)) + } + break + } + Array.prototype[method].apply(track, args) +} + + + + + + + + + + + + + + + +/********************************************************************* + * 依赖调度系统 * + **********************************************************************/ + +//检测两个对象间的依赖关系 +var dependencyDetection = (function () { + var outerFrames = [] + var currentFrame + return { + begin: function (binding) { + //accessorObject为一个拥有callback的对象 + outerFrames.push(currentFrame) + currentFrame = binding + }, + end: function () { + currentFrame = outerFrames.pop() + }, + collectDependency: function (array) { + if (currentFrame) { + //被dependencyDetection.begin调用 + currentFrame.callback(array) + } + } + }; +})() + +//将绑定对象注入到其依赖项的订阅数组中 +var roneval = /^on$/ + +function returnRandom() { + return new Date() - 0 +} + +yua.injectBinding = function (binding) { + + binding.handler = binding.handler || directives[binding.type].update || noop + binding.update = function () { + var begin = false + if (!binding.getter) { + begin = true + dependencyDetection.begin({ + callback: function (array) { + injectDependency(array, binding) + } + }) + binding.getter = parseExpr(binding.expr, binding.vmodels, binding) + binding.observers.forEach(function (a) { + a.v.$watch(a.p, binding) + }) + delete binding.observers + } + try { + var args = binding.fireArgs, a, b + delete binding.fireArgs + if (!args) { + if (binding.type === "on") { + a = binding.getter + "" + } else { + try { + a = binding.getter.apply(0, binding.args) + } catch(e) { + a = null + } + } + } else { + a = args[0] + b = args[1] + } + b = typeof b === "undefined" ? binding.oldValue : b + if (binding._filters) { + a = filters.$filter.apply(0, [a].concat(binding._filters)) + } + if (binding.signature) { + var xtype = yua.type(a) + if (xtype !== "array" && xtype !== "object") { + throw Error("warning:" + binding.expr + "只能是对象或数组") + } + binding.xtype = xtype + var vtrack = getProxyIds(binding.proxies || [], xtype) + var mtrack = a.$track || (xtype === "array" ? createTrack(a.length) : + Object.keys(a)) + binding.track = mtrack + if (vtrack !== mtrack.join(";")) { + binding.handler(a, b) + binding.oldValue = 1 + } + } else if (Array.isArray(a) ? a.length !== (b && b.length) : false) { + binding.handler(a, b) + binding.oldValue = a.concat() + } else if (!("oldValue" in binding) || a !== b) { + binding.handler(a, b) + binding.oldValue = Array.isArray(a) ? a.concat() : a + } + } catch (e) { + delete binding.getter + log("warning:exception throwed in [yua.injectBinding] ", e) + var node = binding.element + if (node && node.nodeType === 3) { + node.nodeValue = openTag + (binding.oneTime ? "::" : "") + binding.expr + closeTag + } + } finally { + begin && dependencyDetection.end() + + } + } + binding.update() +} + +//将依赖项(比它高层的访问器或构建视图刷新函数的绑定对象)注入到订阅者数组 +function injectDependency(list, binding) { + if (binding.oneTime) + return + if (list && yua.Array.ensure(list, binding) && binding.element) { + injectDisposeQueue(binding, list) + if (new Date() - beginTime > 444) { + rejectDisposeQueue() + } + } +} + +function getProxyIds(a, isArray) { + var ret = [] + for (var i = 0, el; el = a[i++]; ) { + ret.push(isArray ? el.$id : el.$key) + } + return ret.join(";") +} + +/********************************************************************* + * 定时GC回收机制 (基于1.6基于频率的GC) * + **********************************************************************/ + +var disposeQueue = yua.$$subscribers = [] +var beginTime = new Date() + +//添加到回收列队中 +function injectDisposeQueue(data, list) { + data.list = list + data.i = ~~data.i + if (!data.uuid) { + data.uuid = "_" + (++bindingID) + } + if (!disposeQueue[data.uuid]) { + disposeQueue[data.uuid] = "__" + disposeQueue.push(data) + } +} + +var lastGCIndex = 0 +function rejectDisposeQueue(data) { + var i = lastGCIndex || disposeQueue.length + var threshold = 0 + while (data = disposeQueue[--i]) { + if (data.i < 7) { + if (data.element === null) { + disposeQueue.splice(i, 1) + if (data.list) { + yua.Array.remove(data.list, data) + delete disposeQueue[data.uuid] + } + continue + } + if (shouldDispose(data.element)) { //如果它的虚拟DOM不在VTree上或其属性不在VM上 + disposeQueue.splice(i, 1) + yua.Array.remove(data.list, data) + disposeData(data) + //yua会在每次全量更新时,比较上次执行时间, + //假若距离上次有半秒,就会发起一次GC,并且只检测当中的500个绑定 + //而一个正常的页面不会超过2000个绑定(500即取其4分之一) + //用户频繁操作页面,那么2,3秒内就把所有绑定检测一遍,将无效的绑定移除 + if (threshold++ > 500) { + lastGCIndex = i + break + } + continue + } + data.i++ + //基于检测频率,如果检测过7次,可以认为其是长久存在的节点,那么以后每7次才检测一次 + if (data.i === 7) { + data.i = 14 + } + } else { + data.i-- + } + } + beginTime = new Date() +} + +function disposeData(data) { + delete disposeQueue[data.uuid] // 先清除,不然无法回收了 + data.element = null + data.rollback && data.rollback() + for (var key in data) { + data[key] = null + } +} + +function shouldDispose(el) { + try {//IE下,如果文本节点脱离DOM树,访问parentNode会报错 + var fireError = el.parentNode.nodeType + } catch (e) { + return true + } + if (el.ifRemove) { + // 如果节点被放到ifGroup,才移除 + if (!root.contains(el.ifRemove) && (ifGroup === el.parentNode)) { + el.parentNode && el.parentNode.removeChild(el) + return true + } + } + return el.msRetain ? 0 : (el.nodeType === 1 ? !root.contains(el) : !yua.contains(root, el)) +} + + + + + + + + + + + + + + + + + + + +/************************************************************************ + * HTML处理(parseHTML, innerHTML, clearHTML) * + *************************************************************************/ + +//parseHTML的辅助变量 +var tagHooks = new function() {// jshint ignore:line + yua.mix(this, { + option: DOC.createElement("select"), + thead: DOC.createElement("table"), + td: DOC.createElement("tr"), + area: DOC.createElement("map"), + tr: DOC.createElement("tbody"), + col: DOC.createElement("colgroup"), + legend: DOC.createElement("fieldset"), + _default: DOC.createElement("div"), + "g": DOC.createElementNS("http://www.w3.org/2000/svg", "svg") + }) + this.optgroup = this.option + this.tbody = this.tfoot = this.colgroup = this.caption = this.thead + this.th = this.td +}// jshint ignore:line +String("circle,defs,ellipse,image,line,path,polygon,polyline,rect,symbol,text,use").replace(rword, function(tag) { + tagHooks[tag] = tagHooks.g //处理SVG +}) + +var rtagName = /<([\w:]+)/ +var rxhtml = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/ig +var scriptTypes = oneObject(["", "text/javascript", "text/ecmascript", "application/ecmascript", "application/javascript"]) +var script = DOC.createElement("script") +var rhtml = /<|&#?\w+;/ + +yua.parseHTML = function(html) { + var fragment = yuaFragment.cloneNode(false) + if (typeof html !== "string" ) { + return fragment + } + if (!rhtml.test(html)) { + fragment.appendChild(DOC.createTextNode(html)) + return fragment + } + html = html.replace(rxhtml, "<$1>").trim() + var tag = (rtagName.exec(html) || ["", ""])[1].toLowerCase(), + //取得其标签名 + wrapper = tagHooks[tag] || tagHooks._default, + firstChild + wrapper.innerHTML = html + var els = wrapper.getElementsByTagName("script") + if (els.length) { //使用innerHTML生成的script节点不会发出请求与执行text属性 + for (var i = 0, el; el = els[i++]; ) { + if (scriptTypes[el.type]) { + var neo = script.cloneNode(false) //FF不能省略参数 + ap.forEach.call(el.attributes, function(attr) { + neo.setAttribute(attr.name, attr.value) + })// jshint ignore:line + neo.text = el.text + el.parentNode.replaceChild(neo, el) + } + } + } + + while (firstChild = wrapper.firstChild) { // 将wrapper上的节点转移到文档碎片上! + fragment.appendChild(firstChild) + } + return fragment +} + +yua.innerHTML = function(node, html) { + var a = this.parseHTML(html) + this.clearHTML(node).appendChild(a) +} + +yua.clearHTML = function(node) { + node.textContent = "" + while (node.firstChild) { + node.removeChild(node.firstChild) + } + return node +} + +/********************************************************************* + * yua的原型方法定义区 * + **********************************************************************/ + +function hyphen(target) { + //转换为连字符线风格 + return target.replace(/([a-z\d])([A-Z]+)/g, "$1-$2").toLowerCase() +} + +function camelize(target) { + //转换为驼峰风格 + if (target.indexOf("-") < 0 && target.indexOf("_") < 0) { + return target //提前判断,提高getStyle等的效率 + } + return target.replace(/[-_][^-_]/g, function (match) { + return match.charAt(1).toUpperCase() + }) +} + +"add,remove".replace(rword, function (method) { + yua.fn[method + "Class"] = function (cls) { + var el = this[0] + //https://developer.mozilla.org/zh-CN/docs/Mozilla/Firefox/Releases/26 + if (cls && typeof cls === "string" && el && el.nodeType === 1) { + cls.replace(/\S+/g, function (c) { + el.classList[method](c) + }) + } + return this + } +}) + +yua.fn.mix({ + hasClass: function (cls) { + var el = this[0] || {} //IE10+, chrome8+, firefox3.6+, safari5.1+,opera11.5+支持classList,chrome24+,firefox26+支持classList2.0 + return el.nodeType === 1 && el.classList.contains(cls) + }, + toggleClass: function (value, stateVal) { + var className, i = 0 + var classNames = String(value).match(/\S+/g) || [] + var isBool = typeof stateVal === "boolean" + while ((className = classNames[i++])) { + var state = isBool ? stateVal : !this.hasClass(className) + this[state ? "addClass" : "removeClass"](className) + } + return this + }, + attr: function (name, value) { + if (arguments.length === 2) { + this[0].setAttribute(name, value) + return this + } else { + return this[0].getAttribute(name) + } + }, + data: function (name, value) { + name = "data-" + hyphen(name || "") + switch (arguments.length) { + case 2: + this.attr(name, value) + return this + case 1: + var val = this.attr(name) + return parseData(val) + case 0: + var ret = {} + ap.forEach.call(this[0].attributes, function (attr) { + if (attr) { + name = attr.name + if (!name.indexOf("data-")) { + name = camelize(name.slice(5)) + ret[name] = parseData(attr.value) + } + } + }) + return ret + } + }, + removeData: function (name) { + name = "data-" + hyphen(name) + this[0].removeAttribute(name) + return this + }, + css: function (name, value) { + if (yua.isPlainObject(name)) { + for (var i in name) { + yua.css(this, i, name[i]) + } + } else { + var ret = yua.css(this, name, value) + } + return ret !== void 0 ? ret : this + }, + position: function () { + var offsetParent, offset, + elem = this[0], + parentOffset = { + top: 0, + left: 0 + }; + if (!elem) { + return + } + if (this.css("position") === "fixed") { + offset = elem.getBoundingClientRect() + } else { + offsetParent = this.offsetParent() //得到真正的offsetParent + offset = this.offset() // 得到正确的offsetParent + if (offsetParent[0].tagName !== "HTML") { + parentOffset = offsetParent.offset() + } + parentOffset.top += yua.css(offsetParent[0], "borderTopWidth", true) + parentOffset.left += yua.css(offsetParent[0], "borderLeftWidth", true) + // Subtract offsetParent scroll positions + parentOffset.top -= offsetParent.scrollTop() + parentOffset.left -= offsetParent.scrollLeft() + } + return { + top: offset.top - parentOffset.top - yua.css(elem, "marginTop", true), + left: offset.left - parentOffset.left - yua.css(elem, "marginLeft", true) + } + }, + offsetParent: function () { + var offsetParent = this[0].offsetParent + while (offsetParent && yua.css(offsetParent, "position") === "static") { + offsetParent = offsetParent.offsetParent; + } + return yua(offsetParent || root) + }, + bind: function (type, fn, phase) { + if (this[0]) { //此方法不会链 + return yua.bind(this[0], type, fn, phase) + } + }, + unbind: function (type, fn, phase) { + if (this[0]) { + yua.unbind(this[0], type, fn, phase) + } + return this + }, + val: function (value) { + var node = this[0] + if (node && node.nodeType === 1) { + var get = arguments.length === 0 + var access = get ? ":get" : ":set" + var fn = valHooks[getValType(node) + access] + if (fn) { + var val = fn(node, value) + } else if (get) { + return (node.value || "").replace(/\r/g, "") + } else { + node.value = value + } + } + return get ? val : this + } +}) + +if (root.dataset) { + yua.fn.data = function (name, val) { + name = name && camelize(name) + var dataset = this[0].dataset + switch (arguments.length) { + case 2: + dataset[name] = val + return this + case 1: + val = dataset[name] + return parseData(val) + case 0: + var ret = createMap() + for (name in dataset) { + ret[name] = parseData(dataset[name]) + } + return ret + } + } +} + +yua.parseJSON = JSON.parse + +var rbrace = /(?:\{[\s\S]*\}|\[[\s\S]*\])$/ +function parseData(data) { + try { + if (typeof data === "object") + return data + data = data === "true" ? true : + data === "false" ? false : + data === "null" ? null : +data + "" === data ? +data : rbrace.test(data) ? JSON.parse(data) : data + } catch (e) { + } + return data +} + +yua.fireDom = function (elem, type, opts) { + var hackEvent = DOC.createEvent("Events"); + hackEvent.initEvent(type, true, true) + yua.mix(hackEvent, opts) + elem.dispatchEvent(hackEvent) +} + +yua.each({ + scrollLeft: "pageXOffset", + scrollTop: "pageYOffset" +}, function (method, prop) { + yua.fn[method] = function (val) { + var node = this[0] || {}, + win = getWindow(node), + top = method === "scrollTop" + if (!arguments.length) { + return win ? win[prop] : node[method] + } else { + if (win) { + win.scrollTo(!top ? val : win[prop], top ? val : win[prop]) + } else { + node[method] = val + } + } + } +}) + +function getWindow(node) { + return node.window && node.document ? node : node.nodeType === 9 ? node.defaultView : false +} + + + + + + +//=============================css相关================================== + +var cssHooks = yua.cssHooks = createMap() +var prefixes = ["", "-webkit-", "-moz-", "-ms-"] //去掉opera-15的支持 +var cssMap = { + "float": "cssFloat" +} + +yua.cssNumber = oneObject("animationIterationCount,animationIterationCount,columnCount,order,flex,flexGrow,flexShrink,fillOpacity,fontWeight,lineHeight,opacity,orphans,widows,zIndex,zoom") + +yua.cssName = function (name, host, camelCase) { + if (cssMap[name]) { + return cssMap[name] + } + host = host || root.style + for (var i = 0, n = prefixes.length; i < n; i++) { + camelCase = camelize(prefixes[i] + name) + if (camelCase in host) { + return (cssMap[name] = camelCase) + } + } + return null +} + +cssHooks["@:set"] = function (node, name, value) { + node.style[name] = value +} + +cssHooks["@:get"] = function (node, name) { + if (!node || !node.style) { + throw new Error("getComputedStyle要求传入一个节点 " + node) + } + var ret, computed = getComputedStyle(node) + if (computed) { + ret = name === "filter" ? computed.getPropertyValue(name) : computed[name] + if (ret === "") { + ret = node.style[name] //其他浏览器需要我们手动取内联样式 + } + } + return ret +} +cssHooks["opacity:get"] = function (node) { + var ret = cssHooks["@:get"](node, "opacity") + return ret === "" ? "1" : ret +} + +"top,left".replace(rword, function (name) { + cssHooks[name + ":get"] = function (node) { + var computed = cssHooks["@:get"](node, name) + return /px$/.test(computed) ? computed : + yua(node).position()[name] + "px" + } +}) + +var cssShow = { + position: "absolute", + visibility: "hidden", + display: "block" +} +var rdisplayswap = /^(none|table(?!-c[ea]).+)/ +function showHidden(node, array) { + //http://www.cnblogs.com/rubylouvre/archive/2012/10/27/2742529.html + if (node.offsetWidth <= 0) { //opera.offsetWidth可能小于0 + var styles = getComputedStyle(node, null) + if (rdisplayswap.test(styles["display"])) { + var obj = { + node: node + } + for (var name in cssShow) { + obj[name] = styles[name] + node.style[name] = cssShow[name] + } + array.push(obj) + } + var parent = node.parentNode + if (parent && parent.nodeType === 1) { + showHidden(parent, array) + } + } +} + +"Width,Height".replace(rword, function (name) { //fix 481 + var method = name.toLowerCase(), + clientProp = "client" + name, + scrollProp = "scroll" + name, + offsetProp = "offset" + name + cssHooks[method + ":get"] = function (node, which, override) { + var boxSizing = -4 + if (typeof override === "number") { + boxSizing = override + } + which = name === "Width" ? ["Left", "Right"] : ["Top", "Bottom"] + var ret = node[offsetProp] // border-box 0 + if (boxSizing === 2) { // margin-box 2 + return ret + yua.css(node, "margin" + which[0], true) + yua.css(node, "margin" + which[1], true) + } + if (boxSizing < 0) { // padding-box -2 + ret = ret - yua.css(node, "border" + which[0] + "Width", true) - yua.css(node, "border" + which[1] + "Width", true) + } + if (boxSizing === -4) { // content-box -4 + ret = ret - yua.css(node, "padding" + which[0], true) - yua.css(node, "padding" + which[1], true) + } + return ret + } + cssHooks[method + "&get"] = function (node) { + var hidden = []; + showHidden(node, hidden); + var val = cssHooks[method + ":get"](node) + for (var i = 0, obj; obj = hidden[i++]; ) { + node = obj.node + for (var n in obj) { + if (typeof obj[n] === "string") { + node.style[n] = obj[n] + } + } + } + return val; + } + yua.fn[method] = function (value) { //会忽视其display + var node = this[0] + if (arguments.length === 0) { + if (node.setTimeout) { //取得窗口尺寸,IE9后可以用node.innerWidth /innerHeight代替 + return node["inner" + name] + } + if (node.nodeType === 9) { //取得页面尺寸 + var doc = node.documentElement + //FF chrome html.scrollHeight< body.scrollHeight + //IE 标准模式 : html.scrollHeight> body.scrollHeight + //IE 怪异模式 : html.scrollHeight 最大等于可视窗口多一点? + return Math.max(node.body[scrollProp], doc[scrollProp], node.body[offsetProp], doc[offsetProp], doc[clientProp]) + } + return cssHooks[method + "&get"](node) + } else { + return this.css(method, value) + } + } + yua.fn["inner" + name] = function () { + return cssHooks[method + ":get"](this[0], void 0, -2) + } + yua.fn["outer" + name] = function (includeMargin) { + return cssHooks[method + ":get"](this[0], void 0, includeMargin === true ? 2 : 0) + } +}) + +yua.fn.offset = function () { //取得距离页面左右角的坐标 + var node = this[0] + try { + var rect = node.getBoundingClientRect() + // Make sure element is not hidden (display: none) or disconnected + // https://github.com/jquery/jquery/pull/2043/files#r23981494 + if (rect.width || rect.height || node.getClientRects().length) { + var doc = node.ownerDocument + var root = doc.documentElement + var win = doc.defaultView + return { + top: rect.top + win.pageYOffset - root.clientTop, + left: rect.left + win.pageXOffset - root.clientLeft + } + } + } catch (e) { + return { + left: 0, + top: 0 + } + } +} + + + + + + + + +//=============================val相关======================= + +function getValType(elem) { + var ret = elem.tagName.toLowerCase() + return ret === "input" && /checkbox|radio/.test(elem.type) ? "checked" : ret +} + +var valHooks = { + "select:get": function (node, value) { + var option, options = node.options, + index = node.selectedIndex, + one = node.type === "select-one" || index < 0, + values = one ? null : [], + max = one ? index + 1 : options.length, + i = index < 0 ? max : one ? index : 0 + for (; i < max; i++) { + option = options[i] + //旧式IE在reset后不会改变selected,需要改用i === index判定 + //我们过滤所有disabled的option元素,但在safari5下,如果设置select为disable,那么其所有孩子都disable + //因此当一个元素为disable,需要检测其是否显式设置了disable及其父节点的disable情况 + if ((option.selected || i === index) && !option.disabled) { + value = option.value + if (one) { + return value + } + //收集所有selected值组成数组返回 + values.push(value) + } + } + return values + }, + "select:set": function (node, values, optionSet) { + values = [].concat(values) //强制转换为数组 + for (var i = 0, el; el = node.options[i++]; ) { + if ((el.selected = values.indexOf(el.value) > -1)) { + optionSet = true + } + } + if (!optionSet) { + node.selectedIndex = -1 + } + } +} + +var keyMap = {} +var keys = ["break,case,catch,continue,debugger,default,delete,do,else,false", + "finally,for,function,if,in,instanceof,new,null,return,switch,this", + "throw,true,try,typeof,var,void,while,with", /* 关键字*/ + "abstract,boolean,byte,char,class,const,double,enum,export,extends", + "final,float,goto,implements,import,int,interface,long,native", + "package,private,protected,public,short,static,super,synchronized", + "throws,transient,volatile", /*保留字*/ + "arguments,let,yield,undefined"].join(",") +keys.replace(/\w+/g, function (a) { + keyMap[a] = true +}) + +var ridentStart = /[a-z_$]/i +var rwhiteSpace = /[\s\uFEFF\xA0]/ +function getIdent(input, lastIndex) { + var result = [] + var subroutine = !!lastIndex + lastIndex = lastIndex || 0 + //将表达式中的标识符抽取出来 + var state = "unknown" + var variable = "" + for (var i = 0; i < input.length; i++) { + var c = input.charAt(i) + if (c === "'" || c === '"') {//字符串开始 + if (state === "unknown") { + state = c + } else if (state === c) {//字符串结束 + state = "unknown" + } + } else if (c === "\\") { + if (state === "'" || state === '"') { + i++ + } + } else if (ridentStart.test(c)) {//碰到标识符 + if (state === "unknown") { + state = "variable" + variable = c + } else if (state === "maybePath") { + variable = result.pop() + variable += "." + c + state = "variable" + } else if (state === "variable") { + variable += c + } + } else if (/\w/.test(c)) { + if (state === "variable") { + variable += c + } + } else if (c === ".") { + if (state === "variable") { + if (variable) { + result.push(variable) + variable = "" + state = "maybePath" + } + } + } else if (c === "[") { + if (state === "variable" || state === "maybePath") { + if (variable) {//如果前面存在变量,收集它 + result.push(variable) + variable = "" + } + var lastLength = result.length + var last = result[lastLength - 1] + var innerResult = getIdent(input.slice(i), i) + if (innerResult.length) {//如果括号中存在变量,那么这里添加通配符 + result[lastLength - 1] = last + ".*" + result = innerResult.concat(result) + } else { //如果括号中的东西是确定的,直接转换为其子属性 + var content = input.slice(i + 1, innerResult.i) + try { + var text = (scpCompile(["return " + content]))() + result[lastLength - 1] = last + "." + text + } catch (e) { + } + } + state = "maybePath"//]后面可能还接东西 + i = innerResult.i + } + } else if (c === "]") { + if (subroutine) { + result.i = i + lastIndex + addVar(result, variable) + return result + } + } else if (rwhiteSpace.test(c) && c !== "\r" && c !== "\n") { + if (state === "variable") { + if (addVar(result, variable)) { + state = "maybePath" // aaa . bbb 这样的情况 + } + variable = "" + } + } else { + addVar(result, variable) + state = "unknown" + variable = "" + } + } + addVar(result, variable) + return result +} + +function addVar(array, element) { + if (element && !keyMap[element]) { + array.push(element) + return true + } +} + +function addAssign(vars, vmodel, name, binding) { + var ret = [], + prefix = " = " + name + "." + for (var i = vars.length, prop; prop = vars[--i]; ) { + var arr = prop.split("."), a + var first = arr[0] + while (a = arr.shift()) { + if (vmodel.hasOwnProperty(a)) { + ret.push(first + prefix + first) + binding.observers.push({ + v: vmodel, + p: prop + }) + vars.splice(i, 1) + } else { + break + } + } + } + return ret +} + +var rproxy = /(\$proxy\$[a-z]+)\-[\-0-9a-f]+$/ +var variablePool = new Cache(218) +//缓存求值函数,以便多次利用 +var evaluatorPool = new Cache(128) + +function getVars(expr) { + expr = expr.trim() + var ret = variablePool.get(expr) + if (ret) { + return ret.concat() + } + var array = getIdent(expr) + var uniq = {} + var result = [] + for (var i = 0, el; el = array[i++]; ) { + if (!uniq[el]) { + uniq[el] = 1 + result.push(el) + } + } + return variablePool.put(expr, result).concat() +} + +function parseExpr(expr, vmodels, binding) { + var filters = binding.filters + if (typeof filters === "string" && filters.trim() && !binding._filters) { + binding._filters = parseFilter(filters.trim()) + } + + var vars = getVars(expr) + var expose = new Date() - 0 + var assigns = [] + var names = [] + var args = [] + binding.observers = [] + for (var i = 0, sn = vmodels.length; i < sn; i++) { + if (vars.length) { + var name = "vm" + expose + "_" + i + names.push(name) + args.push(vmodels[i]) + assigns.push.apply(assigns, addAssign(vars, vmodels[i], name, binding)) + } + } + binding.args = args + var dataType = binding.type + var exprId = vmodels.map(function (el) { + return String(el.$id).replace(rproxy, "$1") + }) + expr + dataType + var getter = evaluatorPool.get(exprId) //直接从缓存,免得重复生成 + if (getter) { + if (dataType === "duplex") { + var setter = evaluatorPool.get(exprId + "setter") + binding.setter = setter.apply(setter, binding.args) + } + return binding.getter = getter + } + + if (!assigns.length) { + assigns.push("fix" + expose) + } + + if (dataType === "duplex") { + var nameOne = {} + assigns.forEach(function (a) { + var arr = a.split("=") + nameOne[arr[0].trim()] = arr[1].trim() + }) + expr = expr.replace(/[\$\w]+/, function (a) { + return nameOne[a] ? nameOne[a] : a + }) + /* jshint ignore:start */ + var fn2 = scpCompile(names.concat("'use strict';" + + "return function(vvv){" + expr + " = vvv\n}\n")) + /* jshint ignore:end */ + evaluatorPool.put(exprId + "setter", fn2) + binding.setter = fn2.apply(fn2, binding.args) + } + + if (dataType === "on") { //事件绑定 + if (expr.indexOf("(") === -1) { + expr += ".call(this, $event)" + } else { + expr = expr.replace("(", ".call(this,") + } + names.push("$event") + expr = "\nreturn " + expr + ";" //IE全家 Function("return ")出错,需要Function("return ;") + var lastIndex = expr.lastIndexOf("\nreturn") + var header = expr.slice(0, lastIndex) + var footer = expr.slice(lastIndex) + expr = header + "\n" + footer + } else { + expr = "\nreturn " + expr + ";" //IE全家 Function("return ")出错,需要Function("return ;") + } + /* jshint ignore:start */ + getter = scpCompile(names.concat("'use strict';\nvar " + + assigns.join(",\n") + expr)) + /* jshint ignore:end */ + + return evaluatorPool.put(exprId, getter) +} + +function normalizeExpr(code) { + var hasExpr = rexpr.test(code) //比如:class="width{{w}}"的情况 + if (hasExpr) { + var array = scanExpr(code) + if (array.length === 1) { + return array[0].expr + } + return array.map(function (el) { + return el.type ? "(" + el.expr + ")" : quote(el.expr) + }).join(" + ") + } else { + return code + } +} + +yua.normalizeExpr = normalizeExpr +yua.parseExprProxy = parseExpr + +var rthimRightParentheses = /\)\s*$/ +var rthimOtherParentheses = /\)\s*\|/g +var rquoteFilterName = /\|\s*([$\w]+)/g +var rpatchBracket = /"\s*\["/g +var rthimLeftParentheses = /"\s*\(/g +function parseFilter(filters) { + filters = filters + .replace(rthimRightParentheses, "")//处理最后的小括号 + .replace(rthimOtherParentheses, function () {//处理其他小括号 + return "],|" + }) + .replace(rquoteFilterName, function (a, b) { //处理|及它后面的过滤器的名字 + return "[" + quote(b) + }) + .replace(rpatchBracket, function () { + return '"],["' + }) + .replace(rthimLeftParentheses, function () { + return '",' + }) + "]" + /* jshint ignore:start */ + return scpCompile(["return [" + filters + "]"])() + /* jshint ignore:end */ +} + + + + + + +/********************************************************************* + * 编译系统 * + **********************************************************************/ + +var quote = JSON.stringify + + + + + + + + + + + + +/********************************************************************* + * 扫描系统 * + **********************************************************************/ + +yua.scan = function (elem, vmodel) { + elem = elem || root + var vmodels = vmodel ? [].concat(vmodel) : [] + scanTag(elem, vmodels) +} + +//http://www.w3.org/TR/html5/syntax.html#void-elements +var stopScan = oneObject("area,base,basefont,br,col,command,embed,hr,img,input,link,meta,param,source,track,wbr,noscript,script,style,textarea".toUpperCase()) + +function checkScan(elem, callback, innerHTML) { + var id = setTimeout(function () { + var currHTML = elem.innerHTML + clearTimeout(id) + if (currHTML === innerHTML) { + callback() + } else { + checkScan(elem, callback, currHTML) + } + }) +} + +function createSignalTower(elem, vmodel) { + var id = elem.getAttribute("yuactrl") || vmodel.$id + elem.setAttribute("yuactrl", id) + if (vmodel.$events) { + vmodel.$events.expr = elem.tagName + '[yuactrl="' + id + '"]' + } +} + +function getBindingCallback(elem, name, vmodels) { + var callback = elem.getAttribute(name) + if (callback) { + for (var i = 0, vm; vm = vmodels[i++]; ) { + if (vm.hasOwnProperty(callback) && typeof vm[callback] === "function") { + return vm[callback] + } + } + } +} + +function executeBindings(bindings, vmodels) { + for (var i = 0, binding; binding = bindings[i++]; ) { + binding.vmodels = vmodels + directives[binding.type].init(binding) + + yua.injectBinding(binding) + if (binding.getter && binding.element.nodeType === 1) { //移除数据绑定,防止被二次解析 + //chrome使用removeAttributeNode移除不存在的特性节点时会报错 https://github.com/RubyLouvre/yua/issues/99 + binding.element.removeAttribute(binding.name) + } + } + bindings.length = 0 +} + +//https://github.com/RubyLouvre/yua/issues/636 +var mergeTextNodes = IEVersion && window.MutationObserver ? function (elem) { + var node = elem.firstChild, text + while (node) { + var aaa = node.nextSibling + if (node.nodeType === 3) { + if (text) { + text.nodeValue += node.nodeValue + elem.removeChild(node) + } else { + text = node + } + } else { + text = null + } + node = aaa + } +} : 0 +var roneTime = /^\s*::/ +var rmsAttr = /:(\w+)-?(.*)/ + +var events = oneObject("animationend,blur,change,input,click,dblclick,focus,keydown,keypress,keyup,mousedown,mouseenter,mouseleave,mousemove,mouseout,mouseover,mouseup,scan,scroll,submit") +var obsoleteAttrs = oneObject("value,title,alt,checked,selected,disabled,readonly,enabled,href,src") +function bindingSorter(a, b) { + return a.priority - b.priority +} + +var rnoCollect = /^(:\S+|data-\S+|on[a-z]+|id|style|class)$/ +var ronattr = /^on\-[\w-]+$/ +function getOptionsFromTag(elem, vmodels) { + var attributes = elem.attributes + var ret = {} + for (var i = 0, attr; attr = attributes[i++]; ) { + var name = attr.name + if (attr.specified && !rnoCollect.test(name)) { + var camelizeName = camelize(attr.name) + if (/^on\-[\w-]+$/.test(name)) { + ret[camelizeName] = getBindingCallback(elem, name, vmodels) + } else { + ret[camelizeName] = parseData(attr.value) + } + } + + } + return ret +} + +function scanAttr(elem, vmodels, match) { + var scanNode = true + if (vmodels.length) { + var attributes = elem.attributes + var bindings = [] + var uniq = {} + for (var i = 0, attr; attr = attributes[i++]; ) { + var name = attr.name + if (uniq[name]) {//IE8下:repeat,:with BUG + continue + } + uniq[name] = 1 + if (attr.specified) { + if (match = name.match(rmsAttr)) { + //如果是以指定前缀命名的 + var type = match[1] + var param = match[2] || "" + var value = attr.value + if (events[type]) { + param = type + type = "on" + } else if (obsoleteAttrs[type]) { + param = type + type = "attr" + name = ":" + type + "-" + param + log("warning!请改用" + name + "代替" + attr.name + "!") + } + if (directives[type]) { + var newValue = value.replace(roneTime, "") + var oneTime = value !== newValue + var binding = { + type: type, + param: param, + element: elem, + name: name, + expr: newValue, + oneTime: oneTime, + uuid: "_" + (++bindingID), + priority: (directives[type].priority || type.charCodeAt(0) * 10) + (Number(param.replace(/\D/g, "")) || 0) + } + if (type === "html" || type === "text") { + + var filters = getToken(value).filters + binding.expr = binding.expr.replace(filters, "") + binding.filters = filters.replace(rhasHtml, function () { + binding.type = "html" + binding.group = 1 + return "" + }).trim() // jshint ignore:line + } else if (type === "duplex") { + var hasDuplex = name + } else if (name === ":if-loop") { + binding.priority += 100 + } else if (name === ":attr-value") { + var hasAttrValue = name + } + bindings.push(binding) + } + } + } + } + if (bindings.length) { + bindings.sort(bindingSorter) + + if (hasDuplex && hasAttrValue && elem.type === "text") { + log("warning!一个控件不能同时定义:attr-value与" + hasDuplex) + } + + for (i = 0; binding = bindings[i]; i++) { + type = binding.type + if (rnoscanAttrBinding.test(type)) { + return executeBindings(bindings.slice(0, i + 1), vmodels) + } else if (scanNode) { + scanNode = !rnoscanNodeBinding.test(type) + } + } + executeBindings(bindings, vmodels) + } + } + if (scanNode && !stopScan[elem.tagName] && (isWidget(elem) ? elem.msResolved : 1)) { + mergeTextNodes && mergeTextNodes(elem) + scanNodeList(elem, vmodels) //扫描子孙元素 + } +} + +var rnoscanAttrBinding = /^if|widget|repeat$/ +var rnoscanNodeBinding = /^html|include$/ + +function scanNodeList(parent, vmodels) { + var nodes = yua.slice(parent.childNodes) + scanNodeArray(nodes, vmodels) +} + +function scanNodeArray(nodes, vmodels) { + function _delay_component(name) { + setTimeout(function () { + yua.component(name) + }) + } + for (var i = 0, node; node = nodes[i++]; ) { + switch (node.nodeType) { + case 1: + var elem = node + if (!elem.msResolved && elem.parentNode && elem.parentNode.nodeType === 1) { + var library = isWidget(elem) + if (library) { + var widget = elem.localName ? elem.localName.replace(library + ":", "") : elem.nodeName + var fullName = library + ":" + camelize(widget) + componentQueue.push({ + library: library, + element: elem, + fullName: fullName, + widget: widget, + vmodels: vmodels, + name: "widget" + }) + if (yua.components[fullName]) { + //确保所有:attr-name扫描完再处理 + _delay_component(fullName) + } + } + } + + scanTag(node, vmodels) //扫描元素节点 + + if (node.msHasEvent) { + yua.fireDom(node, "datasetchanged", { + bubble: node.msHasEvent + }) + } + + break + case 3: + if (rexpr.test(node.nodeValue)) { + scanText(node, vmodels, i) //扫描文本节点 + } + break + } + + } +} + +function scanTag(elem, vmodels, node) { + //扫描顺序 :skip(0) --> :important(1) --> :controller(2) --> :if(10) --> :repeat(100) + //--> :if-loop(110) --> :attr(970) ...--> :each(1400)-->:with(1500)--〉:duplex(2000)垫后 + var a = elem.getAttribute(":skip") + var b = elem.getAttributeNode(":important") + var c = elem.getAttributeNode(":controller") + if (typeof a === "string") { + return + } else if (node = b || c) { + + var newVmodel = yua.vmodels[node.value] + + if (!newVmodel) { + return + } + + //把父级VM补上 + newVmodel.$up = vmodels[0] + //:important不包含父VM,:controller相反 + vmodels = node === b ? [newVmodel] : [newVmodel].concat(vmodels) + + elem.removeAttribute(node.name) //removeAttributeNode不会刷新[:controller]样式规则 + elem.classList.remove(node.name) + createSignalTower(elem, newVmodel) + } + scanAttr(elem, vmodels) //扫描特性节点 + + if (newVmodel) { + setTimeout(function () { + newVmodel.$fire(":scan-end", elem) + }) + } +} +var rhasHtml = /\|\s*html(?:\b|$)/, + r11a = /\|\|/g, + rlt = /</g, + rgt = />/g, + rstringLiteral = /(['"])(\\\1|.)+?\1/g, + rline = /\r?\n/g +function getToken(value) { + if (value.indexOf("|") > 0) { + var scapegoat = value.replace(rstringLiteral, function (_) { + return Array(_.length + 1).join("1") // jshint ignore:line + }) + var index = scapegoat.replace(r11a, "\u1122\u3344").indexOf("|") //干掉所有短路或 + if (index > -1) { + return { + type: "text", + filters: value.slice(index).trim(), + expr: value.slice(0, index) + } + } + } + return { + type: "text", + expr: value, + filters: "" + } +} + +function scanExpr(str) { + var tokens = [], + value, start = 0, + stop + do { + stop = str.indexOf(openTag, start) + if (stop === -1) { + break + } + value = str.slice(start, stop) + if (value) { // {{ 左边的文本 + tokens.push({ + expr: value + }) + } + start = stop + openTag.length + stop = str.indexOf(closeTag, start) + if (stop === -1) { + break + } + value = str.slice(start, stop) + if (value) { //处理{{ }}插值表达式 + tokens.push(getToken(value.replace(rline,""))) + } + start = stop + closeTag.length + } while (1) + value = str.slice(start) + if (value) { //}} 右边的文本 + tokens.push({ + expr: value + }) + } + return tokens +} + +function scanText(textNode, vmodels, index) { + var bindings = [], + tokens = scanExpr(textNode.data) + if (tokens.length) { + for (var i = 0, token; token = tokens[i++];) { + var node = DOC.createTextNode(token.expr) //将文本转换为文本节点,并替换原来的文本节点 + if (token.type) { + token.expr = token.expr.replace(roneTime, function () { + token.oneTime = true + return "" + }) // jshint ignore:line + token.element = node + token.filters = token.filters.replace(rhasHtml, function () { + token.type = "html" + return "" + }) // jshint ignore:line + token.pos = index * 1000 + i + bindings.push(token) //收集带有插值表达式的文本 + } + yuaFragment.appendChild(node) + } + textNode.parentNode.replaceChild(yuaFragment, textNode) + if (bindings.length) + executeBindings(bindings, vmodels) + } +} + +//使用来自游戏界的双缓冲技术,减少对视图的冗余刷新 +var Buffer = function () { + this.queue = [] +} +Buffer.prototype = { + render: function (isAnimate) { + if (!this.locked) { + this.locked = isAnimate ? root.offsetHeight + 10 : 1 + var me = this + yua.nextTick(function () { + me.flush() + }) + } + }, + flush: function () { + for (var i = 0, sub; sub = this.queue[i++]; ) { + sub.update && sub.update() + } + this.locked = 0 + this.queue = [] + } +} + +var buffer = new Buffer() + +var componentQueue = [] +var widgetList = [] +var componentHooks = { + $construct: function () { + return yua.mix.apply(null, arguments) + }, + $ready: noop, + $init: noop, + $dispose: noop, + $container: null, + $childReady: noop, + $replace: false, + $extend: null, + $$template: function (str) { + return str + } +} + +yua.components = {} +yua.component = function (name, opts) { + if (opts) { + yua.components[name] = yua.mix({}, componentHooks, opts) + } + for (var i = 0, obj; obj = componentQueue[i]; i++) { + if (name === obj.fullName) { + componentQueue.splice(i, 1) + i--; + + (function (host, hooks, elem, widget) { + //如果elem已从Document里移除,直接返回 + //issuse : https://github.com/RubyLouvre/yua2/issues/40 + if (!yua.contains(DOC, elem) || elem.msResolved) { + yua.Array.remove(componentQueue, host) + return + } + + var dependencies = 1 + var library = host.library + var global = yua.libraries[library] || componentHooks + + //===========收集各种配置======= + if (elem.getAttribute(":attr-identifier")) { + //如果还没有解析完,就延迟一下 #1155 + return + } + var elemOpts = getOptionsFromTag(elem, host.vmodels) + var vmOpts = getOptionsFromVM(host.vmodels, elemOpts.config || host.fullName) + var $id = elemOpts.$id || elemOpts.identifier || generateID(widget) + delete elemOpts.config + delete elemOpts.$id + delete elemOpts.identifier + var componentDefinition = {} + + var parentHooks = yua.components[hooks.$extend] + if (parentHooks) { + yua.mix(true, componentDefinition, parentHooks) + componentDefinition = parentHooks.$construct.call(elem, componentDefinition, {}, {}) + } else { + yua.mix(true, componentDefinition, hooks) + } + componentDefinition = yua.components[name].$construct.call(elem, componentDefinition, vmOpts, elemOpts) + + componentDefinition.$refs = {} + componentDefinition.$id = $id + + //==========构建VM========= + var keepSlot = componentDefinition.$slot + var keepReplace = componentDefinition.$replace + var keepContainer = componentDefinition.$container + var keepTemplate = componentDefinition.$template + delete componentDefinition.$slot + delete componentDefinition.$replace + delete componentDefinition.$container + delete componentDefinition.$construct + + var vmodel = yua.define(componentDefinition) || {} + elem.msResolved = 1 //防止二进扫描此元素 + vmodel.$init(vmodel, elem) + global.$init(vmodel, elem) + var nodes = elem.childNodes + //收集插入点 + var slots = {}, snode + for (var s = 0, el; el = nodes[s++]; ) { + var type = el.nodeType === 1 && el.getAttribute("slot") || keepSlot + if (type) { + if (slots[type]) { + slots[type].push(el) + } else { + slots[type] = [el] + } + } + } + + if (vmodel.$$template) { + yua.clearHTML(elem) + elem.innerHTML = vmodel.$$template(keepTemplate) + } + for (s in slots) { + if (vmodel.hasOwnProperty(s)) { + var ss = slots[s] + if (ss.length) { + var fragment = yuaFragment.cloneNode(true) + for (var ns = 0; snode = ss[ns++]; ) { + fragment.appendChild(snode) + } + vmodel[s] = fragment + } + slots[s] = null + } + } + slots = null + var child = elem.children[0] || elem.firstChild + if (keepReplace) { + elem.parentNode.replaceChild(child, elem) + child.msResolved = 1 + var cssText = elem.style.cssText + var className = elem.className + elem = host.element = child + elem.style.cssText += ";"+ cssText + if (className) { + yua(elem).addClass(className) + } + } + if (keepContainer) { + keepContainer.appendChild(elem) + } + yua.fireDom(elem, "datasetchanged", + {library: library, vm: vmodel, childReady: 1}) + var children = 0 + var removeFn = yua.bind(elem, "datasetchanged", function (e) { + if (e.childReady && e.library === library) { + dependencies += e.childReady + if (vmodel !== e.vm) { + vmodel.$refs[e.vm.$id] = e.vm + if (e.childReady === -1) { + children++ + vmodel.$childReady(vmodel, elem, e) + } + e.stopPropagation() + } + } + if (dependencies === 0) { + var id1 = setTimeout(function () { + clearTimeout(id1) + + vmodel.$ready(vmodel, elem, host.vmodels) + global.$ready(vmodel, elem, host.vmodels) + }, children ? Math.max(children * 17, 100) : 17) + yua.unbind(elem, "datasetchanged", removeFn) + //================== + host.rollback = function () { + try { + vmodel.$dispose(vmodel, elem) + global.$dispose(vmodel, elem) + } catch (e) { + } + delete yua.vmodels[vmodel.$id] + } + injectDisposeQueue(host, widgetList) + if (window.chrome) { + elem.addEventListener("DOMNodeRemovedFromDocument", function () { + setTimeout(rejectDisposeQueue) + }) + } + + } + }) + scanTag(elem, [vmodel].concat(host.vmodels)) + yua.vmodels[vmodel.$id] = vmodel + if (!elem.childNodes.length) { + yua.fireDom(elem, "datasetchanged", {library: library, vm: vmodel, childReady: -1}) + } else { + var id2 = setTimeout(function () { + clearTimeout(id2) + yua.fireDom(elem, "datasetchanged", {library: library, vm: vmodel, childReady: -1}) + }, 17) + } + + })(obj, yua.components[name], obj.element, obj.widget)// jshint ignore:line + + } + } +} + + +function getOptionsFromVM(vmodels, pre) { + if (pre) { + for (var i = 0, v; v = vmodels[i++]; ) { + if (v.hasOwnProperty(pre) && typeof v[pre] === "object") { + var vmOptions = v[pre] + return vmOptions.$model || vmOptions + break + } + } + } + return {} +} + +yua.libraries = [] +yua.library = function (name, opts) { + if (DOC.namespaces) { + DOC.namespaces.add(name, 'http://www.w3.org/1999/xhtml'); + } + yua.libraries[name] = yua.mix({ + $init: noop, + $ready: noop, + $dispose: noop + }, opts || {}) +} + +yua.library("ms") +yua.library("do") + + + + + + + + + + + +/* + broswer nodeName scopeName localName + IE9 ONI:BUTTON oni button + IE10 ONI:BUTTON undefined oni:button + IE8 button oni undefined + chrome ONI:BUTTON undefined oni:button + + */ +function isWidget(el) { //如果为自定义标签,返回UI库的名字 + if (el.scopeName && el.scopeName !== "HTML") { + return el.scopeName + } + var fullName = el.nodeName.toLowerCase() + var index = fullName.indexOf(":") + if (index > 0) { + return fullName.slice(0, index) + } +} +//各种MVVM框架在大型表格下的性能测试 +// https://github.com/RubyLouvre/yua/issues/859 + +var bools = ["autofocus,autoplay,async,allowTransparency,checked,controls", + "declare,disabled,defer,defaultChecked,defaultSelected", + "contentEditable,isMap,loop,multiple,noHref,noResize,noShade", + "open,readOnly,selected" +].join(",") +var boolMap = {} +bools.replace(rword, function (name) { + boolMap[name.toLowerCase()] = name +}) + +var propMap = {//属性名映射 + "accept-charset": "acceptCharset", + "char": "ch", + "charoff": "chOff", + "class": "className", + "for": "htmlFor", + "http-equiv": "httpEquiv" +} + +var anomaly = ["accessKey,bgColor,cellPadding,cellSpacing,codeBase,codeType,colSpan", + "dateTime,defaultValue,frameBorder,longDesc,maxLength,marginWidth,marginHeight", + "rowSpan,tabIndex,useMap,vSpace,valueType,vAlign" +].join(",") +anomaly.replace(rword, function (name) { + propMap[name.toLowerCase()] = name +}) + + + + + + + + + + + + + + + + + + + + + + + + + + + +var attrDir = yua.directive("attr", { + init: function (binding) { + //{{aaa}} --> aaa + //{{aaa}}/bbb.html --> (aaa) + "/bbb.html" + binding.expr = normalizeExpr(binding.expr.trim()) + if (binding.type === "include") { + var elem = binding.element + effectBinding(elem, binding) + binding.includeRendered = getBindingCallback(elem, "data-include-rendered", binding.vmodels) + binding.includeLoaded = getBindingCallback(elem, "data-include-loaded", binding.vmodels) + var outer = binding.includeReplace = !!yua(elem).data("includeReplace") + if (yua(elem).data("includeCache")) { + binding.templateCache = {} + } + binding.start = DOC.createComment(":include") + binding.end = DOC.createComment(":include-end") + if (outer) { + binding.element = binding.end + binding._element = elem + elem.parentNode.insertBefore(binding.start, elem) + elem.parentNode.insertBefore(binding.end, elem.nextSibling) + } else { + elem.insertBefore(binding.start, elem.firstChild) + elem.appendChild(binding.end) + } + } + }, + update: function (val) { + + var elem = this.element + var obj = val + + if(typeof obj === 'object' && obj !== null){ + if(!yua.isPlainObject(obj)) + obj = obj.$model + }else{ + if(!this.param) + return + + obj = {} + obj[this.param] = val + } + + for(var i in obj){ + if(i === 'href' || i === 'src'){ + //处理IE67自动转义的问题 + if(!root.hasAttribute) + obj[i] = obj[i].replace(/&/g, '&') + + elem[i] = obj[i] + + //chrome v37- 下embed标签动态设置的src,无法发起请求 + if(window.chrome && elem.tagName === 'EMBED'){ + var parent = elem.parentNode + var com = document.createComment(':src') + parent.replaceChild(com, elem) + parent.replaceChild(elem, com) + } + }else{ + var k = i + //古董IE下,部分属性名字要进行映射 + if(!W3C && propMap[k]) + k = propMap[k] + + if(typeof elem[boolMap[k]] === 'boolean'){ + //布尔属性必须使用el.xxx = true|false方式设值 + elem[boolMap[k]] = !!obj[i] + + //如果为false, IE全系列下相当于setAttribute(xxx, ''),会影响到样式,需要进一步处理 + if(!obj[i]) + obj[i] = !!obj[i] + } + if(obj[i] === false || obj[i] === null || obj[i] === undefined) + return elem.removeAttribute(k) + + //SVG只能使用setAttribute(xxx, yyy), VML只能使用elem.xxx = yyy ,HTML的固有属性必须elem.xxx = yyy + var isInnate = rsvg.test(elem) ? false : (DOC.namespaces && isVML(elem)) ? true : k in elem.cloneNode(false) + if (isInnate) { + elem[k] = obj[i] + } else { + elem.setAttribute(k, obj[i]) + } + } + } + } +}) + +//这几个指令都可以使用插值表达式,如:src="aaa/{{b}}/{{c}}.html" +"title,alt,src,value,css,include,href,data".replace(rword, function (name) { + directives[name] = attrDir +}) + +//类名定义, :class="xx:yy" :class="{xx: yy}" :class="xx" :class="{{xx}}" +yua.directive("class", { + init: function (binding) { + if(!/^\{.*\}$/.test(binding.expr)){ + + var expr = binding.expr.split(':') + expr[1] = expr[1] && expr[1].trim() || 'true' + var arr = expr[0].split(/\s+/) + binding.expr = '{' + arr.map(function(it){ + return it + ': ' + expr[1] + }).join(', ') + '}' + + }else if(/^\{\{.*\}\}$/.test(binding.expr)){ + + binding.expr = binding.expr.slice(2, -2) + } + + if (binding.type === "hover" || binding.type === "active") { //确保只绑定一次 + if (!binding.hasBindEvent) { + var elem = binding.element + var $elem = yua(elem) + var activate = "mouseenter" //在移出移入时切换类名 + var abandon = "mouseleave" + if (binding.type === "active") { //在聚焦失焦中切换类名 + elem.tabIndex = elem.tabIndex || -1 + activate = "mousedown" + abandon = "mouseup" + var fn0 = $elem.bind("mouseleave", function () { + binding.toggleClass && $elem.removeClass(binding.newClass) + }) + } + } + + var fn1 = $elem.bind(activate, function () { + binding.toggleClass && $elem.addClass(binding.newClass) + }) + var fn2 = $elem.bind(abandon, function () { + binding.toggleClass && $elem.removeClass(binding.newClass) + }) + binding.rollback = function () { + $elem.unbind("mouseleave", fn0) + $elem.unbind(activate, fn1) + $elem.unbind(abandon, fn2) + } + binding.hasBindEvent = true + } + + }, + update: function (val) { + var obj = val + if(!obj) + return log('class绑定错误') + + if(typeof obj === 'string'){ + obj = {} + obj[val] = true + } + + if(!yua.isPlainObject(obj)){ + obj = obj.$model + } + + if(this.param) + return log('不再支持:class-xx="yy"的写法', this.name) + + var $elem = yua(this.element) + for(var i in obj){ + $elem.toggleClass(i, !!obj[i]) + } + } +}) + +"hover,active".replace(rword, function (name) { + directives[name] = directives["class"] +}) + +//样式定义 :css-width="200" +//:css="{width: 200}" +yua.directive("css", { + init: directives.attr.init, + update: function (val) { + var $elem = yua(this.element) + if(!this.param){ + var obj = val + try{ + if(typeof val === 'object'){ + if(!yua.isPlainObject(val)) + obj = val.$model + }else{ + obj = new Function('return ' + val)() + } + for(var i in obj){ + $elem.css(i, obj[i]) + } + }catch(err){ + log('样式格式错误', val) + } + }else{ + $elem.css(this.param, val) + } + } +}) + +//兼容2种写法 :data-xx="yy", :data="{xx: yy}" +yua.directive("data", { + priority: 100, + update: function (val) { + var obj = val + if(typeof obj === 'object' && obj !== null){ + if(!yua.isPlainObject(obj)) + obj = val.$model + + for(var i in obj){ + this.element.setAttribute('data-' + i, obj[i]) + } + }else{ + if(!this.param) + return + + this.element.setAttribute('data-' + this.param, obj) + } + } +}) + + + +/*------ 表单验证 -------*/ +var __rules = {}; +yua.validate = function(key){ + return !__rules[key] || __rules[key].every(function(it){ return it.checked}) +}; +yua.directive('rule', { + priority: 2010, + init: function(binding){ + if(binding.param && !__rules[binding.param]){ + __rules[binding.param] = []; + binding.target = __rules[binding.param] + } + }, + update: function(obj){ + var _this = this, + elem = this.element, + ruleID = -1; + + if(!['INPUT', 'TEXTAREA'].includes(elem.nodeName)) + return + + if(this.target){ + ruleID = this.target.length; + this.target.push({checked: true}) + } + + //如果父级元素没有定位属性,则加上相对定位 + if(getComputedStyle(elem.parentNode).position === 'static'){ + elem.parentNode.style.position = 'relative' + } + + var $elem = yua(elem), + ol = elem.offsetLeft + elem.offsetWidth - 50, + ot = elem.offsetTop + elem.offsetHeight + 8, + tips = document.createElement('div'); + + tips.className = 'do-rule-tips' + tips.style.left = ol + 'px' + tips.style.bottom = ot + 'px' + + + function checked(ev){ + var txt = '', + val = elem.value; + + if(obj.require && (val === '' || val === null)) + txt = '必填项' + + if(!txt && obj.isNumeric) + txt = !isFinite(val) ? '必须为合法数字' : '' + + if(!txt && obj.isEmail) + txt = !/^[\w\.\-]+@\w+([\.\-]\w+)*\.\w+$/.test(val) ? 'Email格式错误' : '' + + if(!txt && obj.isPhone) + txt = !/^1[34578]\d{9}$/.test(val) ? '手机格式错误' : '' + + if(!txt && obj.isCN) + txt = !/^[\u4e00-\u9fa5]+$/.test(val) ? '必须为纯中文' : '' + + if(!txt && obj.exp) + txt = !obj.exp.test(val) ? (obj.msg || '格式错误') : '' + + if(!txt && obj.maxLen) + txt = val.length > obj.maxLen ? ('长度不得超过' + obj.maxLen + '位') : '' + + if(!txt && obj.minLen) + txt = val.length < obj.minLen ? ('长度不得小于' + obj.minLen + '位') : '' + + if(!txt && obj.hasOwnProperty('max')) + txt = val > obj.max ? ('输入值不能大于' + obj.max) : '' + + if(!txt && obj.hasOwnProperty('min')) + txt = val < obj.min ? ('输入值不能小于' + obj.min) : '' + + if(!txt && obj.eq){ + var eqEl = document.querySelector('#' + obj.eq) + txt = val !== eqEl.value ? (obj.msg || '2次值不一致') : '' + } + + + if(txt){ + if(ev){ + tips.textContent = txt + elem.parentNode.appendChild(tips) + } + //必须是"必填项"才会更新验证状态 + if(_this.target && obj.require){ + _this.target[ruleID].checked = false + } + }else{ + if(_this.target){ + _this.target[ruleID].checked = true + } + try{ + elem.parentNode.removeChild(tips) + }catch(err){} + } + } + + + $elem.bind('change,blur', checked) + $elem.bind('focus', function(ev){ + try{ + elem.parentNode.removeChild(tips) + }catch(err){} + + }) + + checked() + } +}) + + +//双工绑定 +var rduplexType = /^(?:checkbox|radio)$/ +var rduplexParam = /^(?:radio|checked)$/ +var rnoduplexInput = /^(file|button|reset|submit|checkbox|radio|range)$/ +var duplexBinding = yua.directive("duplex", { + priority: 2000, + init: function (binding, hasCast) { + var elem = binding.element + var vmodels = binding.vmodels + binding.changed = getBindingCallback(elem, "data-duplex-changed", vmodels) || noop + var params = [] + var casting = oneObject("string,number,boolean,checked") + if (elem.type === "radio" && binding.param === "") { + binding.param = "checked" + } + + binding.param.replace(rw20g, function (name) { + if (rduplexType.test(elem.type) && rduplexParam.test(name)) { + if (name === "radio") + log(":duplex-radio已经更名为:duplex-checked") + name = "checked" + binding.isChecked = true + binding.xtype = "radio" + } + if (name === "bool") { + name = "boolean" + log(":duplex-bool已经更名为:duplex-boolean") + } else if (name === "text") { + name = "string" + log(":duplex-text已经更名为:duplex-string") + } + if (casting[name]) { + hasCast = true + } + yua.Array.ensure(params, name) + }) + if (!hasCast) { + params.push("string") + } + binding.param = params.join("-") + if (!binding.xtype) { + binding.xtype = elem.tagName === "SELECT" ? "select" : + elem.type === "checkbox" ? "checkbox" : + elem.type === "radio" ? "radio" : + /^change/.test(elem.getAttribute("data-duplex-event")) ? "change" : + "input" + } + //===================绑定事件====================== + var bound = binding.bound = function (type, callback) { + elem.addEventListener(type, callback, false) + var old = binding.rollback + binding.rollback = function () { + elem.yuaSetter = null + yua.unbind(elem, type, callback) + old && old() + } + } + function callback(value) { + binding.changed.call(this, value, binding) + } + var composing = false + function compositionStart() { + composing = true + } + function compositionEnd() { + composing = false + setTimeout(updateVModel) + } + var updateVModel = function (e) { + + var val = elem.value; + //防止递归调用形成死循环 + //处理中文输入法在minlengh下引发的BUG + if (composing || val === binding.oldValue || binding.pipe === null){ + return + } + + var lastValue = binding.pipe(val, binding, "get") + binding.oldValue = val + binding.setter(lastValue) + + callback.call(elem, lastValue) + yua.fireDom(elem, 'change') + + } + switch (binding.xtype) { + case "radio": + bound("click", function () { + var lastValue = binding.pipe(elem.value, binding, "get") + binding.setter(lastValue) + callback.call(elem, lastValue) + }) + break + case "checkbox": + bound("change", function () { + var method = elem.checked ? "ensure" : "remove" + var array = binding.getter.apply(0, binding.vmodels) + if (!Array.isArray(array)) { + log(":duplex应用于checkbox上要对应一个数组") + array = [array] + } + var val = binding.pipe(elem.value, binding, "get") + yua.Array[method](array, val) + callback.call(elem, array) + }) + break + case "change": + bound("change", updateVModel) + break + case "input": + bound("input", updateVModel) + bound("keyup", updateVModel) + if (!IEVersion) { + bound("compositionstart", compositionStart) + bound("compositionend", compositionEnd) + bound("DOMAutoComplete", updateVModel) + } + break + case "select": + bound("change", function () { + var val = yua(elem).val() //字符串或字符串数组 + if (Array.isArray(val)) { + val = val.map(function (v) { + return binding.pipe(v, binding, "get") + }) + } else { + val = binding.pipe(val, binding, "get") + } + if (val + "" !== binding.oldValue) { + try { + binding.setter(val) + } catch (ex) { + log(ex) + } + } + }) + bound("datasetchanged", function (e) { + if (e.bubble === "selectDuplex") { + var value = binding._value + var curValue = Array.isArray(value) ? value.map(String) : value + "" + yua(elem).val(curValue) + elem.oldValue = curValue + "" + callback.call(elem, curValue) + } + }) + break + } + if (binding.xtype === "input" && !rnoduplexInput.test(elem.type)) { + if (elem.type !== "hidden") { + bound("focus", function () { + elem.msFocus = true + }) + bound("blur", function () { + elem.msFocus = false + }) + } + elem.yuaSetter = updateVModel //#765 + watchValueInTimer(function () { + if (root.contains(elem)) { + if (!elem.msFocus) { + updateVModel() + } + } else if (!elem.msRetain) { + return false + } + }) + } + + }, + update: function (value) { + var elem = this.element, binding = this, curValue + if (!this.init) { + for (var i in yua.vmodels) { + var v = yua.vmodels[i] + v.$fire("yua-duplex-init", binding) + } + var cpipe = binding.pipe || (binding.pipe = pipe) + cpipe(null, binding, "init") + this.init = 1 + } + switch (this.xtype) { + case "input": + case "change": + curValue = this.pipe(value, this, "set") //fix #673 + if (curValue !== this.oldValue) { + var fixCaret = false + if (elem.msFocus) { + try { + var start = elem.selectionStart + var end = elem.selectionEnd + if (start === end) { + var pos = start + fixCaret = true + } + } catch (e) { + } + } + elem.value = this.oldValue = curValue + if (fixCaret && !elem.readOnly) { + elem.selectionStart = elem.selectionEnd = pos + } + } + break + case "radio": + curValue = binding.isChecked ? !!value : value + "" === elem.value + elem.checked = curValue + break + case "checkbox": + var array = [].concat(value) //强制转换为数组 + curValue = this.pipe(elem.value, this, "get") + elem.checked = array.indexOf(curValue) > -1 + break + case "select": + //必须变成字符串后才能比较 + binding._value = value + if (!elem.msHasEvent) { + elem.msHasEvent = "selectDuplex" + //必须等到其孩子准备好才触发 + } else { + yua.fireDom(elem, "datasetchanged", { + bubble: elem.msHasEvent + }) + } + break + } + } +}) + + +function fixNull(val) { + return val == null ? "" : val +} +yua.duplexHooks = { + checked: { + get: function (val, binding) { + return !binding.oldValue + } + }, + string: { + get: function (val) { //同步到VM + return val + }, + set: fixNull + }, + "boolean": { + get: function (val) { + return val === "true" + }, + set: fixNull + }, + number: { + get: function (val, binding) { + var number = val - 0 + if (-val === -number) { + return number + } + var arr = /strong|medium|weak/.exec(binding.element.getAttribute("data-duplex-number")) || ["medium"] + switch (arr[0]) { + case "strong": + return 0 + case "medium": + return val === "" ? "" : 0 + case "weak": + return val + } + }, + set: fixNull + } +} + +function pipe(val, binding, action, e) { + binding.param.replace(rw20g, function (name) { + var hook = yua.duplexHooks[name] + if (hook && typeof hook[action] === "function") { + val = hook[action](val, binding) + } + }) + return val +} + +var TimerID, ribbon = [] + +yua.tick = function (fn) { + if (ribbon.push(fn) === 1) { + TimerID = setInterval(ticker, 60) + } +} + +function ticker() { + for (var n = ribbon.length - 1; n >= 0; n--) { + var el = ribbon[n] + if (el() === false) { + ribbon.splice(n, 1) + } + } + if (!ribbon.length) { + clearInterval(TimerID) + } +} + +var watchValueInTimer = noop +new function () { // jshint ignore:line + try { //#272 IE9-IE11, firefox + var setters = {} + var aproto = HTMLInputElement.prototype + var bproto = HTMLTextAreaElement.prototype + function newSetter(value) { // jshint ignore:line + setters[this.tagName].call(this, value) + if (!this.msFocus && this.yuaSetter) { + this.yuaSetter() + } + } + var inputProto = HTMLInputElement.prototype + Object.getOwnPropertyNames(inputProto) //故意引发IE6-8等浏览器报错 + setters["INPUT"] = Object.getOwnPropertyDescriptor(aproto, "value").set + + Object.defineProperty(aproto, "value", { + set: newSetter + }) + setters["TEXTAREA"] = Object.getOwnPropertyDescriptor(bproto, "value").set + Object.defineProperty(bproto, "value", { + set: newSetter + }) + } catch (e) { + //在chrome 43中 :duplex终于不需要使用定时器实现双向绑定了 + // http://updates.html5rocks.com/2015/04/DOM-attributes-now-on-the-prototype + // https://docs.google.com/document/d/1jwA8mtClwxI-QJuHT7872Z0pxpZz8PBkf2bGAbsUtqs/edit?pli=1 + watchValueInTimer = yua.tick + } +} // jshint ignore:line + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +/*-------------动画------------*/ + +yua.directive("effect", { + priority: 5, + init: function (binding) { + var text = binding.expr, + className, + rightExpr + var colonIndex = text.replace(rexprg, function (a) { + return a.replace(/./g, "0") + }).indexOf(":") //取得第一个冒号的位置 + if (colonIndex === -1) { // 比如 :class/effect="aaa bbb ccc" 的情况 + className = text + rightExpr = true + } else { // 比如 :class/effect-1="ui-state-active:checked" 的情况 + className = text.slice(0, colonIndex) + rightExpr = text.slice(colonIndex + 1) + } + if (!rexpr.test(text)) { + className = quote(className) + } else { + className = normalizeExpr(className) + } + binding.expr = "[" + className + "," + rightExpr + "]" + }, + update: function (arr) { + var name = arr[0] + var elem = this.element + if (elem.getAttribute("data-effect-name") === name) { + return + } else { + elem.removeAttribute("data-effect-driver") + } + var inlineStyles = elem.style + var computedStyles = window.getComputedStyle ? window.getComputedStyle(elem) : null + var useAni = false + if (computedStyles && (supportTransition || supportAnimation)) { + + //如果支持CSS动画 + var duration = inlineStyles[transitionDuration] || computedStyles[transitionDuration] + if (duration && duration !== '0s') { + elem.setAttribute("data-effect-driver", "t") + useAni = true + } + + if (!useAni) { + + duration = inlineStyles[animationDuration] || computedStyles[animationDuration] + if (duration && duration !== '0s') { + elem.setAttribute("data-effect-driver", "a") + useAni = true + } + + } + } + + if (!useAni) { + if (yua.effects[name]) { + elem.setAttribute("data-effect-driver", "j") + useAni = true + } + } + if (useAni) { + elem.setAttribute("data-effect-name", name) + } + } +}) + +yua.effects = {} +yua.effect = function (name, callbacks) { + yua.effects[name] = callbacks +} + + + +var supportTransition = false +var supportAnimation = false + +var transitionEndEvent +var animationEndEvent +var transitionDuration = yua.cssName("transition-duration") +var animationDuration = yua.cssName("animation-duration") +new function () {// jshint ignore:line + var checker = { + 'TransitionEvent': 'transitionend', + 'WebKitTransitionEvent': 'webkitTransitionEnd', + 'OTransitionEvent': 'oTransitionEnd', + 'otransitionEvent': 'otransitionEnd' + } + var tran + //有的浏览器同时支持私有实现与标准写法,比如webkit支持前两种,Opera支持1、3、4 + for (var name in checker) { + if (window[name]) { + tran = checker[name] + break; + } + try { + var a = document.createEvent(name); + tran = checker[name] + break; + } catch (e) { + } + } + if (typeof tran === "string") { + supportTransition = true + transitionEndEvent = tran + } + + //大致上有两种选择 + //IE10+, Firefox 16+ & Opera 12.1+: animationend + //Chrome/Safari: webkitAnimationEnd + //http://blogs.msdn.com/b/davrous/archive/2011/12/06/introduction-to-css3-animat ions.aspx + //IE10也可以使用MSAnimationEnd监听,但是回调里的事件 type依然为animationend + // el.addEventListener("MSAnimationEnd", function(e) { + // alert(e.type)// animationend!!! + // }) + checker = { + 'AnimationEvent': 'animationend', + 'WebKitAnimationEvent': 'webkitAnimationEnd' + } + var ani; + for (name in checker) { + if (window[name]) { + ani = checker[name]; + break; + } + } + if (typeof ani === "string") { + supportTransition = true + animationEndEvent = ani + } + +}() + +var effectPool = []//重复利用动画实例 +function effectFactory(el, opts) { + if (!el || el.nodeType !== 1) { + return null + } + if (opts) { + var name = opts.effectName + var driver = opts.effectDriver + } else { + name = el.getAttribute("data-effect-name") + driver = el.getAttribute("data-effect-driver") + } + if (!name || !driver) { + return null + } + + var instance = effectPool.pop() || new Effect() + instance.el = el + instance.driver = driver + instance.useCss = driver !== "j" + if (instance.useCss) { + opts && yua(el).addClass(opts.effectClass) + instance.cssEvent = driver === "t" ? transitionEndEvent : animationEndEvent + } + instance.name = name + instance.callbacks = yua.effects[name] || {} + + return instance + + +} + +function effectBinding(elem, binding) { + var name = elem.getAttribute("data-effect-name") + if (name) { + binding.effectName = name + binding.effectDriver = elem.getAttribute("data-effect-driver") + var stagger = +elem.getAttribute("data-effect-stagger") + binding.effectLeaveStagger = +elem.getAttribute("data-effect-leave-stagger") || stagger + binding.effectEnterStagger = +elem.getAttribute("data-effect-enter-stagger") || stagger + binding.effectClass = elem.className || NaN + } +} +function upperFirstChar(str) { + return str.replace(/^[\S]/g, function (m) { + return m.toUpperCase() + }) +} +var effectBuffer = new Buffer() +function Effect() { +}//动画实例,做成类的形式,是为了共用所有原型方法 + +Effect.prototype = { + contrustor: Effect, + enterClass: function () { + return getEffectClass(this, "enter") + }, + leaveClass: function () { + return getEffectClass(this, "leave") + }, + // 共享一个函数 + actionFun: function (name, before, after) { + if (document.hidden) { + return + } + var me = this + var el = me.el + var isLeave = name === "leave" + name = isLeave ? "leave" : "enter" + var oppositeName = isLeave ? "enter" : "leave" + callEffectHook(me, "abort" + upperFirstChar(oppositeName)) + callEffectHook(me, "before" + upperFirstChar(name)) + if (!isLeave) + before(el) //这里可能做插入DOM树的操作,因此必须在修改类名前执行 + var cssCallback = function (cancel) { + el.removeEventListener(me.cssEvent, me.cssCallback) + if (isLeave) { + before(el) //这里可能做移出DOM树操作,因此必须位于动画之后 + yua(el).removeClass(me.cssClass) + } else { + if (me.driver === "a") { + yua(el).removeClass(me.cssClass) + } + } + if (cancel !== true) { + callEffectHook(me, "after" + upperFirstChar(name)) + after && after(el) + } + me.dispose() + } + if (me.useCss) { + if (me.cssCallback) { //如果leave动画还没有完成,立即完成 + me.cssCallback(true) + } + + me.cssClass = getEffectClass(me, name) + me.cssCallback = cssCallback + + me.update = function () { + el.addEventListener(me.cssEvent, me.cssCallback) + if (!isLeave && me.driver === "t") {//transtion延迟触发 + yua(el).removeClass(me.cssClass) + } + } + yua(el).addClass(me.cssClass)//animation会立即触发 + + effectBuffer.render(true) + effectBuffer.queue.push(me) + + } else { + callEffectHook(me, name, cssCallback) + + } + }, + enter: function (before, after) { + this.actionFun.apply(this, ["enter"].concat(yua.slice(arguments))) + + }, + leave: function (before, after) { + this.actionFun.apply(this, ["leave"].concat(yua.slice(arguments))) + + }, + dispose: function () {//销毁与回收到池子中 + this.update = this.cssCallback = null + if (effectPool.unshift(this) > 100) { + effectPool.pop() + } + } + + +} + + +function getEffectClass(instance, type) { + var a = instance.callbacks[type + "Class"] + if (typeof a === "string") + return a + if (typeof a === "function") + return a() + return instance.name + "-" + type +} + + +function callEffectHook(effect, name, cb) { + var hook = effect.callbacks[name] + if (hook) { + hook.call(effect, effect.el, cb) + } +} + +var applyEffect = function (el, dir/*[before, [after, [opts]]]*/) { + var args = aslice.call(arguments, 0) + if (typeof args[2] !== "function") { + args.splice(2, 0, noop) + } + if (typeof args[3] !== "function") { + args.splice(3, 0, noop) + } + var before = args[2] + var after = args[3] + var opts = args[4] + var effect = effectFactory(el, opts) + if (!effect) { + before() + after() + return false + } else { + var method = dir ? 'enter' : 'leave' + effect[method](before, after) + } +} + +yua.mix(yua.effect, { + apply: applyEffect, + append: function (el, parent, after, opts) { + return applyEffect(el, 1, function () { + parent.appendChild(el) + }, after, opts) + }, + before: function (el, target, after, opts) { + return applyEffect(el, 1, function () { + target.parentNode.insertBefore(el, target) + }, after, opts) + }, + remove: function (el, parent, after, opts) { + return applyEffect(el, 0, function () { + if (el.parentNode === parent) + parent.removeChild(el) + }, after, opts) + } +}) + + + + + + + + + + + + + + + + + + + + + + +yua.directive("html", { + update: function (val) { + var binding = this + var elem = this.element + var isHtmlFilter = elem.nodeType !== 1 + var parent = isHtmlFilter ? elem.parentNode : elem + if (!parent) + return + val = val == null ? "" : val + + if (elem.nodeType === 3) { + var signature = generateID("html") + parent.insertBefore(DOC.createComment(signature), elem) + binding.element = DOC.createComment(signature + ":end") + parent.replaceChild(binding.element, elem) + elem = binding.element + } + if (typeof val !== "object") {//string, number, boolean + var fragment = yua.parseHTML(String(val)) + } else if (val.nodeType === 11) { //将val转换为文档碎片 + fragment = val + } else if (val.nodeType === 1 || val.item) { + var nodes = val.nodeType === 1 ? val.childNodes : val.item + fragment = yuaFragment.cloneNode(true) + while (nodes[0]) { + fragment.appendChild(nodes[0]) + } + } + + nodes = yua.slice(fragment.childNodes) + //插入占位符, 如果是过滤器,需要有节制地移除指定的数量,如果是html指令,直接清空 + if (isHtmlFilter) { + var endValue = elem.nodeValue.slice(0, -4) + while (true) { + var node = elem.previousSibling + if (!node || node.nodeType === 8 && node.nodeValue === endValue) { + break + } else { + parent.removeChild(node) + } + } + parent.insertBefore(fragment, elem) + } else { + yua.clearHTML(elem).appendChild(fragment) + } + scanNodeArray(nodes, binding.vmodels) + } +}) + +yua.directive("if", { + priority: 10, + update: function (val) { + var binding = this + var elem = this.element + var stamp = binding.stamp = +(new Date()) + var par + var after = function () { + if (stamp !== binding.stamp) + return + binding.recoverNode = null + } + if (binding.recoverNode) + binding.recoverNode() // 还原现场,有移动节点的都需要还原现场 + try { + if (!elem.parentNode) + return + par = elem.parentNode + } catch (e) { + return + } + if (val) { //插回DOM树 + function alway() {// jshint ignore:line + if (elem.getAttribute(binding.name)) { + elem.removeAttribute(binding.name) + scanAttr(elem, binding.vmodels) + } + binding.rollback = null + } + if (elem.nodeType === 8) { + var keep = binding.keep + var hasEffect = yua.effect.apply(keep, 1, function () { + if (stamp !== binding.stamp) + return + elem.parentNode.replaceChild(keep, elem) + elem = binding.element = keep //这时可能为null + if (keep.getAttribute("_required")) {//#1044 + elem.required = true + elem.removeAttribute("_required") + } + if (elem.querySelectorAll) { + yua.each(elem.querySelectorAll("[_required=true]"), function (el) { + el.required = true + el.removeAttribute("_required") + }) + } + alway() + }, after) + hasEffect = hasEffect === false + } + if (!hasEffect) + alway() + } else { //移出DOM树,并用注释节点占据原位置 + if (elem.nodeType === 1) { + if (elem.required === true) { + elem.required = false + elem.setAttribute("_required", "true") + } + try {//如果不支持querySelectorAll或:required,可以直接无视 + yua.each(elem.querySelectorAll(":required"), function (el) { + elem.required = false + el.setAttribute("_required", "true") + }) + } catch (e) { + } + + var node = binding.element = DOC.createComment(":if"), + pos = elem.nextSibling + binding.recoverNode = function () { + binding.recoverNode = null + if (node.parentNode !== par) { + par.insertBefore(node, pos) + binding.keep = elem + } + } + + yua.effect.apply(elem, 0, function () { + binding.recoverNode = null + if (stamp !== binding.stamp) + return + elem.parentNode.replaceChild(node, elem) + binding.keep = elem //元素节点 + ifGroup.appendChild(elem) + binding.rollback = function () { + if (elem.parentNode === ifGroup) { + ifGroup.removeChild(elem) + } + } + }, after) + } + } + } +}) + +//:important绑定已经在scanTag 方法中实现 +var rnoscripts = /(?:[\s\S]+?)<\/noscript>/img +var rnoscriptText = /([\s\S]+?)<\/noscript>/im + +var getXHR = function () { + return new window.XMLHttpRequest() // jshint ignore:line +} +//将所有远程加载的模板,以字符串形式存放到这里 +var templatePool = yua.templateCache = {} + +function getTemplateContainer(binding, id, text) { + var div = binding.templateCache && binding.templateCache[id] + if (div) { + var dom = DOC.createDocumentFragment(), + firstChild + while (firstChild = div.firstChild) { + dom.appendChild(firstChild) + } + return dom + } + return yua.parseHTML(text) + +} +function nodesToFrag(nodes) { + var frag = DOC.createDocumentFragment() + for (var i = 0, len = nodes.length; i < len; i++) { + frag.appendChild(nodes[i]) + } + return frag +} +yua.directive("include", { + init: directives.attr.init, + update: function (val) { + var binding = this + var elem = this.element + var vmodels = binding.vmodels + var rendered = binding.includeRendered + var effectClass = binding.effectName && binding.effectClass // 是否开启动画 + var templateCache = binding.templateCache // 是否data-include-cache + var outer = binding.includeReplace // 是否data-include-replace + var loaded = binding.includeLoaded + var target = outer ? elem.parentNode : elem + var _ele = binding._element // data-include-replace binding.element === binding.end + + binding.recoverNodes = binding.recoverNodes || yua.noop + + var scanTemplate = function (text) { + var _stamp = binding._stamp = +(new Date()) // 过滤掉频繁操作 + if (loaded) { + var newText = loaded.apply(target, [text].concat(vmodels)) + if (typeof newText === "string") + text = newText + } + if (rendered) { + checkScan(target, function () { + rendered.call(target) + }, NaN) + } + var lastID = binding.includeLastID || "_default" // 默认 + + binding.includeLastID = val + var leaveEl = templateCache && templateCache[lastID] || DOC.createElement(elem.tagName || binding._element.tagName) // 创建一个离场元素 + + if (effectClass) { + leaveEl.className = effectClass + target.insertBefore(leaveEl, binding.start) // 插入到start之前,防止被错误的移动 + } + + // cache or animate,移动节点 + (templateCache || {})[lastID] = leaveEl + var fragOnDom = binding.recoverNodes() // 恢复动画中的节点 + if (fragOnDom) { + target.insertBefore(fragOnDom, binding.end) + } + while (true) { + var node = binding.start.nextSibling + if (node && node !== leaveEl && node !== binding.end) { + leaveEl.appendChild(node) + } else { + break + } + } + + // 元素退场 + yua.effect.remove(leaveEl, target, function () { + if (templateCache) { // write cache + if (_stamp === binding._stamp) + ifGroup.appendChild(leaveEl) + } + }, binding) + + + var enterEl = target, + before = yua.noop, + after = yua.noop; + + var fragment = getTemplateContainer(binding, val, text) + var nodes = yua.slice(fragment.childNodes) + + if (outer && effectClass) { + enterEl = _ele + enterEl.innerHTML = "" // 清空 + enterEl.setAttribute(":skip", "true") + target.insertBefore(enterEl, binding.end.nextSibling) // 插入到bingding.end之后避免被错误的移动 + before = function () { + enterEl.insertBefore(fragment, null) // 插入节点 + } + after = function () { + binding.recoverNodes = yua.noop + if (_stamp === binding._stamp) { + fragment = nodesToFrag(nodes) + target.insertBefore(fragment, binding.end) // 插入真实element + scanNodeArray(nodes, vmodels) + } + if (enterEl.parentNode === target) + target.removeChild(enterEl) // 移除入场动画元素 + } + binding.recoverNodes = function () { + binding.recoverNodes = yua.noop + return nodesToFrag(nodes) + } + } else { + before = function () {//新添加元素的动画 + target.insertBefore(fragment, binding.end) + scanNodeArray(nodes, vmodels) + } + } + + yua.effect.apply(enterEl, "enter", before, after) + + } + + if(!val) + return + + var el = val + + + if(typeof el === 'object'){ + if(el.nodeType !== 1) + return log('include 不支持非DOM对象') + }else{ + el = DOC.getElementById(val) + if(!el){ + if (typeof templatePool[val] === "string") { + yua.nextTick(function () { + scanTemplate(templatePool[val]) + }) + } else if (Array.isArray(templatePool[val])) { //#805 防止在循环绑定中发出许多相同的请求 + templatePool[val].push(scanTemplate) + } else { + var xhr = getXHR() + xhr.onload = function () { + if(xhr.status !== 200) + return log('获取网络资源出错, httpError[' + xhr.status + ']') + + var text = xhr.responseText + for (var f = 0, fn; fn = templatePool[val][f++]; ) { + fn(text) + } + templatePool[val] = text + } + xhr.onerror = function () { + log(":include load [" + val + "] error") + } + templatePool[val] = [scanTemplate] + xhr.open("GET", val, true) + if ("withCredentials" in xhr) { + xhr.withCredentials = true + } + xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest") + xhr.send(null) + } + return + } + } + + yua.nextTick(function () { + scanTemplate(el.value || el.innerText || el.innerHTML) + }) + + } +}) + +var rdash = /\(([^)]*)\)/ +var onDir = yua.directive("on", { + priority: 3000, + init: function (binding) { + var value = binding.expr + binding.type = "on" + var eventType = binding.param.replace(/-\d+$/, "") // :on-mousemove-10 + if (typeof onDir[eventType + "Hook"] === "function") { + onDir[eventType + "Hook"](binding) + } + if (value.indexOf("(") > 0 && value.indexOf(")") > -1) { + var matched = (value.match(rdash) || ["", ""])[1].trim() + if (matched === "" || matched === "$event") { // aaa() aaa($event)当成aaa处理 + value = value.replace(rdash, "") + } + } + binding.expr = value + }, + update: function (callback) { + var binding = this + var elem = this.element + callback = function (e) { + var fn = binding.getter || noop + return fn.apply(this, binding.args.concat(e)) + } + + var eventType = binding.param.replace(/-\d+$/, "") // :on-mousemove-10 + if (eventType === "scan") { + callback.call(elem, { + type: eventType + }) + } else if (typeof binding.specialBind === "function") { + binding.specialBind(elem, callback) + } else { + var removeFn = yua.bind(elem, eventType, callback) + } + binding.rollback = function () { + if (typeof binding.specialUnbind === "function") { + binding.specialUnbind() + } else { + yua.unbind(elem, eventType, removeFn) + } + } + } +}) + +yua.directive("repeat", { + priority: 90, + init: function (binding) { + var type = binding.type + binding.cache = {} //用于存放代理VM + binding.enterCount = 0 + + var elem = binding.element + if (elem.nodeType === 1) { + elem.removeAttribute(binding.name) + effectBinding(elem, binding) + binding.param = binding.param || "el" + binding.sortedCallback = getBindingCallback(elem, "data-with-sorted", binding.vmodels) + var rendered = getBindingCallback(elem, "data-" + type + "-rendered", binding.vmodels) + + var signature = generateID(type) + var start = DOC.createComment(signature + ":start") + var end = binding.element = DOC.createComment(signature + ":end") + binding.signature = signature + binding.start = start + binding.template = yuaFragment.cloneNode(false) + if (type === "repeat") { + var parent = elem.parentNode + parent.replaceChild(end, elem) + parent.insertBefore(start, end) + binding.template.appendChild(elem) + } else { + while (elem.firstChild) { + binding.template.appendChild(elem.firstChild) + } + elem.appendChild(start) + elem.appendChild(end) + parent = elem + } + binding.element = end + + if (rendered) { + var removeFn = yua.bind(parent, "datasetchanged", function () { + rendered.apply(parent, parent.args) + yua.unbind(parent, "datasetchanged", removeFn) + parent.msRendered = rendered + }) + } + } + }, + update: function (value, oldValue) { + var binding = this + var xtype = this.xtype + + this.enterCount += 1 + var init = !oldValue + if (init) { + binding.$outer = {} + var check0 = "$key" + var check1 = "$val" + if (xtype === "array") { + check0 = "$first" + check1 = "$last" + } + for (var i = 0, v; v = binding.vmodels[i++]; ) { + if (v.hasOwnProperty(check0) && v.hasOwnProperty(check1)) { + binding.$outer = v + break + } + } + } + var track = this.track + if (binding.sortedCallback) { //如果有回调,则让它们排序 + var keys2 = binding.sortedCallback.call(parent, track) + if (keys2 && Array.isArray(keys2)) { + track = keys2 + } + } + + var action = "move" + binding.$repeat = value + var fragments = [] + var transation = init && yuaFragment.cloneNode(false) + var proxies = [] + var param = this.param + var retain = yua.mix({}, this.cache) + var elem = this.element + var length = track.length + + var parent = elem.parentNode + + //检查新元素数量 + var newCount = 0 + for (i = 0; i < length; i++) { + var keyOrId = track[i] + if (!retain[keyOrId]) + newCount++ + } + var oldCount = 0 + for (i in retain){ + oldCount++ + } + var clear = (!length || newCount === length) && oldCount > 10 //当全部是新元素,且移除元素较多(10)时使用clear + + var kill = elem.previousSibling + var start = binding.start + + /*log(kill === start, kill) + while(kill !== start && kill.nodeName !== '#comment'){ + parent.removeChild(kill) + kill = elem.previousSibling + }*/ + if (clear){ + while(kill !== start){ + parent.removeChild(kill) + kill = elem.previousSibling + } + } + + + for (i = 0; i < length; i++) { + + keyOrId = track[i] //array为随机数, object 为keyName + var proxy = retain[keyOrId] + if (!proxy) { + + proxy = getProxyVM(this) + proxy.$up = null + if (xtype === "array") { + action = "add" + proxy.$id = keyOrId + var valueItem = value[i] + proxy[param] = valueItem //index + if (Object(valueItem) === valueItem) { + valueItem.$ups = valueItem.$ups || {} + valueItem.$ups[param] = proxy + } + + } else { + action = "append" + proxy.$key = keyOrId + proxy.$val = value[keyOrId] //key + proxy[param] = { $key: proxy.$key, $val: proxy.$val } + } + this.cache[keyOrId] = proxy + var node = proxy.$anchor || (proxy.$anchor = elem.cloneNode(false)) + node.nodeValue = this.signature + shimController(binding, transation, proxy, fragments, init && !binding.effectDriver) + decorateProxy(proxy, binding, xtype) + } else { + // if (xtype === "array") { + // proxy[param] = value[i] + // } + fragments.push({}) + retain[keyOrId] = true + } + + //重写proxy + if (this.enterCount === 1) {//防止多次进入,导致位置不对 + proxy.$active = false + proxy.$oldIndex = proxy.$index + proxy.$active = true + proxy.$index = i + + } + + if (xtype === "array") { + proxy.$first = i === 0 + proxy.$last = i === length - 1 + // proxy[param] = value[i] + } else { + proxy.$val = toJson(value[keyOrId]) //这里是处理vm.object = newObject的情况 + } + proxies.push(proxy) + } + this.proxies = proxies + if (init && !binding.effectDriver) { + parent.insertBefore(transation, elem) + fragments.forEach(function (fragment) { + scanNodeArray(fragment.nodes || [], fragment.vmodels) + //if(fragment.vmodels.length > 2) + fragment.nodes = fragment.vmodels = null + })// jshint ignore:line + } else { + + var staggerIndex = binding.staggerIndex = 0 + for (keyOrId in retain) { + if (retain[keyOrId] !== true) { + + action = "del" + !clear && removeItem(retain[keyOrId].$anchor, binding,true) + // 相当于delete binding.cache[key] + proxyRecycler(this.cache, keyOrId, param) + retain[keyOrId] = null + } + } + + for (i = 0; i < length; i++) { + proxy = proxies[i] + keyOrId = xtype === "array" ? proxy.$id : proxy.$key + var pre = proxies[i - 1] + var preEl = pre ? pre.$anchor : binding.start + if (!retain[keyOrId]) {//如果还没有插入到DOM树,进行插入动画 + (function (fragment, preElement) { + var nodes = fragment.nodes + var vmodels = fragment.vmodels + if (nodes) { + staggerIndex = mayStaggerAnimate(binding.effectEnterStagger, function () { + parent.insertBefore(fragment.content, preElement.nextSibling) + scanNodeArray(nodes, vmodels) + !init && animateRepeat(nodes, 1, binding) + }, staggerIndex) + } + fragment.nodes = fragment.vmodels = null + })(fragments[i], preEl)// jshint ignore:line + + } else if (proxy.$index !== proxy.$oldIndex) {//进行移动动画 + (function (proxy2, preElement) { + staggerIndex = mayStaggerAnimate(binding.effectEnterStagger, function () { + var curNode = removeItem(proxy2.$anchor) + var inserted = yua.slice(curNode.childNodes) + parent.insertBefore(curNode, preElement.nextSibling) + animateRepeat(inserted, 1, binding) + }, staggerIndex) + })(proxy, preEl)// jshint ignore:line + + } + } + + } + if (!value.$track) {//如果是非监控对象,那么就将其$events清空,阻止其持续监听 + for (keyOrId in this.cache) { + proxyRecycler(this.cache, keyOrId, param) + } + + } + + //repeat --> duplex + (function (args) { + parent.args = args + if (parent.msRendered) {//第一次事件触发,以后直接调用 + parent.msRendered.apply(parent, args) + } + })(kernel.newWatch ? arguments : [action]); + var id = setTimeout(function () { + clearTimeout(id) + //触发上层的select回调及自己的rendered回调 + yua.fireDom(parent, "datasetchanged", { + bubble: parent.msHasEvent + }) + }) + this.enterCount -= 1 + + } + +}) + + + + +function animateRepeat(nodes, isEnter, binding) { + for (var i = 0, node; node = nodes[i++]; ) { + if (node.className === binding.effectClass) { + yua.effect.apply(node, isEnter, noop, noop, binding) + } + } +} + +function mayStaggerAnimate(staggerTime, callback, index) { + if (staggerTime) { + setTimeout(callback, (++index) * staggerTime) + } else { + callback() + } + return index +} + +function removeItem(node, binding, flagRemove) { + var fragment = yuaFragment.cloneNode(false) + var last = node + var breakText = last.nodeValue + var staggerIndex = binding && Math.max(+binding.staggerIndex, 0) + var nodes = yua.slice(last.parentNode.childNodes) + var index = nodes.indexOf(last) + while (true) { + var pre = nodes[--index] //node.previousSibling + if (!pre || String(pre.nodeValue).indexOf(breakText) === 0) { + break + } + if (!flagRemove && binding && (pre.className === binding.effectClass)) { + node = pre; + (function (cur) { + binding.staggerIndex = mayStaggerAnimate(binding.effectLeaveStagger, function () { + yua.effect.apply(cur, 0, noop, function () { + fragment.appendChild(cur) + }, binding) + }, staggerIndex) + })(pre);// jshint ignore:line + } else { + fragment.insertBefore(pre, fragment.firstChild) + } + } + fragment.appendChild(last) + return fragment +} + +function shimController(data, transation, proxy, fragments, init) { + var content = data.template.cloneNode(true) + var nodes = yua.slice(content.childNodes) + content.appendChild(proxy.$anchor) + init && transation.appendChild(content) + var itemName = data.param || "el" + var valueItem = proxy[itemName], nv + + nv = [proxy].concat(data.vmodels) + + + var fragment = { + nodes: nodes, + vmodels: nv, + content: content + } + fragments.push(fragment) +} +// {} --> {xx: 0, yy: 1, zz: 2} add +// {xx: 0, yy: 1, zz: 2} --> {xx: 0, yy: 1, zz: 2, uu: 3} +// [xx: 0, yy: 1, zz: 2} --> {xx: 0, zz: 1, yy: 2} + +function getProxyVM(binding) { + var agent = binding.xtype === "object" ? withProxyAgent : eachProxyAgent + var proxy = agent(binding) + var node = proxy.$anchor || (proxy.$anchor = binding.element.cloneNode(false)) + node.nodeValue = binding.signature + proxy.$outer = binding.$outer + return proxy +} + +function decorateProxy(proxy, binding, type) { + if (type === "array") { + proxy.$remove = function () { + binding.$repeat.removeAt(proxy.$index) + } + var param = binding.param + proxy.$watch(param, function (a) { + var index = proxy.$index + binding.$repeat[index] = a + }) + } else { + proxy.$watch("$val", function fn(a) { + binding.$repeat[proxy.$key] = a + }) + } +} + + +var eachProxyPool = [] + +function eachProxyAgent(data, proxy) { + var itemName = data.param || "el" + for (var i = 0, n = eachProxyPool.length; i < n; i++) { + var candidate = eachProxyPool[i] + if (candidate && candidate.hasOwnProperty(itemName)) { + eachProxyPool.splice(i, 1) + proxy = candidate + break + } + } + if (!proxy) { + proxy = eachProxyFactory(itemName) + } + return proxy +} + +function eachProxyFactory(itemName) { + var source = { + $outer: {}, + $index: 0, + $oldIndex: 0, + $anchor: null, + //----- + $first: false, + $last: false, + $remove: yua.noop + } + source[itemName] = NaN + var force = { + $last: 1, + $first: 1, + $index: 1 + } + force[itemName] = 1 + var proxy = modelFactory(source, { + force: force + }) + proxy.$id = generateID("$proxy$each") + return proxy +} + +var withProxyPool = [] + +function withProxyAgent(data) { + var itemName = data.param || "el" + return withProxyPool.pop() || withProxyFactory(itemName) +} + +function withProxyFactory(itemName) { + var source = { + $key: "", + $val: NaN, + $index: 0, + $oldIndex: 0, + $outer: {}, + $anchor: null + } + source[itemName] = NaN + var force = { + $key: 1, + $val: 1, + $index: 1 + } + force[itemName] = 1 + var proxy = modelFactory(source, { + force: force + }) + proxy.$id = generateID("$proxy$with") + return proxy +} + + +function proxyRecycler(cache, key, param) { + var proxy = cache[key] + if (proxy) { + var proxyPool = proxy.$id.indexOf("$proxy$each") === 0 ? eachProxyPool : withProxyPool + proxy.$outer = {} + + for (var i in proxy.$events) { + var a = proxy.$events[i] + if (Array.isArray(a)) { + a.length = 0 + if (i === param) { + proxy[param] = NaN + } else if (i === "$val") { + proxy.$val = NaN + } + } + } + + if (proxyPool.unshift(proxy) > kernel.maxRepeatSize) { + proxyPool.pop() + } + delete cache[key] + } +} + + + + + + +/********************************************************************* + * 各种指令 * + **********************************************************************/ + +//:skip绑定已经在scanTag 方法中实现 +yua.directive("text", { + update: function (val) { + var elem = this.element + val = val == null ? "" : val //不在页面上显示undefined null + if (elem.nodeType === 3) { //绑定在文本节点上 + try { //IE对游离于DOM树外的节点赋值会报错 + elem.data = val + } catch (e) { + } + } else { //绑定在特性节点上 + elem.textContent = val + } + } +}) +function parseDisplay(nodeName, val) { + //用于取得此类标签的默认display值 + var key = "_" + nodeName + if (!parseDisplay[key]) { + var node = DOC.createElement(nodeName) + root.appendChild(node) + if (W3C) { + val = getComputedStyle(node, null).display + } else { + val = node.currentStyle.display + } + root.removeChild(node) + parseDisplay[key] = val + } + return parseDisplay[key] +} + +yua.parseDisplay = parseDisplay + +yua.directive("visible", { + init: function (binding) { + effectBinding(binding.element, binding) + }, + update: function (val) { + var binding = this, elem = this.element, stamp + var noEffect = !this.effectName + if (!this.stamp) { + stamp = this.stamp = +new Date() + if (val) { + elem.style.display = binding.display || "" + if (yua(elem).css("display") === "none") { + elem.style.display = binding.display = parseDisplay(elem.nodeName) + } + } else { + elem.style.display = "none" + } + return + } + stamp = this.stamp = +new Date() + if (val) { + yua.effect.apply(elem, 1, function () { + if (stamp !== binding.stamp) + return + var driver = elem.getAttribute("data-effect-driver") || "a" + + if (noEffect) {//不用动画时走这里 + elem.style.display = binding.display || "" + } + // "a", "t" + if (driver === "a" || driver === "t") { + if (yua(elem).css("display") === "none") { + elem.style.display = binding.display || parseDisplay(elem.nodeName) + } + } + }) + } else { + yua.effect.apply(elem, 0, function () { + if (stamp !== binding.stamp) + return + elem.style.display = "none" + }) + } + } +}) + + + + + + + + + + + + + + +/********************************************************************* + * 自带过滤器 * + **********************************************************************/ + +var rscripts = /]*>([\S\s]*?)<\/script\s*>/gim +var ron = /\s+(on[^=\s]+)(?:=("[^"]*"|'[^']*'|[^\s>]+))?/g +var ropen = /<\w+\b(?:(["'])[^"]*?(\1)|[^>])*>/ig +var rsanitize = { + a: /\b(href)\=("javascript[^"]*"|'javascript[^']*')/ig, + img: /\b(src)\=("javascript[^"]*"|'javascript[^']*')/ig, + form: /\b(action)\=("javascript[^"]*"|'javascript[^']*')/ig +} +var rsurrogate = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g +var rnoalphanumeric = /([^\#-~| |!])/g; + +function numberFormat(number, decimals, point, thousands) { + //form http://phpjs.org/functions/number_format/ + //number 必需,要格式化的数字 + //decimals 可选,规定多少个小数位。 + //point 可选,规定用作小数点的字符串(默认为 . )。 + //thousands 可选,规定用作千位分隔符的字符串(默认为 , ),如果设置了该参数,那么所有其他参数都是必需的。 + number = (number + '') + .replace(/[^0-9+\-Ee.]/g, '') + var n = !isFinite(+number) ? 0 : +number, + prec = !isFinite(+decimals) ? 3 : Math.abs(decimals), + sep = thousands || ",", + dec = point || ".", + s = '', + toFixedFix = function(n, prec) { + var k = Math.pow(10, prec) + return '' + (Math.round(n * k) / k) + .toFixed(prec) + } + // Fix for IE parseFloat(0.55).toFixed(0) = 0; + s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)) + .split('.') + if (s[0].length > 3) { + s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep) + } + if ((s[1] || '') + .length < prec) { + s[1] = s[1] || '' + s[1] += new Array(prec - s[1].length + 1) + .join('0') + } + return s.join(dec) +} + +var filters = yua.filters = { + uppercase: function(str) { + return str.toUpperCase() + }, + lowercase: function(str) { + return str.toLowerCase() + }, + //字符串截取,超过指定长度以mark标识接上 + truncate: function(str, len, mark) { + len = len || 30 + mark = typeof mark === 'string' ? mark : '...' + return str.slice(0, len) + (str.length <= len ? '' : mark) + }, + //小值秒数转化为 时间格式 + time: function(str){ + str = str>>0; + var s = str % 60; + m = Math.floor(str / 60), + h = Math.floor(m / 60), + + m = m % 60; + m = m < 10 ? '0' + m : m; + s = s < 10 ? '0' + s : s; + + if(h > 0){ + h = h < 10 ? '0' + h : h; + return h + ':' + m + ':' + s; + } + return m + ':' + s; + }, + $filter: function(val) { + for (var i = 1, n = arguments.length; i < n; i++) { + var array = arguments[i] + var fn = yua.filters[array[0]] + if (typeof fn === "function") { + var arr = [val].concat(array.slice(1)) + val = fn.apply(null, arr) + } + } + return val + }, + camelize: camelize, + //https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet + // chrome + // chrome + // IE67chrome + // IE67chrome + // IE67chrome + sanitize: function(str) { + return str.replace(rscripts, "").replace(ropen, function(a, b) { + var match = a.toLowerCase().match(/<(\w+)\s/) + if (match) { //处理a标签的href属性,img标签的src属性,form标签的action属性 + var reg = rsanitize[match[1]] + if (reg) { + a = a.replace(reg, function(s, name, value) { + var quote = value.charAt(0) + return name + "=" + quote + "javascript:void(0)" + quote// jshint ignore:line + }) + } + } + return a.replace(ron, " ").replace(/\s+/g, " ") //移除onXXX事件 + }) + }, + escape: function(str) { + //将字符串经过 str 转义得到适合在页面中显示的内容, 例如替换 < 为 < + return String(str). + replace(/&/g, '&'). + replace(rsurrogate, function(value) { + var hi = value.charCodeAt(0) + var low = value.charCodeAt(1) + return '&#' + (((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000) + ';' + }). + replace(rnoalphanumeric, function(value) { + return '&#' + value.charCodeAt(0) + ';' + }). + replace(//g, '>') + }, + currency: function(amount, symbol, fractionSize) { + return (symbol || "\u00a5") + numberFormat(amount, isFinite(fractionSize) ? fractionSize : 2) + }, + number: numberFormat, + //日期格式化,类似php的date函数, + date: function(stamp, str, second){ + second = (second === undefined) ? false : true + var oDate; + if(!Date.isDate(stamp)){ + + if(!/[^\d]/.test(stamp)){ + stamp -= 0 + if(second){ + stamp *= 1000 + } + } + + oDate = new Date(stamp); + if((oDate + '') === 'Invalid Date') + return 'Invalid Date' + + }else{ + oDate = stamp + } + return oDate.format(str) + + } +} + + + + +/********************************************************************* + * AMD加载器 * + **********************************************************************/ + +//https://www.devbridge.com/articles/understanding-amd-requirejs/ +//http://maxogden.com/nested-dependencies.html +var modules = yua.modules = { + "domReady!": { + exports: yua, + state: 3 + }, + "yua": { + exports: yua, + state: 4 + } +} +//Object(modules[id]).state拥有如下值 +// undefined 没有定义 +// 1(send) 已经发出请求 +// 2(loading) 已经被执行但还没有执行完成,在这个阶段define方法会被执行 +// 3(loaded) 执行完毕,通过onload/onreadystatechange回调判定,在这个阶段checkDeps方法会执行 +// 4(execute) 其依赖也执行完毕, 值放到exports对象上,在这个阶段fireFactory方法会执行 +modules.exports = modules.yua +var otherRequire = window.require +var otherDefine = window.define +var innerRequire +plugins.loader = function (builtin) { + var flag = innerRequire && builtin + window.require = flag ? innerRequire : otherRequire + window.define = flag ? innerRequire.define : otherDefine +} +new function () {// jshint ignore:line + var loadings = [] //正在加载中的模块列表 + var factorys = [] //放置define方法的factory函数 + var rjsext = /\.js$/i + function makeRequest(name, config) { + //1. 去掉资源前缀 + var res = "js" + name = name.replace(/^(\w+)\!/, function (a, b) { + res = b + return "" + }) + if (res === "ready") { + log("debug: ready!已经被废弃,请使用domReady!") + res = "domReady" + } + //2. 去掉querystring, hash + var query = "" + name = name.replace(rquery, function (a) { + query = a + return "" + }) + //3. 去掉扩展名 + var suffix = "." + res + var ext = /js|css/.test(suffix) ? suffix : "" + name = name.replace(/\.[a-z0-9]+$/g, function (a) { + if (a === suffix) { + ext = a + return "" + } else { + return a + } + }) + var req = yua.mix({ + query: query, + ext: ext, + res: res, + name: name, + toUrl: toUrl + }, config) + req.toUrl(name) + return req + } + + function fireRequest(req) { + var name = req.name + var res = req.res + //1. 如果该模块已经发出请求,直接返回 + var module = modules[name] + var urlNoQuery = name && req.urlNoQuery + if (module && module.state >= 1) { + return name + } + module = modules[urlNoQuery] + if (module && module.state >= 3) { + innerRequire(module.deps || [], module.factory, urlNoQuery) + return urlNoQuery + } + if (name && !module) { + module = modules[urlNoQuery] = { + id: urlNoQuery, + state: 1 //send + } + var wrap = function (obj) { + resources[res] = obj + obj.load(name, req, function (a) { + if (arguments.length && a !== void 0) { + module.exports = a + } + module.state = 4 + checkDeps() + }) + } + + if (!resources[res]) { + innerRequire([res], wrap) + } else { + wrap(resources[res]) + } + } + return name ? urlNoQuery : res + "!" + } + + //核心API之一 require + var requireQueue = [] + var isUserFirstRequire = false + innerRequire = yua.require = function (array, factory, parentUrl, defineConfig) { + if (!isUserFirstRequire) { + requireQueue.push(yua.slice(arguments)) + if (arguments.length <= 2) { + isUserFirstRequire = true + var queue = requireQueue.splice(0, requireQueue.length), args + while (args = queue.shift()) { + innerRequire.apply(null, args) + } + } + return + } + + if (!Array.isArray(array)) { + yua.error("require方法的第一个参数应为数组 " + array) + } + var deps = [] // 放置所有依赖项的完整路径 + var uniq = createMap() + var id = parentUrl || "callback" + setTimeout("1")// jshint ignore:line + defineConfig = defineConfig || createMap() + defineConfig.baseUrl = kernel.baseUrl + var isBuilt = !!defineConfig.built + if (parentUrl) { + defineConfig.parentUrl = parentUrl.substr(0, parentUrl.lastIndexOf("/")) + defineConfig.mapUrl = parentUrl.replace(rjsext, "") + } + if (isBuilt) { + var req = makeRequest(defineConfig.defineName, defineConfig) + id = req.urlNoQuery + } else { + array.forEach(function (name) { + if(!name){ + return + } + var req = makeRequest(name, defineConfig) + var url = fireRequest(req) //加载资源,并返回该资源的完整地址 + if (url) { + if (!uniq[url]) { + deps.push(url) + uniq[url] = "yua" //去重 + } + } + }) + } + + var module = modules[id] + if (!module || module.state !== 4) { + modules[id] = { + id: id, + deps: isBuilt ? array.concat() : deps, + factory: factory || noop, + state: 3 + } + } + if (!module) { + //如果此模块是定义在另一个JS文件中, 那必须等该文件加载完毕, 才能放到检测列队中 + loadings.push(id) + } + checkDeps() + } + + //核心API之二 require + innerRequire.define = function (name, deps, factory) { //模块名,依赖列表,模块本身 + if (typeof name !== "string") { + factory = deps + deps = name + name = "anonymous" + } + if (!Array.isArray(deps)) { + factory = deps + deps = [] + } + var config = { + built: !isUserFirstRequire, //用r.js打包后,所有define会放到requirejs之前 + defineName: name + } + var args = [deps, factory, config] + factory.require = function (url) { + args.splice(2, 0, url) + if (modules[url]) { + modules[url].state = 3 //loaded + var isCycle = false + try { + isCycle = checkCycle(modules[url].deps, url) + } catch (e) { + } + if (isCycle) { + yua.error(url + "模块与之前的模块存在循环依赖,请不要直接用script标签引入" + url + "模块") + } + } + delete factory.require //释放内存 + innerRequire.apply(null, args) //0,1,2 --> 1,2,0 + } + //根据标准,所有遵循W3C标准的浏览器,script标签会按标签的出现顺序执行。 + //老的浏览器中,加载也是按顺序的:一个文件下载完成后,才开始下载下一个文件。 + //较新的浏览器中(IE8+ 、FireFox3.5+ 、Chrome4+ 、Safari4+),为了减小请求时间以优化体验, + //下载可以是并行的,但是执行顺序还是按照标签出现的顺序。 + //但如果script标签是动态插入的, 就未必按照先请求先执行的原则了,目测只有firefox遵守 + //唯一比较一致的是,IE10+及其他标准浏览器,一旦开始解析脚本, 就会一直堵在那里,直接脚本解析完毕 + //亦即,先进入loading阶段的script标签(模块)必然会先进入loaded阶段 + var url = config.built ? "unknown" : getCurrentScript() + if (url) { + var module = modules[url] + if (module) { + module.state = 2 + } + factory.require(url) + } else {//合并前后的safari,合并后的IE6-9走此分支 + factorys.push(factory) + } + } + //核心API之三 require.config(settings) + innerRequire.config = kernel + //核心API之四 define.amd 标识其符合AMD规范 + innerRequire.define.amd = modules + + //==========================对用户配置项进行再加工========================== + var allpaths = kernel["orig.paths"] = createMap() + var allmaps = kernel["orig.map"] = createMap() + var allpackages = kernel["packages"] = [] + var allargs = kernel["orig.args"] = createMap() + yua.mix(plugins, { + paths: function (hash) { + yua.mix(allpaths, hash) + kernel.paths = makeIndexArray(allpaths) + }, + map: function (hash) { + yua.mix(allmaps, hash) + var list = makeIndexArray(allmaps, 1, 1) + yua.each(list, function (_, item) { + item.val = makeIndexArray(item.val) + }) + kernel.map = list + }, + packages: function (array) { + array = array.concat(allpackages) + var uniq = createMap() + var ret = [] + for (var i = 0, pkg; pkg = array[i++]; ) { + pkg = typeof pkg === "string" ? {name: pkg} : pkg + var name = pkg.name + if (!uniq[name]) { + var url = joinPath(pkg.location || name, pkg.main || "main") + url = url.replace(rjsext, "") + ret.push(pkg) + uniq[name] = pkg.location = url + pkg.reg = makeMatcher(name) + } + } + kernel.packages = ret.sort() + }, + urlArgs: function (hash) { + if (typeof hash === "string") { + hash = {"*": hash} + } + yua.mix(allargs, hash) + kernel.urlArgs = makeIndexArray(allargs, 1) + }, + baseUrl: function (url) { + if (!isAbsUrl(url)) { + var baseElement = head.getElementsByTagName("base")[0] + if (baseElement) { + head.removeChild(baseElement) + } + var node = DOC.createElement("a") + node.href = url + url = node.href + if (baseElement) { + head.insertBefore(baseElement, head.firstChild) + } + } + if (url.length > 3) + kernel.baseUrl = url + }, + shim: function (obj) { + for (var i in obj) { + var value = obj[i] + if (Array.isArray(value)) { + value = obj[i] = { + deps: value + } + } + if (!value.exportsFn && (value.exports || value.init)) { + value.exportsFn = makeExports(value) + } + } + kernel.shim = obj + } + + }) + + + //==============================内部方法================================= + function checkCycle(deps, nick) { + //检测是否存在循环依赖 + for (var i = 0, id; id = deps[i++]; ) { + if (modules[id].state !== 4 && + (id === nick || checkCycle(modules[id].deps, nick))) { + return true + } + } + } + + function checkFail(node, onError) { + var id = trimQuery(node.src) //检测是否死链 + node.onload = node.onerror = null + if (onError) { + setTimeout(function () { + head.removeChild(node) + node = null // 处理旧式IE下的循环引用问题 + }) + log("debug: 加载 " + id + " 失败" + onError + " " + (!modules[id].state)) + } else { + return true + } + } + + function checkDeps() { + //检测此JS模块的依赖是否都已安装完毕,是则安装自身 + loop: for (var i = loadings.length, id; id = loadings[--i]; ) { + var obj = modules[id], + deps = obj.deps + if (!deps) + continue + for (var j = 0, key; key = deps[j]; j++) { + if (Object(modules[key]).state !== 4) { + continue loop + } + } + //如果deps是空对象或者其依赖的模块的状态都是4 + if (obj.state !== 4) { + loadings.splice(i, 1) //必须先移除再安装,防止在IE下DOM树建完后手动刷新页面,会多次执行它 + fireFactory(obj.id, obj.deps, obj.factory) + checkDeps() //如果成功,则再执行一次,以防有些模块就差本模块没有安装好 + } + } + } + + function loadJS(url, id, callback) { + //通过script节点加载目标模块 + var node = DOC.createElement("script") + node.className = subscribers //让getCurrentScript只处理类名为subscribers的script节点 + node.onload = function () { + var factory = factorys.pop() + factory && factory.require(id) + if (callback) { + callback() + } + id && loadings.push(id) + checkDeps() + } + node.onerror = function () { + checkFail(node, true) + } + + head.insertBefore(node, head.firstChild) //chrome下第二个参数不能为null + node.src = url //插入到head的第一个节点前,防止IE6下head标签没闭合前使用appendChild抛错,更重要的是IE6下可以收窄getCurrentScript的寻找范围 + } + + var resources = innerRequire.plugins = { + //三大常用资源插件 js!, css!, text!, domReady! + domReady: { + load: noop + }, + js: { + load: function (name, req, onLoad) { + var url = req.url + var id = req.urlNoQuery + var shim = kernel.shim[name.replace(rjsext, "")] + if (shim) { //shim机制 + innerRequire(shim.deps || [], function () { + var args = yua.slice(arguments) + loadJS(url, id, function () { + onLoad(shim.exportsFn ? shim.exportsFn.apply(0, args) : void 0) + }) + }) + } else { + loadJS(url, id) + } + } + }, + css: { + load: function (name, req, onLoad) { + var url = req.url + head.insertAdjacentHTML("beforeend", '') + onLoad() + } + }, + text: { + load: function (name, req, onLoad) { + var url = req.url + var xhr = getXHR() + xhr.onload = function () { + var status = xhr.status; + if (status > 399 && status < 600) { + yua.error(url + " 对应资源不存在或没有开启 CORS") + } else { + onLoad(xhr.responseText) + } + } + var time = "_=" + (new Date() - 0) + var _url = url.indexOf("?") === -1 ? url + "?" + time : url + "&" + time + xhr.open("GET", _url, true) + if ("withCredentials" in xhr) {//这是处理跨域 + xhr.withCredentials = true + } + xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest")//告诉后端这是AJAX请求 + xhr.send() + } + } + } + innerRequire.checkDeps = checkDeps + + var rquery = /(\?[^#]*)$/ + function trimQuery(url) { + return (url || "").replace(rquery, "") + } + + function isAbsUrl(path) { + //http://stackoverflow.com/questions/10687099/how-to-test-if-a-url-string-is-absolute-or-relative + return /^(?:[a-z\-]+:)?\/\//i.test(String(path)) + } + + + function getCurrentScript() { + // inspireb by https://github.com/samyk/jiagra/blob/master/jiagra.js + var stack + try { + a.b.c() //强制报错,以便捕获e.stack + } catch (e) { //safari5的sourceURL,firefox的fileName,它们的效果与e.stack不一样 + stack = e.stack + } + if (stack) { + /**e.stack最后一行在所有支持的浏览器大致如下: + *chrome23: + * at http://113.93.50.63/data.js:4:1 + *firefox17: + *@http://113.93.50.63/query.js:4 + *opera12:http://www.oldapps.com/opera.php?system=Windows_XP + *@http://113.93.50.63/data.js:4 + *IE10: + * at Global code (http://113.93.50.63/data.js:4:1) + * //firefox4+ 可以用document.currentScript + */ + stack = stack.split(/[@ ]/g).pop() //取得最后一行,最后一个空格或@之后的部分 + stack = stack[0] === "(" ? stack.slice(1, -1) : stack.replace(/\s/, "") //去掉换行符 + return trimQuery(stack.replace(/(:\d+)?:\d+$/i, "")) //去掉行号与或许存在的出错字符起始位置 + } + var nodes = head.getElementsByTagName("script") //只在head标签中寻找 + for (var i = nodes.length, node; node = nodes[--i]; ) { + if (node.className === subscribers && node.readyState === "interactive") { + var url = node.src + return node.className = trimQuery(url) + } + } + } + + var rcallback = /^callback\d+$/ + function fireFactory(id, deps, factory) { + var module = Object(modules[id]) + module.state = 4 + for (var i = 0, array = [], d; d = deps[i++]; ) { + if (d === "exports") { + var obj = module.exports || (module.exports = createMap()) + array.push(obj) + } else { + array.push(modules[d].exports) + } + } + try { + var ret = factory.apply(window, array) + } catch (e) { + log("执行[" + id + "]模块的factory抛错: ", e) + } + if (ret !== void 0) { + module.exports = ret + } + if (rcallback.test(id)) { + delete modules[id] + } + delete module.factory + return ret + } + function toUrl(id) { + if (id.indexOf(this.res + "!") === 0) { + id = id.slice(this.res.length + 1) //处理define("css!style",[], function(){})的情况 + } + var url = id + //1. 是否命中paths配置项 + var usePath = 0 + var baseUrl = this.baseUrl + var rootUrl = this.parentUrl || baseUrl + eachIndexArray(id, kernel.paths, function (value, key) { + url = url.replace(key, value) + usePath = 1 + }) + //2. 是否命中packages配置项 + if (!usePath) { + eachIndexArray(id, kernel.packages, function (value, key, item) { + url = url.replace(item.name, item.location) + }) + } + //3. 是否命中map配置项 + if (this.mapUrl) { + eachIndexArray(this.mapUrl, kernel.map, function (array) { + eachIndexArray(url, array, function (mdValue, mdKey) { + url = url.replace(mdKey, mdValue) + rootUrl = baseUrl + }) + }) + } + var ext = this.ext + if (ext && usePath && url.slice(-ext.length) === ext) { + url = url.slice(0, -ext.length) + } + //4. 转换为绝对路径 + if (!isAbsUrl(url)) { + rootUrl = this.built || /^\w/.test(url) ? baseUrl : rootUrl + url = joinPath(rootUrl, url) + } + //5. 还原扩展名,query + var urlNoQuery = url + ext + url = urlNoQuery + this.query + urlNoQuery = url.replace(rquery, function (a) { + this.query = a + return "" + }) + //6. 处理urlArgs + eachIndexArray(id, kernel.urlArgs, function (value) { + url += (url.indexOf("?") === -1 ? "?" : "&") + value; + }) + this.url = url + return this.urlNoQuery = urlNoQuery + } + + function makeIndexArray(hash, useStar, part) { + //创建一个经过特殊算法排好序的数组 + var index = hash2array(hash, useStar, part) + index.sort(descSorterByName) + return index + } + + function makeMatcher(prefix) { + return new RegExp('^' + prefix + '(/|$)') + } + + function makeExports(value) { + return function () { + var ret + if (value.init) { + ret = value.init.apply(window, arguments) + } + return ret || (value.exports && getGlobal(value.exports)) + } + } + + + function hash2array(hash, useStar, part) { + var array = []; + for (var key in hash) { + // if (hash.hasOwnProperty(key)) {//hash是由createMap创建没有hasOwnProperty + var item = { + name: key, + val: hash[key] + } + array.push(item) + item.reg = key === "*" && useStar ? /^/ : makeMatcher(key) + if (part && key !== "*") { + item.reg = new RegExp('\/' + key.replace(/^\//, "") + '(/|$)') + } + // } + } + return array + } + + function eachIndexArray(moduleID, array, matcher) { + array = array || [] + for (var i = 0, el; el = array[i++]; ) { + if (el.reg.test(moduleID)) { + matcher(el.val, el.name, el) + return false + } + } + } + // 根据元素的name项进行数组字符数逆序的排序函数 + function descSorterByName(a, b) { + var aaa = a.name + var bbb = b.name + if (bbb === "*") { + return -1 + } + if (aaa === "*") { + return 1 + } + return bbb.length - aaa.length + } + + var rdeuce = /\/\w+\/\.\./ + function joinPath(a, b) { + if (a.charAt(a.length - 1) !== "/") { + a += "/" + } + if (b.slice(0, 2) === "./") { //相对于兄弟路径 + return a + b.slice(2) + } + if (b.slice(0, 2) === "..") { //相对于父路径 + a += b + while (rdeuce.test(a)) { + a = a.replace(rdeuce, "") + } + return a + } + if (b.slice(0, 1) === "/") { + return a + b.slice(1) + } + return a + b + } + + function getGlobal(value) { + if (!value) { + return value + } + var g = window + value.split(".").forEach(function (part) { + g = g[part] + }) + return g + } + + var mainNode = DOC.scripts[DOC.scripts.length - 1] + var dataMain = mainNode.getAttribute("data-main") + if (dataMain) { + plugins.baseUrl(dataMain) + var href = kernel.baseUrl + kernel.baseUrl = href.slice(0, href.lastIndexOf("/") + 1) + loadJS(href.replace(rjsext, "") + ".js") + } else { + var loaderUrl = trimQuery(mainNode.src) + kernel.baseUrl = loaderUrl.slice(0, loaderUrl.lastIndexOf("/") + 1) + } +}// jshint ignore:line + + + + + + + + + + + + + + + + + + + + +/********************************************************************* + * DOMReady * + **********************************************************************/ + +var readyList = [], + isReady +var fireReady = function (fn) { + isReady = true + var require = yua.require + if (require && require.checkDeps) { + modules["domReady!"].state = 4 + require.checkDeps() + } + while (fn = readyList.shift()) { + fn(yua) + } +} + +if (DOC.readyState === "complete") { + setTimeout(fireReady) //如果在domReady之外加载 +} else { + DOC.addEventListener("DOMContentLoaded", fireReady) +} +window.addEventListener("load", fireReady) +yua.ready = function (fn) { + if (!isReady) { + readyList.push(fn) + } else { + fn(yua) + } +} + +yua.config({ + loader: true +}) +yua.ready(function () { + yua.scan(DOC.body) +}) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + if (typeof define === "function" && define.amd) { + define("yua", [], function() { + return yua + }) + } +// Map over yua in case of overwrite + var _yua = window.yua + yua.noConflict = function(deep) { + if (deep && window.yua === yua) { + window.yua = _yua + } + return yua + } +// Expose yua identifiers, even in AMD +// and CommonJS for browser emulators + if (noGlobal === void 0) { + window.yua = yua + } + return yua + +})); \ No newline at end of file