snappymail/dev/External/ko.js

1294 lines
28 KiB
JavaScript
Raw Normal View History

2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
var
window = require('window'),
ko = window.ko,
_ = require('_'),
$ = require('$'),
JSON = require('JSON'),
Opentip = require('Opentip'),
Pikaday = require('pikaday'),
fDisposalTooltipHelper = function(oElement) {
ko.utils.domNodeDisposal.addDisposeCallback(oElement, function() {
if (oElement && oElement.__opentip)
{
oElement.__opentip.deactivate();
}
});
};
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
ko.bindingHandlers.updateWidth = {
init: function(oElement, fValueAccessor) {
var
$w = $(window),
$oEl = $(oElement),
fValue = fValueAccessor(),
fInit = function() {
fValue($oEl.width());
window.setTimeout(function() {
2016-04-29 04:32:54 +08:00
fValue($oEl.width());
2016-06-30 08:02:45 +08:00
}, 500);
};
2016-04-29 04:32:54 +08:00
2016-06-30 08:02:45 +08:00
$w.on('resize', fInit);
fInit();
2016-04-29 04:32:54 +08:00
2016-06-30 08:02:45 +08:00
ko.utils.domNodeDisposal.addDisposeCallback(oElement, function() {
$w.off('resize', fInit);
});
}
};
2016-04-29 04:32:54 +08:00
2016-06-30 08:02:45 +08:00
ko.bindingHandlers.editor = {
init: function(oElement, fValueAccessor) {
2015-04-07 03:32:19 +08:00
2016-06-30 08:02:45 +08:00
var
oEditor = null,
fValue = fValueAccessor(),
2015-04-17 01:55:13 +08:00
2016-06-30 08:02:45 +08:00
fUpdateEditorValue = function() {
if (fValue && fValue.__editor)
{
fValue.__editor.setHtmlOrPlain(fValue());
}
},
2015-04-17 01:55:13 +08:00
2016-06-30 08:02:45 +08:00
fUpdateKoValue = function() {
if (fValue && fValue.__editor)
{
fValue(fValue.__editor.getDataWithHtmlMark());
}
},
2015-04-17 01:55:13 +08:00
2016-06-30 08:02:45 +08:00
fOnReady = function() {
fValue.__editor = oEditor;
fUpdateEditorValue();
},
2015-04-17 01:55:13 +08:00
2016-06-30 08:02:45 +08:00
HtmlEditor = require('Common/HtmlEditor');
2015-04-07 03:32:19 +08:00
2016-06-30 08:02:45 +08:00
if (ko.isObservable(fValue) && HtmlEditor)
{
oEditor = new HtmlEditor(oElement, fUpdateKoValue, fOnReady, fUpdateKoValue);
2015-04-17 01:55:13 +08:00
2016-06-30 08:02:45 +08:00
fValue.__fetchEditorValue = fUpdateKoValue;
2015-04-07 03:32:19 +08:00
2016-06-30 08:02:45 +08:00
fValue.subscribe(fUpdateEditorValue);
2015-04-17 01:55:13 +08:00
2016-06-30 08:02:45 +08:00
// ko.utils.domNodeDisposal.addDisposeCallback(oElement, function() {
2015-04-17 01:55:13 +08:00
// });
2015-04-07 03:32:19 +08:00
}
2016-06-30 08:02:45 +08:00
}
};
2015-04-07 03:32:19 +08:00
2016-06-30 08:02:45 +08:00
ko.bindingHandlers.json = {
init: function(oElement, fValueAccessor) {
$(oElement).text(JSON.stringify(ko.unwrap(fValueAccessor())));
},
update: function(oElement, fValueAccessor) {
$(oElement).text(JSON.stringify(ko.unwrap(fValueAccessor())));
}
};
2016-06-30 08:02:45 +08:00
ko.bindingHandlers.scrollerShadows = {
init: function(oElement) {
2016-06-30 08:02:45 +08:00
var
iLimit = 8,
$oEl = $(oElement),
$win = $(window),
oCont = $oEl.find('[data-scroller-shadows-content]')[0] || null,
fFunc = _.throttle(function() {
$oEl
.toggleClass('scroller-shadow-top', iLimit < oCont.scrollTop)
.toggleClass('scroller-shadow-bottom', oCont.scrollTop + iLimit < oCont.scrollHeight - oCont.clientHeight);
}, 100);
if (oCont)
{
$(oCont).on('scroll resize', fFunc);
$win.on('resize', fFunc);
2016-06-24 05:57:35 +08:00
2016-06-30 08:02:45 +08:00
ko.utils.domNodeDisposal.addDisposeCallback(oCont, function() {
$(oCont).off();
$win.off('resize', fFunc);
});
2016-06-24 05:57:35 +08:00
}
2016-06-30 08:02:45 +08:00
}
};
2016-06-30 08:02:45 +08:00
ko.bindingHandlers.pikaday = {
init: function(oElement, fValueAccessor, fAllBindingsAccessor, oViewModel) {
2016-07-02 06:49:59 +08:00
ko.bindingHandlers.textInput
.init.apply(oViewModel, Array.prototype.slice.call(arguments)); // eslint-disable-line prefer-rest-params
2015-04-14 02:45:09 +08:00
2016-06-30 08:02:45 +08:00
if (Pikaday)
{
oElement.__pikaday = new Pikaday({
field: oElement
});
2014-08-20 23:03:12 +08:00
}
2016-06-30 08:02:45 +08:00
}
};
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
ko.bindingHandlers.tooltip = {
init: function(oElement, fValueAccessor) {
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
var
bi18n = true,
sValue = '',
Translator = null,
$oEl = $(oElement),
fValue = fValueAccessor(),
bMobile = 'on' === ($oEl.data('tooltip-mobile') || 'off'),
Globals = require('Common/Globals');
if (!Globals.bMobileDevice || bMobile)
{
bi18n = 'on' === ($oEl.data('tooltip-i18n') || 'on');
sValue = !ko.isObservable(fValue) && _.isFunction(fValue) ? fValue() : ko.unwrap(fValue);
2014-08-20 23:03:12 +08:00
oElement.__opentip = new Opentip(oElement, {
2016-06-30 08:02:45 +08:00
'style': 'rainloopTip',
'element': oElement,
2016-06-30 08:02:45 +08:00
'tipJoint': $oEl.data('tooltip-join') || 'bottom'
2014-08-20 23:03:12 +08:00
});
2016-06-30 08:02:45 +08:00
Globals.dropdownVisibility.subscribe(function(bV) {
if (bV) {
oElement.__opentip.hide();
}
2014-08-20 23:03:12 +08:00
});
2014-10-04 19:58:01 +08:00
2016-06-30 08:02:45 +08:00
if ('' === sValue)
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
oElement.__opentip.hide();
oElement.__opentip.deactivate();
oElement.__opentip.setContent('');
}
else
{
oElement.__opentip.activate();
2014-08-20 23:03:12 +08:00
}
2016-06-30 08:02:45 +08:00
if (bi18n)
2014-10-04 19:58:01 +08:00
{
2016-06-30 08:02:45 +08:00
Translator = require('Common/Translator');
2015-03-21 06:22:06 +08:00
2016-06-30 08:02:45 +08:00
oElement.__opentip.setContent(Translator.i18n(sValue));
Translator.trigger.subscribe(function() {
oElement.__opentip.setContent(Translator.i18n(sValue));
2015-03-16 05:58:50 +08:00
});
2016-06-30 08:02:45 +08:00
Globals.dropdownVisibility.subscribe(function() {
if (oElement && oElement.__opentip)
{
oElement.__opentip.setContent(require('Common/Translator').i18n(sValue));
}
});
2014-10-04 19:58:01 +08:00
}
2016-06-30 08:02:45 +08:00
else
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
oElement.__opentip.setContent(sValue);
}
}
},
update: function(oElement, fValueAccessor) {
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
var
bi18n = true,
sValue = '',
$oEl = $(oElement),
fValue = fValueAccessor(),
bMobile = 'on' === ($oEl.data('tooltip-mobile') || 'off'),
Globals = require('Common/Globals');
if ((!Globals.bMobileDevice || bMobile) && oElement.__opentip)
{
bi18n = 'on' === ($oEl.data('tooltip-i18n') || 'on');
sValue = !ko.isObservable(fValue) && _.isFunction(fValue) ? fValue() : ko.unwrap(fValue);
2016-06-30 08:02:45 +08:00
if (sValue)
{
oElement.__opentip.setContent(
bi18n ? require('Common/Translator').i18n(sValue) : sValue);
oElement.__opentip.activate();
}
else
{
oElement.__opentip.hide();
oElement.__opentip.deactivate();
oElement.__opentip.setContent('');
2014-08-20 23:03:12 +08:00
}
}
2016-06-30 08:02:45 +08:00
}
};
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
ko.bindingHandlers.tooltipErrorTip = {
init: function(oElement) {
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
var $oEl = $(oElement);
2014-10-04 19:58:01 +08:00
2016-06-30 08:02:45 +08:00
oElement.__opentip = new Opentip(oElement, {
'style': 'rainloopErrorTip',
'hideOn': 'mouseout click',
'element': oElement,
'tipJoint': $oEl.data('tooltip-join') || 'top'
});
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
oElement.__opentip.deactivate();
$(window.document).on('click', function() {
if (oElement && oElement.__opentip)
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
oElement.__opentip.hide();
2014-08-20 23:03:12 +08:00
}
2016-06-30 08:02:45 +08:00
});
fDisposalTooltipHelper(oElement);
},
update: function(oElement, fValueAccessor) {
var
$oEl = $(oElement),
fValue = fValueAccessor(),
sValue = !ko.isObservable(fValue) && _.isFunction(fValue) ? fValue() : ko.unwrap(fValue),
oOpenTips = oElement.__opentip;
if (oOpenTips)
{
if ('' === sValue)
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
oOpenTips.hide();
oOpenTips.deactivate();
oOpenTips.setContent('');
2014-08-20 23:03:12 +08:00
}
else
{
2016-06-30 08:02:45 +08:00
_.delay(function() {
if ($oEl.is(':visible'))
{
oOpenTips.setContent(sValue);
oOpenTips.activate();
oOpenTips.show();
}
else
{
oOpenTips.hide();
oOpenTips.deactivate();
oOpenTips.setContent('');
}
}, 100);
2014-08-20 23:03:12 +08:00
}
}
2016-06-30 08:02:45 +08:00
}
};
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
ko.bindingHandlers.registrateBootstrapDropdown = {
init: function(oElement) {
var Globals = require('Common/Globals');
if (Globals && Globals.data.aBootstrapDropdowns)
{
Globals.data.aBootstrapDropdowns.push($(oElement));
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
$(oElement).click(function() {
require('Common/Utils').detectDropdownVisibility();
});
2016-06-30 08:02:45 +08:00
// ko.utils.domNodeDisposal.addDisposeCallback(oElement, function() {
// });
}
2016-06-30 08:02:45 +08:00
}
};
2016-06-30 08:02:45 +08:00
ko.bindingHandlers.openDropdownTrigger = {
update: function(oElement, fValueAccessor) {
if (ko.unwrap(fValueAccessor()))
{
var $oEl = $(oElement);
if (!$oEl.hasClass('open'))
{
$oEl.find('.dropdown-toggle').dropdown('toggle');
}
2014-10-04 19:58:01 +08:00
2016-06-30 08:02:45 +08:00
$oEl.find('.dropdown-toggle').focus();
require('Common/Utils').detectDropdownVisibility();
fValueAccessor()(false);
2014-08-20 23:03:12 +08:00
}
2016-06-30 08:02:45 +08:00
}
};
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
ko.bindingHandlers.dropdownCloser = {
init: function(oElement) {
$(oElement).closest('.dropdown').on('click', '.e-item', function() {
$(oElement).dropdown('toggle');
});
}
};
2016-06-30 08:02:45 +08:00
ko.bindingHandlers.popover = {
init: function(oElement, fValueAccessor) {
$(oElement).popover(ko.unwrap(fValueAccessor()));
2016-06-30 08:02:45 +08:00
ko.utils.domNodeDisposal.addDisposeCallback(oElement, function() {
$(oElement).popover('destroy');
});
}
};
2014-10-04 19:58:01 +08:00
2016-06-30 08:02:45 +08:00
ko.bindingHandlers.csstext = {};
ko.bindingHandlers.csstext.init = ko.bindingHandlers.csstext.update = function(oElement, fValueAccessor) {
if (oElement && oElement.styleSheet && 'undefined' !== typeof oElement.styleSheet.cssText)
{
oElement.styleSheet.cssText = ko.unwrap(fValueAccessor());
}
else
{
$(oElement).text(ko.unwrap(fValueAccessor()));
}
};
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'
});
}
};
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
ko.bindingHandlers.onEnter = {
init: function(oElement, fValueAccessor, fAllBindingsAccessor, oViewModel) {
$(oElement).on('keypress.koOnEnter', function(oEvent) {
if (oEvent && 13 === window.parseInt(oEvent.keyCode, 10))
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
$(oElement).trigger('change');
fValueAccessor().call(oViewModel);
2014-08-20 23:03:12 +08:00
}
2016-06-30 08:02:45 +08:00
});
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
ko.utils.domNodeDisposal.addDisposeCallback(oElement, function() {
$(oElement).off('keypress.koOnEnter');
});
}
};
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
ko.bindingHandlers.onSpace = {
init: function(oElement, fValueAccessor, fAllBindingsAccessor, oViewModel) {
$(oElement).on('keyup.koOnSpace', function(oEvent) {
if (oEvent && 32 === window.parseInt(oEvent.keyCode, 10))
{
fValueAccessor().call(oViewModel, oEvent);
}
});
2014-08-25 15:10:51 +08:00
2016-06-30 08:02:45 +08:00
ko.utils.domNodeDisposal.addDisposeCallback(oElement, function() {
$(oElement).off('keyup.koOnSpace');
});
}
};
2015-02-15 08:30:21 +08:00
2016-06-30 08:02:45 +08:00
ko.bindingHandlers.onTab = {
init: function(oElement, fValueAccessor, fAllBindingsAccessor, oViewModel) {
$(oElement).on('keydown.koOnTab', function(oEvent) {
if (oEvent && 9 === window.parseInt(oEvent.keyCode, 10))
{
return fValueAccessor().call(oViewModel, !!oEvent.shiftKey);
}
2016-07-01 06:50:11 +08:00
return true;
2016-06-30 08:02:45 +08:00
});
2015-02-15 08:30:21 +08:00
2016-06-30 08:02:45 +08:00
ko.utils.domNodeDisposal.addDisposeCallback(oElement, function() {
$(oElement).off('keydown.koOnTab');
});
}
};
2015-02-15 08:30:21 +08:00
2016-06-30 08:02:45 +08:00
ko.bindingHandlers.onEsc = {
init: function(oElement, fValueAccessor, fAllBindingsAccessor, oViewModel) {
$(oElement).on('keypress.koOnEsc', function(oEvent) {
if (oEvent && 27 === window.parseInt(oEvent.keyCode, 10))
2015-02-15 08:30:21 +08:00
{
2016-06-30 08:02:45 +08:00
$(oElement).trigger('change');
fValueAccessor().call(oViewModel);
2015-02-15 08:30:21 +08:00
}
2016-06-30 08:02:45 +08:00
});
2015-02-15 08:30:21 +08:00
2016-06-30 08:02:45 +08:00
ko.utils.domNodeDisposal.addDisposeCallback(oElement, function() {
$(oElement).off('keypress.koOnEsc');
});
}
};
2015-03-07 08:32:06 +08:00
2016-06-30 08:02:45 +08:00
ko.bindingHandlers.clickOnTrue = {
update: function(oElement, fValueAccessor) {
if (ko.unwrap(fValueAccessor()))
{
$(oElement).click();
2015-03-07 08:32:06 +08:00
}
2016-06-30 08:02:45 +08:00
}
};
2015-03-07 08:32:06 +08:00
2016-06-30 08:02:45 +08:00
ko.bindingHandlers.modal = {
init: function(oElement, fValueAccessor) {
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
var
Globals = require('Common/Globals'),
Utils = require('Common/Utils');
$(oElement)
.toggleClass('fade', !Globals.bMobileDevice)
.modal({
'keyboard': false,
'show': ko.unwrap(fValueAccessor())
})
.on('shown.koModal', Utils.windowResizeCallback)
.find('.close').on('click.koModal', function() {
fValueAccessor()(false);
});
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
ko.utils.domNodeDisposal.addDisposeCallback(oElement, function() {
$(oElement)
.off('shown.koModal')
.find('.close')
.off('click.koModal');
});
},
update: function(oElement, fValueAccessor) {
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
var Globals = require('Common/Globals');
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
$(oElement).modal(ko.unwrap(fValueAccessor()) ? 'show' : 'hide');
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
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
}
2015-01-09 07:31:31 +08:00
2016-06-30 08:02:45 +08:00
}
};
ko.bindingHandlers.moment = {
init: function(oElement, fValueAccessor) {
require('Common/Momentor').momentToNode(
$(oElement).addClass('moment').data('moment-time', ko.unwrap(fValueAccessor()))
);
},
update: function(oElement, fValueAccessor) {
require('Common/Momentor').momentToNode(
$(oElement).data('moment-time', ko.unwrap(fValueAccessor()))
);
}
};
ko.bindingHandlers.i18nInit = {
init: function(oElement) {
require('Common/Translator').i18nToNodes(oElement);
}
};
ko.bindingHandlers.translatorInit = {
init: function(oElement) {
require('Common/Translator').i18nToNodes(oElement);
}
};
ko.bindingHandlers.i18nUpdate = {
update: function(oElement, fValueAccessor) {
ko.unwrap(fValueAccessor());
require('Common/Translator').i18nToNodes(oElement);
}
};
ko.bindingHandlers.link = {
update: function(oElement, fValueAccessor) {
$(oElement).attr('href', ko.unwrap(fValueAccessor()));
}
};
ko.bindingHandlers.title = {
update: function(oElement, fValueAccessor) {
$(oElement).attr('title', ko.unwrap(fValueAccessor()));
}
};
ko.bindingHandlers.textF = {
init: function(oElement, fValueAccessor) {
$(oElement).text(ko.unwrap(fValueAccessor()));
}
};
ko.bindingHandlers.initDom = {
init: function(oElement, fValueAccessor) {
fValueAccessor()(oElement);
}
};
ko.bindingHandlers.initFixedTrigger = {
init: function(oElement, fValueAccessor) {
var
aValues = ko.unwrap(fValueAccessor()),
$oContainer = null,
$oElement = $(oElement),
oOffset = null,
2015-01-09 07:31:31 +08:00
2016-06-30 08:02:45 +08:00
iTop = aValues[1] || 0;
2014-08-25 15:10:51 +08:00
2016-06-30 08:02:45 +08:00
$oContainer = $(aValues[0] || null);
$oContainer = $oContainer[0] ? $oContainer : null;
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
if ($oContainer)
{
$(window).resize(function() {
oOffset = $oContainer.offset();
if (oOffset && oOffset.top)
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
$oElement.css('top', oOffset.top + iTop);
2014-08-20 23:03:12 +08:00
}
2016-06-30 08:02:45 +08:00
});
}
}
};
ko.bindingHandlers.initResizeTrigger = {
init: function(oElement, fValueAccessor) {
var aValues = ko.unwrap(fValueAccessor());
$(oElement).css({
'height': aValues[1],
'min-height': aValues[1]
});
},
update: function(oElement, fValueAccessor) {
var
Utils = require('Common/Utils'),
Globals = require('Common/Globals'),
aValues = ko.unwrap(fValueAccessor()),
iValue = Utils.pInt(aValues[1]),
iSize = 0,
iOffset = $(oElement).offset().top;
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
if (0 < iOffset)
{
iOffset += Utils.pInt(aValues[2]);
iSize = Globals.$win.height() - iOffset;
if (iValue < iSize)
{
iValue = iSize;
2014-08-20 23:03:12 +08:00
}
2016-06-30 08:02:45 +08:00
$(oElement).css({
'height': iValue,
'min-height': iValue
});
2014-08-20 23:03:12 +08:00
}
2016-06-30 08:02:45 +08:00
}
};
ko.bindingHandlers.appendDom = {
update: function(oElement, fValueAccessor) {
$(oElement).hide().empty().append(ko.unwrap(fValueAccessor())).show();
}
};
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
ko.bindingHandlers.draggable = {
init: function(oElement, fValueAccessor, fAllBindingsAccessor) {
var
Globals = require('Common/Globals'),
Utils = require('Common/Utils');
2014-10-04 19:58:01 +08:00
2016-06-30 08:02:45 +08:00
if (!Globals.bMobileDevice)
{
2014-08-22 23:08:56 +08:00
var
2016-06-30 08:02:45 +08:00
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
};
2014-10-04 19:58:01 +08:00
2016-06-30 08:02:45 +08:00
if (sDroppableSelector)
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
oConf.drag = function(oEvent) {
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
$(sDroppableSelector).each(function() {
var
moveUp = null,
moveDown = null,
$this = $(this),
oOffset = $this.offset(),
bottomPos = oOffset.top + $this.height();
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
window.clearInterval($this.data('timerScroll'));
$this.data('timerScroll', false);
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
if (oEvent.pageX >= oOffset.left && oEvent.pageX <= oOffset.left + $this.width())
{
if (oEvent.pageY >= bottomPos - iTriggerZone && oEvent.pageY <= bottomPos)
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
moveUp = function() {
$this.scrollTop($this.scrollTop() + iScrollSpeed);
Utils.windowResize();
};
$this.data('timerScroll', window.setInterval(moveUp, 10));
moveUp();
2014-08-20 23:03:12 +08:00
}
2016-06-30 08:02:45 +08:00
if (oEvent.pageY >= oOffset.top && oEvent.pageY <= oOffset.top + iTriggerZone)
{
moveDown = function() {
$this.scrollTop($this.scrollTop() - iScrollSpeed);
Utils.windowResize();
};
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
$this.data('timerScroll', window.setInterval(moveDown, 10));
moveDown();
}
}
});
2014-08-20 23:03:12 +08:00
};
2016-06-30 08:02:45 +08:00
oConf.stop = function() {
$(sDroppableSelector).each(function() {
window.clearInterval($(this).data('timerScroll'));
$(this).data('timerScroll', false);
});
};
2014-08-20 23:03:12 +08:00
}
2016-06-30 08:02:45 +08:00
oConf.helper = function(oEvent) {
return fValueAccessor()(oEvent && oEvent.target ? ko.dataFor(oEvent.target) : null);
};
$(oElement).draggable(oConf).on('mousedown.koDraggable', function() {
Utils.removeInFocus();
});
ko.utils.domNodeDisposal.addDisposeCallback(oElement, function() {
$(oElement)
.off('mousedown.koDraggable')
.draggable('destroy');
});
2014-08-20 23:03:12 +08:00
}
2016-06-30 08:02:45 +08:00
}
};
ko.bindingHandlers.droppable = {
init: function(oElement, fValueAccessor, fAllBindingsAccessor) {
var Globals = require('Common/Globals');
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'
};
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
if (fValueFunc)
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
oConf.drop = function(oEvent, oUi) {
fValueFunc(oEvent, oUi);
};
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
if (fOverCallback)
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
oConf.over = function(oEvent, oUi) {
fOverCallback(oEvent, oUi);
2014-08-20 23:03:12 +08:00
};
2016-06-30 08:02:45 +08:00
}
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
if (fOutCallback)
{
oConf.out = function(oEvent, oUi) {
fOutCallback(oEvent, oUi);
};
}
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
$(oElement).droppable(oConf);
2014-10-04 19:58:01 +08:00
2016-06-30 08:02:45 +08:00
ko.utils.domNodeDisposal.addDisposeCallback(oElement, function() {
$(oElement).droppable('destroy');
});
2014-08-20 23:03:12 +08:00
}
}
2016-06-30 08:02:45 +08:00
}
};
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
ko.bindingHandlers.nano = {
init: function(oElement) {
2016-06-09 02:07:16 +08:00
2016-06-30 08:02:45 +08:00
var
Globals = require('Common/Globals'),
Settings = require('Storage/Settings');
2016-06-09 02:07:16 +08:00
2016-06-30 08:02:45 +08:00
if (!Globals.bDisableNanoScroll && !Settings.appSettingsGet('useNativeScrollbars'))
{
$(oElement)
.addClass('nano')
.nanoScroller({
'iOSNativeScrolling': false,
'preventPageScrolling': true
});
2014-08-20 23:03:12 +08:00
}
2016-06-30 08:02:45 +08:00
}
};
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
ko.bindingHandlers.saveTrigger = {
init: function(oElement) {
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
var $oEl = $(oElement);
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
$oEl.data('save-trigger-type', $oEl.is('input[type=text],input[type=email],input[type=password],select,textarea') ? 'input' : 'custom');
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
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
mValue = ko.unwrap(fValueAccessor()),
$oEl = $(oElement);
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
if ('custom' === $oEl.data('save-trigger-type'))
{
switch (mValue.toString())
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
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;
2014-08-20 23:03:12 +08:00
}
2016-06-30 08:02:45 +08:00
}
else
{
switch (mValue.toString())
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
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;
2014-08-20 23:03:12 +08:00
}
}
2016-06-30 08:02:45 +08:00
}
};
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
ko.bindingHandlers.emailsTags = {
init: function(oElement, fValueAccessor, fAllBindingsAccessor) {
2014-08-25 15:10:51 +08:00
2016-06-30 08:02:45 +08:00
var
Utils = require('Common/Utils'),
2016-07-07 05:03:30 +08:00
EmailModel = require('Model/Email').default,
2016-06-30 08:02:45 +08:00
$oEl = $(oElement),
fValue = fValueAccessor(),
fAllBindings = fAllBindingsAccessor(),
fAutoCompleteSource = fAllBindings.autoCompleteSource || null,
fFocusCallback = function(bValue) {
if (fValue && fValue.focused)
{
fValue.focused(!!bValue);
2014-08-20 23:03:12 +08:00
}
2016-06-30 08:02:45 +08:00
};
$oEl.inputosaurus({
'parseOnBlur': true,
'allowDragAndDrop': true,
'focusCallback': fFocusCallback,
'inputDelimiters': [',', ';', '\n'],
'autoCompleteSource': fAutoCompleteSource,
// 'elementHook': function(oEl, oItem) {
2015-03-21 06:22:06 +08:00
// if (oEl && oItem)
// {
// oEl.addClass('pgp');
// window.console.log(arguments);
// }
// },
2016-06-30 08:02:45 +08:00
'parseHook': function(aInput) {
2015-06-05 02:02:31 +08:00
2016-06-30 08:02:45 +08:00
return _.map(aInput, function(sInputValue) {
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
var
sValue = Utils.trim(sInputValue),
oEmail = null;
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
if ('' !== sValue)
{
oEmail = new EmailModel();
oEmail.mailsoParse(sValue);
return [oEmail.toLine(false), oEmail];
}
2015-07-06 02:06:19 +08:00
2016-06-30 08:02:45 +08:00
return [sValue, null];
2015-07-06 02:06:19 +08:00
2016-06-30 08:02:45 +08:00
});
2015-06-05 02:02:31 +08:00
2015-07-06 02:06:19 +08:00
// var aResult = [];
//
2016-06-30 08:02:45 +08:00
// _.each(aInput, function(sInputValue) {
2015-07-06 02:06:19 +08:00
//
// var
// aM = null,
// aValues = [],
// sValue = Utils.trim(sInputValue),
// oEmail = null
// ;
//
// if ('' !== sValue)
// {
// aM = sValue.match(/[@]/g);
// if (aM && 0 < aM.length)
// {
// sValue = sValue.replace(/[\r\n]+/g, '; ').replace(/[\s]+/g, ' ');
// aValues = EmailModel.splitHelper(sValue, ';');
//
2016-06-30 08:02:45 +08:00
// _.each(aValues, function(sV) {
2015-07-06 02:06:19 +08:00
//
// oEmail = new EmailModel();
// oEmail.mailsoParse(sV);
//
// if (oEmail.email)
// {
// aResult.push([oEmail.toLine(false), oEmail]);
// }
// else
// {
// aResult.push(['', null]);
// }
// });
// }
// else
// {
// aResult.push([sInputValue, null]);
// }
// }
// else
// {
// aResult.push([sInputValue, null]);
// }
// });
//
// return aResult;
2016-06-30 08:02:45 +08:00
},
'change': _.bind(function(oEvent) {
$oEl.data('EmailsTagsValue', oEvent.target.value);
fValue(oEvent.target.value);
}, this)
});
2015-02-16 05:55:59 +08:00
2016-06-30 08:02:45 +08:00
if (fValue && fValue.focused && fValue.focused.subscribe)
{
fValue.focused.subscribe(function(bValue) {
$oEl.inputosaurus(bValue ? 'focus' : 'blur');
});
}
},
update: function(oElement, fValueAccessor) {
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
var
$oEl = $(oElement),
fValue = fValueAccessor(),
sValue = ko.unwrap(fValue);
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
if ($oEl.data('EmailsTagsValue') !== sValue)
{
$oEl.val(sValue);
$oEl.data('EmailsTagsValue', sValue);
$oEl.inputosaurus('refresh');
2014-08-20 23:03:12 +08:00
}
2016-06-30 08:02:45 +08:00
}
};
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
ko.bindingHandlers.command = {
init: function(oElement, fValueAccessor, fAllBindingsAccessor, oViewModel) {
var
jqElement = $(oElement),
oCommand = fValueAccessor();
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
if (!oCommand || !oCommand.enabled || !oCommand.canExecute)
{
throw new Error('You are not using command function');
}
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
jqElement.addClass('command');
ko.bindingHandlers[jqElement.is('form') ? 'submit' : 'click']
2016-07-02 06:49:59 +08:00
.init.apply(oViewModel, Array.prototype.slice.call(arguments)); // eslint-disable-line prefer-rest-params
2016-06-30 08:02:45 +08:00
},
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
update: function(oElement, fValueAccessor) {
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
var
bResult = true,
jqElement = $(oElement),
oCommand = fValueAccessor();
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
bResult = oCommand.enabled();
jqElement.toggleClass('command-not-enabled', !bResult);
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
if (bResult)
{
bResult = oCommand.canExecute();
jqElement.toggleClass('command-can-not-be-execute', !bResult);
}
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
jqElement.toggleClass('command-disabled disable disabled', !bResult).toggleClass('no-disabled', !!bResult);
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
if (jqElement.is('input') || jqElement.is('button'))
{
jqElement.prop('disabled', !bResult);
2014-08-20 23:03:12 +08:00
}
2016-06-30 08:02:45 +08:00
}
};
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
// extenders
2014-10-04 19:58:01 +08:00
2016-06-30 08:02:45 +08:00
ko.extenders.trimmer = function(oTarget)
{
var
Utils = require('Common/Utils'),
oResult = ko.computed({
'read': oTarget,
'write': function(sNewValue) {
oTarget(Utils.trim(sNewValue.toString()));
},
'owner': this
});
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
oResult(oTarget());
return oResult;
};
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
ko.extenders.posInterer = function(oTarget, iDefault)
{
var
Utils = require('Common/Utils'),
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
2016-06-30 08:02:45 +08:00
if (iNew === oTarget() && '' + iNew !== '' + sNewValue)
{
oTarget(iNew + 1);
2014-08-22 23:08:56 +08:00
}
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
oTarget(iNew);
}
});
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
oResult(oTarget());
return oResult;
};
2016-06-30 08:02:45 +08:00
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);
2016-06-30 08:02:45 +08:00
if (Utils.isNonEmptyArray(aList))
{
if (-1 < Utils.inArray(sNewValue, aList))
{
2016-06-30 08:02:45 +08:00
oTarget(sNewValue);
}
else if (-1 < Utils.inArray(sCurrentValue, aList))
{
oTarget(sCurrentValue + ' ');
oTarget(sCurrentValue);
}
else
{
2016-06-30 08:02:45 +08:00
oTarget(aList[0] + ' ');
oTarget(aList[0]);
}
}
2016-06-30 08:02:45 +08:00
else
{
oTarget('');
}
}
}).extend({'notify': 'always'});
2016-06-30 08:02:45 +08:00
oResult(oTarget());
2016-06-30 08:02:45 +08:00
if (!oResult.valueHasMutated)
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
oResult.valueHasMutated = function() {
oTarget.valueHasMutated();
2014-08-20 23:03:12 +08:00
};
2016-06-30 08:02:45 +08:00
}
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
return oResult;
};
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
ko.extenders.reversible = function(oTarget)
{
var mValue = oTarget();
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
oTarget.commit = function()
{
mValue = oTarget();
2014-08-20 23:03:12 +08:00
};
2016-06-30 08:02:45 +08:00
oTarget.reverse = function()
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
oTarget(mValue);
2014-08-20 23:03:12 +08:00
};
2016-06-30 08:02:45 +08:00
oTarget.commitedValue = function()
{
2016-06-30 08:02:45 +08:00
return mValue;
};
2016-06-30 08:02:45 +08:00
return oTarget;
};
2016-06-30 08:02:45 +08:00
ko.extenders.toggleSubscribe = function(oTarget, oOptions)
{
oTarget.subscribe(oOptions[1], oOptions[0], 'beforeChange');
oTarget.subscribe(oOptions[2], oOptions[0]);
2016-06-30 08:02:45 +08:00
return oTarget;
};
2016-06-30 08:02:45 +08:00
ko.extenders.toggleSubscribeProperty = function(oTarget, oOptions)
{
var sProp = oOptions[1];
if (sProp)
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
oTarget.subscribe(function(oPrev) {
if (oPrev && oPrev[sProp])
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
oPrev[sProp](false);
2014-08-20 23:03:12 +08:00
}
2016-06-30 08:02:45 +08:00
}, oOptions[0], 'beforeChange');
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
oTarget.subscribe(function(oNext) {
if (oNext && oNext[sProp])
{
oNext[sProp](true);
}
}, oOptions[0]);
}
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
return oTarget;
};
ko.extenders.falseTimeout = function(oTarget, iOption)
{
oTarget.iFalseTimeoutTimeout = 0;
oTarget.subscribe(function(bValue) {
if (bValue)
2015-03-31 08:12:05 +08:00
{
2016-06-30 08:02:45 +08:00
window.clearTimeout(oTarget.iFalseTimeoutTimeout);
oTarget.iFalseTimeoutTimeout = window.setTimeout(function() {
oTarget(false);
oTarget.iFalseTimeoutTimeout = 0;
}, require('Common/Utils').pInt(iOption));
}
});
return oTarget;
};
ko.extenders.specialThrottle = function(oTarget, iOption)
{
oTarget.iSpecialThrottleTimeoutValue = require('Common/Utils').pInt(iOption);
if (0 < oTarget.iSpecialThrottleTimeoutValue)
{
oTarget.iSpecialThrottleTimeout = 0;
oTarget.valueForRead = ko.observable(!!oTarget()).extend({'throttle': 10});
2015-03-31 08:12:05 +08:00
2016-06-30 08:02:45 +08:00
return ko.computed({
'read': oTarget.valueForRead,
'write': function(bValue) {
2015-03-31 08:12:05 +08:00
2016-06-30 08:02:45 +08:00
if (bValue)
{
oTarget.valueForRead(bValue);
}
else
{
if (oTarget.valueForRead())
2015-03-31 08:12:05 +08:00
{
2016-06-30 08:02:45 +08:00
window.clearTimeout(oTarget.iSpecialThrottleTimeout);
oTarget.iSpecialThrottleTimeout = window.setTimeout(function() {
oTarget.valueForRead(false);
oTarget.iSpecialThrottleTimeout = 0;
}, oTarget.iSpecialThrottleTimeoutValue);
2015-03-31 08:12:05 +08:00
}
else
{
2016-06-30 08:02:45 +08:00
oTarget.valueForRead(bValue);
2015-03-31 08:12:05 +08:00
}
}
2016-06-30 08:02:45 +08:00
}
});
}
2015-03-31 08:12:05 +08:00
2016-06-30 08:02:45 +08:00
return oTarget;
};
2015-03-31 08:12:05 +08:00
2016-07-16 03:54:37 +08:00
ko.extenders.idleTrigger = function(oTarget)
{
var Enums = require('Common/Enums');
oTarget.trigger = ko.observable(Enums.SaveSettingsStep.Idle);
return oTarget;
};
2016-06-30 08:02:45 +08:00
// functions
2014-10-04 19:58:01 +08:00
2016-07-16 05:29:42 +08:00
ko.observable.fn.idleTrigger = function()
{
return this.extend({'idleTrigger': true});
};
2016-06-30 08:02:45 +08:00
ko.observable.fn.validateNone = function()
{
this.hasError = ko.observable(false);
return this;
};
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
ko.observable.fn.validateEmail = function()
{
var Utils = require('Common/Utils');
2014-08-25 15:10:51 +08:00
2016-06-30 08:02:45 +08:00
this.hasError = ko.observable(false);
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
this.subscribe(function(sValue) {
sValue = Utils.trim(sValue);
this.hasError('' !== sValue && !(/^[^@\s]+@[^@\s]+$/.test(sValue)));
}, this);
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
this.valueHasMutated();
return this;
};
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
ko.observable.fn.validateSimpleEmail = function()
{
var Utils = require('Common/Utils');
2014-08-22 23:08:56 +08:00
2016-06-30 08:02:45 +08:00
this.hasError = ko.observable(false);
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
this.subscribe(function(sValue) {
sValue = Utils.trim(sValue);
this.hasError('' !== sValue && !(/^.+@.+$/.test(sValue)));
}, this);
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
this.valueHasMutated();
return this;
};
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
ko.observable.fn.deleteAccessHelper = function()
{
this.extend({'falseTimeout': 3000}).extend({'toggleSubscribe': [null,
function(oPrev) {
if (oPrev && oPrev.deleteAccess)
{
oPrev.deleteAccess(false);
}
2016-06-30 08:02:45 +08:00
}, function(oNext) {
if (oNext && oNext.deleteAccess)
{
oNext.deleteAccess(true);
}
}
]});
2016-06-30 08:02:45 +08:00
return this;
};
2014-08-25 15:10:51 +08:00
2016-06-30 08:02:45 +08:00
ko.observable.fn.validateFunc = function(fFunc)
{
var Utils = require('Common/Utils');
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
this.hasFuncError = ko.observable(false);
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
if (Utils.isFunc(fFunc))
{
this.subscribe(function(sValue) {
this.hasFuncError(!fFunc(sValue));
}, this);
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
this.valueHasMutated();
}
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
return this;
};
2014-08-20 23:03:12 +08:00
2016-06-30 08:02:45 +08:00
module.exports = ko;