Write own Javascript plugins for creating audio player - javascript

I write my own Javascript plugins for creating an audio player.
I created one in the AudioPlayer.js file:
(function ($) {
jQuery.fn.myPlayer = function (options) {
var defaults = {
id: "#myPlayer",
url: ""
};
var settings = $.extend({}, defaults, options);
return this.each(function () {
var AudioPlayer = $("<audio>");
AudioPlayer.attr('id', settings.id);
AudioPlayer.attr('controls', 'true');
AudioPlayer.appendTo(this);
var source = $("<source>");
source.attr('src', settings.url);
source.attr('type', 'audio/mp3');
source.appendTo(AudioPlayer);
});
}
}(jQuery));
Above code is in AudioPlayer.js, and add this reference to a "HTML" page, and
Now I am accessing this file in html page like this:
<script>
$(document).ready(function () {
$('.playerDemo').myPlayer({
id: "myAudio",
url: "https://vzstr.blob.core.windows.net/media/e2d4255c-a03a-45ac-b34a-42bd3101f902/59006.mp3"
});
});
</script>
Using above code my player will display on browser,
and I have two buttons one for play audio and second for paused audio,
how can achieve this functionality using Javascript plugins.
I want to write method for "PLAY" and "PAUSE" method in Javascript plugins.
How to achieve this functionality?

Write this code in AudioPlayer.js file.
(function ($) {
jQuery.fn.myPlayer = function (options) {
var defaults = {
id: "#myPlayer",
url: ""
};
var settings = $.extend({}, defaults, options);
var AudioPlayer = $("<audio>");
AudioPlayer.attr('id', settings.id);
AudioPlayer.attr('controls', 'true');
AudioPlayer.appendTo(this);
var source = $("<source>");
source.attr('src', settings.url);
source.attr('type', 'audio/mp3');
source.appendTo(AudioPlayer);
var AuPlayer = AudioPlayer[0];
return {
play: function () {
AuPlayer.play();
},
pause: function () {
AuPlayer.pause();
}
}
}
}(jQuery));
Bellow code in HTML File,
<script>
var playerDemo;
$(document).ready(function () {
playerDemo = $('.playerDemo').myPlayer({
id: "myAudio",
url: "https://vzstr.blob.core.windows.net/media/e2d4255c-a03a-45ac-b34a-42bd3101f902/59006.mp3"
});
});
function playAudio() {
playerDemo.play();
}
</script>

Related

How to override js function in odoo

I want to load /shop/checkout url.
function is :
odoo.define('change_info_order.website_sale_change_info_order', function (require) {
"use strict";
$('.oe_website_sale').each(function () {
var oe_website_sale = this;
$(oe_website_sale).on('click', 'a.js_checkout_btn', function (ev) {
var e = document.getElementById("js_customer_change");
// var customer_id = e.options[e.selectedIndex].value;
var customer_id = **e.value**;
if (customer_id) {
window.location.href = '/shop/checkout';
}
else {
Dialog.alert(self, _t("Please select the customer"));
};
});
});
});
My inherit code is:
odoo.define('website_sale_product_list.website_sale_change_info_order', function (require) {
"use strict";
$('.oe_website_sale').each(function () {
var oe_website_sale = this;
$(oe_website_sale).on('click', 'a.js_checkout_btn', function (ev) {
window.location.href = '/shop/checkout';
});
});
});
Problem is can't load my inherited javascript file. How to fix this problem?
I need any solution.
Console:
the line code is:
/comment:My js file can't load/

My JQuery Plugin Appends "0" to the body element

I created a small JQuery Plugin and I'm using thickbox for popups.
I don't know where it came from but I have a "0" appended to the end of the body element.
I suspect that one of those outputs a success indicator maybe?
Is anyone familiar with this behavior?
Edit:
PHP
add_action('wp_ajax_product_picker', 'popup_content');
function popup_content() {
iframe_header();
echo 'aaa';
iframe_footer();
}
JS
var currentPicker = {};
(function ($) {
$.fn.pick_product = function (options) {
var settings = $.extend({
callback: function(){}
}, options);
this.on('click',function () {
var url = product_picker.ajax_url + "?action=product_picker&TB_iframe=true&width=600&height=550";
tb_show("My Caption", url);
});
return this;
};
}(jQuery));
jQuery(function ($) {
$('#test-product-picker').pick_product({
callback: function (variation_id, size) {
alert('aaa');
}
});
});
If you are using it on the front end of your site you have to use the
wp_ajax_nopriv_product_picker and also place die() after your function.
function popup_content() {
iframe_header();
echo 'aaa';
iframe_footer();
die();
}

