Skip to content

Commit

Permalink
1.2.6
Browse files Browse the repository at this point in the history
Fixes #17. Introduced experimental .lang i18n feature.
  • Loading branch information
ermouth committed Jun 27, 2016
1 parent 03066d6 commit fbabb6a
Show file tree
Hide file tree
Showing 8 changed files with 4,554 additions and 56 deletions.
4,394 changes: 4,394 additions & 0 deletions Release/1.2/jquerymy-1.2.6.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Release/1.2/jquerymy-1.2.6.min.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Release/jquery.my.last.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jquerymy",
"version": "1.2.5",
"version": "1.2.6",
"description": "A lightweight jQuery plugin for complex two-way data binding in real time.",
"main": "./jquerymy.js",
"authors": [
Expand All @@ -21,7 +21,7 @@
],

"dependencies": {
"jquery":"~1.11",
"jquery":"~2",
"sugar":"~1.4"
},

Expand Down
183 changes: 141 additions & 42 deletions jquerymy.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
/*
* jQuery.my 1.2.5
* jQuery.my 1.2.6
* Requires jQuery 1.11.0+, SugarJS 1.3.9-1.4.x
*
* — fixes #24, subforms propagates own validity correctly
* — fixes #25, tojson no more stringifies DOM and other non-native objects
* — fixes #17, modal detects Bootstrap and pass request through if seems BT needed
* — added beta support for .lang field
* — forms with empty .ui section are initialzed bit more fast
*
* More details at jquerymy.com
*
* (c) @ermouth, thanks @carpogoryanin, @ftescht
* 2016-05-12
* 2016-06-27
*/

