/// // ChocolateChipStatic -- DOM creation, etc. $(function() { alert('Ready to do stuff!'); }); var docRoot = $(); var divTag = $('div'); var divClass = $('.div'); var divID = $('#div'); var divAttr = $('[name=div]'); var documentElement = $(); var divTag2 = $(divTag); var nodeName = divTag2[0].nodeName; // Assorted properties and functions on ChocolateChipStatic: var version = $.version; var libraryName = $.libraryName; var els = $('li'); var madeEls = $.make('

Stuff

'); var moreEls = $.html('

Stuff

'); var oldTag = $('#oldTag'); var newTag = $('#newTag'); $.replace(oldTag, newTag); $.require('./scripts/myscript.js', function() { $.noop; }); $.defer(function() { console.log("This comes after Squawk!"); }); var concatenation = $.concat('This', ' ', 'is', ' ', 'a', ' ', 'string', '.'); var arrayOfStringWords = $.w('This is a string'); // Boolean tests: $.isString('This is a string'); $.isArray([1, 2, 3]); $.isFunction($.noop); $.isObject({ name: 'Me' }); $.isObject(new Object()); $.isEmptyObject({}); $.isNumber(123); $.isInteger(123); $.isInteger(123.123); // should return false $.isFloat(123.123); // should return true var newUuid = $.makeUuid(); $.each(['a', 'b', 'c'], function(ctx: string, idx: number) { console.log(ctx); console.log(idx); }); // Plugin interface for ChocolateChipElementArray: $.fn.extend({ whateverProperty: "whatever", whateverMethod: function() { alert("Whatever!"); } }); // ChocolateChipElementArray extensions: $('li').each(function(ctx, idx) { console.log(ctx.nodeName); }); var uniqueElements = $('li').unique(); var secondElement = $('li').eq(1); var lastElement = $('li').eq(-1); var whichIndex = $('li').index($('.selected')); var whichListItemIndex = $('li').eq(3).index(); $('.elems').is('div').each(function(ctx) { console.log('This element is a div.'); }); $('.elems').isnt('p').each(function() { console.log('This element is not a paragraph tag.'); }); $('li').has('p').each(function(ctx) { console.log('This list item has a paragraph tag.') }); $('li').hasnt('p').each(function(ctx) { console.log('This list item does not have a paragraph tag.') }); $('ul').find('li').each(function(ctx) { console.log(ctx); }); $('li').css('color'); $('li').css('color', 'red'); $('li').css({ "color": "red", "background-color": "yellow" }); var elemWidth = $('#header').width(); var elemHeight = $('#header').height(); var offset = $('h1').offset(); console.log(offset.top); console.log(offset.left); console.log(offset.bottom); console.log(offset.right); $('li.selected').prependTo('#selectedItems'); $('li.selected').appendTo('#selectedItems'); $('ul').before("

Subtitle

"); $('ul').after("

Footer stuff here.