JQuery plugin - callback breaks other features

I'm trying to build a basic color picker plugin (mainly as an exercise to learn about plugin development). I have a callback called "onSelected" that fires when you pick a color, but it breaks another feature of the plugin (the ability to toggle the visibility of the swatch list).
I am new to plugin development so I'm sure it's a simple mistake I'm making...
jsfiddle
Plugin:
(function ($) {
$.colorPicker2 = function (el, options) {
// the wrapper around the colors
var $pickerContainer = $("<div>");
// To avoid scope issues, use 'base' instead of 'this'
// to reference this class from internal events and functions.
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
// Add a reverse reference to the DOM object
base.$el.data("colorPicker2", base);
base.init = function () {
console.log("base.init");
base.options = $.extend({}, $.colorPicker2.defaultOptions, options);
// Put your initialization code here
// code goes here
$.each(base.options.colors, function (index, value) {
var $item = $('<div class="colorPicker-colorOption">').css({
"background-color": "#" + value
})
$item.click(function () {
console.log("item.click");
base.selectColor(value);
})
$pickerContainer.append($item);
});
//$pickerContainer.hide();
base.$el.append($pickerContainer);
if (base.options.toggleElement != null) {
base.options.toggleElement.click(function (e) {
base.togglePicker();
e.preventDefault();
});
}
};
base.togglePicker = function()
{
$pickerContainer.toggle();
}
base.selectColor = function (color) {
base.togglePicker();
if (typeof base.options.onSelected == 'function') {
base.options.onSelected.call(this, color);
}
}
// Sample Function, Uncomment to use
// base.functionName = function(paramaters){
//
// };
// Run initializer
base.init();
};
$.colorPicker2.defaultOptions = {
colors: [
'000000', '993300', '333300', '000080', '333399', '333333', '800000', 'FF6600',
'808000', '008000', '008080', '0000FF', '666699', '808080', 'FF0000', 'FF9900',
'99CC00', '339966', '33CCCC', '3366FF', '800080', '999999', 'FF00FF', 'FFCC00',
'FFFF00', '00FF00', '00FFFF', '00CCFF', '993366', 'C0C0C0', 'FF99CC', 'FFCC99',
'FFFF99', 'CCFFFF', '99CCFF', 'FFFFFF'
],
toggleElement: null,
onSelected: function (color) { }
};
$.fn.colorPicker2 = function (options) {
return this.each(function () {
(new $.colorPicker2(this, options));
});
};
})(jQuery);
How I hook into the onSelected event:
$(function () {
$('#primaryColorPicker').colorPicker2({
toggleElement: $('#selectPrimaryColor'),
onSelected: function (color) {
$('#selectedPrimaryColor').html("(#" + color + ")");
}
});
});
The HTML:
<a id="selectPrimaryColor">Toggle Color Picker</a>
<span id="selectedPrimaryColor" />
<div id="primaryColorPicker"></div>
You just have to learn how to write valid HTML
replace
<span id="selectedPrimaryColor" />
with
<span id="selectedPrimaryColor"></span>
FIDDLE

Two jQuery codes but only one executed

