/*global define, escape, locales, Jed */ (function (root, factory) { define([ "jquery", "jquery.browser", "lodash", "tpl!field", "tpl!select_option", "tpl!form_select", "tpl!form_textarea", "tpl!form_checkbox", "tpl!form_username", "tpl!form_input", "tpl!form_captcha" ], factory); }(this, function ( $, dummy, _, tpl_field, tpl_select_option, tpl_form_select, tpl_form_textarea, tpl_form_checkbox, tpl_form_username, tpl_form_input, tpl_form_captcha ) { "use strict"; var XFORM_TYPE_MAP = { 'text-private': 'password', 'text-single': 'text', 'fixed': 'label', 'boolean': 'checkbox', 'hidden': 'hidden', 'jid-multi': 'textarea', 'list-single': 'dropdown', 'list-multi': 'dropdown' }; var afterAnimationEnd = function (el, callback) { el.classList.remove('visible'); if (_.isFunction(callback)) { callback(); } }; var isImage = function (url) { var deferred = new $.Deferred(); var img = new Image(); var timer = window.setTimeout(function () { deferred.reject(); img = null; }, 3000); img.onerror = img.onabort = function () { clearTimeout(timer); deferred.reject(); }; img.onload = function () { clearTimeout(timer); deferred.resolve(img); }; img.src = url; return deferred.promise(); }; $.expr[':'].emptyVal = function(obj){ return obj.value === ''; }; $.fn.hasScrollBar = function() { if (!$.contains(document, this.get(0))) { return false; } if(this.parent().height() < this.get(0).scrollHeight) { return true; } return false; }; $.fn.throttledHTML = _.throttle($.fn.html, 500); $.fn.addHyperlinks = function () { if (this.length > 0) { this.each(function (i, obj) { var prot, escaped_url; var $obj = $(obj); var x = $obj.html(); var list = x.match(/\b(https?:\/\/|www\.|https?:\/\/www\.)[^\s<]{2,200}\b/g ); if (list) { for (i=0; i'+ list[i] + '' ); } } $obj.html(x); _.forEach(list, function (url) { isImage(url).then(function (img) { var prot = url.indexOf('http://') === 0 || url.indexOf('https://') === 0 ? '' : 'http://'; var escaped_url = encodeURI(decodeURI(url)).replace(/[!'()]/g, escape).replace(/\*/g, "%2A"); var new_url = ''+ url + ''; img.className = 'chat-image'; x = x.replace(new_url, img.outerHTML); $obj.throttledHTML(x); }); }); }); } return this; }; $.fn.addEmoticons = function (allowed) { if (allowed) { if (this.length > 0) { this.each(function (i, obj) { var text = $(obj).html(); text = text.replace(/>:\)/g, ''); text = text.replace(/:\)/g, ''); text = text.replace(/:\-\)/g, ''); text = text.replace(/;\)/g, ''); text = text.replace(/;\-\)/g, ''); text = text.replace(/:D/g, ''); text = text.replace(/:\-D/g, ''); text = text.replace(/:P/g, ''); text = text.replace(/:\-P/g, ''); text = text.replace(/:p/g, ''); text = text.replace(/:\-p/g, ''); text = text.replace(/8\)/g, ''); text = text.replace(/:S/g, ''); text = text.replace(/:\\/g, ''); text = text.replace(/:\/ /g, ''); text = text.replace(/>:\(/g, ''); text = text.replace(/:\(/g, ''); text = text.replace(/:\-\(/g, ''); text = text.replace(/:O/g, ''); text = text.replace(/:\-O/g, ''); text = text.replace(/\=\-O/g, ''); text = text.replace(/\(\^.\^\)b/g, ''); text = text.replace(/<3/g, ''); $(obj).html(text); }); } } return this; }; var utils = { // Translation machinery // --------------------- __: function (str) { if (typeof Jed === "undefined") { return str; } // FIXME: this can be refactored to take the i18n obj as a // parameter. // Translation factory if (typeof this.i18n === "undefined") { this.i18n = locales.en; } if (typeof this.i18n === "string") { this.i18n = $.parseJSON(this.i18n); } if (typeof this.jed === "undefined") { this.jed = new Jed(this.i18n); } var t = this.jed.translate(str); if (arguments.length>1) { return t.fetch.apply(t, [].slice.call(arguments,1)); } else { return t.fetch(); } }, ___: function (str) { /* XXX: This is part of a hack to get gettext to scan strings to be * translated. Strings we cannot send to the function above because * they require variable interpolation and we don't yet have the * variables at scan time. * * See actionInfoMessages in src/converse-muc.js */ return str; }, isLocaleAvailable: function (locale, available) { /* Check whether the locale or sub locale (e.g. en-US, en) is supported. * * Parameters: * (Function) available - returns a boolean indicating whether the locale is supported */ if (available(locale)) { return locale; } else { var sublocale = locale.split("-")[0]; if (sublocale !== locale && available(sublocale)) { return sublocale; } } }, detectLocale: function (library_check) { /* Determine which locale is supported by the user's system as well * as by the relevant library (e.g. converse.js or moment.js). * * Parameters: * (Function) library_check - returns a boolean indicating whether the locale is supported */ var locale, i; if (window.navigator.userLanguage) { locale = utils.isLocaleAvailable(window.navigator.userLanguage, library_check); } if (window.navigator.languages && !locale) { for (i=0; i into consideration var options = [], j, $options, $values, value, values; if ($field.attr('type') === 'list-single' || $field.attr('type') === 'list-multi') { values = []; $values = $field.children('value'); for (j=0; j<$values.length; j++) { values.push($($values[j]).text()); } $options = $field.children('option'); for (j=0; j<$options.length; j++) { value = $($options[j]).find('value').text(); options.push(tpl_select_option({ value: value, label: $($options[j]).attr('label'), selected: _.startsWith(values, value), required: $field.find('required').length })); } return tpl_form_select({ name: $field.attr('var'), label: $field.attr('label'), options: options.join(''), multiple: ($field.attr('type') === 'list-multi'), required: $field.find('required').length }); } else if ($field.attr('type') === 'fixed') { return $('

').text($field.find('value').text()); } else if ($field.attr('type') === 'jid-multi') { return tpl_form_textarea({ name: $field.attr('var'), label: $field.attr('label') || '', value: $field.find('value').text(), required: $field.find('required').length }); } else if ($field.attr('type') === 'boolean') { return tpl_form_checkbox({ name: $field.attr('var'), type: XFORM_TYPE_MAP[$field.attr('type')], label: $field.attr('label') || '', checked: $field.find('value').text() === "1" && 'checked="1"' || '', required: $field.find('required').length }); } else if ($field.attr('type') && $field.attr('var') === 'username') { return tpl_form_username({ domain: ' @'+this.domain, name: $field.attr('var'), type: XFORM_TYPE_MAP[$field.attr('type')], label: $field.attr('label') || '', value: $field.find('value').text(), required: $field.find('required').length }); } else if ($field.attr('type')) { return tpl_form_input({ name: $field.attr('var'), type: XFORM_TYPE_MAP[$field.attr('type')], label: $field.attr('label') || '', value: $field.find('value').text(), required: $field.find('required').length }); } else { if ($field.attr('var') === 'ocr') { // Captcha return _.reduce(_.map($field.find('uri'), $.proxy(function (uri) { return tpl_form_captcha({ label: this.$field.attr('label'), name: this.$field.attr('var'), data: this.$stanza.find('data[cid="'+uri.textContent.replace(/^cid:/, '')+'"]').text(), type: uri.getAttribute('type'), required: this.$field.find('required').length }); }, {'$stanza': $stanza, '$field': $field}) ), function (memo, num) { return memo + num; }, '' ); } } } }; utils.contains.not = function (attr, query) { return function (item) { return !(utils.contains(attr, query)(item)); }; }; return utils; }));