/* This file is part of Tryton. The COPYRIGHT file at the top level of this repository contains the full copyright notices and license terms. */ (function() { 'use strict'; Sao.common = {}; Sao.common.BACKSPACE_KEYCODE = 8; Sao.common.TAB_KEYCODE = 9; Sao.common.RETURN_KEYCODE = 13; Sao.common.UP_KEYCODE = 38; Sao.common.DOWN_KEYCODE = 40; Sao.common.DELETE_KEYCODE = 46; Sao.common.F2_KEYCODE = 113; Sao.common.F3_KEYCODE = 114; Sao.common.SELECTION_NONE = 1; Sao.common.SELECTION_SINGLE = 2; // Not implemented yet Sao.common.SELECTION_MULTIPLE = 3; Sao.common.BIG_IMAGE_SIZE = Math.pow(10, 6); Sao.common.compare = function(arr1, arr2) { if (arr1.length != arr2.length) { return false; } for (var i = 0; i < arr1.length; i++) { if (arr1[i] instanceof Array && arr2[i] instanceof Array) { if (!Sao.common.compare(arr1[i], arr2[i])) { return false; } } else if (arr1[i] != arr2[i]) { return false; } } return true; }; Sao.common.contains = function(array1, array2) { for (var i = 0; i < array1.length; i++) { if (Sao.common.compare(array1[i], array2)) { return true; } } return false; }; // Find the intersection of two arrays. // The arrays must be sorted. Sao.common.intersect = function(a, b) { var ai = 0, bi = 0; var result = []; while (ai < a.length && bi < b.length) { if (a[ai] < b[bi]) { ai++; } else if (a[ai] > b[bi]) { bi++; } else { result.push(a[ai]); ai++; bi++; } } return result; }; // Cartesian product Sao.common.product = function(array, repeat) { repeat = repeat || 1; var pools = []; var i = 0; while (i < repeat) { pools = pools.concat(array); i++; } var result = [[]]; pools.forEach(function(pool) { var tmp = []; result.forEach(function(x) { pool.forEach(function(y) { tmp.push(x.concat([y])); }); }); result = tmp; }); return result; }; Sao.common.selection = function(title, values, alwaysask) { if (alwaysask === undefined) { alwaysask = false; } var prm = jQuery.Deferred(); var keys = Object.keys(values).sort(); if ((keys.length == 1) && (!alwaysask)) { var key = keys[0]; prm.resolve(values[key]); return prm; } var dialog = new Sao.Dialog( title || 'Your selection:', 'selection-dialog'); keys.forEach(function(k, i) { jQuery('
', { 'class': 'checkbox' }).append(jQuery('