Created
March 26, 2015 09:33
-
-
Save victorpavlenko/d3bee8044cec78939b3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function dispatchAction(action, onComplete) { | |
var reqType = 'GET'; | |
if ( | |
action['action'] == 'addPost' || | |
action['action'] == 'addReview' || | |
action['action'] == 'addReviewPhoto' || | |
action['action'] == 'addRepost' | |
) reqType = 'POST'; | |
$.ajax( | |
'/sodabox_resources/php/actions.php', { | |
data: action, | |
type: reqType, | |
error: function (xhdr, msg) { | |
alert(msg); | |
}, | |
success: function (data) { | |
if (action['action'] == 'getPost' || action['action'] == 'getChatMessages') { | |
onComplete(data); | |
} else { | |
if (typeof(data) != 'object') { | |
try { | |
data = JSON.parse(data); | |
} | |
catch (e) { | |
if (parseInt(data) != 0) { | |
//alert('error ' + data); | |
sysPopup('Непредвиденная ошибка', '#attention-popup'); | |
} | |
} | |
} | |
if (typeof(onComplete) == 'function') onComplete(data); | |
} | |
} | |
} | |
); | |
} | |
function setCookie(name, value, expires, path, domain, secure) { | |
var d = new Date(); | |
d.setTime(d.getTime() + (expires * 24 * 60 * 60 * 1000)); | |
document.cookie = name + "=" + value + | |
((expires) ? "; expires=" + d.toUTCString() : "") + | |
((path) ? "; path=" + path : "") + | |
((domain) ? "; domain=" + domain : "") + | |
((secure) ? "; secure" : ""); | |
} | |
// example: console.log(25, plural(25, ['товар', 'товара', 'товаров'])) | |
function plural(n, forms) { | |
var i = n % 10 === 1 && n % 100 !== 11 ? 0 | |
: (n % 10 >= 2 && n % 10 <= 4) && (n % 100 < 10 || n % 100 >= 20) ? 1 | |
: 2; | |
return forms[i]; | |
} | |
// ^^^plural^^^ is better :-) | |
function declension(num, type) { | |
var data, stringEnd, numb, $input, stringStart; | |
// type can be: goods, friends, subscriptions, points | |
$input = $('input[data-name="nouns-data"]'); | |
data = $input.data('value'); | |
numb = Math.floor(num); | |
stringStart = ' ' + data[type][0]; | |
if (numb > 4 && numb < 21) { | |
stringEnd = data[type][3]; | |
} else if (numb % 10 == 0) { | |
stringEnd = data[type][3]; | |
} else if (num % 10 == 1) { | |
stringEnd = data[type][1]; | |
} else if (num % 10 < 5) { | |
stringEnd = data[type][2]; | |
} else { | |
stringEnd = data[type][3]; | |
} | |
return stringStart + stringEnd; | |
} | |
function print_r(arr, level) { | |
var print_red_text = ""; | |
if (!level) level = 0; | |
var level_padding = ""; | |
for (var j = 0; j < level + 1; j++) level_padding += " "; | |
if (typeof(arr) == 'object') { | |
for (var item in arr) { | |
var value = arr[item]; | |
if (typeof(value) == 'object') { | |
print_red_text += level_padding + "'" + item + "' :\n"; | |
print_red_text += print_r(value, level + 1); | |
} | |
else | |
print_red_text += level_padding + "'" + item + "' => \"" + value + "\"\n"; | |
} | |
} | |
else print_red_text = "===>" + arr + "<===(" + typeof(arr) + ")"; | |
return print_red_text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment