Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 140 lines (108 sloc) 4.901 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
<!DOCTYPE html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <title></title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width">

        <!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
        <link rel="stylesheet" type="text/css" media="all" href="css/oocss/all.css"/>

        <style type="text/css">
        #pagesarea {
            float: left;
            width: 644px;
            margin-left: 16px;
            margin-bottom: 60px;
            border: 1px solid #DCDDDE;
            border-radius: 5px;
        }
         
        </style>

        <script src="js/q/0.8.10/q.min.js" type="text/javascript"></script>
        <script src="js/jquery/1.8.1/jquery.min.js" type="text/javascript"></script>
        <script src="js/arraylike/arraylike.js" type="text/javascript"></script>
        
    </head>
    <body class="polkadots">
        <!--[if lt IE 7]>
<p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">activate Google Chrome Frame</a> to improve your experience.</p>
<![endif]-->
        <div class="page" style="height:800px;">

            <div id="pagesarea">
                <div id="pagesarea-inner-wrap" style="height: 500px;">
            
                </div>
            </div>

        </div>

        <script type="text/javascript">

            var pageSizes = new ArrayLike();
            var pages = new ArrayLike();

            var SIZES = new Object();
            SIZES['Original'] = {'ratio' : 0.70};
            SIZES['Common'] = {'ratio' : 0.65};

            function traverseFiles() {
                // collect all the promises
                // of the traversed files undergoing calculateRatio
                var promises = [];
                
                for (var i=0, l=pages.length; i<l; i++) {
                    promises.push(
                        getSizeSettingsFromPage(pages[i], calculateRatio)
                    );
                }
                // this returns 0
                // *** Call checkWhenReady after all promises have been resolved
                Q.all(promises).then(checkWhenReady);
                
            }

            function getSizeSettingsFromPage(file, whenReady) {
                // *** Create a Deferred
                var deferred = Q.defer();
                reader = new FileReader();
                reader.onload = function(evt) {
                    var image = new Image();
                    image.onload = function(evt) {
                        var width = this.width;
                        var height = this.height;
                        var filename = file.name;
                        if (whenReady) {
                            whenReady(width, height, filename);
                        }
                        // *** Resolve the Deferred
                        deferred.resolve();
                    };
                    image.src = evt.target.result;
                };
                reader.readAsDataURL(file);
                // *** Return a Promise
                return deferred.promise;
            }

            function calculateRatio(width, height, filename) {

                var ratio = width/height;

                var object = new Object();
                object['height'] = width;
                object['width'] = height;
                object['ratio'] = ratio;
                object['size'] = 'Original';

                for (var size in SIZES) {
                    var min = SIZES[size].ratio - 0.01;
                    var max = SIZES[size].ratio + 0.01;
            
                    if (ratio <= max && ratio >= min) {
                        object['size'] = size;
                    }
                }

                pageSizes.add(filename, object);
            }

            function checkWhenReady() {
                alert(pageSizes.length);
            }

            $(document).ready(function (){

                pageareaInnerWrap = document.getElementById("pagesarea-inner-wrap");

                pageareaInnerWrap.addEventListener("drop", function (evt) {
                    pages.convertFrom(evt.dataTransfer.files);
                    // after the above code, pages will now contain all the
                    // files dropped into the div#pagesarea-inner-wrap
                    traverseFiles();
                
                    evt.preventDefault();
                    evt.stopPropagation();
                    pageareaInnerWrap.classList.remove("over");

                }, false);

            });


        </script>
        
    </body>
</html>
Something went wrong with that request. Please try again.