342 lines
11 KiB
JavaScript
342 lines
11 KiB
JavaScript
/*!
|
|
* =====================================================
|
|
* SUI Mobile - http://m.sui.taobao.org/
|
|
*
|
|
* =====================================================
|
|
*/
|
|
;$.smVersion = "0.6.2";+function ($) {
|
|
"use strict";
|
|
|
|
//全局配置
|
|
var defaults = {
|
|
autoInit: false, //自动初始化页面
|
|
showPageLoadingIndicator: true, //push.js加载页面的时候显示一个加载提示
|
|
router: true, //默认使用router
|
|
swipePanel: "left", //滑动打开侧栏
|
|
swipePanelOnlyClose: true //只允许滑动关闭,不允许滑动打开侧栏
|
|
};
|
|
|
|
$.smConfig = $.extend(defaults, $.config);
|
|
|
|
}(Zepto);
|
|
|
|
|
|
|
|
/*======================================================
|
|
************ Modals ************
|
|
======================================================*/
|
|
/*jshint unused: false*/
|
|
+function ($) {
|
|
"use strict";
|
|
var _modalTemplateTempDiv = document.createElement('div');
|
|
|
|
$.modalStack = [];
|
|
|
|
$.modalStackClearQueue = function () {
|
|
if ($.modalStack.length) {
|
|
($.modalStack.shift())();
|
|
}
|
|
};
|
|
$.modal = function (params) {
|
|
params = params || {};
|
|
var modalHTML = '';
|
|
var buttonsHTML = '';
|
|
if (params.buttons && params.buttons.length > 0) {
|
|
for (var i = 0; i < params.buttons.length; i++) {
|
|
buttonsHTML += '<span class="modal-button' + (params.buttons[i].bold ? ' modal-button-bold' : '') + '">' + params.buttons[i].text + '</span>';
|
|
}
|
|
}
|
|
var extraClass = params.extraClass || '';
|
|
var titleHTML = params.title ? '<div class="modal-title">' + params.title + '</div>' : '';
|
|
var textHTML = params.text ? '<div class="modal-text">' + params.text + '</div>' : '';
|
|
var afterTextHTML = params.afterText ? params.afterText : '';
|
|
var noButtons = !params.buttons || params.buttons.length === 0 ? 'modal-no-buttons' : '';
|
|
var verticalButtons = params.verticalButtons ? 'modal-buttons-vertical' : '';
|
|
modalHTML = '<div class="modal ' + extraClass + ' ' + noButtons + '"><div class="modal-inner">' + (titleHTML + textHTML + afterTextHTML) + '</div><div class="modal-buttons ' + verticalButtons + '">' + buttonsHTML + '</div></div>';
|
|
|
|
_modalTemplateTempDiv.innerHTML = modalHTML;
|
|
|
|
var modal = $(_modalTemplateTempDiv).children();
|
|
|
|
$(defaults.modalContainer).append(modal[0]);
|
|
|
|
// Add events on buttons
|
|
modal.find('.modal-button').each(function (index, el) {
|
|
$(el).on('click', function (e) {
|
|
if (params.buttons[index].close !== false) $.closeModal(modal);
|
|
if (params.buttons[index].onClick) params.buttons[index].onClick(modal, e);
|
|
if (params.onClick) params.onClick(modal, index);
|
|
});
|
|
});
|
|
$.openModal(modal);
|
|
return modal[0];
|
|
};
|
|
$.openModal = function (modal, cb) {
|
|
modal = $(modal);
|
|
var isModal = modal.hasClass('modal'),
|
|
isNotToast = !modal.hasClass('toast');
|
|
if ($('.modal.modal-in:not(.modal-out)').length && defaults.modalStack && isModal && isNotToast) {
|
|
$.modalStack.push(function () {
|
|
$.openModal(modal, cb);
|
|
});
|
|
return;
|
|
}
|
|
var isPopup = modal.hasClass('popup');
|
|
var isLoginScreen = modal.hasClass('login-screen');
|
|
var isPickerModal = modal.hasClass('picker-modal');
|
|
var isToast = modal.hasClass('toast');
|
|
if (isModal) {
|
|
modal.show();
|
|
modal.css({
|
|
marginTop: - Math.round(modal.outerHeight() / 2) + 'px'
|
|
});
|
|
}
|
|
if (isToast) {
|
|
modal.css({
|
|
marginLeft: - Math.round(modal.outerWidth() / 2 / 1.185) + 'px' //1.185 是初始化时候的放大效果
|
|
});
|
|
}
|
|
|
|
var overlay;
|
|
if (!isLoginScreen && !isPickerModal && !isToast) {
|
|
if ($('.modal-overlay').length === 0 && !isPopup) {
|
|
$(defaults.modalContainer).append('<div class="modal-overlay"></div>');
|
|
}
|
|
if ($('.popup-overlay').length === 0 && isPopup) {
|
|
$(defaults.modalContainer).append('<div class="popup-overlay"></div>');
|
|
}
|
|
overlay = isPopup ? $('.popup-overlay') : $('.modal-overlay');
|
|
}
|
|
|
|
//Make sure that styles are applied, trigger relayout;
|
|
var clientLeft = modal[0].clientLeft;
|
|
|
|
// Trugger open event
|
|
modal.trigger('open');
|
|
|
|
// Picker modal body class
|
|
if (isPickerModal) {
|
|
$(defaults.modalContainer).addClass('with-picker-modal');
|
|
}
|
|
|
|
// Classes for transition in
|
|
if (!isLoginScreen && !isPickerModal && !isToast) overlay.addClass('modal-overlay-visible');
|
|
modal.removeClass('modal-out').addClass('modal-in').transitionEnd(function (e) {
|
|
if (modal.hasClass('modal-out')) modal.trigger('closed');
|
|
else modal.trigger('opened');
|
|
});
|
|
// excute callback
|
|
if (typeof cb === 'function') {
|
|
cb.call(this);
|
|
}
|
|
return true;
|
|
};
|
|
$.closeModal = function (modal) {
|
|
modal = $(modal || '.modal-in');
|
|
if (typeof modal !== 'undefined' && modal.length === 0) {
|
|
return;
|
|
}
|
|
var isModal = modal.hasClass('modal'),
|
|
isPopover = modal.hasClass('popover'),
|
|
isPopup = modal.hasClass('popup'),
|
|
isToast = modal.hasClass('toast'),
|
|
isLoginScreen = modal.hasClass('login-screen'),
|
|
isPickerModal = modal.hasClass('picker-modal'),
|
|
removeOnClose = modal.hasClass('remove-on-close'),
|
|
overlay = isPopup ? $('.popup-overlay') : $('.modal-overlay');
|
|
if (isPopup){
|
|
if (modal.length === $('.popup.modal-in').length) {
|
|
overlay.removeClass('modal-overlay-visible');
|
|
}
|
|
}
|
|
else if (!(isPickerModal || isToast)) {
|
|
overlay.removeClass('modal-overlay-visible');
|
|
}
|
|
|
|
modal.trigger('close');
|
|
|
|
// Picker modal body class
|
|
if (isPickerModal) {
|
|
$(defaults.modalContainer).removeClass('with-picker-modal');
|
|
$(defaults.modalContainer).addClass('picker-modal-closing');
|
|
}
|
|
if (!isPopover) {
|
|
modal.removeClass('modal-in').addClass('modal-out').transitionEnd(function (e) {
|
|
if (modal.hasClass('modal-out')) modal.trigger('closed');
|
|
else modal.trigger('opened');
|
|
|
|
if (isPickerModal) {
|
|
$(defaults.modalContainer).removeClass('picker-modal-closing');
|
|
}
|
|
if (isPopup || isLoginScreen || isPickerModal) {
|
|
modal.removeClass('modal-out').hide();
|
|
if (removeOnClose && modal.length > 0) {
|
|
modal.remove();
|
|
}
|
|
}
|
|
else {
|
|
modal.remove();
|
|
}
|
|
});
|
|
if (isModal && defaults.modalStack ) {
|
|
$.modalStackClearQueue();
|
|
}
|
|
}
|
|
else {
|
|
modal.removeClass('modal-in modal-out').trigger('closed').hide();
|
|
if (removeOnClose) {
|
|
modal.remove();
|
|
}
|
|
}
|
|
return true;
|
|
|
|
};
|
|
function handleClicks(e) {
|
|
/*jshint validthis:true */
|
|
var clicked = $(this);
|
|
var url = clicked.attr('href');
|
|
|
|
|
|
//Collect Clicked data- attributes
|
|
var clickedData = clicked.dataset();
|
|
|
|
// Popover
|
|
if (clicked.hasClass('open-popover')) {
|
|
var popover;
|
|
if (clickedData.popover) {
|
|
popover = clickedData.popover;
|
|
}
|
|
else popover = '.popover';
|
|
$.popover(popover, clicked);
|
|
}
|
|
if (clicked.hasClass('close-popover')) {
|
|
$.closeModal('.popover.modal-in');
|
|
}
|
|
|
|
// Popup
|
|
var popup;
|
|
if (clicked.hasClass('open-popup')) {
|
|
if (clickedData.popup) {
|
|
popup = clickedData.popup;
|
|
}
|
|
else popup = '.popup';
|
|
$.popup(popup);
|
|
}
|
|
if (clicked.hasClass('close-popup')) {
|
|
if (clickedData.popup) {
|
|
popup = clickedData.popup;
|
|
}
|
|
else popup = '.popup.modal-in';
|
|
$.closeModal(popup);
|
|
}
|
|
|
|
// Close Modal
|
|
if (clicked.hasClass('modal-overlay')) {
|
|
if ($('.modal.modal-in').length > 0 && defaults.modalCloseByOutside)
|
|
$.closeModal('.modal.modal-in');
|
|
if ($('.actions-modal.modal-in').length > 0 && defaults.actionsCloseByOutside)
|
|
$.closeModal('.actions-modal.modal-in');
|
|
|
|
if ($('.popover.modal-in').length > 0) $.closeModal('.popover.modal-in');
|
|
}
|
|
if (clicked.hasClass('popup-overlay')) {
|
|
if ($('.popup.modal-in').length > 0 && defaults.popupCloseByOutside)
|
|
$.closeModal('.popup.modal-in');
|
|
}
|
|
|
|
}
|
|
$(document).on('click', ' .modal-overlay, .popup-overlay, .close-popup, .open-popup, .close-picker', handleClicks);
|
|
var defaults = $.modal.prototype.defaults = {
|
|
modalStack: true,
|
|
modalButtonOk: '确定',
|
|
modalButtonCancel: '取消',
|
|
modalPreloaderTitle: '加载中',
|
|
modalContainer : document.body
|
|
};
|
|
}(Zepto);
|
|
|
|
|
|
|
|
+ function($) {
|
|
"use strict";
|
|
|
|
var today = new Date();
|
|
var getDays = function(max) {
|
|
var days = [];
|
|
for(var i=1; i<= (max||31);i++) {
|
|
days.push(i < 10 ? "0"+i : i);
|
|
}
|
|
return days;
|
|
};
|
|
|
|
var getDaysByMonthAndYear = function(month, year) {
|
|
var int_d = new Date(year, parseInt(month)+1-1, 1);
|
|
var d = new Date(int_d - 1);
|
|
return getDays(d.getDate());
|
|
};
|
|
|
|
var formatNumber = function (n) {
|
|
return n < 10 ? "0" + n : n;
|
|
};
|
|
|
|
var initMonthes = ('01 02 03 04 05 06 07 08 09 10 11 12').split(' ');
|
|
|
|
var initYears = (function () {
|
|
var arr = [];
|
|
for (var i = 1950; i <= 2050; i++) { arr.push(i); }
|
|
return arr;
|
|
})();
|
|
|
|
|
|
var defaults = {
|
|
|
|
rotateEffect: false, //为了性能
|
|
|
|
value: [today.getFullYear(), formatNumber(today.getMonth()+1), formatNumber(today.getDate())],
|
|
|
|
onChange: function (picker, values, displayValues) {
|
|
var inputId = picker.params.input.id;
|
|
var deftime = $("#"+inputId).val();
|
|
if(deftime == ""){
|
|
picker.cols[0].setValue(today.getFullYear())
|
|
}
|
|
var days = getDaysByMonthAndYear(picker.cols[1].value, picker.cols[0].value);
|
|
var currentValue = picker.cols[2].value;
|
|
if(currentValue > days.length) currentValue = days.length;
|
|
picker.cols[2].setValue(currentValue);
|
|
},
|
|
|
|
formatValue: function (p, values, displayValues) {
|
|
return displayValues[0] + '-' + values[1] + '-' + values[2];
|
|
},
|
|
|
|
cols: [
|
|
// Years
|
|
{
|
|
values: initYears
|
|
},
|
|
// Months
|
|
{
|
|
values: initMonthes
|
|
},
|
|
// Days
|
|
{
|
|
values: getDays()
|
|
}
|
|
]
|
|
};
|
|
|
|
$.fn.datetimePicker = function(params) {
|
|
return this.each(function() {
|
|
if(!this) return;
|
|
var p = $.extend(defaults, params);
|
|
$(this).picker(p);
|
|
if (params.value) $(this).val(p.formatValue(p, p.value, p.value));
|
|
});
|
|
};
|
|
|
|
}(Zepto);
|
|
|
|
|
|
|