<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">define(["jquery", "localforage", "jssha"], function($, localforage, jsSHA) {

	localforage.config({
		driver      : localforage.LOCALSTORAGE, // Force WebSQL; same as using setDriver()
		name        : 'IQ',
		version     : 1.0,
		size        : 4980736, // Size of database, in bytes. WebSQL-only for now.
		storeName   : 'keyvaluepairs', // Should be alphanumeric, with underscores.
		description : 'Favoris Illustration Quebec'
	});

	var storageName = "iq-favorites",
		$a          = $("a#nav-favorites"),
		$badge      = $("&lt;span /&gt;").addClass("favorites-badge hide").appendTo( $a );

	// badge animation reset
	$badge.one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function() {
		$(this).removeClass("animated bounceIn");
	});

	// dummy favorites data
	// localforage.setItem(storageName, [(new Date()).getTime(), (new Date()).getTime(), 523543,5254325423,35325243534]);

	var Favorites = {

		initialize : function() {
			// set Icons
			Favorites.setButtons();

			// register events
			Favorites.register();

			// set favorited and toolbar badge after 500ms
			setTimeout(function() {
				Favorites.favoritesList();
				Favorites.setBadge();
			}, 500);

			// button in header
			Favorites.list();
		},

		setButtons : function() {
			$("[data-favorites]").each(function() {
				var $e = $(this),
					$img = $(this).find('img'),
					$btn = $("&lt;a/&gt;").html('&lt;i class="icon-star"&gt;&lt;/i&gt;').addClass('add-to-favorites hint--right').attr('data-hint', LOCALES["add_to_favorites"]);

				$btn.appendTo( $e );
			});
		},

		favoritesList : function() {
			localforage.getItem(storageName, function(err, data) {
				$(data).each(function(i, e) {
					var src = e[0].src,
						$img = $("img[src='" + src + "']");

					// set image favorited
					Favorites.setFavorite($img);
				});
			});
		},

		setFavorite : function($img) {
			$img.parent().parent().addClass('favorited');
		},

		setBadge : function() {
			localforage.getItem(storageName, function(err, data) {
				if (data !== null) {
					$badge.removeClass('hide').text(data.length).addClass('animated bounceIn');
				} else {
					$badge.addClass('hide');
				}
			});
		},

		register : function() {
			$("[data-favorites] a.add-to-favorites").on('click', function(event) {
				var $parent = $(this).parent(),
					$img = $parent.find('img'),
					src = $img.attr('src') || $img.css('background-image'),
					title = $img.attr('alt') || src;

				// delay half a second and add to favorites
				$(this).delay(500).queue(function(){
					$(this).addClass('animated bounceIn');
					Favorites.closeModal();
					Favorites.setFavorite($img);
					Favorites.addFavorite(src, title);
				});

			});
		},

		addFavorite : function(src, title) {
			var shaObj = new jsSHA(src, "TEXT"),
				hash = shaObj.getHash("SHA-1", "HEX");

			var newData = [{
				hash: hash,
				src: src,
				title: title
			}];

			// console.log(newData);

			localforage.getItem(storageName, function(err, data) {
				var current = data;

				if ( !Favorites.hashInFavorites(current, hash) ) {

					// console.log(current);
					if (data != null) {
						current.push(newData);
					} else {
						current = [newData];
					}
					// console.log(data);

					localforage.setItem(storageName, current, function() {
						Favorites.setBadge();
					});

				};


			});

		},

		hashInFavorites : function(data, hash) {
			var exist = false;

			$(data).each(function(i, e) {
				if (e[0].hash === hash)
					exist = true;
			});

			return exist;
		},

		list : function() {
			$("a#nav-favorites").on('click', function(event) {
				event.preventDefault();
				/* Act on the event */
				var $e = $(this);

				localforage.getItem(storageName, function(err, data) {
					Favorites.modal(data, $e.parent());
				});
			});
		},

		modal : function(data, $container) {
			var $current = $("aside#favorites-list");

			if ($current.get(0)) {
				Favorites.closeModal();
			} else {
				var $modal  = $("&lt;aside/&gt;").attr('id', "favorites-list").appendTo( $container ).addClass('visible'),
					$header = $("&lt;h4/&gt;").text(LOCALES["favorites"]).appendTo( $modal ),
					$imagesContainer = $("&lt;div/&gt;").addClass('images-container').appendTo( $modal ),
					params  = {};

				$(data).each(function(i, e) {
					var item = e[0];

					// $.extend(params, item);
					params[i] = item;

					// console.log(item);
					var $article = $("&lt;img/&gt;").attr('src', item.src).attr('width', 300).addClass('img-responsive').appendTo( $imagesContainer );

					// set image favorited
					// Favorites.setFavorite($img);
				});


				// then finaly insert button print button
				$btn = $("&lt;a/&gt;")
					.attr("href", "/fr/favorites-print/")
					.attr("target", "_blank")
					.addClass('btn btn-default block small')
					.text(LOCALES["print"])
					.appendTo( $modal );

				$btn.on('click', function(event) {
					Favorites.closeModal();
				});
			}


		},

		closeModal : function() {
			$("aside#favorites-list").fadeOut(250, function() {
				$(this).remove();
			});
		},

		printPage : function() {
			var $container = $("section#favorites-list");

			setTimeout(function() {
				localforage.getItem(storageName, function(err, data) {
					$(data).each(function(i, e) {
						// current favorite item data
						var item = e[0];

						// container
						var $favorite = $("&lt;article/&gt;").addClass('favorite-print').appendTo( $container );

						// title
						var $title = $("&lt;h4/&gt;").text(item.title).appendTo( $favorite );

						// create image and append
						var $img = $("&lt;img/&gt;").attr('src', item.src).addClass('img-responsive').appendTo( $favorite );

					});
				});
			}, 500);

		}

	};

	return Favorites;

});
</pre></body></html>