Best JavaScript code snippet using wpt
jquery.multi-select.js
Source:jquery.multi-select.js
1/*2 * MultiSelect v0.83 * Copyright (c) 2012 Louis Cuny4 *5 * This program is free software. It comes without any warranty, to6 * the extent permitted by applicable law. You can redistribute it7 * and/or modify it under the terms of the Do What The Fuck You Want8 * To Public License, Version 2, as published by Sam Hocevar. See9 * http://sam.zoy.org/wtfpl/COPYING for more details.10 */11(function ($) {12 var msMethods = {13 'init': function (options) {14 this.settings = {15 disabledClass: 'disabled',16 selectCallbackOnInit: false,17 keepOrder: false18 };19 if (options) {20 this.settings = $.extend(this.settings, options);21 }22 var multiSelects = this;23 multiSelects.css('position', 'absolute').css('left', '-9999px');24 return multiSelects.each(function () {25 var ms = $(this);26 if (ms.next('.ms-container').length == 0) {27 ms.attr('id', ms.attr('id') ? ms.attr('id') : 'ms-' + Math.ceil(Math.random() * 1000));28 var container = $('<div id="ms-' + ms.attr('id') + '" class="ms-container"></div>'),29 selectableContainer = $('<div class="ms-selectable"></div>'),30 selectedContainer = $('<div class="ms-selection"></div>'),31 selectableUl = $('<ul class="ms-list"></ul>'),32 selectedUl = $('<ul class="ms-list"></ul>');33 ms.data('settings', multiSelects.settings);34 var optgroupLabel = null,35 optgroupId = null,36 optgroupCpt = 0,37 scroll = 0;38 ms.find('optgroup,option').each(function () {39 if ($(this).is('optgroup')) {40 optgroupLabel = $(this).attr('label');41 optgroupId = 'ms-' + ms.attr('id') + '-optgroup-' + optgroupCpt;42 selectableUl.append($('<li class="ms-optgroup-container" id="' +43 optgroupId + '"><ul class="ms-optgroup"><li class="ms-optgroup-label">' +44 optgroupLabel + '</li></ul></li>'));45 optgroupCpt++;46 }47 if ($(this).is("option:not(option[value=''])")) {48 var klass = $(this).attr('class') ? ' ' + $(this).attr('class') : '';49 var selectableLi = $('<li class="ms-elem-selectable' + klass + '" ms-value="' + $(this).val() + '">' + $(this).text() + '</li>');50 if ($(this).attr('title'))51 selectableLi.attr('title', $(this).attr('title'));52 if ($(this).attr('disabled') || ms.attr('disabled')) {53 selectableLi.attr('disabled', 'disabled');54 selectableLi.addClass(multiSelects.settings.disabledClass);55 }56 selectableLi.click(function () {57 ms.multiSelect('select', $(this).attr('ms-value'));58 });59 var container = optgroupId ? selectableUl.children('#' + optgroupId).find('ul').first() : selectableUl;60 container.append(selectableLi);61 }62 });63 if (multiSelects.settings.selectableHeader) {64 selectableContainer.append(multiSelects.settings.selectableHeader);65 }66 selectableContainer.append(selectableUl);67 if (multiSelects.settings.selectedHeader) {68 selectedContainer.append(multiSelects.settings.selectedHeader);69 }70 selectedContainer.append(selectedUl);71 container.append(selectableContainer);72 container.append(selectedContainer);73 ms.after(container);74 ms.find('option:selected').each(function () {75 ms.multiSelect('select', $(this).val(), 'init');76 });77 $('.ms-elem-selectable', selectableUl).on('mouseenter', function () {78 $('li', container).removeClass('ms-hover');79 $(this).addClass('ms-hover');80 }).on('mouseout', function () {81 $('li', container).removeClass('ms-hover');82 });83 selectableUl.on('focusin', function () {84 $(this).addClass('ms-focus');85 selectedUl.focusout();86 }).on('focusout', function () {87 $(this).removeClass('ms-focus');88 $('li', container).removeClass('ms-hover');89 });90 selectedUl.on('focusin', function () {91 $(this).addClass('ms-focus');92 }).on('focusout', function () {93 $(this).removeClass('ms-focus');94 $('li', container).removeClass('ms-hover');95 });96 ms.on('focusin', function () {97 selectableUl.focus();98 }).on('focusout', function () {99 selectableUl.removeClass('ms-focus');100 selectedUl.removeClass('ms-focus');101 });102 ms.onKeyDown = function (e, keyContainer) {103 var selectables = $('.' + keyContainer + ' li:visible:not(.ms-optgroup-label, .ms-optgroup-container)', container),104 selectablesLength = selectables.length,105 selectableFocused = $('.' + keyContainer + ' li.ms-hover', container),106 selectableFocusedIndex = $('.' + keyContainer + ' li:visible:not(.ms-optgroup-label, .ms-optgroup-container)', container).index(selectableFocused),107 liHeight = selectables.first().outerHeight(),108 numberOfItemsDisplayed = Math.ceil(container.outerHeight() / liHeight),109 scrollStart = Math.ceil(numberOfItemsDisplayed / 4);110 selectables.removeClass('ms-hover');111 if (e.keyCode == 32) { // space112 var method = keyContainer == 'ms-selectable' ? 'select' : 'deselect';113 ms.multiSelect(method, selectableFocused.first().attr('ms-value'));114 } else if (e.keyCode == 40) { // Down115 var nextIndex = (selectableFocusedIndex + 1 != selectablesLength) ? selectableFocusedIndex + 1 : 0,116 nextSelectableLi = selectables.eq(nextIndex);117 nextSelectableLi.addClass('ms-hover');118 if (nextIndex > scrollStart) {119 scroll += liHeight;120 } else if (nextIndex == 0) {121 scroll = 0;122 }123 $('.' + keyContainer + ' ul', container).scrollTop(scroll);124 } else if (e.keyCode == 38) { // Up125 var prevIndex = (selectableFocusedIndex - 1 >= 0) ? selectableFocusedIndex - 1 : selectablesLength - 1,126 prevSelectableLi = selectables.eq(prevIndex);127 selectables.removeClass('ms-hover');128 prevSelectableLi.addClass('ms-hover');129 if (selectablesLength - prevIndex + 1 < scrollStart) {130 scroll = liHeight * (selectablesLength - scrollStart);131 } else {132 scroll -= liHeight;133 }134 $('.' + keyContainer + ' ul', container).scrollTop(scroll);135 } else if (e.keyCode == 37 || e.keyCode == 39) { // Right and Left136 if (selectableUl.hasClass('ms-focus')) {137 selectableUl.focusout();138 selectedUl.focusin();139 } else {140 selectableUl.focusin();141 selectedUl.focusout();142 }143 }144 }145 ms.on('keydown', function (e) {146 if (ms.is(':focus')) {147 var keyContainer = selectableUl.hasClass('ms-focus') ? 'ms-selectable' : 'ms-selection';148 ms.onKeyDown(e, keyContainer);149 }150 });151 }152 });153 },154 'refresh': function () {155 $("#ms-" + $(this).attr("id")).remove();156 $(this).multiSelect("init", $(this).data("settings"));157 },158 'select': function (value, method) {159 var ms = this,160 selectedOption = ms.find('option[value="' + value + '"]'),161 text = selectedOption.text(),162 klass = selectedOption.attr('class'),163 titleAttr = selectedOption.attr('title');164 var selectedLi = $('<li class="ms-elem-selected' + (klass ? ' ' + klass : '') + '" ms-value="' + value + '">' + text + '</li>'),165 selectableUl = $('#ms-' + ms.attr('id') + ' .ms-selectable ul'),166 selectedUl = $('#ms-' + ms.attr('id') + ' .ms-selection ul'),167 selectableLi = selectableUl.children('li[ms-value="' + value + '"]'),168 haveToSelect = null;169 if (method == 'init') {170 haveToSelect = !selectableLi.hasClass(ms.data('settings').disabledClass) && selectedOption.prop('selected');171 } else {172 haveToSelect = !selectableLi.hasClass(ms.data('settings').disabledClass);173 ms.focus();174 }175 if (haveToSelect && value && value != '' && selectedUl.children('li[ms-value="' + value + '"]').length == 0) {176 var parentOptgroup = selectableLi.parent('.ms-optgroup');177 if (parentOptgroup.length > 0)178 if (parentOptgroup.children('.ms-elem-selectable:not(:hidden)').length == 1)179 parentOptgroup.children('.ms-optgroup-label').hide();180 selectableLi.addClass('ms-selected');181 selectableLi.hide();182 selectedOption.prop('selected', true);183 if (titleAttr) {184 selectedLi.attr('title', titleAttr)185 }186 if (selectableLi.hasClass(ms.data('settings').disabledClass)) {187 selectedLi.addClass(ms.data('settings').disabledClass);188 } else {189 selectedLi.click(function () {190 ms.multiSelect('deselect', $(this).attr('ms-value'));191 });192 }193 var selectedUlLis = selectedUl.children('.ms-elem-selected');194 if (method != 'init' && ms.data('settings').keepOrder && selectedUlLis.length > 0) {195 var getIndexOf = function (value) {196 elems = selectableUl.children('.ms-elem-selectable');197 return (elems.index(elems.closest('[ms-value="' + value + '"]')));198 }199 var index = getIndexOf(selectedLi.attr('ms-value'));200 if (index == 0)201 selectedUl.prepend(selectedLi);202 else {203 for (i = index - 1; i >= 0; i--) {204 if (selectedUlLis[i] && getIndexOf($(selectedUlLis[i]).attr('ms-value')) < index) {205 $(selectedUlLis[i]).after(selectedLi);206 break;207 } else if (i == 0) {208 $(selectedUlLis[i]).before(selectedLi);209 }210 }211 }212 } else {213 selectedUl.append(selectedLi);214 }215 selectedLi.on('mouseenter', function () {216 $('li', selectedUl).removeClass('ms-hover');217 $(this).addClass('ms-hover');218 }).on('mouseout', function () {219 $('li', selectedUl).removeClass('ms-hover');220 });221 if (ms.find("option[value='']")) {222 ms.find("option[value='']").prop('selected', false);223 }224 if (method == "select_all" && parentOptgroup.children('.ms-elem-selectable').length > 0) {225 parentOptgroup.children('.ms-optgroup-label').hide();226 }227 if (method != 'init' || ms.data('settings').selectCallbackOnInit) {228 ms.trigger('change');229 selectedUl.trigger('change');230 selectableUl.trigger('change');231 if (typeof ms.data('settings').afterSelect == 'function' &&232 (method != 'init' || ms.data('settings').selectCallbackOnInit)) {233 ms.data('settings').afterSelect.call(this, value, text);234 }235 }236 }237 },238 'deselect': function (value) {239 var ms = this,240 selectedUl = $('#ms-' + ms.attr('id') + ' .ms-selection ul'),241 selectedOption = ms.find('option[value="' + value + '"]'),242 selectedLi = selectedUl.children('li[ms-value="' + value + '"]');243 if (selectedLi) {244 selectedUl.focusin();245 var selectableUl = $('#ms-' + ms.attr('id') + ' .ms-selectable ul'),246 selectedUl = $('#ms-' + ms.attr('id') + ' .ms-selection ul'),247 selectableLi = selectableUl.children('li[ms-value="' + value + '"]'),248 selectedLi = selectedUl.children('li[ms-value="' + value + '"]');249 var parentOptgroup = selectableLi.parent('.ms-optgroup');250 if (parentOptgroup.length > 0) {251 parentOptgroup.children('.ms-optgroup-label').addClass('ms-collapse').show();252 parentOptgroup.children('.ms-elem-selectable:not(.ms-selected)').show();253 }254 selectedOption.prop('selected', false);255 selectableLi.show();256 selectableLi.removeClass('ms-selected');257 selectedLi.remove();258 selectedUl.trigger('change');259 selectableUl.trigger('change');260 ms.trigger('change');261 if (typeof ms.data('settings').afterDeselect == 'function') {262 ms.data('settings').afterDeselect.call(this, value, selectedLi.text());263 }264 }265 },266 'select_all': function (visible) {267 var ms = this,268 selectableUl = $('#ms-' + ms.attr('id') + ' .ms-selectable ul');269 ms.find("option:not(option[value=''])").each(function () {270 var value = $(this).val();271 if (visible) {272 var selectableLi = selectableUl.children('li[ms-value="' + value + '"]');273 if (selectableLi.filter(':visible').length > 0) {274 ms.multiSelect('select', value, 'select_all');275 }276 } else {277 ms.multiSelect('select', value, 'select_all');278 }279 });280 },281 'deselect_all': function () {282 var ms = this;283 ms.find("option:not(option[value=''])").each(function () {284 ms.multiSelect('deselect', $(this).val(), 'deselect_all');285 });286 }287 };288 $.fn.multiSelect = function (method) {289 if (msMethods[method]) {290 return msMethods[method].apply(this, Array.prototype.slice.call(arguments, 1));291 } else if (typeof method === 'object' || !method) {292 return msMethods.init.apply(this, arguments);293 } else {294 if (console.log) console.log('Method ' + method + ' does not exist on jquery.multiSelect');295 }296 return false;297 };...
e14a091_part_1_jquery.multi-select_1.js
Source:e14a091_part_1_jquery.multi-select_1.js
1/*2 * MultiSelect v0.83 * Copyright (c) 2012 Louis Cuny4 *5 * This program is free software. It comes without any warranty, to6 * the extent permitted by applicable law. You can redistribute it7 * and/or modify it under the terms of the Do What The Fuck You Want8 * To Public License, Version 2, as published by Sam Hocevar. See9 * http://sam.zoy.org/wtfpl/COPYING for more details.10 */11(function ($) {12 var msMethods = {13 'init': function (options) {14 this.settings = {15 disabledClass: 'disabled',16 selectCallbackOnInit: false,17 keepOrder: false18 };19 if (options) {20 this.settings = $.extend(this.settings, options);21 }22 var multiSelects = this;23 multiSelects.css('position', 'absolute').css('left', '-9999px');24 return multiSelects.each(function () {25 var ms = $(this);26 if (ms.next('.ms-container').length == 0) {27 ms.attr('id', ms.attr('id') ? ms.attr('id') : 'ms-' + Math.ceil(Math.random() * 1000));28 var container = $('<div id="ms-' + ms.attr('id') + '" class="ms-container"></div>'),29 selectableContainer = $('<div class="ms-selectable"></div>'),30 selectedContainer = $('<div class="ms-selection"></div>'),31 selectableUl = $('<ul class="ms-list"></ul>'),32 selectedUl = $('<ul class="ms-list"></ul>');33 ms.data('settings', multiSelects.settings);34 var optgroupLabel = null,35 optgroupId = null,36 optgroupCpt = 0,37 scroll = 0;38 ms.find('optgroup,option').each(function () {39 if ($(this).is('optgroup')) {40 optgroupLabel = $(this).attr('label');41 optgroupId = 'ms-' + ms.attr('id') + '-optgroup-' + optgroupCpt;42 selectableUl.append($('<li class="ms-optgroup-container" id="' +43 optgroupId + '"><ul class="ms-optgroup"><li class="ms-optgroup-label">' +44 optgroupLabel + '</li></ul></li>'));45 optgroupCpt++;46 }47 if ($(this).is("option:not(option[value=''])")) {48 var klass = $(this).attr('class') ? ' ' + $(this).attr('class') : '';49 var selectableLi = $('<li class="ms-elem-selectable' + klass + '" ms-value="' + $(this).val() + '">' + $(this).text() + '</li>');50 if ($(this).attr('title'))51 selectableLi.attr('title', $(this).attr('title'));52 if ($(this).attr('disabled') || ms.attr('disabled')) {53 selectableLi.attr('disabled', 'disabled');54 selectableLi.addClass(multiSelects.settings.disabledClass);55 }56 selectableLi.click(function () {57 ms.multiSelect('select', $(this).attr('ms-value'));58 });59 var container = optgroupId ? selectableUl.children('#' + optgroupId).find('ul').first() : selectableUl;60 container.append(selectableLi);61 }62 });63 if (multiSelects.settings.selectableHeader) {64 selectableContainer.append(multiSelects.settings.selectableHeader);65 }66 selectableContainer.append(selectableUl);67 if (multiSelects.settings.selectedHeader) {68 selectedContainer.append(multiSelects.settings.selectedHeader);69 }70 selectedContainer.append(selectedUl);71 container.append(selectableContainer);72 container.append(selectedContainer);73 ms.after(container);74 ms.find('option:selected').each(function () {75 ms.multiSelect('select', $(this).val(), 'init');76 });77 $('.ms-elem-selectable', selectableUl).on('mouseenter', function () {78 $('li', container).removeClass('ms-hover');79 $(this).addClass('ms-hover');80 }).on('mouseout', function () {81 $('li', container).removeClass('ms-hover');82 });83 selectableUl.on('focusin', function () {84 $(this).addClass('ms-focus');85 selectedUl.focusout();86 }).on('focusout', function () {87 $(this).removeClass('ms-focus');88 $('li', container).removeClass('ms-hover');89 });90 selectedUl.on('focusin', function () {91 $(this).addClass('ms-focus');92 }).on('focusout', function () {93 $(this).removeClass('ms-focus');94 $('li', container).removeClass('ms-hover');95 });96 ms.on('focusin', function () {97 selectableUl.focus();98 }).on('focusout', function () {99 selectableUl.removeClass('ms-focus');100 selectedUl.removeClass('ms-focus');101 });102 ms.onKeyDown = function (e, keyContainer) {103 var selectables = $('.' + keyContainer + ' li:visible:not(.ms-optgroup-label, .ms-optgroup-container)', container),104 selectablesLength = selectables.length,105 selectableFocused = $('.' + keyContainer + ' li.ms-hover', container),106 selectableFocusedIndex = $('.' + keyContainer + ' li:visible:not(.ms-optgroup-label, .ms-optgroup-container)', container).index(selectableFocused),107 liHeight = selectables.first().outerHeight(),108 numberOfItemsDisplayed = Math.ceil(container.outerHeight() / liHeight),109 scrollStart = Math.ceil(numberOfItemsDisplayed / 4);110 selectables.removeClass('ms-hover');111 if (e.keyCode == 32) { // space112 var method = keyContainer == 'ms-selectable' ? 'select' : 'deselect';113 ms.multiSelect(method, selectableFocused.first().attr('ms-value'));114 } else if (e.keyCode == 40) { // Down115 var nextIndex = (selectableFocusedIndex + 1 != selectablesLength) ? selectableFocusedIndex + 1 : 0,116 nextSelectableLi = selectables.eq(nextIndex);117 nextSelectableLi.addClass('ms-hover');118 if (nextIndex > scrollStart) {119 scroll += liHeight;120 } else if (nextIndex == 0) {121 scroll = 0;122 }123 $('.' + keyContainer + ' ul', container).scrollTop(scroll);124 } else if (e.keyCode == 38) { // Up125 var prevIndex = (selectableFocusedIndex - 1 >= 0) ? selectableFocusedIndex - 1 : selectablesLength - 1,126 prevSelectableLi = selectables.eq(prevIndex);127 selectables.removeClass('ms-hover');128 prevSelectableLi.addClass('ms-hover');129 if (selectablesLength - prevIndex + 1 < scrollStart) {130 scroll = liHeight * (selectablesLength - scrollStart);131 } else {132 scroll -= liHeight;133 }134 $('.' + keyContainer + ' ul', container).scrollTop(scroll);135 } else if (e.keyCode == 37 || e.keyCode == 39) { // Right and Left136 if (selectableUl.hasClass('ms-focus')) {137 selectableUl.focusout();138 selectedUl.focusin();139 } else {140 selectableUl.focusin();141 selectedUl.focusout();142 }143 }144 }145 ms.on('keydown', function (e) {146 if (ms.is(':focus')) {147 var keyContainer = selectableUl.hasClass('ms-focus') ? 'ms-selectable' : 'ms-selection';148 ms.onKeyDown(e, keyContainer);149 }150 });151 }152 });153 },154 'refresh': function () {155 $("#ms-" + $(this).attr("id")).remove();156 $(this).multiSelect("init", $(this).data("settings"));157 },158 'select': function (value, method) {159 var ms = this,160 selectedOption = ms.find('option[value="' + value + '"]'),161 text = selectedOption.text(),162 klass = selectedOption.attr('class'),163 titleAttr = selectedOption.attr('title');164 var selectedLi = $('<li class="ms-elem-selected' + (klass ? ' ' + klass : '') + '" ms-value="' + value + '">' + text + '</li>'),165 selectableUl = $('#ms-' + ms.attr('id') + ' .ms-selectable ul'),166 selectedUl = $('#ms-' + ms.attr('id') + ' .ms-selection ul'),167 selectableLi = selectableUl.children('li[ms-value="' + value + '"]'),168 haveToSelect = null;169 if (method == 'init') {170 haveToSelect = !selectableLi.hasClass(ms.data('settings').disabledClass) && selectedOption.prop('selected');171 } else {172 haveToSelect = !selectableLi.hasClass(ms.data('settings').disabledClass);173 ms.focus();174 }175 if (haveToSelect && value && value != '' && selectedUl.children('li[ms-value="' + value + '"]').length == 0) {176 var parentOptgroup = selectableLi.parent('.ms-optgroup');177 if (parentOptgroup.length > 0)178 if (parentOptgroup.children('.ms-elem-selectable:not(:hidden)').length == 1)179 parentOptgroup.children('.ms-optgroup-label').hide();180 selectableLi.addClass('ms-selected');181 selectableLi.hide();182 selectedOption.prop('selected', true);183 if (titleAttr) {184 selectedLi.attr('title', titleAttr)185 }186 if (selectableLi.hasClass(ms.data('settings').disabledClass)) {187 selectedLi.addClass(ms.data('settings').disabledClass);188 } else {189 selectedLi.click(function () {190 ms.multiSelect('deselect', $(this).attr('ms-value'));191 });192 }193 var selectedUlLis = selectedUl.children('.ms-elem-selected');194 if (method != 'init' && ms.data('settings').keepOrder && selectedUlLis.length > 0) {195 var getIndexOf = function (value) {196 elems = selectableUl.children('.ms-elem-selectable');197 return (elems.index(elems.closest('[ms-value="' + value + '"]')));198 }199 var index = getIndexOf(selectedLi.attr('ms-value'));200 if (index == 0)201 selectedUl.prepend(selectedLi);202 else {203 for (i = index - 1; i >= 0; i--) {204 if (selectedUlLis[i] && getIndexOf($(selectedUlLis[i]).attr('ms-value')) < index) {205 $(selectedUlLis[i]).after(selectedLi);206 break;207 } else if (i == 0) {208 $(selectedUlLis[i]).before(selectedLi);209 }210 }211 }212 } else {213 selectedUl.append(selectedLi);214 }215 selectedLi.on('mouseenter', function () {216 $('li', selectedUl).removeClass('ms-hover');217 $(this).addClass('ms-hover');218 }).on('mouseout', function () {219 $('li', selectedUl).removeClass('ms-hover');220 });221 if (ms.find("option[value='']")) {222 ms.find("option[value='']").prop('selected', false);223 }224 if (method == "select_all" && parentOptgroup.children('.ms-elem-selectable').length > 0) {225 parentOptgroup.children('.ms-optgroup-label').hide();226 }227 if (method != 'init' || ms.data('settings').selectCallbackOnInit) {228 ms.trigger('change');229 selectedUl.trigger('change');230 selectableUl.trigger('change');231 if (typeof ms.data('settings').afterSelect == 'function' &&232 (method != 'init' || ms.data('settings').selectCallbackOnInit)) {233 ms.data('settings').afterSelect.call(this, value, text);234 }235 }236 }237 },238 'deselect': function (value) {239 var ms = this,240 selectedUl = $('#ms-' + ms.attr('id') + ' .ms-selection ul'),241 selectedOption = ms.find('option[value="' + value + '"]'),242 selectedLi = selectedUl.children('li[ms-value="' + value + '"]');243 if (selectedLi) {244 selectedUl.focusin();245 var selectableUl = $('#ms-' + ms.attr('id') + ' .ms-selectable ul'),246 selectedUl = $('#ms-' + ms.attr('id') + ' .ms-selection ul'),247 selectableLi = selectableUl.children('li[ms-value="' + value + '"]'),248 selectedLi = selectedUl.children('li[ms-value="' + value + '"]');249 var parentOptgroup = selectableLi.parent('.ms-optgroup');250 if (parentOptgroup.length > 0) {251 parentOptgroup.children('.ms-optgroup-label').addClass('ms-collapse').show();252 parentOptgroup.children('.ms-elem-selectable:not(.ms-selected)').show();253 }254 selectedOption.prop('selected', false);255 selectableLi.show();256 selectableLi.removeClass('ms-selected');257 selectedLi.remove();258 selectedUl.trigger('change');259 selectableUl.trigger('change');260 ms.trigger('change');261 if (typeof ms.data('settings').afterDeselect == 'function') {262 ms.data('settings').afterDeselect.call(this, value, selectedLi.text());263 }264 }265 },266 'select_all': function (visible) {267 var ms = this,268 selectableUl = $('#ms-' + ms.attr('id') + ' .ms-selectable ul');269 ms.find("option:not(option[value=''])").each(function () {270 var value = $(this).val();271 if (visible) {272 var selectableLi = selectableUl.children('li[ms-value="' + value + '"]');273 if (selectableLi.filter(':visible').length > 0) {274 ms.multiSelect('select', value, 'select_all');275 }276 } else {277 ms.multiSelect('select', value, 'select_all');278 }279 });280 },281 'deselect_all': function () {282 var ms = this;283 ms.find("option:not(option[value=''])").each(function () {284 ms.multiSelect('deselect', $(this).val(), 'deselect_all');285 });286 }287 };288 $.fn.multiSelect = function (method) {289 if (msMethods[method]) {290 return msMethods[method].apply(this, Array.prototype.slice.call(arguments, 1));291 } else if (typeof method === 'object' || !method) {292 return msMethods.init.apply(this, arguments);293 } else {294 if (console.log) console.log('Method ' + method + ' does not exist on jquery.multiSelect');295 }296 return false;297 };...
basic.js
Source:basic.js
1var Stream = require('stream')2var tap = require('tap')3var MS = require('../mute.js')4// some marker objects5var END = {}6var PAUSE = {}7var RESUME = {}8function PassThrough () {9 Stream.call(this)10 this.readable = this.writable = true11}12PassThrough.prototype = Object.create(Stream.prototype, {13 constructor: {14 value: PassThrough15 },16 write: {17 value: function (c) {18 this.emit('data', c)19 return true20 }21 },22 end: {23 value: function (c) {24 if (c) this.write(c)25 this.emit('end')26 }27 },28 pause: {29 value: function () {30 this.emit('pause')31 }32 },33 resume: {34 value: function () {35 this.emit('resume')36 }37 }38})39tap.test('incoming', function (t) {40 var ms = new MS41 var str = new PassThrough42 str.pipe(ms)43 var expect = ['foo', 'boo', END]44 ms.on('data', function (c) {45 t.equal(c, expect.shift())46 })47 ms.on('end', function () {48 t.equal(END, expect.shift())49 t.end()50 })51 str.write('foo')52 ms.mute()53 str.write('bar')54 ms.unmute()55 str.write('boo')56 ms.mute()57 str.write('blaz')58 str.end('grelb')59})60tap.test('outgoing', function (t) {61 var ms = new MS62 var str = new PassThrough63 ms.pipe(str)64 var expect = ['foo', 'boo', END]65 str.on('data', function (c) {66 t.equal(c, expect.shift())67 })68 str.on('end', function () {69 t.equal(END, expect.shift())70 t.end()71 })72 ms.write('foo')73 ms.mute()74 ms.write('bar')75 ms.unmute()76 ms.write('boo')77 ms.mute()78 ms.write('blaz')79 ms.end('grelb')80})81tap.test('isTTY', function (t) {82 var str = new PassThrough83 str.isTTY = true84 str.columns=8085 str.rows=2486 var ms = new MS87 t.equal(ms.isTTY, false)88 t.equal(ms.columns, undefined)89 t.equal(ms.rows, undefined)90 ms.pipe(str)91 t.equal(ms.isTTY, true)92 t.equal(ms.columns, 80)93 t.equal(ms.rows, 24)94 str.isTTY = false95 t.equal(ms.isTTY, false)96 t.equal(ms.columns, 80)97 t.equal(ms.rows, 24)98 str.isTTY = true99 t.equal(ms.isTTY, true)100 t.equal(ms.columns, 80)101 t.equal(ms.rows, 24)102 ms.isTTY = false103 t.equal(ms.isTTY, false)104 t.equal(ms.columns, 80)105 t.equal(ms.rows, 24)106 ms = new MS107 t.equal(ms.isTTY, false)108 str.pipe(ms)109 t.equal(ms.isTTY, true)110 str.isTTY = false111 t.equal(ms.isTTY, false)112 str.isTTY = true113 t.equal(ms.isTTY, true)114 ms.isTTY = false115 t.equal(ms.isTTY, false)116 t.end()117})118tap.test('pause/resume incoming', function (t) {119 var str = new PassThrough120 var ms = new MS121 str.on('pause', function () {122 t.equal(PAUSE, expect.shift())123 })124 str.on('resume', function () {125 t.equal(RESUME, expect.shift())126 })127 var expect = [PAUSE, RESUME, PAUSE, RESUME]128 str.pipe(ms)129 ms.pause()130 ms.resume()131 ms.pause()132 ms.resume()133 t.equal(expect.length, 0, 'saw all events')134 t.end()135})136tap.test('replace with *', function (t) {137 var str = new PassThrough138 var ms = new MS({replace: '*'})139 str.pipe(ms)140 var expect = ['foo', '*****', 'bar', '***', 'baz', 'boo', '**', '****']141 ms.on('data', function (c) {142 t.equal(c, expect.shift())143 })144 str.write('foo')145 ms.mute()146 str.write('12345')147 ms.unmute()148 str.write('bar')149 ms.mute()150 str.write('baz')151 ms.unmute()152 str.write('baz')153 str.write('boo')154 ms.mute()155 str.write('xy')156 str.write('xyzΩ')157 t.equal(expect.length, 0)158 t.end()159})160tap.test('replace with ~YARG~', function (t) {161 var str = new PassThrough162 var ms = new MS({replace: '~YARG~'})163 str.pipe(ms)164 var expect = ['foo', '~YARG~~YARG~~YARG~~YARG~~YARG~', 'bar',165 '~YARG~~YARG~~YARG~', 'baz', 'boo', '~YARG~~YARG~',166 '~YARG~~YARG~~YARG~~YARG~']167 ms.on('data', function (c) {168 t.equal(c, expect.shift())169 })170 // also throw some unicode in there, just for good measure.171 str.write('foo')172 ms.mute()173 str.write('ΩΩ')174 ms.unmute()175 str.write('bar')176 ms.mute()177 str.write('Ω')178 ms.unmute()179 str.write('baz')180 str.write('boo')181 ms.mute()182 str.write('Ω')183 str.write('ΩΩ')184 t.equal(expect.length, 0)185 t.end()...
Using AI Code Generation
1var webpagetest = require('webpagetest');2var wpt = new webpagetest('www.webpagetest.org');3var fs = require('fs');4var url = process.argv[2];5var key = process.argv[3];6var location = process.argv[4];7var label = process.argv[5];8var browser = process.argv[6];9var testId = '';10var testResults = '';11var testStatus = '';12var testStatusText = '';13var testUrl = '';14var testSummary = '';15var testRawData = '';16var testRawDataJSON = '';17var testRawDataJSONData = '';18var test = wpt.runTest(url, {19}, function(err, data) {20 if (err) {21 console.log(err);22 }23 else {24 testId = data.data.testId;25 testResults = data.data.jsonUrl;26 testStatus = data.statusCode;27 testStatusText = data.statusText;28 testUrl = data.data.userUrl;29 testSummary = data.data.summary;30 testRawData = data.data;31 testRawDataJSON = data.data.jsonUrl;32 testRawDataJSONData = data.data.jsonUrlData;
Using AI Code Generation
1var page = require('webpage').create();2var system = require('system');3var fs = require('fs');4var url = system.args[1];5page.viewportSize = { width: 1024, height: 768 };6page.settings.resourceTimeout = 10000;7page.open(url, function(status) {8 if(status === "success") {9 var html = page.evaluate(function() {10 return document.documentElement.innerHTML;11 });12 var json = page.evaluate(function() {13 return JSON.stringify(window.performance.timing);14 });15 var json2 = page.evaluate(function() {16 return JSON.stringify(window.performance);17 });18 var json3 = page.evaluate(function() {19 return JSON.stringify(window.performance.getEntries());20 });21 var json4 = page.evaluate(function() {22 return JSON.stringify(window.performance.getEntriesByType("resource"));23 });24 var json5 = page.evaluate(function() {25 });26 var json6 = page.evaluate(function() {27 return JSON.stringify(window.performance.toJSON());28 });29 var json7 = page.evaluate(function() {30 return JSON.stringify(window.performance.memory);31 });32 var json8 = page.evaluate(function() {33 return JSON.stringify(window.performance.navigation);34 });35 var json9 = page.evaluate(function() {36 return JSON.stringify(window.performance.timing.toJSON());37 });38 var json10 = page.evaluate(function() {39 return JSON.stringify(window.performance.timing.toJSON());40 });41 var json11 = page.evaluate(function() {42 return JSON.stringify(window.performance.timing.toJSON());43 });44 var json12 = page.evaluate(function() {45 return JSON.stringify(window.performance.timing.toJSON());46 });47 var json13 = page.evaluate(function() {48 return JSON.stringify(window.performance.timing.toJSON());49 });
Using AI Code Generation
1var WebPageTest = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.3f6d3d6b8b6c1b6f1c6d9b6f2c6e9b6');3wpt.runTest(url, {video: true, strategy: 'mobile'}, function(err, data) {4 if (err) return console.error(err);5 console.log('Test Results: ' + data.data.jsonUrl);6});
Using AI Code Generation
1var wpt = new WebPageTest('www.webpagetest.org', 'A.9f0c8b9d2e2c0f2e0e7b8f8d1c7e1b0c');2wpt.runTest(url, {location: 'Dulles:Chrome'}, function(err, data) {3 if (err) return console.error(err);4 console.log(data);5});6var wpt = new WebPageTest('www.webpagetest.org', 'A.9f0c8b9d2e2c0f2e0e7b8f8d1c7e1b0c');7wpt.runTest(url, {location: 'Dulles:Chrome'}, function(err, data) {8 if (err) return console.error(err);9 console.log(data);10});
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!