;(function ($) {

var _version = "jQuery.my 1.2.5";
var _version = "jQuery.my 1.2.6";

// Some shortcuts and constants
var TMP, lang = "en",
wURL = window.URL || window.webkitURL,
ie8 = !document.addEventListener,

Lang = ((window.navigator.language || window.navigator.userLanguage || 'en')+'').split('-')[0].toLowerCase(),

d8 = "{yyyy}-{MM}-{dd}",
h24="{HH}:{mm}",
Ob = "object",
Expand Down Expand Up @@ -519,15 +522,15 @@
//########################################################

msg:{
"":{en:"Invalid input", ru:(TMP="Неверное значение")},
"":{en:"Invalid input", ru:"Неверное значение"},

formError:{en:"Form error",ru:"Ошибка формы"},
initFailed:{
en:'<p class="my-error">Form init failed</p>',
ru:'<p class="my-error">Ошибка инициализации формы</p>'
},

badInput:{en:"Invalid input", ru:TMP},
badInput:{en:"Invalid input", ru:"Неверное значение"},
patternMismatch:{en:"Pattern mismatch", ru:"Не соответствует шаблону"},
rangeOverflow:{en:"Over maximum", ru:"Больше максимума"},
rangeUnderflow:{en:"Under minimum", ru:"Меньше минимума"},
Expand Down Expand Up @@ -655,8 +658,8 @@
delay:0, // default delay of bind invocation
strict:false, // if true form assumed unjsonned
restyle:-1, // delay of <style> repaint on screen resize, -1 for no repaint
locale:(TMP=(navigator.language||navigator.userLanguage||"en").substr(0,2)),
messages:Object.map(MY.msg, function (k,v){return v[TMP]||v.en;}),
locale:Lang, // locale
messages:Object.map(MY.msg, function (k,v){return v[Lang]||v.en;}),
errorTip:".my-error-tip", // $ selector of err tip container
errorCss:"my-error", // class applied on container on err
animate:0, // err tips animation duration
Expand Down Expand Up @@ -1050,7 +1053,7 @@
restyle: (function (skipChilds) {
// restyles dynamic styles of the form and its childs
var cids = [], i, cid,
$o = this.is(".my-form")?this:this.parents(".my-form").eq(0);
$o = this.hasClass("my-form")?this:this.parents(".my-form").eq(0);
if ($o.size() && $o.is(":visible") ) {
// get cids
cid = ($o.data("my")||{}).cid;
Expand Down Expand Up @@ -1128,6 +1131,10 @@
_broadcast($(document), isS(channel)?{channel:channel, message:msg}:channel);
},
rules:MY,
locale:function(lang){
if (isS(lang)) Lang = lang.toLowerCase();
else return Lang;
},
ajax: function(A1){
if (isF(A1)) return _ajax = A1;
else return _ajax.apply(this, _slice(arguments, 0));
Expand Down Expand Up @@ -2048,6 +2055,61 @@

//=======================================

function _locale (man0, lang0) {

// Processes .lang field of a manifest
// according to lang or default locale.
// Changes man.lang

if (!isO(man0)) return false;

var man = man0,
lang = isS(lang0)?lang0:Lang,
L = man.lang,
keys = {};

if (L && isO(L.en)) {

if (L._LANG == lang) return true;
lang = isO(L[lang])?lang:'en';
L._LANG = lang;

// Apply lang
Object.keys(L[lang]).forEach(_copyLocale.fill(void 0, void 0, lang, keys));

// Fill gaps with keys from en dict
if (lang != 'en') Object.keys(L.en).forEach(_copyLocale.fill(void 0, void 0, 'en', keys));

return true;
}

return false;

// - - - - - - - - -

function _copyLocale (k, idx, l, m){
var fx;
if (keys[k]) return;
if (k.length<3 && k.to(1).toLowerCase() == k.to(1)) return;
if (!isF(L[l][k])) L[k] = L[l][k];
else {
fx = L[l][k].bind(man);
L[k] = fx;
L[k].toString = fx;
L[k].assign = function(){
var a = [], i = 0, r = '';
for (;i<arguments.length;i++) a.push(arguments[i]);
r = fx.apply(null, a);
if (isS(r)) return r.assign.apply(r, a);
return r;
}
}
keys[k] = true;
}
}

//=======================================

function _mask (src, mask0) {
// Returns src obj masked with mask,
// _mask ({x:{t:5},y:3,z:[5,6]},["x","z.1"]) => [{t:5},6]
Expand Down Expand Up @@ -2607,10 +2669,10 @@

// unwind stringified fn and regexps defs
if (!p.strict && !isF(data.init)) _unjson(data, true);
manifest = $E(true,manifest,Object.reject(data, ["data"]));
manifest = $E(true, manifest, Object.reject(data, ["data"]));

// normalize ui section
ui = _normalize($E(true,{}, data.ui||{}), manifest, p);
ui = _normalize($E(true, {}, data.ui || {}), manifest, p);

// normalize radio section
if (manifest.radio) manifest.radio = _functionize(manifest.radio);
Expand All @@ -2624,14 +2686,17 @@
mid = _sdbm(myid);
manifest.id = myid;

p.form=$root;
p.form = $root;

if (data.params && data.params.depth) p.recalcDepth=data.params.depth;

//bind ‘this’ to 1st level manifest functions
for (i in manifest) if (isF(manifest[i])) manifest[i] = manifest[i].bind(manifest);
// bind ‘this’ to 1st level manifest functions
Object.keys(manifest)
.forEach(function(i){
if (typeof manifest[i] == 'function') manifest[i] = manifest[i].bind(manifest);
});

// 1.2.0 add .my property
// 1.2.0, add .my property
if (!ie8) {
Object.defineProperty(manifest, "my", {
get:function(){
Expand All @@ -2644,7 +2709,7 @@
manifest.my = _thismy ($root);
}

//mount error handler
// mount error handler
if (data.error) {
if (isS(manifest.error)) {
ehandler = function (msg,err) {
Expand All @@ -2656,14 +2721,17 @@
} else if (isF(data.error)) {
ehandler = function (err, stack) {
html=null;
try {html = data.error(err,stack);}
try {html = data.error.call(manifest, err, stack);}
catch (e) { html = p.messages.initFailed;}
return html;
};
}
}

//1.2.6, mount lang
if (manifest.lang) _locale(manifest, p.locale || Lang);

//mount params to form DOM node
// mount params to form DOM node
$E($root.data("my"), {
id: myid,
cid: cid,
Expand Down Expand Up @@ -2784,26 +2852,30 @@

function _controls (){
// Build and init controls
var formState={}, ctr=Object.size(ui);
var formState={},
k = Object.keys(ui),
ctr = k.length;

$root.addClass(initCss);

// build controls (init and premount)
Object.each(ui, function (selector) {
if (_fail) return;
var $o = $root.find(selector),
built = _build($o, $root, ui[selector], selector);
controls[selector] = $o;
if (isP(built)) {
//we've got promise
built.then(
countdown.fill(selector)
).fail(function (msg, obj){
_f("Error building "+selector+", "+msg, obj);
});
}
else if (!_fail) countdown(selector);
});
if (ctr === 0) _end();
else {
$root.addClass(initCss);
// build controls (init and premount)
k.forEach(function (selector) {
if (_fail) return;
var $o = $root.find(selector),
built = _build($o, $root, ui[selector], selector);
controls[selector] = $o;
if (isP(built)) {
//we've got promise
built.then(
countdown.fill(selector)
).fail(function (msg, obj){
_f("Error building "+selector+", "+msg, obj);
});
}
else if (!_fail) countdown(selector);
});
}

function countdown(selector){
if (!_fail) {
Expand Down Expand Up @@ -2844,14 +2916,21 @@
}
}
}
_end();
}

//-----------------------------------------------------

function _end (){
$root.removeClass(initCss);
$root.on("recalc.my, redraw.my", function (evt) {
evt.stopPropagation();
$root.my("redraw");
});
backup=null;
backup = null;
pi.resolve(d);
}


//-----------------------------------------------------

Expand Down Expand Up @@ -3501,7 +3580,7 @@

//#############################################################################################

/* jQuery.my.modal 1.1.5
/* jQuery.my.modal 1.1.7
* Requires Sugar 1.4.~, jQuery 1.11+, $.my 1.2.0+
*
* Modal dialog constructor/manager.
Expand Down Expand Up @@ -3562,12 +3641,18 @@

(function ($){

var root={}, parent = "body", parentBack, isOpen = false,
$E = $.extend, M = {},
var root={},
parent = "body",
parentBack,
isOpen = false,
$E = $.extend,
M = {},
BTmodal,
_indom = $.my.f.indom,
isA = Object.isArray, isB = Object.isBoolean, isS = Object.isString, isO = Object.isObject,
isN = Object.isNumber, isR = Object.isRegExp, isF = Object.isFunction;

if ($.fn.modal && isF($().emulateTransitionEnd)) BTmodal = $.fn.modal;

//Close modal on escape

Expand Down Expand Up @@ -3729,6 +3814,20 @@
// Extend jQuery with modal plugin

$.fn.modal = function (obj0, done0, width0) {

// Detect Bootstrap

if (BTmodal) {
if (obj0 === void 0 && done0 === void 0 && width0 === void 0) return BTmodal.call(this);
else if (isO(obj0) && (
obj0.hasOwnProperty('backdrop')
|| obj0.hasOwnProperty('keyborad')
|| obj0.hasOwnProperty('show')
)) return BTmodal.call(this, obj0, done0);
else if (isS(obj0) && /^(toggle|show|hide|handleUpdate)$/.test(obj0))
return BTmodal.call(this, obj0, done0);
}

var pi = $.Deferred(), o={},
$m, $f, $o = this, $r, $bg, $cl, $nose, padx=0, pady=0,
done = isF(done0)?done0:function(){ return false; },
Expand Down
8 changes: 4 additions & 4 deletions jquerymy.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions my.jquery.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"keywords": [
"MVVM", "framework", "ui", "form", "validation", "data binding"
],
"version": "1.2.5",
"version": "1.2.6",
"author": {
"name": "ermouth",
"url": "https://github.com/ermouth"
Expand All @@ -28,7 +28,7 @@
"demo": "http://jquerymy.com/#CW-demos",
"download": "http://jquerymy.com/download.html",
"dependencies": {
"jquery": ">=1.11.0",
"jquery": "~2",
"sugarjs": "~1.4.0"
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jquerymy",
"version": "1.2.5",
"version": "1.2.6",
"description": "A lightweight jQuery plugin for complex two-way data binding in real time.",
"homepage": "http://jquerymy.com/",
"author": "ermouth <[email protected]>",
Expand All @@ -27,7 +27,7 @@
},

"dependencies": {
"jquery":"~1.11",
"jquery":"~2",
"sugar":"~1.4"
},

Expand Down

0 comments on commit fbabb6a

Please sign in to comment.