"); var h1Text = $('h1').text(); $('h1').text('The New Title'); $('ul').insert("
  • 1
  • 2
  • 3
  • ", "first"); $('ul').insert("
  • 1
  • 2
  • 3
  • ", "last"); $('ul').insert("
  • 1
  • 2
  • 3
  • ", 3); $('ul').insert("
  • 1
  • 2
  • 3
  • "); $('ul').html('
  • 1
  • <2/li>
  • 3
  • '); $('ul').html(''); var listContent = $('ul').html(); $('ul').prepend('
  • The title
  • '); $('ul').append('
  • The Last Item
  • '); var inputName = $('input').attr('name'); $('input').attr('name', 'wobba'); var inputProperty = $('input').prop('disabled'); $('input[type=checked]').prop('checked', true); $('input').removeProp('disabled'); $('input').hasAttr('disabled').css('border', 'solid 1px red'); $('input').removeAttr('disabled'); $('article').hasClass('current').css('display', 'block'); $('article').addClass('current'); $('article').removeClass('current'); $('article').toggleClass("current"); $('h1').dataset('status', 'ready'); var theStatus = $('h1').dataset('status'); var theText = $('textarea').val(); $('textarea').val('This is the new text.'); $('input').disable(); $('input').enable(); $('ul').hide(); $('ul').hide('slow'); $('ul').hide('fast'); $('ul').hide(1000); $('ul').hide('fast', function() { console.log('Finished hiding these.'); }); $('ul').show(); $('ul').show('slow'); $('ul').show('fast'); $('ul').show(1000); $('ul').show('fast', function() { console.log('Finished hiding these.'); }); $('#list').prev().css('display', 'none'); $('#list').next().css('display', 'none'); $('ul').first().css('font-weight', 'bold'); $('ul').last().css('font-style', 'italic'); $('ul').children().css('display', 'block'); $('li').parent().css('border', 'solid 2px green'); $('#list').ancestor('article').css('margin', "20px"); $('#list').ancestor(5).css('margin', "20px"); $('#list').closest('article').css('margin', "20px"); $('button').siblings().css('padding', "20px"); $('button').siblings("p").css('padding', "20px"); var cloned = $('#list').clone(); $('#list').wrap('
    '); $('#list').unwrap(); $('#list').remove(); $('#list').empty(); var thePrice = $('#list').data('price'); $('#list').data('price', '$1000'); $('#list').removeData('price'); // Animate: $('#list').animate({ "transform": "rotate3d(30, 150, 200, 180deg) scale(3) translate3d(-50%, -30%, 140%)", "opacity": .25, "transform-style": "preserve-3d", "perspective": 500 }, '2s', "ease-in-out" ); // Form to JSON: $.form2JSON($('form')[0], '.'); // Strings: $.camelize("this-is-a-string"); // should return "thisIsAString" $.deCamelize("thisIsAString"); // Should return "this-is-a-string" $.capitalize("a string"); // Should return "A string" $.capitalize("a string", true); // Should return "A STRING" // Booleans: var isiPhone = $.isiPhone; var isiPad = $.isiPad; var isiPod = $.isiPod; var isiOS = $.isiOS; var isAndroid = $.isAndroid; var isWebOS = $.isWebOS; var isBlackberry = $.isBlackberry; var isTouchEnabled = $.isTouchEnabled; var isOnline = $.isOnline; var isStandalone = $.isStandalone; var isiOS6 = $.isiOS6; var isiOS7 = $.isiOS7; var isWin = $.isWin; var isWinPhone = $.isWinPhone; var isIE10 = $.isIE10; var isIE11 = $.isIE11; var isWebkit = $.isWebkit; var isMobile = $.isMobile; var isDesktop = $.isDesktop; var isSafari = $.isSafari; var isNativeAndroid = $.isNativeAndroid; // Events: $('li').bind('click', function() { $.noop; }, false); $('li').unbind('click', function() { $.noop; }, false); $('ul').delegate('click', 'li', function() { $.noop; }, false); $('ul').undelegate('click', 'li', function() { $.noop; }, false); $('li').on('click', function() { $.noop; }) $('ul').on('click', 'li', function() { console.log($(this).text()) }); $('li').off('click'); $('li').off('click', 'li'); $('button').trigger('click'); $('.selected').data('selection', 'This is awesome!'); // set the value of data value to "This is Awesome!" $('.selected').data('selection'); // return the data value of "selection" on ".selected" // Promises: var myPromise = new Promise(function(resolve, reject) { $.noop; }); var myPromise = new Promise(function(resolve, reject) { // Resolve the promise: resolve('Success!'); // or reject it: // reject('Lost in Space!'); }); myPromise.then(function(value) { // Success: console.log(value); }, // Opps! There was a problem: function(reason) { console.log(reason); }); // Fetch API //=========== // GET: interface WineObject { data: Array; } interface WineInterface { wine: { name: string; } } fetch('../data/wines.json') .then($.json) .then(function(obj: any):any { $('#message_ajax').empty(); obj.data.forEach(function(wine: any) { $('#message_ajax').append('
  • ' + wine.name + '
  • '); }) }); // POST: interface postData { email: string; name: string; msg: string; } var formData = $.serialize($('form')[0]); fetch('../controllers/php-post.php', { method: 'post', headers: { "Content-type": "application/x-www-form-urlencoded; charset=UTF-8" }, body: formData }) .then($.json) .then(function(data: any): any { if(data.email_check == "valid"){ $("#message_ajax").html("
    " + data.email + " is a valid e-mail address. Thank you, " + data.name + ".
    "); $("#message_ajax").append('

    ' + data.msg + '

    '); } else { $("#message_ajax").html("
    Sorry " + data.name + ", " + data.email + " is NOT a valid e-mail address. Try again.
    "); } }); // PUT: interface putData { base: string; result: string; fileName: string; } var putData = $('#fileText').val(); fetch('../controllers/php-put.php', { method: 'put', headers: { "Content-type": "application/x-www-form-urlencoded; charset=UTF-8" }, body: putData }) .then($.json) .then(function(data:any): any { console.dir(data.base); $("#message_ajax").append('

    ' + data.result + '

    '); $("#message_ajax").append('

    The file name is: ' + data.fileName + '

    '); }) .catch(function(error:Error) { console.log(error); $("#message_ajax").html("
    Sorry, put was not successful.
    "); }); // DELETE: interface deleteData { result: string; } var file = $('#fileName').val(); fetch('../controllers/php-delete.php', { method: 'delete', headers: { "Content-type": "application/x-www-form-urlencoded; charset=UTF-8" }, body: file }) .then($.json) .then(function(data: any): any { $("#message_ajax").html("
    DELETE was sent to the server successfully.
    "); $("#message_ajax").append('

    ' + data.result + '

    '); }, function(data: any) { console.log('PROBLEM') console.log(data); }) .catch(function(error: any) { $("#message_ajax").html("
    Sorry, 'DELETE' was not successful.
    "); error.reject(); }); // Timeout: var formData2 = $.serialize($('form')[0]); fetch('../controllers/php-post.php', { method: 'post', headers: { "Content-type": "application/x-www-form-urlencoded; charset=UTF-8" }, body: formData, // Set timeout: timeout: 10000 }) .then($.json) .then(function (data: any): any { if (data.email_check == "valid") { $("#message_ajax").html("
    " + data.email + " is a valid e-mail address. Thank you, " + data.name + ".
    "); $("#message_ajax").append('

    ' + data.msg + '

    '); } else { $("#message_ajax").html("
    Sorry " + data.name + ", " + data.email + " is NOT a valid e-mail address. Try again.
    "); } }) // Catch timeout: .catch(function(error) { console.log(error); }); // $.jsonp: $.jsonp('https://api.github.com/users/rbiggs/repos?name=chipper', {timeout: 10000}) .then($.json) .then(function(obj: any): any { console.log(obj); obj.data.forEach(function(repo: any): any { $('#message_ajax').append("
  • " + repo.name + "
  • "); }); }) .catch(function(error: any): any { $('#message_ajax').append("
  • " + error.message + "
  • ") }); // Templates: var myTemplate = '
  • Name: [[= data.name]]
  • '; var userInfo = { name: 'Wobba', age: 100, job: 'Rocket Scientist', salary: '$1,000,000,000' }; var parsedTempl8 = $.template(myTemplate); $('#user').html(parsedTempl8(userInfo)); // Output a simple array of data: var simpleArray = ['One', 'Two', 'Three', 'Four', 'Five']; var repeaterTmplate1 = '
  • [[= data ]]
  • '; $.template.repeater($('#arrayList'), repeaterTmplate1, simpleArray); // Output an array of objects: var luminaries = { persons: [ { firstName: "Albert", lastName: "Einstein" }, { firstName: "Steven", lastName: "Hawking" }, { firstName: "Neil", lastName: "deGrasse Tyson" }, { firstName: "Leonardo", lastName: "Da Vinci" }, { firstName: "Nicholas", lastName: "Copernicus" } ] }; var repeaterTmplate2 = '
  • [[= data.firstName ]], [[= data.lastName]]
  • '; // Pass in the array of persons: $.template.repeater($('#objectArrayList'), repeaterTmplate2, luminaries.persons); // Code for declarative repeater: $.template.data['myRepeater'] = [{ name: "Joe" }, { name: "Sally" }, {name: "Tom" }]; $.template.repeater(); // Pub/Sub: var arraySubscriber = function(topic: string, data: any): any { $('.list').append('
  • ' + topic + '

    ' + data + '

  • '); }; var newsSubscription = $.subscribe('news/update', arraySubscriber); $.publish('news/update', 'The New York Stock Exchange rose an unprecedented 1000 points in just three minutes. Analysts and investors are confused and uncertain how to respond.'); $.unsubscribe('news/update'); // Due to being unsubscribed above, this does nothing: $.publish('news/update', 'We have nothing further to comment at this time.');