I have two functions in jQuery. One of them looks for image extensions from form and another is getting image dimensions and triggers an alert() when an image is not big enough.
Both functions are correctly executed in demos but together only one is executed. Only part where extensions is getting is executed. Sorry for the length of the code but it was the only way to show the problem.
(function ($) {
$.fn.checkFileType = function (options) {
var defaults = {
allowedExtensions: [],
success: function () {},
error: function () {}
};
options = $.extend(defaults, options);
return this.each(function () {
$(this).on('change', function () {
var value = $(this).val(),
file = value.toLowerCase(),
extension = file.substring(file.lastIndexOf('.') + 1);
if ($.inArray(extension, options.allowedExtensions) == -1) {
options.error();
$(this).focus();
} else {
options.success();
}
});
});
};
})(jQuery);
$("#filput").on('change', function () {
var fr = new FileReader;
fr.onload = function () { // file is loaded
var img = new Image;
img.onload = function () {
// image is loaded; sizes are available
var w = img.width
if (w < 500) {
alert("too small");
} else {
alert("big enough");
}
};
img.src = fr.result;
};
fr.readAsDataURL(this.files[0]);
});
$(function () {
$('#filput').checkFileType({
allowedExtensions: ['jpg', 'jpeg', 'png'],
error: function () {
alert('error');
}
});
});
Only part where extentions is getting is executed
That code is wrapped by $(function() { ... }); so, you should probably put both pieces of code there:
$(function() {
$("#filput").on('change', function () {
var fr = new FileReader;
// ...
});
$('#filput').checkFileType({
// ...
});
});
Based on what you have described, the input element wasn't present yet at the time your code is run; by putting both code segments in the DOMReady event handler, you're making sure it exists.

pjax/ajax and browser back button issues

I use pjax to ajaxify my menu links. This works fine until I use the browser back button. In my javascript file I use Common Script files (to load all the necessary js files when the user hits the url) and Script files with respect to each menu links (when navigated through pjax)
function myFunction(){
/*All the script files */
}
$(document).ready(function(){
myFunction();
/*pjax menu loading block*/
$(document).on('click', 'a[data-pjax]', function(event) {
$.pjax.click(event, '#pjax-container');
$(document).on('pjax:end', function() {
myFunction();
});
});
});
Now when I navigate to a menu item and try to come back by clicking the browser back button, the script files are getting duplicated (eg: slider images getting duplicated and table sorting not working).How to overcome this issue?
You can implement the url specific loading this way, create a queue of functions which you want to load and unload on pjax complete
The solution is based on js prototyping
// create queue for load and unload
var onLoad = new PjaxExecQueue();
var onUnload = new PjaxExecQueue();
// way to add functions to queue to run on pjax load
onLoad.queue(function() {
someFunction();
});
// way to add functions to queue to unload on pjax load
onUnload.queue(function() {
someOtherFunction();
});
// load function if url contain particular path name
onLoad.queue_for_url(function_name, 'url_section');
// check for url specific function
var URLPjaxQueueElement = function(exec_function, url) {
this.method = exec_function;
if(url) {
this.url = new RegExp(url);
} else {
this.url = /.*/;
}
};
// create a queue object
var PjaxExecQueue = function () {
this.url_exec_queue = [];
this.id_exec_queue = [];
this.fired = false;
this.indicating_loading = false;
this.content = $('#content');
};
PjaxExecQueue.prototype = {
queue: function (exec_function) {
this.url_exec_queue.unshift(new URLPjaxQueueElement(exec_function));
},
queue_for_url: function (exec_function, url_pattern) {
this.url_exec_queue.unshift(new URLPjaxQueueElement(exec_function, url_pattern));
},
queue_if_id_present: function(exec_function, id) {
this.id_exec_queue.unshift(new IDPjaxQueueElement(exec_function, id));
},
fire: function () {
if(this.indicating_loading) {
this.content.removeClass("indicate-loading");
this.indicating_loading = false;
}
if(!this.fired) {
var match_loc = window.location.pathname;
var i = this.url_exec_queue.length;
while(i--) {
this.url_exec_queue[i].fire(match_loc);
}
i = this.id_exec_queue.length;
while(i--) {
this.id_exec_queue[i].fire(match_loc);
}
}
this.fired = true;
},
reset: function() {
this.fired = false;
},
loading: function () {
this.content.addClass("indicate-loading");
this.indicating_loading = true;
this.reset();
},
count: function () {
return exec_queue.length;
},
show: function (for_url) {
for (var i=0; i < exec_queue.length; i++) {
if(for_url) {
if(exec_queue[i].url.test(for_url)) {
console.log("" + exec_queue[i].method);
}
} else{
console.log(exec_queue[i].url + " : " + exec_queue[i].method);
}
}
}
};
// before send
$(document).on('pjax:beforeSend', function() {
onLoad.loading();
onUnload.fire();
});
// after pjax complete
$(document).on('pjax:complete', function() {
onLoad.fire();
onUnload.reset();
});

Categories