snappymail/dev/External/ko.js

1009 lines
22 KiB
JavaScript
Raw Normal View History

2014-08-20 23:03:12 +08:00
2014-10-29 06:05:50 +08:00
(function (ko) {
2014-08-20 23:03:12 +08:00
2014-08-25 23:49:01 +08:00
'use strict';
2014-08-20 23:03:12 +08:00
var
2014-08-25 23:49:01 +08:00
window = require('window'),
_ = require('_'),
2014-10-04 19:58:01 +08:00
$ = require('$'),
fDisposalTooltipHelper = function (oElement, $oEl, oSubscription) {
ko.utils.domNodeDisposal.addDisposeCallback(oElement, function () {
if (oSubscription && oSubscription.dispose)
{
oSubscription.dispose();
}
if ($oEl)
{
$oEl.off('click.koTooltip');
if ($oEl.tooltip)
{
$oEl.tooltip('destroy');
}
}
$oEl = null;
});
}
2014-08-20 23:03:12 +08:00
;
ko.bindingHandlers.tooltip = {
'init': function (oElement, fValueAccessor) {
2014-08-22 23:08:56 +08:00
var
2014-10-04 19:58:01 +08:00
$oEl = null,
bi18n = true,
2014-10-04 19:58:01 +08:00
sClass = '',
sPlacement = '',
oSubscription = null,
2014-09-05 06:49:03 +08:00
Globals = require('Common/Globals'),
Translator = require('Common/Translator')
2014-08-22 23:08:56 +08:00
;
2014-08-20 23:03:12 +08:00
if (!Globals.bMobileDevice)
{
2014-10-04 19:58:01 +08:00
$oEl = $(oElement);
sClass = $oEl.data('tooltip-class') || '';
bi18n = 'on' === ($oEl.data('tooltip-i18n') || 'on');
2014-10-04 19:58:01 +08:00
sPlacement = $oEl.data('tooltip-placement') || 'top';
2014-08-20 23:03:12 +08:00
$oEl.tooltip({
'delay': {
'show': 500,
'hide': 100
},
'html': true,
'container': 'body',
'placement': sPlacement,
'trigger': 'hover',
'title': function () {
var sValue = bi18n ? ko.unwrap(fValueAccessor()) : fValueAccessor()();
return '' === sValue || $oEl.is('.disabled') || Globals.dropdownVisibility() ? '' :
'<span class="tooltip-class ' + sClass + '">' + (bi18n ? Translator.i18n(sValue) : sValue) + '</span>';
2014-08-20 23:03:12 +08:00
}
2014-10-04 19:58:01 +08:00
}).on('click.koTooltip', function () {
2014-08-20 23:03:12 +08:00
$oEl.tooltip('hide');
});
2014-10-04 19:58:01 +08:00
oSubscription = Globals.tooltipTrigger.subscribe(function () {
2014-08-20 23:03:12 +08:00
$oEl.tooltip('hide');
});
2014-10-04 19:58:01 +08:00
fDisposalTooltipHelper(oElement, $oEl, oSubscription);
2014-08-20 23:03:12 +08:00
}
}
};
ko.bindingHandlers.tooltipForTest = {
2014-08-20 23:03:12 +08:00
'init': function (oElement) {
2014-08-22 23:08:56 +08:00
var
$oEl = $(oElement),
2014-10-04 19:58:01 +08:00
oSubscription = null,
2014-09-05 06:49:03 +08:00
Globals = require('Common/Globals')
2014-08-22 23:08:56 +08:00
;
2014-08-20 23:03:12 +08:00
$oEl.tooltip({
'container': 'body',
'trigger': 'hover manual',
'title': function () {
return $oEl.data('tooltip3-data') || '';
}
});
2014-10-04 19:58:01 +08:00
$(window.document).on('click', function () {
2014-08-20 23:03:12 +08:00
$oEl.tooltip('hide');
});
2014-10-04 19:58:01 +08:00
oSubscription = Globals.tooltipTrigger.subscribe(function () {
2014-08-20 23:03:12 +08:00
$oEl.tooltip('hide');
});
2014-10-04 19:58:01 +08:00
fDisposalTooltipHelper(oElement, $oEl, oSubscription);
2014-08-20 23:03:12 +08:00
},
'update': function (oElement, fValueAccessor) {
2014-10-04 19:58:01 +08:00
var sValue = ko.unwrap(fValueAccessor());
2014-08-20 23:03:12 +08:00
if ('' === sValue)
{
$(oElement).data('tooltip3-data', '').tooltip('hide');
}
else
{
2015-01-29 05:24:58 +08:00
$(oElement).data('tooltip3-data', sValue);
2015-01-29 00:27:23 +08:00
_.delay(function () {
if ($(oElement).is(':visible'))
{
$(oElement).tooltip('show');
}
}, 100);
2014-08-20 23:03:12 +08:00
}
}
};
ko.bindingHandlers.registrateBootstrapDropdown = {
'init': function (oElement) {
2014-09-05 06:49:03 +08:00
var Globals = require('Common/Globals');
2014-10-04 19:58:01 +08:00
if (Globals && Globals.aBootstrapDropdowns)
{
Globals.aBootstrapDropdowns.push($(oElement));
// ko.utils.domNodeDisposal.addDisposeCallback(oElement, function () {
// });
}
2014-08-20 23:03:12 +08:00
}
};
ko.bindingHandlers.openDropdownTrigger = {
'update': function (oElement, fValueAccessor) {
2014-10-04 19:58:01 +08:00
if (ko.unwrap(fValueAccessor()))
2014-08-20 23:03:12 +08:00
{
2014-08-22 23:08:56 +08:00
var
2014-10-04 19:58:01 +08:00
$oEl = $(oElement),
2014-09-05 06:49:03 +08:00
Utils = require('Common/Utils')
2014-08-22 23:08:56 +08:00
;
2014-08-25 15:10:51 +08:00
2014-10-04 19:58:01 +08:00
if (!$oEl.hasClass('open'))
2014-08-20 23:03:12 +08:00
{
2014-10-04 19:58:01 +08:00
$oEl.find('.dropdown-toggle').dropdown('toggle');
2014-08-20 23:03:12 +08:00
Utils.detectDropdownVisibility();
}
fValueAccessor()(false);
}
}
};
ko.bindingHandlers.dropdownCloser = {
'init': function (oElement) {
$(oElement).closest('.dropdown').on('click', '.e-item', function () {
$(oElement).dropdown('toggle');
});
}
};
ko.bindingHandlers.popover = {
'init': function (oElement, fValueAccessor) {
2014-10-04 19:58:01 +08:00
$(oElement).popover(ko.unwrap(fValueAccessor()));
ko.utils.domNodeDisposal.addDisposeCallback(oElement, function () {
$(oElement).popover('destroy');
});
2014-08-20 23:03:12 +08:00
}
};
ko.bindingHandlers.csstext = {
'init': function (oElement, fValueAccessor) {
2014-09-05 06:49:03 +08:00
var Utils = require('Common/Utils');
2014-08-20 23:03:12 +08:00
if (oElement && oElement.styleSheet && !Utils.isUnd(oElement.styleSheet.cssText))
{
2014-10-04 19:58:01 +08:00
oElement.styleSheet.cssText = ko.unwrap(fValueAccessor());
2014-08-20 23:03:12 +08:00
}
else
{
2014-10-04 19:58:01 +08:00
$(oElement).text(ko.unwrap(fValueAccessor()));
2014-08-20 23:03:12 +08:00
}
},
'update': function (oElement, fValueAccessor) {
2014-09-05 06:49:03 +08:00
var Utils = require('Common/Utils');
2014-08-20 23:03:12 +08:00
if (oElement && oElement.styleSheet && !Utils.isUnd(oElement.styleSheet.cssText))
{
2014-10-04 19:58:01 +08:00
oElement.styleSheet.cssText = ko.unwrap(fValueAccessor());
2014-08-20 23:03:12 +08:00
}
else
{
2014-10-04 19:58:01 +08:00
$(oElement).text(ko.unwrap(fValueAccessor()));
2014-08-20 23:03:12 +08:00
}
}
};
ko.bindingHandlers.resizecrop = {
'init': function (oElement) {
$(oElement).addClass('resizecrop').resizecrop({
'width': '100',
'height': '100',
'wrapperCSS': {
'border-radius': '10px'
}
});
},
'update': function (oElement, fValueAccessor) {
fValueAccessor()();
$(oElement).resizecrop({
'width': '100',
'height': '100'
});
}
};
ko.bindingHandlers.onEnter = {
'init': function (oElement, fValueAccessor, fAllBindingsAccessor, oViewModel) {
2014-10-04 19:58:01 +08:00
$(oElement).on('keypress.koOnEnter', function (oEvent) {
2014-08-20 23:03:12 +08:00
if (oEvent && 13 === window.parseInt(oEvent.keyCode, 10))
{
$(oElement).trigger('change');
fValueAccessor().call(oViewModel);
}
});
2014-10-04 19:58:01 +08:00
ko.utils.domNodeDisposal.addDisposeCallback(oElement, function () {
$(oElement).off('keypress.koOnEnter');
});
2014-08-20 23:03:12 +08:00
}
};
ko.bindingHandlers.onEsc = {
'init': function (oElement, fValueAccessor, fAllBindingsAccessor, oViewModel) {
2014-10-04 19:58:01 +08:00
$(oElement).on('keypress.koOnEsc', function (oEvent) {
2014-08-20 23:03:12 +08:00
if (oEvent && 27 === window.parseInt(oEvent.keyCode, 10))
{
$(oElement).trigger('change');
fValueAccessor().call(oViewModel);
}
});
2014-10-04 19:58:01 +08:00
ko.utils.domNodeDisposal.addDisposeCallback(oElement, function () {
$(oElement).off('keypress.koOnEsc');
});
2014-08-20 23:03:12 +08:00
}
};
ko.bindingHandlers.clickOnTrue = {
'update': function (oElement, fValueAccessor) {
2014-10-04 19:58:01 +08:00
if (ko.unwrap(fValueAccessor()))
2014-08-20 23:03:12 +08:00
{
$(oElement).click();
}
}
};
ko.bindingHandlers.modal = {
'init': function (oElement, fValueAccessor) {
2014-08-22 23:08:56 +08:00
var
2014-09-05 06:49:03 +08:00
Globals = require('Common/Globals'),
Utils = require('Common/Utils')
2014-08-22 23:08:56 +08:00
;
2014-08-20 23:03:12 +08:00
$(oElement).toggleClass('fade', !Globals.bMobileDevice).modal({
'keyboard': false,
2014-10-04 19:58:01 +08:00
'show': ko.unwrap(fValueAccessor())
2014-08-20 23:03:12 +08:00
})
.on('shown.koModal', Utils.windowResizeCallback)
2014-10-04 19:58:01 +08:00
.find('.close').on('click.koModal', function () {
2014-08-20 23:03:12 +08:00
fValueAccessor()(false);
});
2014-08-25 15:10:51 +08:00
2014-10-04 19:58:01 +08:00
ko.utils.domNodeDisposal.addDisposeCallback(oElement, function () {
$(oElement)
.off('shown.koModal')
.find('.close')
.off('click.koModal')
;
});
2014-08-20 23:03:12 +08:00
},
'update': function (oElement, fValueAccessor) {
2015-02-15 08:30:21 +08:00
var Globals = require('Common/Globals');
$(oElement).modal(!!ko.unwrap(fValueAccessor()) ? 'show' : 'hide');
if (Globals.$html.hasClass('rl-anim'))
{
Globals.$html.addClass('rl-modal-animation');
_.delay(function () {
Globals.$html.removeClass('rl-modal-animation');
}, 400);
}
2014-08-20 23:03:12 +08:00
}
};
ko.bindingHandlers.i18nInit = {
'init': function (oElement) {
require('Common/Translator').i18nToNode(oElement);
2014-08-20 23:03:12 +08:00
}
};
ko.bindingHandlers.i18nUpdate = {
'update': function (oElement, fValueAccessor) {
2014-10-04 19:58:01 +08:00
ko.unwrap(fValueAccessor());
require('Common/Translator').i18nToNode(oElement);
2014-08-20 23:03:12 +08:00
}
};
ko.bindingHandlers.link = {
'update': function (oElement, fValueAccessor) {
2014-10-04 19:58:01 +08:00
$(oElement).attr('href', ko.unwrap(fValueAccessor()));
2014-08-20 23:03:12 +08:00
}
};
ko.bindingHandlers.title = {
'update': function (oElement, fValueAccessor) {
2014-10-04 19:58:01 +08:00
$(oElement).attr('title', ko.unwrap(fValueAccessor()));
2014-08-20 23:03:12 +08:00
}
};
ko.bindingHandlers.textF = {
'init': function (oElement, fValueAccessor) {
2014-10-04 19:58:01 +08:00
$(oElement).text(ko.unwrap(fValueAccessor()));
2014-08-20 23:03:12 +08:00
}
};
ko.bindingHandlers.initDom = {
'init': function (oElement, fValueAccessor) {
fValueAccessor()(oElement);
}
};
2015-01-09 07:31:31 +08:00
ko.bindingHandlers.initFixedTrigger = {
'init': function (oElement, fValueAccessor) {
var
aValues = ko.unwrap(fValueAccessor()),
$oContainer = null,
$oElement = $(oElement),
oOffset = null,
iTop = aValues[1] || 0
;
$oContainer = $(aValues[0] || null);
$oContainer = $oContainer[0] ? $oContainer : null;
if ($oContainer)
{
$(window).resize(function () {
oOffset = $oContainer.offset();
if (oOffset && oOffset.top)
{
$oElement.css('top', oOffset.top + iTop);
}
});
}
}
};
2014-08-20 23:03:12 +08:00
ko.bindingHandlers.initResizeTrigger = {
'init': function (oElement, fValueAccessor) {
2014-10-04 19:58:01 +08:00
var aValues = ko.unwrap(fValueAccessor());
2014-08-20 23:03:12 +08:00
$(oElement).css({
'height': aValues[1],
'min-height': aValues[1]
});
},
'update': function (oElement, fValueAccessor) {
2014-08-25 15:10:51 +08:00
2014-08-20 23:03:12 +08:00
var
2014-09-05 06:49:03 +08:00
Utils = require('Common/Utils'),
Globals = require('Common/Globals'),
2014-10-04 19:58:01 +08:00
aValues = ko.unwrap(fValueAccessor()),
2014-08-20 23:03:12 +08:00
iValue = Utils.pInt(aValues[1]),
iSize = 0,
iOffset = $(oElement).offset().top
;
if (0 < iOffset)
{
iOffset += Utils.pInt(aValues[2]);
2014-09-02 00:05:32 +08:00
iSize = Globals.$win.height() - iOffset;
2014-08-20 23:03:12 +08:00
if (iValue < iSize)
{
iValue = iSize;
}
$(oElement).css({
'height': iValue,
'min-height': iValue
});
}
}
};
ko.bindingHandlers.appendDom = {
'update': function (oElement, fValueAccessor) {
2014-10-04 19:58:01 +08:00
$(oElement).hide().empty().append(ko.unwrap(fValueAccessor())).show();
2014-08-20 23:03:12 +08:00
}
};
ko.bindingHandlers.draggable = {
'init': function (oElement, fValueAccessor, fAllBindingsAccessor) {
2014-10-04 19:58:01 +08:00
2014-08-22 23:08:56 +08:00
var
2014-09-05 06:49:03 +08:00
Globals = require('Common/Globals'),
Utils = require('Common/Utils')
2014-08-22 23:08:56 +08:00
;
2014-10-04 19:58:01 +08:00
2014-08-20 23:03:12 +08:00
if (!Globals.bMobileDevice)
{
var
iTriggerZone = 100,
iScrollSpeed = 3,
fAllValueFunc = fAllBindingsAccessor(),
sDroppableSelector = fAllValueFunc && fAllValueFunc['droppableSelector'] ? fAllValueFunc['droppableSelector'] : '',
oConf = {
'distance': 20,
'handle': '.dragHandle',
'cursorAt': {'top': 22, 'left': 3},
'refreshPositions': true,
'scroll': true
}
;
if (sDroppableSelector)
{
oConf['drag'] = function (oEvent) {
$(sDroppableSelector).each(function () {
var
moveUp = null,
moveDown = null,
$this = $(this),
oOffset = $this.offset(),
bottomPos = oOffset.top + $this.height()
;
window.clearInterval($this.data('timerScroll'));
$this.data('timerScroll', false);
if (oEvent.pageX >= oOffset.left && oEvent.pageX <= oOffset.left + $this.width())
{
if (oEvent.pageY >= bottomPos - iTriggerZone && oEvent.pageY <= bottomPos)
{
moveUp = function() {
$this.scrollTop($this.scrollTop() + iScrollSpeed);
Utils.windowResize();
};
$this.data('timerScroll', window.setInterval(moveUp, 10));
moveUp();
}
if (oEvent.pageY >= oOffset.top && oEvent.pageY <= oOffset.top + iTriggerZone)
{
moveDown = function() {
$this.scrollTop($this.scrollTop() - iScrollSpeed);
Utils.windowResize();
};
$this.data('timerScroll', window.setInterval(moveDown, 10));
moveDown();
}
}
});
};
oConf['stop'] = function() {
$(sDroppableSelector).each(function () {
window.clearInterval($(this).data('timerScroll'));
$(this).data('timerScroll', false);
});
};
}
oConf['helper'] = function (oEvent) {
return fValueAccessor()(oEvent && oEvent.target ? ko.dataFor(oEvent.target) : null);
};
2014-10-04 19:58:01 +08:00
$(oElement).draggable(oConf).on('mousedown.koDraggable', function () {
2014-08-20 23:03:12 +08:00
Utils.removeInFocus();
});
2014-10-04 19:58:01 +08:00
ko.utils.domNodeDisposal.addDisposeCallback(oElement, function () {
$(oElement)
.off('mousedown.koDraggable')
.draggable('destroy')
;
});
2014-08-20 23:03:12 +08:00
}
}
};
ko.bindingHandlers.droppable = {
'init': function (oElement, fValueAccessor, fAllBindingsAccessor) {
2014-09-05 06:49:03 +08:00
var Globals = require('Common/Globals');
2014-08-20 23:03:12 +08:00
if (!Globals.bMobileDevice)
{
var
fValueFunc = fValueAccessor(),
fAllValueFunc = fAllBindingsAccessor(),
fOverCallback = fAllValueFunc && fAllValueFunc['droppableOver'] ? fAllValueFunc['droppableOver'] : null,
fOutCallback = fAllValueFunc && fAllValueFunc['droppableOut'] ? fAllValueFunc['droppableOut'] : null,
oConf = {
'tolerance': 'pointer',
'hoverClass': 'droppableHover'
}
;
if (fValueFunc)
{
oConf['drop'] = function (oEvent, oUi) {
fValueFunc(oEvent, oUi);
};
if (fOverCallback)
{
oConf['over'] = function (oEvent, oUi) {
fOverCallback(oEvent, oUi);
};
}
if (fOutCallback)
{
oConf['out'] = function (oEvent, oUi) {
fOutCallback(oEvent, oUi);
};
}
$(oElement).droppable(oConf);
2014-10-04 19:58:01 +08:00
ko.utils.domNodeDisposal.addDisposeCallback(oElement, function () {
$(oElement).droppable('destroy');
});
2014-08-20 23:03:12 +08:00
}
}
}
};
ko.bindingHandlers.nano = {
'init': function (oElement) {
2014-09-05 06:49:03 +08:00
var Globals = require('Common/Globals');
2014-08-20 23:03:12 +08:00
if (!Globals.bDisableNanoScroll)
{
$(oElement)
.addClass('nano')
.nanoScroller({
'iOSNativeScrolling': false,
'preventPageScrolling': true
})
;
}
}
};
ko.bindingHandlers.saveTrigger = {
'init': function (oElement) {
var $oEl = $(oElement);
$oEl.data('save-trigger-type', $oEl.is('input[type=text],input[type=email],input[type=password],select,textarea') ? 'input' : 'custom');
if ('custom' === $oEl.data('save-trigger-type'))
{
$oEl.append(
'&nbsp;&nbsp;<i class="icon-spinner animated"></i><i class="icon-remove error"></i><i class="icon-ok success"></i>'
).addClass('settings-saved-trigger');
}
else
{
$oEl.addClass('settings-saved-trigger-input');
}
},
'update': function (oElement, fValueAccessor) {
var
2014-10-04 19:58:01 +08:00
mValue = ko.unwrap(fValueAccessor()),
2014-08-20 23:03:12 +08:00
$oEl = $(oElement)
;
if ('custom' === $oEl.data('save-trigger-type'))
{
switch (mValue.toString())
{
case '1':
$oEl
.find('.animated,.error').hide().removeClass('visible')
.end()
.find('.success').show().addClass('visible')
;
break;
case '0':
$oEl
.find('.animated,.success').hide().removeClass('visible')
.end()
.find('.error').show().addClass('visible')
;
break;
case '-2':
$oEl
.find('.error,.success').hide().removeClass('visible')
.end()
.find('.animated').show().addClass('visible')
;
break;
default:
$oEl
.find('.animated').hide()
.end()
.find('.error,.success').removeClass('visible')
;
break;
}
}
else
{
switch (mValue.toString())
{
case '1':
$oEl.addClass('success').removeClass('error');
break;
case '0':
$oEl.addClass('error').removeClass('success');
break;
case '-2':
// $oEl;
break;
default:
$oEl.removeClass('error success');
break;
}
}
}
};
ko.bindingHandlers.emailsTags = {
2014-08-22 23:08:56 +08:00
'init': function(oElement, fValueAccessor, fAllBindingsAccessor) {
2014-08-25 15:10:51 +08:00
2014-08-20 23:03:12 +08:00
var
2014-09-05 06:49:03 +08:00
Utils = require('Common/Utils'),
EmailModel = require('Model/Email'),
2014-08-25 15:10:51 +08:00
2014-08-20 23:03:12 +08:00
$oEl = $(oElement),
fValue = fValueAccessor(),
2014-08-22 23:08:56 +08:00
fAllBindings = fAllBindingsAccessor(),
fAutoCompleteSource = fAllBindings['autoCompleteSource'] || null,
2014-08-20 23:03:12 +08:00
fFocusCallback = function (bValue) {
2015-02-16 05:55:59 +08:00
if (fValue && fValue.focused)
2014-08-20 23:03:12 +08:00
{
2015-02-16 05:55:59 +08:00
fValue.focused(!!bValue);
2014-08-20 23:03:12 +08:00
}
}
;
$oEl.inputosaurus({
'parseOnBlur': true,
'allowDragAndDrop': true,
'focusCallback': fFocusCallback,
'inputDelimiters': [',', ';'],
2014-08-22 23:08:56 +08:00
'autoCompleteSource': fAutoCompleteSource,
2014-08-20 23:03:12 +08:00
'parseHook': function (aInput) {
return _.map(aInput, function (sInputValue) {
var
sValue = Utils.trim(sInputValue),
oEmail = null
;
if ('' !== sValue)
{
oEmail = new EmailModel();
oEmail.mailsoParse(sValue);
return [oEmail.toLine(false), oEmail];
}
return [sValue, null];
});
},
'change': _.bind(function (oEvent) {
$oEl.data('EmailsTagsValue', oEvent.target.value);
fValue(oEvent.target.value);
}, this)
});
2015-02-16 05:55:59 +08:00
if (fValue && fValue.focused && fValue.focused.subscribe)
{
fValue.focused.subscribe(function (bValue) {
$oEl.inputosaurus(!!bValue ? 'focus' : 'blur');
});
}
2014-08-20 23:03:12 +08:00
},
2015-02-16 05:55:59 +08:00
'update': function (oElement, fValueAccessor) {
2014-08-20 23:03:12 +08:00
var
$oEl = $(oElement),
2015-02-16 05:55:59 +08:00
fValue = fValueAccessor(),
sValue = ko.unwrap(fValue)
2014-08-20 23:03:12 +08:00
;
if ($oEl.data('EmailsTagsValue') !== sValue)
{
$oEl.val(sValue);
$oEl.data('EmailsTagsValue', sValue);
$oEl.inputosaurus('refresh');
}
}
};
ko.bindingHandlers.command = {
'init': function (oElement, fValueAccessor, fAllBindingsAccessor, oViewModel) {
var
jqElement = $(oElement),
oCommand = fValueAccessor()
;
if (!oCommand || !oCommand.enabled || !oCommand.canExecute)
{
throw new Error('You are not using command function');
}
jqElement.addClass('command');
ko.bindingHandlers[jqElement.is('form') ? 'submit' : 'click'].init.apply(oViewModel, arguments);
},
'update': function (oElement, fValueAccessor) {
var
bResult = true,
jqElement = $(oElement),
oCommand = fValueAccessor()
;
bResult = oCommand.enabled();
jqElement.toggleClass('command-not-enabled', !bResult);
if (bResult)
{
bResult = oCommand.canExecute();
jqElement.toggleClass('command-can-not-be-execute', !bResult);
}
jqElement.toggleClass('command-disabled disable disabled', !bResult).toggleClass('no-disabled', !!bResult);
if (jqElement.is('input') || jqElement.is('button'))
{
jqElement.prop('disabled', !bResult);
}
}
};
2014-10-04 19:58:01 +08:00
// extenders
2014-08-20 23:03:12 +08:00
ko.extenders.trimmer = function (oTarget)
{
2014-08-25 15:10:51 +08:00
var
2014-09-05 06:49:03 +08:00
Utils = require('Common/Utils'),
2014-08-22 23:08:56 +08:00
oResult = ko.computed({
'read': oTarget,
'write': function (sNewValue) {
oTarget(Utils.trim(sNewValue.toString()));
},
'owner': this
})
;
2014-08-20 23:03:12 +08:00
oResult(oTarget());
return oResult;
};
ko.extenders.posInterer = function (oTarget, iDefault)
{
2014-08-25 15:10:51 +08:00
var
2014-09-05 06:49:03 +08:00
Utils = require('Common/Utils'),
2014-08-22 23:08:56 +08:00
oResult = ko.computed({
'read': oTarget,
'write': function (sNewValue) {
var iNew = Utils.pInt(sNewValue.toString(), iDefault);
if (0 >= iNew)
{
iNew = iDefault;
}
2014-08-20 23:03:12 +08:00
2014-08-22 23:08:56 +08:00
if (iNew === oTarget() && '' + iNew !== '' + sNewValue)
{
oTarget(iNew + 1);
}
2014-08-20 23:03:12 +08:00
2014-08-22 23:08:56 +08:00
oTarget(iNew);
}
})
;
2014-08-20 23:03:12 +08:00
oResult(oTarget());
return oResult;
};
ko.extenders.limitedList = function (oTarget, mList)
{
var
Utils = require('Common/Utils'),
oResult = ko.computed({
'read': oTarget,
'write': function (sNewValue) {
var
sCurrentValue = ko.unwrap(oTarget),
aList = ko.unwrap(mList)
;
if (Utils.isNonEmptyArray(aList))
{
if (-1 < Utils.inArray(sNewValue, aList))
{
oTarget(sNewValue);
}
else if (-1 < Utils.inArray(sCurrentValue, aList))
{
oTarget(sCurrentValue + ' ');
oTarget(sCurrentValue);
}
else
{
oTarget(aList[0] + ' ');
oTarget(aList[0]);
}
}
else
{
oTarget('');
}
}
}).extend({'notify': 'always'})
;
oResult(oTarget());
if (!oResult.valueHasMutated)
{
oResult.valueHasMutated = function () {
oTarget.valueHasMutated();
};
}
return oResult;
};
2014-08-20 23:03:12 +08:00
ko.extenders.reversible = function (oTarget)
{
var mValue = oTarget();
oTarget.commit = function ()
{
mValue = oTarget();
};
oTarget.reverse = function ()
{
oTarget(mValue);
};
oTarget.commitedValue = function ()
{
return mValue;
};
return oTarget;
};
ko.extenders.toggleSubscribe = function (oTarget, oOptions)
{
oTarget.subscribe(oOptions[1], oOptions[0], 'beforeChange');
oTarget.subscribe(oOptions[2], oOptions[0]);
return oTarget;
};
ko.extenders.toggleSubscribeProperty = function (oTarget, oOptions)
{
var sProp = oOptions[1];
if (sProp)
{
oTarget.subscribe(function (oPrev) {
if (oPrev && oPrev[sProp])
{
oPrev[sProp](false);
}
}, oOptions[0], 'beforeChange');
oTarget.subscribe(function (oNext) {
if (oNext && oNext[sProp])
{
oNext[sProp](true);
}
}, oOptions[0]);
}
return oTarget;
};
2014-08-20 23:03:12 +08:00
ko.extenders.falseTimeout = function (oTarget, iOption)
{
2014-09-05 06:49:03 +08:00
var Utils = require('Common/Utils');
2014-08-25 15:10:51 +08:00
2014-08-20 23:03:12 +08:00
oTarget.iTimeout = 0;
oTarget.subscribe(function (bValue) {
if (bValue)
{
window.clearTimeout(oTarget.iTimeout);
oTarget.iTimeout = window.setTimeout(function () {
oTarget(false);
oTarget.iTimeout = 0;
}, Utils.pInt(iOption));
}
});
return oTarget;
};
2014-10-04 19:58:01 +08:00
// functions
2014-08-20 23:03:12 +08:00
ko.observable.fn.validateNone = function ()
{
this.hasError = ko.observable(false);
return this;
};
ko.observable.fn.validateEmail = function ()
{
2014-09-05 06:49:03 +08:00
var Utils = require('Common/Utils');
2014-08-25 15:10:51 +08:00
2014-08-20 23:03:12 +08:00
this.hasError = ko.observable(false);
this.subscribe(function (sValue) {
sValue = Utils.trim(sValue);
this.hasError('' !== sValue && !(/^[^@\s]+@[^@\s]+$/.test(sValue)));
}, this);
this.valueHasMutated();
return this;
};
ko.observable.fn.validateSimpleEmail = function ()
{
2014-09-05 06:49:03 +08:00
var Utils = require('Common/Utils');
2014-08-22 23:08:56 +08:00
2014-08-20 23:03:12 +08:00
this.hasError = ko.observable(false);
this.subscribe(function (sValue) {
sValue = Utils.trim(sValue);
this.hasError('' !== sValue && !(/^.+@.+$/.test(sValue)));
}, this);
this.valueHasMutated();
return this;
};
ko.observable.fn.deleteAccessHelper = function ()
{
this.extend({'falseTimeout': 3000}).extend({'toggleSubscribe': [null,
function (oPrev) {
if (oPrev && oPrev.deleteAccess)
{
oPrev.deleteAccess(false);
}
}, function (oNext) {
if (oNext && oNext.deleteAccess)
{
oNext.deleteAccess(true);
}
}
]});
return this;
};
2014-08-20 23:03:12 +08:00
ko.observable.fn.validateFunc = function (fFunc)
{
2014-09-05 06:49:03 +08:00
var Utils = require('Common/Utils');
2014-08-25 15:10:51 +08:00
2014-08-20 23:03:12 +08:00
this.hasFuncError = ko.observable(false);
if (Utils.isFunc(fFunc))
{
this.subscribe(function (sValue) {
this.hasFuncError(!fFunc(sValue));
}, this);
this.valueHasMutated();
}
return this;
};
module.exports = ko;
2014-10-29 06:05:50 +08:00
}(ko));