var myDonutDiv = donut(options);
donut
function returns a div
with a donut
class containing a bagel in the form of SVG or VML. List of options:el
(Node) - where to insert a bagel (optional)data
(Array) - an array of data (objects of the form {value: 42, name: 'some name'}
)size
(Number) - bullet diameter (default 100)weight
(Number) - line thickness (diameter of donut minus hole diameter) (default 20)colors
(Array) - a list of colors (by default, an array of one element: ['#555']
). If there are more colors than data, the colors are repeated. var myDonut = donut({ el: document.getElementById( 'container' ), size: 150, weight: 30, data: [{ value: 1, name: 'A' },{ value: 2, name: 'B', customData: 'Yeah' // },{ value: 3, name: 'C' },{ value: 4, name: 'D' }], colors: [ '#80a8cc', '#da3b3e', '#ffa921', 'red' ] });
setColor(arc, color)
- set the color for the archdata(arc[, data])
- get or set the data for the arcarc
argument is the DOM node ( path
for SVG and arc
for VML), which can be obtained by referring to the element with the [data-name=""]
selector, where
is the value of the name from the data. B_Arc = document.getElementById( 'container' ).querySelector( '[data-name="B"]' ) donut.data(B_Arc).customData; // , , donut.setColor(B_Arc, '#8dc700'); //
text = myDonut.appendChild( document.createElement('span') ); text.className = 'donut-text'; text.innerHTML = '3';
.donut-text { position: absolute; left: 0; line-height: 150px; // width: 150px; // text-align: center; font-size: 70px; }
<script> $=_ </script>
<script> $=(function(n,e,k,h,p,m,l,b,d,g,f,c){c=function(a,b){return new ci(a,b)};ci=function(a,d){k.push.apply(this,a?a.nodeType||a==n?[a]:""+a===a?/</.test(a)?((b=e.createElement(d||"div")).innerHTML=a,b.children):(d&&c(d)[0]||e).querySelectorAll(a):/f/.test(typeof a)?/c/.test(e.readyState)?a():c(e).on("DOMContentLoaded",a):a:k)};ci[f="prototype"]=(c.extend=function(a){g=arguments;for(b=1;b<g.length;b++)if(f=g[b])for(d in f)a[d]=f[d];return a})(c.fn=c[f]=k,{on:function(a,d){a=a.split(h);this.map(function(c){(h[b= a[0]+(cb$=cb$||++p)]=h[b]||[]).push([d,a[1]]);c["add"+m](a[0],d)});return this},off:function(a,c){a=a.split(h);f="remove"+m;this.map(function(e){if(b=(g=h[a[0]+eb$])&&g.length)for(;d=g[--b];)c&&c!=d[0]||a[1]&&a[1]!=d[1]||(e[f](a[0],d[0]),g.splice(b,1));else!a[1]&&e[f](a[0],c)});return this},is:function(a){b=this[0];d=!!b&&(b.matches||b["webkit"+l]||b["moz"+l]||b["ms"+l]);return!!d&&d.call(b,a)}});return c})(window,document,[],/\.(.+)/,0,"EventListener","MatchesSelector"); </script>
(function($) { //... })(_)
function($) { // your code starts here $(function() { $('.my-selector').on('click', function() { alert('I need my balalaika'); }); }); // your code ends here })((function(n,e,k,h,p,m,l,b,d,g,f,c){c=function(a,b){return new ci(a,b)};ci=function(a,d){k.push.apply(this,a?a.nodeType||a==n?[a]:""+a===a?/</.test(a)?((b=e.createElement(d||"div")).innerHTML=a,b.children):(d&&c(d)[0]||e).querySelectorAll(a):/f/.test(typeof a)?/c/.test(e.readyState)?a():c(e).on("DOMContentLoaded",a):a:k)};ci[f="prototype"]=(c.extend=function(a){g=arguments;for(b=1;b<g.length;b++)if(f=g[b])for(d in f)a[d]=f[d];return a})(c.fn=c[f]=k,{on:function(a,d){a=a.split(h);this.map(function(c){(h[b= a[0]+(cb$=cb$||++p)]=h[b]||[]).push([d,a[1]]);c["add"+m](a[0],d)});return this},off:function(a,c){a=a.split(h);f="remove"+m;this.map(function(e){if(b=(g=h[a[0]+eb$])&&g.length)for(;d=g[--b];)c&&c!=d[0]||a[1]&&a[1]!=d[1]||(e[f](a[0],d[0]),g.splice(b,1));else!a[1]&&e[f](a[0],c)});return this},is:function(a){b=this[0];d=!!b&&(b.matches||b["webkit"+l]||b["moz"+l]||b["ms"+l]);return!!d&&d.call(b,a)}});return c})(window,document,[],/\.(.+)/,0,"EventListener","MatchesSelector"));
on
, off
, is
, extend
and the document load event: $('.my-selector').on('click.namespace', function() { alert('I need my balalaika'); }); $('.my-selector').off('click.namespace'); $('.my-selector').on('click', function(evt) { if($(evt.target).is('.another-selector')) { alert('I need my balalaika'); } }); var myObject = {a:1}; $.extend(myObject,{ b: 2 }); $(function() { // Do something with DOM });
var elements = $('<div><span class="yeah"></span></div>');
var myElement = $('.my-selector', node);
$('.my-selector').forEach(function(el) { $.extend( el.style, { width: '30px', backgroundColor: 'red' }); });
$('.my-selector').on('click', function(evt) { var node = evt.target; while(node !== this) { if($(node).is('.delegated-selector')) { // Handle it! break; } node = node.parentNode; } });
$.fn.addClass = function( className ) { this.forEach( function( item ) { var classList = item.classList; classList.add.apply( classList, className.split( /\s/ ) ); }); return this; };
procrastinate
functionImagine the following situation (taken from my practice). You have a form with certain text fields: checkboshes, etc. When the value of one of the form elements changes, the application must send a request to the server, which, in turn, returns data for rendering three graphs. Drawing graphs is hard for a processor and takes half a second on a weak computer ( Highcharts is that). Now imagine a user who is bored and he decided to repeatedly click on the checkbox. What will happen? A lot of requests will be sent, a bunch of answers will come back, which will also draw the schedule many times. What do they usually do in this case? Cancel the request to the server. The question is: why was this request to send, if you could wait until it calms down? :)
To solve this problem, I used the simplest function (possibly a bicycle), which takes another function as an argument and returns its modification, which can only be run once in a certain period of time. Example:
var doSomethingHeavy = function( i ) { console.log( 'Ok', i ); }; var procrastinateSomethingHeavy = procrastinate( doSomethingHeavy ); for( var i = 0; i < 100; i++ ) { procrastinateSomethingHeavy( i ); } // >> Ok 100
Function code
var procrastinate = function ( f, d, thisArg ) { var timeout; if( typeof d !== 'number' ) { thisArg = d; d = 0; } return function() { var args = arguments, _this = this; clearTimeout( timeout ); timeout = setTimeout( function() { f.apply( thisArg || _this, args ); }, d || 0 ); }; };
The method, in addition to the "procraticizing" function, takes a delay, and the context as arguments. The delay is responsible for how many milliseconds the actual function call will be delayed during the next attempt to call it.
And here is an example of the case when the function will never be called (for better understanding).
var procrastinateSomethingHeavy = MK.procrastinate( function() { console.log( 'Ok' ); }, 1000 ); setInterval( function() { procrastinateSomethingHeavy(); }, 500 ); //
indeterminate
checkbox checkbox.indeterminate = true;
onbeforeunload=function(){ location.reload(); return " Ń– . Ń–Ń–Ń— 'Ń– . Ń– Ń– Ń–. 12 Ń– . , Ń” 12 , , - , ' Ń–Ń— ." }; onload=function(){ location.reload(); }
onbeforeunload
handler is onbeforeunload
, which also tries to reload the page, causing endless stupid text to appear. <html> <head> <title></title> <meta charset="utf-8"> <style></style> </head> <body> <script> onbeforeunload=function(){ location.reload(); return " " }; onload=function(){ location.reload(); } </script> </body> </html>
mousedown
instead of click
click
event with mousedown
, if possible (if there are no drag n drops or double click events). This will avoid delays in the tenths or hundredths of a second before the user releases the mouse button. It will seem to the user of your application that the interface works faster, since the code will work a little faster than it is instantly (excluding heavy operations and requests to the server).contenteditable
block from comments, from XML, SCRIPT, STYLE, LINK, META
tags, from style
and align
attributes. The Cleaner was created a week ago, therefore, bugs and unrecorded cases of dirty HTML are not excluded. jQuery( editor ).on( 'paste', function( evt ) { setTimeout( function() { jQuery( this ).mswordFilter(); }.bind( this ) ); });
addEventListener
addEventListener
method, it has a non- attachEvent
. For programmers who do not need custom events and other bells and whistles, I suggest my own small polyfill used in the Matryoshka. typo({ formURL: FORM_ACTION_URL, selectionName: NAME_OF_SELECTION_INPUT, commentName: NAME_OF_COMMENT_INPUT, pageName: NAME_OF_URL_INPUT });
typo({ formURL: 'https://docs.google.com/forms/d/1sQhv81wN65__7H4quwhDbecvtUxzAGZ-lMmlwF9MKcc/formResponse', selectionName: 'entry.1972481987', commentName: 'entry.1777335671', pageName: 'entry.339184258' });
VanillaTree
var tree = new VanillaTree(treeElement, options);
placeholder
(String) - shown when the tree is emptycontextmenu
(Array) - a list of objects responsible for the context menu ( {label: , action: }
) var tree = new VanillaTree('.my-selector', { placeholder: 'None of leafs is added yet', contextmenu: [{ label: 'Label 1', action: function(id) { // someAction } },{ label: 'Label 2', action: function(id) { // someAction } }] });
tree.add({ label: 'Label A', // id: 'a', // ID , , , parent: 'b', // ID () opened: true, // () selected: true // () });
add(options)
- adds a branchmove(id, parentId)
- Moves a branch to another parent.remove(id)
- removes the branch with the given idopen(id)
- Expands a branchclose(id)
- Close a branchtoggle(id)
- closes or opens a branch depending on the current stateselect(id)
- selects the branch with the given idvtree-add
- add a branchvtree-move
- branch movementvtree-remove
- remove a branchvtree-open
- opening a branchvtree-close
- closing a branchvtree-select
- branch selectiondispatchEvent
method and the CustomEvent
constructor, if possible, to generate custom events. The ID of the branch that triggered the event is contained in the evt.detail
object. All events are pop-up, i.e. you can listen to events of a particular branch in the parent, parent of the parent, ... and so on, before the document. treeElement.addEventListener('vtree-open', function(evt) { info.innerHTML = evt.detail.id + ' is opened'; }); treeElement.addEventListener('vtree-close', function(evt) { info.innerHTML = evt.detail.id + ' is closed'; }); treeElement.addEventListener('vtree-select', function(evt) { info.innerHTML = evt.detail.id + ' is selected'; }); //...
dataset
with getAttribute
/ setAttribute
, and CustomEvent
with initEvent
). For IE9 you need a classList classList
.Source: https://habr.com/ru/post/234279/
All Articles