From 3f31e7ac0cd426bc250b7c61889a1edaec819f69 Mon Sep 17 00:00:00 2001 From: llj Date: Tue, 5 Feb 2013 18:05:24 +0800 Subject: [PATCH] added missing new file --- media/js/base.js | 337 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 337 insertions(+) create mode 100644 media/js/base.js diff --git a/media/js/base.js b/media/js/base.js new file mode 100644 index 0000000000..bdce1764de --- /dev/null +++ b/media/js/base.js @@ -0,0 +1,337 @@ +$('.top-bar-con .manage').css('width', $('.top-bar-con').width() - $('.top-bar-con .account').width() - 30); +$('#title-panel, #left-panel, #right-panel').each(function() { // for ie 7 + if ($(this).children().length == 0) { + $(this).addClass('hide'); + } +}); +$('#tabs').tabs({cookie:{expires:1}}); +$('#tabs-nav a').focus(function() { $(this).blur(); }); + +$('input, textarea').placeholder(); + +// handle messages +if ($('.messages')[0]) { + $('#main').append($('.messages')); + $('.messages').css({'left':($(window).width() - $('.messages').width())/2, 'top':10}).removeClass('hide'); + setTimeout(function() { $('.messages').addClass('hide'); }, 10000); +} + +//highlight the tr when mouse hover on it +$("table tr:gt(0)").hover( + function() { + $(this).addClass('hl'); + }, + function() { + $(this).removeClass('hl'); + } +); +$('#lang-context').click(function() { + if ($(this).attr('data') == 'no-popup') { + $(this).parent().css('position', 'relative'); + $('#lang-context-selector').removeClass('hide'); + $(this).attr('data', 'has-popup'); + } else { + $('#lang-context-selector').addClass('hide'); + $(this).attr('data', 'no-popup'); + } + return false; + }).focus(function() { $(this).blur(); }); +$(document).click(function(e) { + var element = e.target || e.srcElement; + if (element.id != 'lang-context-selector' && element.id != 'lang-context') { + $('#lang-context-selector').addClass('hide'); + $('#lang-context').attr('data', 'no-popup'); + } +}); + +//add op confirm popup +function addConfirmTo(ele, confirm_hd, confirm_con) { + ele.click(function() { + var con = '

' + confirm_hd + '

', + target = ''; + if ($(this).data('target')) { + target = ' ' + $(this).data('target') + ''; + } + con += '

' + confirm_con + target + ' ?

'; + $('#confirm-con').html(con); + $('#confirm-popup').modal({appendTo:'#main'}); + $('#confirm-yes') + .data('url', $(this).data('url')) + .click(function() { location.href = $(this).data('url'); }); + return false;//in case ele is '' + }); +} + +/* + * func: add autocomplete to some input ele + * @param ele_id: autocomplete is added to this ele(ment), e.g-'#xxx' + * @param container_id: id of autocomplete's container, often container of element above + * @param data: tip data in array, e.g- ['xx', 'yy'] + */ +function addAutocomplete(ele_id, container_id, data) { + function split(val) { + return val.split(/,\s*/); + } + function extractLast(term) { + return split(term).pop(); + } + + $(ele_id) + .bind("keydown", function(event) { + if (event.keyCode === $.ui.keyCode.TAB && + $(this).data("autocomplete").menu.active) { + event.preventDefault(); + } + }) + .bind('autocompleteopen', function(e, ui) { + $(ele_id).autocomplete('widget').css({'max-width':$(ele_id).width(), 'max-height':$(window).height() + $(window).scrollTop() - $(ele_id).offset().top - $(ele_id).outerHeight()}); + }) + .autocomplete({ + appendTo: container_id, + autoFocus: true, + delay: 100, + minLength: 0, + source: function(request, response) { + var matcher = new RegExp($.ui.autocomplete.escapeRegex(extractLast(request.term)), "i"); + response($.grep(data, function(value) { + return matcher.test(value.value); + })); + }, + focus: function() { + return false; + }, + select: function(event, ui) { + var terms = split(this.value); + terms.pop(); + terms.push(ui.item.label); + terms.push(""); + this.value = terms.join(", "); + return false; + } + }); +} + +/* + * func: add autocomplete for `@` to some input ele + * @param ele_id: autocomplete is added to this ele(ment), e.g-'#xxx' + * @param ele_css: {'xx':'xxx'}, styles to be applied to ele_cp + * @param container_id: id of autocomplete's container, often container of element above + */ +function addAtAutocomplete(ele_id, container_id, gids, aj_url, ele_css) { + var pos = ''; // cursor position + var cursor_at_end; // Boolean. if cursor at the end or in the middle. + var end_str = ''; // str after '@' when '@' is inserted into the middle of the ele's value + var ele_scrollTop = 0; // scrollTop of ele. defined to fix a bug for ff (after selecting a item, it turns into 0) + + /* + * make a copy of ele, in order to get coordinates of '@' + * make sure ele_cp has the same 'width', 'font', 'line-height', 'border', 'padding', and etc, with ele + */ + var ele_cp = '
'; + $('#main').append(ele_cp); + ele_cp = $(ele_id + '-cp'); + ele_cp.css({'position':'absolute', 'left':0, 'bottom':0, 'z-index':-10000, 'visibility':'hidden', 'white-space':'pre-wrap'}).css(ele_css); + + $(ele_id) + .bind("keydown", function(event) { + if (event.keyCode === $.ui.keyCode.TAB && + $(this).data("autocomplete").menu.active) { + event.preventDefault(); + } + }) + .bind('keypress', function(e) { + if (String.fromCharCode(e.keyCode || e.charCode) == '@') { + var str = ''; + pos = getCaretPos($(this)[0]); + if (pos == $(this).val().length) { + cursor_at_end = true; + str = $(this).val(); + } else { + cursor_at_end = false; + end_str = $(this).val().substring(pos); + str = $(this).val().substring(0, pos); + } + ele_cp.html(str.replace(//g, '>').replace(/`/g, '`').replace(/"/g, '"').replace(/\r\n|\r|\n/g, "
") + '@'); + var line_height = parseInt(ele_cp.css('line-height')), + at_pos = $(ele_id + '-at').position(), + x = at_pos.left, + y = at_pos.top + line_height - 2 - $(ele_id).scrollTop(); + $(this).autocomplete("option", "position", { my : "left top", at: "left top", offset: x + ' ' + y, collision: 'fit'}); + $(this).bind('autocompleteopen', function(e, ui) { + var menu = $(this).autocomplete('widget'), + menu_height = menu.outerHeight(); + if ($(this).offset().top + y + menu_height > $(window).height() + $(window).scrollTop()) { + y = y - menu_height - line_height; + menu.offset({left: $(ele_id).offset().left + x, top:$(ele_id).offset().top + y}); + } + }); + ele_scrollTop = $(ele_id).scrollTop(); + } + }) + .autocomplete({ + appendTo: container_id, + autoFocus: true, + delay: 0, + minLength: 0, + source: function(request, response) { + if (pos === '') { + return false; + } + if ($(ele_id).val().charAt(pos) == '@' && getCaretPos($(ele_id)[0]) > pos) { // cursor is at the right of the current @ + var request_term = ''; + if (cursor_at_end) { + request_term = request.term.substring(pos + 1); + } else { + request_term = request.term.slice(pos + 1, - end_str.length); + } + $.ajax({ + url: aj_url, + dataType: "json", + data: { + gids: gids, + name_startsWith: request_term + }, + success: function(data) { + response($.map(data, function(value) { + return { + label: value.contact_name, + value: value.contact_name + } + })); + } + }); + } else { + response(null); // when cursor is at the left of current @ or @ is deleted + } + }, + focus: function(e, ui) { + return false; + }, + select: function(e, ui) { + var str = $(this).val().substring(0, pos + 1) + ui.item.label + ' '; + if (cursor_at_end) { + $(this).val(str); + } else { + $(this).val(str + end_str); + setCaretPos($(this)[0], pos + ui.item.label.length + 2); + } + $(ele_id).scrollTop(ele_scrollTop); // fix ff problem: ele scrolls to the top most. + return false; + } + }); + $(ele_id).autocomplete('widget').css({'max-width':$(ele_id).width()/2, 'max-height':$(document).height() - $(ele_id).offset().top - $(ele_id).height()}); +} + +function getCaretPos(inputor) { + var end, endRange, len, normalizedValue, pos, range, start, textInputRange; + if (document.selection) { + range = document.selection.createRange(); + pos = 0; + if (range && range.parentElement() === inputor) { + normalizedValue = inputor.value.replace(/\r\n/g, "\n"); + len = normalizedValue.length; + textInputRange = inputor.createTextRange(); + textInputRange.moveToBookmark(range.getBookmark()); + endRange = inputor.createTextRange(); + endRange.collapse(false); + if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) { + start = end = len; + } else { + start = -textInputRange.moveStart("character", -len); + end = -textInputRange.moveEnd("character", -len); + } + } + } else { + start = inputor.selectionStart; + } + return start; +} + +function setCaretPos(inputor, pos) { + var range; + if (document.selection) { + range = inputor.createTextRange(); + range.move("character", pos); + return range.select(); + } else { + return inputor.setSelectionRange(pos, pos); + } +} + +function filesizeformat(bytes, precision) { + var kilobyte = 1024; + var megabyte = kilobyte * 1024; + var gigabyte = megabyte * 1024; + var terabyte = gigabyte * 1024; + + var precision = precision || 0; + + if ((bytes >= 0) && (bytes < kilobyte)) { + return bytes + ' B'; + + } else if ((bytes >= kilobyte) && (bytes < megabyte)) { + return (bytes / kilobyte).toFixed(precision) + ' KB'; + + } else if ((bytes >= megabyte) && (bytes < gigabyte)) { + return (bytes / megabyte).toFixed(precision) + ' MB'; + + } else if ((bytes >= gigabyte) && (bytes < terabyte)) { + return (bytes / gigabyte).toFixed(precision) + ' GB'; + + } else if (bytes >= terabyte) { + return (bytes / terabyte).toFixed(precision) + ' TB'; + + } else { + return bytes + ' B'; + } +} + +function e(str) { + return encodeURIComponent(str); +} + +function prepareCSRFToken(xhr, settings) { + function getCookie(name) { + var cookieValue = null; + if (document.cookie && document.cookie != '') { + var cookies = document.cookie.split(';'); + for (var i = 0; i < cookies.length; i++) { + var cookie = jQuery.trim(cookies[i]); + // Does this cookie string begin with the name we want? + if (cookie.substring(0, name.length + 1) == (name + '=')) { + cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); + break; + } + } + } + return cookieValue; + } + if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) { + // Only send the token to relative URLs i.e. locally. + xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken')); + } +} + +function apply_form_error(formid, error_msg) { + $("#" + formid + " .error").html(error_msg).removeClass('hide'); + $("#simplemodal-container").css({'height':'auto'}); +} + +// show feedback +function feedback(con, type, time) { + var time = time || 5000; + if ($('.messages')[0]) { + $('.messages').html('
  • ' + con + '
  • '); + } else { + var html = ''; + $('#main').append(html); + } + $('.messages').css({'left':($(window).width() - $('.messages').width())/2, 'top':10}).removeClass('hide'); + setTimeout(function() { $('.messages').addClass('hide'); }, time); +} +function disable(btn) { + btn.attr('disabled', 'disabled').addClass('btn-disabled'); +} +function enable(btn) { + btn.removeAttr('disabled').removeClass('btn-disabled'); +}