vuePlatform.js 39.9 KB
Newer Older
李晓兵's avatar
李晓兵 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*!
 * vum.bundle.js is a concatenation of:
 * vum.js, angular.js, angular-animate.js,
 * angular-sanitize.js, angular-ui-router.js,
 * and vum-angular.js
 */

/*!
 * Copyright 2015 Drifty Co.
 * http://drifty.com/
 *
 * vum, v1.3.0
 * A powerful HTML5 mobile app framework.
 * http://vumframework.com/
 *
 * By @maxlynch, @benjsperry, @adamdbradley <3
 *
 * Licensed under the MIT license. Please see LICENSE for more information.
 *
 */
JingChao's avatar
JingChao committed
21
/* eslint-disable */
李晓兵's avatar
李晓兵 committed
22 23 24
(function () {
// Create global vum obj and its namespaces
// build processes may have already created an vum obj
JingChao's avatar
JingChao committed
25
  window.vum = window.vum || {}
李晓兵's avatar
李晓兵 committed
26 27 28
  window.vum.version = '1.3.0';

  (function (window, document, vum) {
JingChao's avatar
JingChao committed
29 30
    var readyCallbacks = []
    var isDomReady = document.readyState === 'complete' || document.readyState === 'interactive'
李晓兵's avatar
李晓兵 committed
31

JingChao's avatar
JingChao committed
32 33
    function domReady () {
      isDomReady = true
李晓兵's avatar
李晓兵 committed
34
      for (var x = 0; x < readyCallbacks.length; x++) {
JingChao's avatar
JingChao committed
35
        vum.requestAnimationFrame(readyCallbacks[x])
李晓兵's avatar
李晓兵 committed
36
      }
JingChao's avatar
JingChao committed
37 38
      readyCallbacks = []
      document.removeEventListener('DOMContentLoaded', domReady)
李晓兵's avatar
李晓兵 committed
39 40 41
    }

    if (!isDomReady) {
JingChao's avatar
JingChao committed
42
      document.addEventListener('DOMContentLoaded', domReady)
李晓兵's avatar
李晓兵 committed
43 44 45 46
    }
  })(window, document, vum);

  (function (window, document, vum) {
JingChao's avatar
JingChao committed
47 48 49 50 51
    function getParameterByName (name) {
      name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]')
      var regex = new RegExp('[\\?&]' + name + '=([^&#]*)'),
        results = regex.exec(location.search)
      return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '))
李晓兵's avatar
李晓兵 committed
52 53
    }

JingChao's avatar
JingChao committed
54 55 56 57 58 59 60
    var IOS = 'ios'
    var ANDROID = 'android'
    var WINDOWS_PHONE = 'windowsphone'
    var EDGE = 'edge'
    var CROSSWALK = 'crosswalk'
    var requestAnimationFrame = requestAnimationFrame // eslint-disable-line
    // From the man himself, Mr. Paul Irish.
李晓兵's avatar
李晓兵 committed
61 62 63 64 65 66 67 68
    // The requestAnimationFrame polyfill
    // Put it on window just to preserve its context
    // without having to use .call
    window._rAF = (function () {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        function (callback) {
JingChao's avatar
JingChao committed
69 70 71
          window.setTimeout(callback, 16)
        }
    })()
李晓兵's avatar
李晓兵 committed
72

JingChao's avatar
JingChao committed
73 74
    function requestAnimationFrame (cb) {
      return window._rAF(cb)
李晓兵's avatar
李晓兵 committed
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    }

    /**
     * @ngdoc utility
     * @name vum.Platform
     * @module vum
     * @description
     * A set of utility methods that can be used to retrieve the device ready state and
     * various other information such as what kind of platform the app is currently installed on.
     *
     * @usage
     * ```js
     * angular.module('PlatformApp', ['vum'])
     * .controller('PlatformCtrl', function($scope) {
   *
   *   vum.Platform.ready(function(){
   *     // will execute when device is ready, or immediately if the device is already ready.
   *   });
   *
   *   var deviceInformation = vum.Platform.device();
   *
   *   var isWebView = vum.Platform.isWebView();
   *   var isIPad = vum.Platform.isIPad();
   *   var isIOS = vum.Platform.isIOS();
   *   var isAndroid = vum.Platform.isAndroid();
   *   var isWindowsPhone = vum.Platform.isWindowsPhone();
   *
   *   var currentPlatform = vum.Platform.platform();
   *   var currentPlatformVersion = vum.Platform.version();
   *
   *   vum.Platform.exitApp(); // stops the app
   * });
     * ```
     */
    var self = vum.Platform = {

      // Put navigator on platform so it can be mocked and set
      // the browser does not allow window.navigator to be set
      navigator: window.navigator,

      /**
       * @ngdoc property
       * @name vum.Platform#isReady
       * @returns {boolean} Whether the device is ready.
       */
      isReady: false,
      /**
       * @ngdoc property
       * @name vum.Platform#isFullScreen
       * @returns {boolean} Whether the device is fullscreen.
       */
      isFullScreen: false,
      /**
       * @ngdoc property
       * @name vum.Platform#platforms
       * @returns {Array(string)} An array of all platforms found.
       */
      platforms: null,
      /**
       * @ngdoc property
       * @name vum.Platform#grade
       * @returns {string} What grade the current platform is.
       */
      grade: null,
      /**
       * @ngdoc property
       * @name vum.Platform#ua
       * @returns {string} What User Agent is.
       */
      ua: navigator.userAgent,

      /**
       * @ngdoc method
       * @name vum.Platform#ready
       * @description
       * Trigger a callback once the device is ready, or immediately
       * if the device is already ready. This method can be run from
       * anywhere and does not need to be wrapped by any additonal methods.
       * When the app is within a WebView (Cordova), it'll fire
       * the callback once the device is ready. If the app is within
       * a web browser, it'll fire the callback after `window.load`.
       * Please remember that Cordova features (Camera, FileSystem, etc) still
       * will not work in a web browser.
       * @param {function} callback The function to call.
       */
      ready: function (cb) {
        // run through tasks to complete now that the device is ready
        if (self.isReady) {
JingChao's avatar
JingChao committed
163
          cb()
李晓兵's avatar
李晓兵 committed
164 165 166
        } else {
          // the platform isn't ready yet, add it to this array
          // which will be called once the platform is ready
JingChao's avatar
JingChao committed
167
          readyCallbacks.push(cb)
李晓兵's avatar
李晓兵 committed
168 169 170 171 172 173 174
        }
      },

      /**
       * @private
       */
      detect: function () {
JingChao's avatar
JingChao committed
175
        self._checkPlatforms()
李晓兵's avatar
李晓兵 committed
176 177 178 179

        requestAnimationFrame(function () {
          // only add to the body class if we got platform info
          for (var i = 0; i < self.platforms.length; i++) {
JingChao's avatar
JingChao committed
180
            document.body.classList.add('platform-' + self.platforms[i])
李晓兵's avatar
李晓兵 committed
181
          }
JingChao's avatar
JingChao committed
182
        })
李晓兵's avatar
李晓兵 committed
183 184 185 186 187 188 189 190 191 192 193
      },

      /**
       * @ngdoc method
       * @name vum.Platform#setGrade
       * @description Set the grade of the device: 'a', 'b', or 'c'. 'a' is the best
       * (most css features enabled), 'c' is the worst.  By default, sets the grade
       * depending on the current device.
       * @param {string} grade The new grade to set.
       */
      setGrade: function (grade) {
JingChao's avatar
JingChao committed
194 195
        var oldGrade = self.grade
        self.grade = grade
李晓兵's avatar
李晓兵 committed
196 197
        requestAnimationFrame(function () {
          if (oldGrade) {
JingChao's avatar
JingChao committed
198
            document.body.classList.remove('grade-' + oldGrade)
李晓兵's avatar
李晓兵 committed
199
          }
JingChao's avatar
JingChao committed
200 201
          document.body.classList.add('grade-' + grade)
        })
李晓兵's avatar
李晓兵 committed
202 203 204 205 206 207 208 209 210
      },

      /**
       * @ngdoc method
       * @name vum.Platform#device
       * @description Return the current device (given by cordova).
       * @returns {object} The device object.
       */
      device: function () {
JingChao's avatar
JingChao committed
211
        return window.device || {}
李晓兵's avatar
李晓兵 committed
212 213 214
      },

      _checkPlatforms: function () {
JingChao's avatar
JingChao committed
215 216
        self.platforms = []
        var grade = 'a'
李晓兵's avatar
李晓兵 committed
217

JingChao's avatar
JingChao committed
218 219
        /*if (self.isWebView()) {
          self.platforms.push('webview')
李晓兵's avatar
李晓兵 committed
220
          if (!(!window.cordova && !window.PhoneGap && !window.phonegap)) {
JingChao's avatar
JingChao committed
221
            self.platforms.push('cordova')
李晓兵's avatar
李晓兵 committed
222
          } else if (typeof window.forge === 'object') {
JingChao's avatar
JingChao committed
223
            self.platforms.push('trigger')
李晓兵's avatar
李晓兵 committed
224 225
          }
        } else {
JingChao's avatar
JingChao committed
226
          self.platforms.push('browser')
李晓兵's avatar
李晓兵 committed
227
        }
JingChao's avatar
JingChao committed
228
        if (self.isIPad()) self.platforms.push('ipad')*/
李晓兵's avatar
李晓兵 committed
229

JingChao's avatar
JingChao committed
230
        var platform = self.platform()
李晓兵's avatar
李晓兵 committed
231
        if (platform) {
JingChao's avatar
JingChao committed
232
          self.platforms.push(platform)
李晓兵's avatar
李晓兵 committed
233

JingChao's avatar
JingChao committed
234
          /*var version = self.version()
李晓兵's avatar
李晓兵 committed
235
          if (version) {
JingChao's avatar
JingChao committed
236
            var v = version.toString()
李晓兵's avatar
李晓兵 committed
237
            if (v.indexOf('.') > 0) {
JingChao's avatar
JingChao committed
238
              v = v.replace('.', '_')
李晓兵's avatar
李晓兵 committed
239
            } else {
JingChao's avatar
JingChao committed
240
              v += '_0'
李晓兵's avatar
李晓兵 committed
241
            }
JingChao's avatar
JingChao committed
242 243
            self.platforms.push(platform + v.split('_')[0])
            self.platforms.push(platform + v)
李晓兵's avatar
李晓兵 committed
244 245

            if (self.isAndroid() && version < 4.4) {
JingChao's avatar
JingChao committed
246
              grade = (version < 4 ? 'c' : 'b')
李晓兵's avatar
李晓兵 committed
247
            } else if (self.isWindowsPhone()) {
JingChao's avatar
JingChao committed
248
              grade = 'b'
李晓兵's avatar
李晓兵 committed
249
            }
JingChao's avatar
JingChao committed
250
          }*/
李晓兵's avatar
李晓兵 committed
251 252
        }

JingChao's avatar
JingChao committed
253
       // self.setGrade(grade)
李晓兵's avatar
李晓兵 committed
254 255 256 257 258 259 260 261
      },

      /**
       * @ngdoc method
       * @name vum.Platform#isWebView
       * @returns {boolean} Check if we are running within a WebView (such as Cordova).
       */
      isWebView: function () {
JingChao's avatar
JingChao committed
262
        return !(!window.cordova && !window.PhoneGap && !window.phonegap && window.forge !== 'object')
李晓兵's avatar
李晓兵 committed
263 264 265 266 267 268 269 270
      },
      /**
       * @ngdoc method
       * @name vum.Platform#isIPad
       * @returns {boolean} Whether we are running on iPad.
       */
      isIPad: function () {
        if (/iPad/i.test(self.navigator.platform)) {
JingChao's avatar
JingChao committed
271
          return true
李晓兵's avatar
李晓兵 committed
272
        }
JingChao's avatar
JingChao committed
273
        return /iPad/i.test(self.ua)
李晓兵's avatar
李晓兵 committed
274 275 276 277 278 279 280
      },
      /**
       * @ngdoc method
       * @name vum.Platform#isIOS
       * @returns {boolean} Whether we are running on iOS.
       */
      isIOS: function () {
JingChao's avatar
JingChao committed
281
        return self.is(IOS)
李晓兵's avatar
李晓兵 committed
282 283 284 285 286 287 288
      },
      /**
       * @ngdoc method
       * @name vum.Platform#isAndroid
       * @returns {boolean} Whether we are running on Android.
       */
      isAndroid: function () {
JingChao's avatar
JingChao committed
289
        return self.is(ANDROID)
李晓兵's avatar
李晓兵 committed
290 291 292 293 294 295 296
      },
      /**
       * @ngdoc method
       * @name vum.Platform#isWindowsPhone
       * @returns {boolean} Whether we are running on Windows Phone.
       */
      isWindowsPhone: function () {
JingChao's avatar
JingChao committed
297
        return self.is(WINDOWS_PHONE)
李晓兵's avatar
李晓兵 committed
298 299 300 301 302 303 304
      },
      /**
       * @ngdoc method
       * @name vum.Platform#isEdge
       * @returns {boolean} Whether we are running on MS Edge/Windows 10 (inc. Phone)
       */
      isEdge: function () {
JingChao's avatar
JingChao committed
305
        return self.is(EDGE)
李晓兵's avatar
李晓兵 committed
306 307 308
      },

      isCrosswalk: function () {
JingChao's avatar
JingChao committed
309
        return self.is(CROSSWALK)
李晓兵's avatar
李晓兵 committed
310 311 312 313 314 315 316 317 318
      },

      /**
       * @ngdoc method
       * @name vum.Platform#platform
       * @returns {string} The name of the current platform.
       */
      platform: function () {
        // singleton to get the platform name
JingChao's avatar
JingChao committed
319 320
        if (platformName === null) self.setPlatform(self.device().platform)
        return platformName
李晓兵's avatar
李晓兵 committed
321 322 323 324 325 326
      },

      /**
       * @private
       */
      setPlatform: function (n) {
JingChao's avatar
JingChao committed
327 328
        if (typeof n !== 'undefined' && n !== null && n.length) {
          platformName = n.toLowerCase()
李晓兵's avatar
李晓兵 committed
329
        } else if (getParameterByName('vumplatform')) {
JingChao's avatar
JingChao committed
330
          platformName = getParameterByName('vumplatform')
李晓兵's avatar
李晓兵 committed
331
        } else if (self.ua.indexOf('Edge') > -1) {
JingChao's avatar
JingChao committed
332
          platformName = EDGE
李晓兵's avatar
李晓兵 committed
333
        } else if (self.ua.indexOf('Windows Phone') > -1) {
JingChao's avatar
JingChao committed
334
          platformName = WINDOWS_PHONE
李晓兵's avatar
李晓兵 committed
335
        } else if (self.ua.indexOf('Android') > 0) {
JingChao's avatar
JingChao committed
336
          platformName = ANDROID
李晓兵's avatar
李晓兵 committed
337
        } else if (/iPhone|iPad|iPod/.test(self.ua)) {
JingChao's avatar
JingChao committed
338
          platformName = IOS
李晓兵's avatar
李晓兵 committed
339
        } else {
JingChao's avatar
JingChao committed
340
          platformName = self.navigator.platform && navigator.platform.toLowerCase().split(' ')[0] || ''
李晓兵's avatar
李晓兵 committed
341 342 343 344 345 346 347 348 349 350
        }
      },

      /**
       * @ngdoc method
       * @name vum.Platform#version
       * @returns {number} The version of the current device platform.
       */
      version: function () {
        // singleton to get the platform version
JingChao's avatar
JingChao committed
351 352
        if (platformVersion === null) self.setVersion(self.device().version)
        return platformVersion
李晓兵's avatar
李晓兵 committed
353 354 355 356 357 358
      },

      /**
       * @private
       */
      setVersion: function (v) {
JingChao's avatar
JingChao committed
359 360 361
        if (typeof v !== 'undefined' && v !== null) {
          v = v.split('.')
          v = parseFloat(v[0] + '.' + (v.length > 1 ? v[1] : 0))
李晓兵's avatar
李晓兵 committed
362
          if (!isNaN(v)) {
JingChao's avatar
JingChao committed
363 364
            platformVersion = v
            return
李晓兵's avatar
李晓兵 committed
365 366 367
          }
        }

JingChao's avatar
JingChao committed
368
        platformVersion = 0
李晓兵's avatar
李晓兵 committed
369 370

        // fallback to user-agent checking
JingChao's avatar
JingChao committed
371
        var pName = self.platform()
李晓兵's avatar
李晓兵 committed
372 373 374
        var versionMatch = {
          'android': /Android (\d+).(\d+)?/,
          'ios': /OS (\d+)_(\d+)?/,
JingChao's avatar
JingChao committed
375 376
          'windowsphone': /Windows Phone (\d+).(\d+)?/,
        }
李晓兵's avatar
李晓兵 committed
377
        if (versionMatch[pName]) {
JingChao's avatar
JingChao committed
378
          v = self.ua.match(versionMatch[pName])
李晓兵's avatar
李晓兵 committed
379
          if (v && v.length > 2) {
JingChao's avatar
JingChao committed
380
            platformVersion = parseFloat(v[1] + '.' + v[2])
李晓兵's avatar
李晓兵 committed
381 382 383 384 385 386 387 388 389 390 391
          }
        }
      },

      /**
       * @ngdoc method
       * @name vum.Platform#is
       * @param {string} Platform name.
       * @returns {boolean} Whether the platform name provided is detected.
       */
      is: function (type) {
JingChao's avatar
JingChao committed
392
        type = type.toLowerCase()
李晓兵's avatar
李晓兵 committed
393 394 395
        // check if it has an array of platforms
        if (self.platforms) {
          for (var x = 0; x < self.platforms.length; x++) {
JingChao's avatar
JingChao committed
396
            if (self.platforms[x] === type) return true
李晓兵's avatar
李晓兵 committed
397 398 399
          }
        }
        // exact match
JingChao's avatar
JingChao committed
400
        var pName = self.platform()
李晓兵's avatar
李晓兵 committed
401
        if (pName) {
JingChao's avatar
JingChao committed
402
          return pName === type.toLowerCase()
李晓兵's avatar
李晓兵 committed
403 404 405
        }

        // A quick hack for to check userAgent
JingChao's avatar
JingChao committed
406
        return self.ua.toLowerCase().indexOf(type) >= 0
李晓兵's avatar
李晓兵 committed
407 408 409 410 411 412 413 414 415
      },

      /**
       * @ngdoc method
       * @name vum.Platform#exitApp
       * @description Exit the app.
       */
      exitApp: function () {
        self.ready(function () {
JingChao's avatar
JingChao committed
416 417
          navigator.app && navigator.app.exitApp && navigator.app.exitApp()
        })
李晓兵's avatar
李晓兵 committed
418 419 420 421 422 423 424 425 426 427
      },

      /**
       * @ngdoc method
       * @name vum.Platform#showStatusBar
       * @description Shows or hides the device status bar (in Cordova). Requires `cordova plugin add org.apache.cordova.statusbar`
       * @param {boolean} shouldShow Whether or not to show the status bar.
       */
      showStatusBar: function (val) {
        // Only useful when run within cordova
JingChao's avatar
JingChao committed
428
        self._showStatusBar = val
李晓兵's avatar
李晓兵 committed
429 430 431 432 433
        self.ready(function () {
          // run this only when or if the platform (cordova) is ready
          requestAnimationFrame(function () {
            if (self._showStatusBar) {
              // they do not want it to be full screen
JingChao's avatar
JingChao committed
434 435
              window.StatusBar && window.StatusBar.show()
              document.body.classList.remove('status-bar-hide')
李晓兵's avatar
李晓兵 committed
436 437
            } else {
              // it should be full screen
JingChao's avatar
JingChao committed
438 439
              window.StatusBar && window.StatusBar.hide()
              document.body.classList.add('status-bar-hide')
李晓兵's avatar
李晓兵 committed
440
            }
JingChao's avatar
JingChao committed
441 442
          })
        })
李晓兵's avatar
李晓兵 committed
443 444 445 446 447 448 449 450 451 452 453 454
      },

      /**
       * @ngdoc method
       * @name vum.Platform#fullScreen
       * @description
       * Sets whether the app is fullscreen or not (in Cordova).
       * @param {boolean=} showFullScreen Whether or not to set the app to fullscreen. Defaults to true. Requires `cordova plugin add org.apache.cordova.statusbar`
       * @param {boolean=} showStatusBar Whether or not to show the device's status bar. Defaults to false.
       */
      fullScreen: function (showFullScreen, showStatusBar) {
        // showFullScreen: default is true if no param provided
JingChao's avatar
JingChao committed
455
        self.isFullScreen = (showFullScreen !== false)
李晓兵's avatar
李晓兵 committed
456 457 458 459 460 461

        // add/remove the fullscreen classname to the body
        vum.DomUtil.ready(function () {
          // run this only when or if the DOM is ready
          requestAnimationFrame(function () {
            if (self.isFullScreen) {
JingChao's avatar
JingChao committed
462
              document.body.classList.add('fullscreen')
李晓兵's avatar
李晓兵 committed
463
            } else {
JingChao's avatar
JingChao committed
464
              document.body.classList.remove('fullscreen')
李晓兵's avatar
李晓兵 committed
465
            }
JingChao's avatar
JingChao committed
466
          })
李晓兵's avatar
李晓兵 committed
467
          // showStatusBar: default is false if no param provided
JingChao's avatar
JingChao committed
468 469 470
          self.showStatusBar((showStatusBar === true))
        })
      },
李晓兵's avatar
李晓兵 committed
471

JingChao's avatar
JingChao committed
472
    }
李晓兵's avatar
李晓兵 committed
473 474 475 476 477

    var platformName = null, // just the name, like iOS or Android
      platformVersion = null, // a float of the major and minor, like 7.1
      readyCallbacks = [],
      windowLoadListenderAttached,
JingChao's avatar
JingChao committed
478
      platformReadyTimer = 2000 // How long to wait for platform ready before emitting a warning
李晓兵's avatar
李晓兵 committed
479

JingChao's avatar
JingChao committed
480
    verifyPlatformReady()
李晓兵's avatar
李晓兵 committed
481 482

    // Warn the user if deviceready did not fire in a reasonable amount of time, and how to fix it.
JingChao's avatar
JingChao committed
483
    function verifyPlatformReady () {
李晓兵's avatar
李晓兵 committed
484 485
      setTimeout(function () {
        if (!self.isReady && self.isWebView()) {
JingChao's avatar
JingChao committed
486
          void 0
李晓兵's avatar
李晓兵 committed
487
        }
JingChao's avatar
JingChao committed
488
      }, platformReadyTimer)
李晓兵's avatar
李晓兵 committed
489 490 491
    }

    // setup listeners to know when the device is ready to go
JingChao's avatar
JingChao committed
492
    function onWindowLoad () {
李晓兵's avatar
李晓兵 committed
493 494 495
      if (self.isWebView()) {
        // the window and scripts are fully loaded, and a cordova/phonegap
        // object exists then let's listen for the deviceready
JingChao's avatar
JingChao committed
496
        document.addEventListener('deviceready', onPlatformReady, false)
李晓兵's avatar
李晓兵 committed
497 498 499
      } else {
        // the window and scripts are fully loaded, but the window object doesn't have the
        // cordova/phonegap object, so its just a browser, not a webview wrapped w/ cordova
JingChao's avatar
JingChao committed
500
        onPlatformReady()
李晓兵's avatar
李晓兵 committed
501 502
      }
      if (windowLoadListenderAttached) {
JingChao's avatar
JingChao committed
503
        window.removeEventListener('load', onWindowLoad, false)
李晓兵's avatar
李晓兵 committed
504 505 506 507
      }
    }

    if (document.readyState === 'complete') {
JingChao's avatar
JingChao committed
508
      onWindowLoad()
李晓兵's avatar
李晓兵 committed
509
    } else {
JingChao's avatar
JingChao committed
510 511
      windowLoadListenderAttached = true
      window.addEventListener('load', onWindowLoad, false)
李晓兵's avatar
李晓兵 committed
512 513
    }

JingChao's avatar
JingChao committed
514
    function onPlatformReady () {
李晓兵's avatar
李晓兵 committed
515
      // the device is all set to go, init our own stuff then fire off our event
JingChao's avatar
JingChao committed
516 517
      self.isReady = true
      self.detect()
李晓兵's avatar
李晓兵 committed
518 519
      for (var x = 0; x < readyCallbacks.length; x++) {
        // fire off all the callbacks that were added before the platform was ready
JingChao's avatar
JingChao committed
520
        readyCallbacks[x]()
李晓兵's avatar
李晓兵 committed
521
      }
JingChao's avatar
JingChao committed
522 523
      readyCallbacks = []
      vum.trigger('platformready', {target: document})
李晓兵's avatar
李晓兵 committed
524 525

      requestAnimationFrame(function () {
JingChao's avatar
JingChao committed
526 527
        document.body.classList.add('platform-ready')
      })
李晓兵's avatar
李晓兵 committed
528 529
    }

JingChao's avatar
JingChao committed
530
    // document.addEventListener('')
李晓兵's avatar
李晓兵 committed
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
  })(window, document, vum);
  /**
   * ion-events.js
   *
   * Author: Max Lynch <max@drifty.com>
   *
   * Framework events handles various mobile browser events, and
   * detects special events like tap/swipe/etc. and emits them
   * as custom events that can be used in an app.
   *
   * Portions lovingly adapted from github.com/maker/ratchet and github.com/alexgibson/tap.js - thanks guys!
   */

  (function (vum) {
    // Custom event polyfill
    vum.CustomEvent = (function () {
JingChao's avatar
JingChao committed
547
      if (typeof window.CustomEvent === 'function') return CustomEvent
李晓兵's avatar
李晓兵 committed
548 549

      var customEvent = function (event, params) {
JingChao's avatar
JingChao committed
550
        var evt
李晓兵's avatar
李晓兵 committed
551 552 553
        params = params || {
          bubbles: false,
          cancelable: false,
JingChao's avatar
JingChao committed
554 555
          detail: undefined,
        }
李晓兵's avatar
李晓兵 committed
556
        try {
JingChao's avatar
JingChao committed
557 558
          evt = document.createEvent('CustomEvent')
          evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail)
李晓兵's avatar
李晓兵 committed
559 560
        } catch (error) {
          // fallback for browsers that don't support createEvent('CustomEvent')
JingChao's avatar
JingChao committed
561
          evt = document.createEvent('Event')
李晓兵's avatar
李晓兵 committed
562
          for (var param in params) {
JingChao's avatar
JingChao committed
563
            evt[param] = params[param]
李晓兵's avatar
李晓兵 committed
564
          }
JingChao's avatar
JingChao committed
565
          evt.initEvent(event, params.bubbles, params.cancelable)
李晓兵's avatar
李晓兵 committed
566
        }
JingChao's avatar
JingChao committed
567 568 569 570 571
        return evt
      }
      customEvent.prototype = window.Event.prototype
      return customEvent
    })()
李晓兵's avatar
李晓兵 committed
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595

    /**
     * @ngdoc utility
     * @name vum.EventController
     * @module vum
     */
    vum.EventController = {
      VIRTUALIZED_EVENTS: ['tap', 'swipe', 'swiperight', 'swipeleft', 'drag', 'hold', 'release'],

      /**
       * @ngdoc method
       * @name vum.EventController#trigger
       * @alias vum.trigger
       * @param {string} eventType The event to trigger.
       * @param {object} data The data for the event. Hint: pass in
       * `{target: targetElement}`
       * @param {boolean=} bubbles Whether the event should bubble up the DOM.
       * @param {boolean=} cancelable Whether the event should be cancelable.
       */
      // Trigger a new event
      trigger: function (eventType, data, bubbles, cancelable) {
        var event = new vum.CustomEvent(eventType, {
          detail: data,
          bubbles: !!bubbles,
JingChao's avatar
JingChao committed
596 597
          cancelable: !!cancelable,
        })
李晓兵's avatar
李晓兵 committed
598 599 600

        // Make sure to trigger the event on the given target, or dispatch it from
        // the window if we don't have an event target
JingChao's avatar
JingChao committed
601
        data && data.target && data.target.dispatchEvent && data.target.dispatchEvent(event) || window.dispatchEvent(event)
李晓兵's avatar
李晓兵 committed
602 603 604 605 606 607 608 609 610 611 612 613
      },

      /**
       * @ngdoc method
       * @name vum.EventController#on
       * @alias vum.on
       * @description Listen to an event on an element.
       * @param {string} type The event to listen for.
       * @param {function} callback The listener to be called.
       * @param {DOMElement} element The element to listen for the event on.
       */
      on: function (type, callback, element) {
JingChao's avatar
JingChao committed
614
        var e = element || window
李晓兵's avatar
李晓兵 committed
615 616 617 618

        // Bind a gesture if it's a virtual event
        for (var i = 0, j = this.VIRTUALIZED_EVENTS.length; i < j; i++) {
          if (type == this.VIRTUALIZED_EVENTS[i]) {
JingChao's avatar
JingChao committed
619 620 621
            var gesture = new vum.Gesture(element)
            gesture.on(type, callback)
            return gesture
李晓兵's avatar
李晓兵 committed
622 623 624 625
          }
        }

        // Otherwise bind a normal event
JingChao's avatar
JingChao committed
626
        e.addEventListener(type, callback)
李晓兵's avatar
李晓兵 committed
627 628 629 630 631 632 633 634 635 636 637 638
      },

      /**
       * @ngdoc method
       * @name vum.EventController#off
       * @alias vum.off
       * @description Remove an event listener.
       * @param {string} type
       * @param {function} callback
       * @param {DOMElement} element
       */
      off: function (type, callback, element) {
JingChao's avatar
JingChao committed
639
        element.removeEventListener(type, callback)
李晓兵's avatar
李晓兵 committed
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
      },

      /**
       * @ngdoc method
       * @name vum.EventController#onGesture
       * @alias vum.onGesture
       * @description Add an event listener for a gesture on an element.
       *
       * Available eventTypes (from [hammer.js](http://eightmedia.github.io/hammer.js/)):
       *
       * `hold`, `tap`, `doubletap`, `drag`, `dragstart`, `dragend`, `dragup`, `dragdown`, <br/>
       * `dragleft`, `dragright`, `swipe`, `swipeup`, `swipedown`, `swipeleft`, `swiperight`, <br/>
       * `transform`, `transformstart`, `transformend`, `rotate`, `pinch`, `pinchin`, `pinchout`, <br/>
       * `touch`, `release`
       *
       * @param {string} eventType The gesture event to listen for.
       * @param {function(e)} callback The function to call when the gesture
       * happens.
       * @param {DOMElement} element The angular element to listen for the event on.
       * @param {object} options object.
       * @returns {vum.Gesture} The gesture object (use this to remove the gesture later on).
       */
      onGesture: function (type, callback, element, options) {
JingChao's avatar
JingChao committed
663 664 665
        var gesture = new vum.Gesture(element, options)
        gesture.on(type, callback)
        return gesture
李晓兵's avatar
李晓兵 committed
666 667 668 669 670 671 672 673 674 675 676 677 678
      },

      /**
       * @ngdoc method
       * @name vum.EventController#offGesture
       * @alias vum.offGesture
       * @description Remove an event listener for a gesture created on an element.
       * @param {vum.Gesture} gesture The gesture that should be removed.
       * @param {string} eventType The gesture event to remove the listener for.
       * @param {function(e)} callback The listener to remove.

       */
      offGesture: function (gesture, type, callback) {
JingChao's avatar
JingChao committed
679
        gesture && gesture.off(type, callback)
李晓兵's avatar
李晓兵 committed
680 681 682
      },

      handlePopState: function () {
JingChao's avatar
JingChao committed
683 684
      },
    }
李晓兵's avatar
李晓兵 committed
685 686 687

    // Map some convenient top-level functions for event handling
    vum.on = function () {
JingChao's avatar
JingChao committed
688 689
      vum.EventController.on.apply(vum.EventController, arguments)
    }
李晓兵's avatar
李晓兵 committed
690
    vum.off = function () {
JingChao's avatar
JingChao committed
691 692 693
      vum.EventController.off.apply(vum.EventController, arguments)
    }
    vum.trigger = vum.EventController.trigger// function() { vum.EventController.trigger.apply(vum.EventController.trigger, arguments); };
李晓兵's avatar
李晓兵 committed
694
    vum.onGesture = function () {
JingChao's avatar
JingChao committed
695 696
      return vum.EventController.onGesture.apply(vum.EventController.onGesture, arguments)
    }
李晓兵's avatar
李晓兵 committed
697
    vum.offGesture = function () {
JingChao's avatar
JingChao committed
698 699
      return vum.EventController.offGesture.apply(vum.EventController.offGesture, arguments)
    }
李晓兵's avatar
李晓兵 committed
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
  })(window.vum);

  (function (vum) {
    vum.$vumPlatform = {

      /**
       * @ngdoc method
       * @name $vumPlatform#onHardwareBackButton
       * @description
       * Some platforms have a hardware back button, so this is one way to
       * bind to it.
       * @param {function} callback the callback to trigger when this event occurs
       */
      onHardwareBackButton: function (cb) {
        vum.Platform.ready(function () {
JingChao's avatar
JingChao committed
715 716
          document.addEventListener('backbutton', cb, false)
        })
李晓兵's avatar
李晓兵 committed
717 718 719 720 721 722 723 724 725 726 727 728
      },

      /**
       * @ngdoc method
       * @name $vumPlatform#offHardwareBackButton
       * @description
       * Remove an event listener for the backbutton.
       * @param {function} callback The listener function that was
       * originally bound.
       */
      offHardwareBackButton: function (fn) {
        vum.Platform.ready(function () {
JingChao's avatar
JingChao committed
729 730
          document.removeEventListener('backbutton', fn)
        })
李晓兵's avatar
李晓兵 committed
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
      },

      /**
       * @ngdoc method
       * @name $vumPlatform#registerBackButtonAction
       * @description
       * Register a hardware back button action. Only one action will execute
       * when the back button is clicked, so this method decides which of
       * the registered back button actions has the highest priority.
       *
       * For example, if an actionsheet is showing, the back button should
       * close the actionsheet, but it should not also go back a page view
       * or close a modal which may be open.
       *
       * The priorities for the existing back button hooks are as follows:
       *   Return to previous view = 100
       *   Close side menu = 150
       *   Dismiss modal = 200
       *   Close action sheet = 300
       *   Dismiss popup = 400
       *   Dismiss loading overlay = 500
       *
       * Your back button action will override each of the above actions
       * whose priority is less than the priority you provide. For example,
       * an action assigned a priority of 101 will override the 'return to
       * previous view' action, but not any of the other actions.
       *
       * @param {function} callback Called when the back button is pressed,
       * if this listener is the highest priority.
       * @param {number} priority Only the highest priority will execute.
       * @param {*=} actionId The id to assign this action. Default: a
       * random unique id.
       * @returns {function} A function that, when called, will deregister
       * this backButtonAction.
       */
      $backButtonActions: {},
      registerBackButtonAction: function (fn, priority, actionId) {
        if (!vum.$vumPlatform._hasBackButtonHandler) {
          // add a back button listener if one hasn't been setup yet
JingChao's avatar
JingChao committed
770 771 772
          vum.$vumPlatform.$backButtonActions = {}
          vum.$vumPlatform.onHardwareBackButton(vum.$vumPlatform.hardwareBackButtonClick)
          vum.$vumPlatform._hasBackButtonHandler = true
李晓兵's avatar
李晓兵 committed
773 774 775
        }

        var action = {
JingChao's avatar
JingChao committed
776 777 778 779 780
          id: (actionId || vum.Utils.nextUid()),
          priority: (priority || 0),
          fn: fn,
        }
        vum.$vumPlatform.$backButtonActions[action.id] = action
李晓兵's avatar
李晓兵 committed
781 782 783

        // return a function to de-register this back button action
        return function () {
JingChao's avatar
JingChao committed
784 785
          delete vum.$vumPlatform.$backButtonActions[action.id]
        }
李晓兵's avatar
李晓兵 committed
786 787 788 789 790 791 792 793
      },

      /**
       * @private
       */
      hardwareBackButtonClick: function (e) {
        // loop through all the registered back button actions
        // and only run the last one of the highest priority
JingChao's avatar
JingChao committed
794
        var priorityAction, actionId
李晓兵's avatar
李晓兵 committed
795 796
        for (actionId in vum.$vumPlatform.$backButtonActions) {
          if (!priorityAction || vum.$vumPlatform.$backButtonActions[actionId].priority >= priorityAction.priority) {
JingChao's avatar
JingChao committed
797
            priorityAction = vum.$vumPlatform.$backButtonActions[actionId]
李晓兵's avatar
李晓兵 committed
798 799 800
          }
        }
        if (priorityAction) {
JingChao's avatar
JingChao committed
801 802
          priorityAction.fn(e)
          return priorityAction
李晓兵's avatar
李晓兵 committed
803 804 805 806
        }
      },

      is: function (type) {
JingChao's avatar
JingChao committed
807
        return vum.Platform.is(type)
李晓兵's avatar
李晓兵 committed
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
      },

      /**
       * @ngdoc method
       * @name $vumPlatform#on
       * @description
       * Add Cordova event listeners, such as `pause`, `resume`, `volumedownbutton`, `batterylow`,
       * `offline`, etc. More information about available event types can be found in
       * [Cordova's event documentation](https://cordova.apache.org/docs/en/edge/cordova_events_events.md.html#Events).
       * @param {string} type Cordova [event type](https://cordova.apache.org/docs/en/edge/cordova_events_events.md.html#Events).
       * @param {function} callback Called when the Cordova event is fired.
       * @returns {function} Returns a deregistration function to remove the event listener.
       */
      on: function (type, cb) {
        vum.Platform.ready(function () {
JingChao's avatar
JingChao committed
823 824
          document.addEventListener(type, cb, false)
        })
李晓兵's avatar
李晓兵 committed
825 826
        return function () {
          vum.Platform.ready(function () {
JingChao's avatar
JingChao committed
827 828 829
            document.removeEventListener(type, cb)
          })
        }
李晓兵's avatar
李晓兵 committed
830 831 832 833 834 835 836 837 838 839 840 841 842
      },

      /**
       * @ngdoc method
       * @name $vumPlatform#ready
       * @description
       * Trigger a callback once the device is ready,
       * or immediately if the device is already ready.
       * @param {function=} callback The function to call.
       */
      ready: function (cb) {
        vum.Platform.ready(function () {
          cb()
JingChao's avatar
JingChao committed
843
        })
李晓兵's avatar
李晓兵 committed
844 845 846
      },

      getWinSize: function () {
JingChao's avatar
JingChao committed
847 848 849 850 851 852 853 854
        let winWidth
        let winHeight
        // 获取窗口宽度
        if (window.innerWidth) { winWidth = window.innerWidth } else if ((document.body) && (document.body.clientWidth))
        // 获取窗口高度
        { winWidth = document.body.clientWidth }
        if (window.innerHeight) { winHeight = window.innerHeight } else if ((document.body) && (document.body.clientHeight)) { winHeight = document.body.clientHeight }
        // 通过深入Document内部对body进行检测,获取窗口大小
李晓兵's avatar
李晓兵 committed
855
        if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth) {
JingChao's avatar
JingChao committed
856 857
          winHeight = document.documentElement.clientHeight
          winWidth = document.documentElement.clientWidth
李晓兵's avatar
李晓兵 committed
858
        }
JingChao's avatar
JingChao committed
859 860
        return {'width': winWidth, 'height': winHeight}
      },
李晓兵's avatar
李晓兵 committed
861

JingChao's avatar
JingChao committed
862
    }
李晓兵's avatar
李晓兵 committed
863

JingChao's avatar
JingChao committed
864
    return vum.$vumPlatform
李晓兵's avatar
李晓兵 committed
865 866 867
  })(window.vum);

  (function () {
JingChao's avatar
JingChao committed
868
    var nextId = 0
李晓兵's avatar
李晓兵 committed
869 870
    vum.Utils = {
      nextUid: function () {
JingChao's avatar
JingChao committed
871
        return 'vue' + (nextId++)
李晓兵's avatar
李晓兵 committed
872 873 874
      },
    }

JingChao's avatar
JingChao committed
875 876
    var jqLite // delay binding since jQuery could be loaded after us.
    var hasOwnProperty = Object.prototype.hasOwnProperty
李晓兵's avatar
李晓兵 committed
877 878 879 880 881 882 883 884 885 886 887 888 889 890

    /**
     * @ngdoc function
     * @name angular.isUndefined
     * @module ng
     * @kind function
     *
     * @description
     * Determines if a reference is undefined.
     *
     * @param {*} value Reference to check.
     * @returns {boolean} True if `value` is undefined.
     */
    vum.isUndefined = function (value) {
JingChao's avatar
JingChao committed
891
      return typeof value === 'undefined'
李晓兵's avatar
李晓兵 committed
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
    }

    /**
     * @ngdoc function
     * @name angular.isDefined
     * @module ng
     * @kind function
     *
     * @description
     * Determines if a reference is defined.
     *
     * @param {*} value Reference to check.
     * @returns {boolean} True if `value` is defined.
     */
    vum.isDefined = function (value) {
JingChao's avatar
JingChao committed
907
      return typeof value !== 'undefined'
李晓兵's avatar
李晓兵 committed
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
    }

    /**
     * @ngdoc function
     * @name angular.isObject
     * @module ng
     * @kind function
     *
     * @description
     * Determines if a reference is an `Object`. Unlike `typeof` in JavaScript, `null`s are not
     * considered to be objects. Note that JavaScript arrays are objects.
     *
     * @param {*} value Reference to check.
     * @returns {boolean} True if `value` is an `Object` but not `null`.
     */
    vum.isObject = function (value) {
      // http://jsperf.com/isobject4
JingChao's avatar
JingChao committed
925
      return value !== null && typeof value === 'object'
李晓兵's avatar
李晓兵 committed
926 927 928 929 930 931 932 933
    }

    /**
     * Determine if a value is an object with a null prototype
     *
     * @returns {boolean} True if `value` is an `Object` with a null prototype
     */
    vum.isBlankObject = function (value) {
JingChao's avatar
JingChao committed
934
      return value !== null && typeof value === 'object' && !getPrototypeOf(value)
李晓兵's avatar
李晓兵 committed
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
    }

    /**
     * @ngdoc function
     * @name angular.isString
     * @module ng
     * @kind function
     *
     * @description
     * Determines if a reference is a `String`.
     *
     * @param {*} value Reference to check.
     * @returns {boolean} True if `value` is a `String`.
     */
    vum.isString = function (value) {
JingChao's avatar
JingChao committed
950
      return typeof value === 'string'
李晓兵's avatar
李晓兵 committed
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
    }

    /**
     * @ngdoc function
     * @name angular.isNumber
     * @module ng
     * @kind function
     *
     * @description
     * Determines if a reference is a `Number`.
     *
     * This includes the "special" numbers `NaN`, `+Infinity` and `-Infinity`.
     *
     * If you wish to exclude these then you can use the native
     * [`isFinite'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isFinite)
     * method.
     *
     * @param {*} value Reference to check.
     * @returns {boolean} True if `value` is a `Number`.
     */
    vum.isNumber = function (value) {
JingChao's avatar
JingChao committed
972
      return typeof value === 'number'
李晓兵's avatar
李晓兵 committed
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
    }

    /**
     * @ngdoc function
     * @name angular.isDate
     * @module ng
     * @kind function
     *
     * @description
     * Determines if a value is a date.
     *
     * @param {*} value Reference to check.
     * @returns {boolean} True if `value` is a `Date`.
     */
    vum.isDate = function (value) {
JingChao's avatar
JingChao committed
988
      return toString.call(value) === '[object Date]'
李晓兵's avatar
李晓兵 committed
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
    }

    /**
     * @ngdoc function
     * @name angular.isArray
     * @module ng
     * @kind function
     *
     * @description
     * Determines if a reference is an `Array`.
     *
     * @param {*} value Reference to check.
     * @returns {boolean} True if `value` is an `Array`.
     */
JingChao's avatar
JingChao committed
1003
    var isArray = Array.isArray
李晓兵's avatar
李晓兵 committed
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017

    /**
     * @ngdoc function
     * @name angular.isFunction
     * @module ng
     * @kind function
     *
     * @description
     * Determines if a reference is a `Function`.
     *
     * @param {*} value Reference to check.
     * @returns {boolean} True if `value` is a `Function`.
     */
    vum.isFunction = function (value) {
JingChao's avatar
JingChao committed
1018
      return typeof value === 'function'
李晓兵's avatar
李晓兵 committed
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
    }

    /**
     * Determines if a value is a regular expression object.
     *
     * @private
     * @param {*} value Reference to check.
     * @returns {boolean} True if `value` is a `RegExp`.
     */
    vum.isRegExp = function (value) {
JingChao's avatar
JingChao committed
1029
      return toString.call(value) === '[object RegExp]'
李晓兵's avatar
李晓兵 committed
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
    }

    /**
     * Checks if `obj` is a window object.
     *
     * @private
     * @param {*} obj Object to check
     * @returns {boolean} True if `obj` is a window obj.
     */
    vum.isWindow = function (obj) {
JingChao's avatar
JingChao committed
1040
      return obj && obj.window === obj
李晓兵's avatar
李晓兵 committed
1041 1042 1043
    }

    vum.isScope = function (obj) {
JingChao's avatar
JingChao committed
1044
      return obj && obj.$evalAsync && obj.$watch
李晓兵's avatar
李晓兵 committed
1045 1046 1047
    }

    vum.isFile = function (obj) {
JingChao's avatar
JingChao committed
1048
      return toString.call(obj) === '[object File]'
李晓兵's avatar
李晓兵 committed
1049 1050 1051
    }

    vum.isFormData = function (obj) {
JingChao's avatar
JingChao committed
1052
      return toString.call(obj) === '[object FormData]'
李晓兵's avatar
李晓兵 committed
1053 1054 1055
    }

    vum.isBlob = function (obj) {
JingChao's avatar
JingChao committed
1056
      return toString.call(obj) === '[object Blob]'
李晓兵's avatar
李晓兵 committed
1057 1058 1059
    }

    vum.isBoolean = function (value) {
JingChao's avatar
JingChao committed
1060
      return typeof value === 'boolean'
李晓兵's avatar
李晓兵 committed
1061 1062 1063
    }

    vum.isPromiseLike = function (obj) {
JingChao's avatar
JingChao committed
1064
      return obj && isFunction(obj.then)
李晓兵's avatar
李晓兵 committed
1065 1066
    }

JingChao's avatar
JingChao committed
1067
    var TYPED_ARRAY_REGEXP = /^\[object (?:Uint8|Uint8Clamped|Uint16|Uint32|Int8|Int16|Int32|Float32|Float64)Array\]$/
李晓兵's avatar
李晓兵 committed
1068 1069

    vum.isTypedArray = function (value) {
JingChao's avatar
JingChao committed
1070
      return value && isNumber(value.length) && TYPED_ARRAY_REGEXP.test(toString.call(value))
李晓兵's avatar
李晓兵 committed
1071 1072 1073
    }

    vum.isArrayBuffer = function (obj) {
JingChao's avatar
JingChao committed
1074
      return toString.call(obj) === '[object ArrayBuffer]'
李晓兵's avatar
李晓兵 committed
1075 1076 1077
    }

    vum.toJsonReplacer = function (key, value) {
JingChao's avatar
JingChao committed
1078
      var val = value
李晓兵's avatar
李晓兵 committed
1079 1080

      if (typeof key === 'string' && key.charAt(0) === '$' && key.charAt(1) === '$') {
JingChao's avatar
JingChao committed
1081
        val = undefined
李晓兵's avatar
李晓兵 committed
1082
      } else if (vum.isWindow(value)) {
JingChao's avatar
JingChao committed
1083
        val = '$WINDOW'
李晓兵's avatar
李晓兵 committed
1084
      } else if (value && document === value) {
JingChao's avatar
JingChao committed
1085
        val = '$DOCUMENT'
李晓兵's avatar
李晓兵 committed
1086
      } else if (vum.isScope(value)) {
JingChao's avatar
JingChao committed
1087
        val = '$SCOPE'
李晓兵's avatar
李晓兵 committed
1088 1089
      }

JingChao's avatar
JingChao committed
1090
      return val
李晓兵's avatar
李晓兵 committed
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
    }

    /**
     * @ngdoc function
     * @name angular.toJson
     * @module ng
     * @kind function
     *
     * @description
     * Serializes input into a JSON-formatted string. Properties with leading $$ characters will be
     * stripped since angular uses this notation internally.
     *
     * @param {Object|Array|Date|string|number} obj Input to be serialized into JSON.
     * @param {boolean|number} [pretty=2] If set to true, the JSON output will contain newlines and whitespace.
     *    If set to an integer, the JSON output will contain that many spaces per indentation.
     * @returns {string|undefined} JSON-ified string representing `obj`.
     */
    vum.toJson = function (obj, pretty) {
JingChao's avatar
JingChao committed
1109
      if (vum.isUndefined(obj)) return undefined
李晓兵's avatar
李晓兵 committed
1110
      if (!vum.isNumber(pretty)) {
JingChao's avatar
JingChao committed
1111
        pretty = pretty ? 2 : null
李晓兵's avatar
李晓兵 committed
1112
      }
JingChao's avatar
JingChao committed
1113
      return JSON.stringify(obj, vum.toJsonReplacer, pretty)
李晓兵's avatar
李晓兵 committed
1114 1115
    }

JingChao's avatar
JingChao committed
1116
    function isArrayLike (obj) {
李晓兵's avatar
李晓兵 committed
1117
      // `null`, `undefined` and `window` are not array-like
JingChao's avatar
JingChao committed
1118
      if (obj == null || isWindow(obj)) return false
李晓兵's avatar
李晓兵 committed
1119 1120 1121 1122 1123

      // arrays, strings and jQuery/jqLite objects are array like
      // * jqLite is either the jQuery or jqLite constructor function
      // * we have to check the existence of jqLite first as this method is called
      //   via the forEach method when constructing the jqLite object in the first place
JingChao's avatar
JingChao committed
1124
      if (isArray(obj) || vum.isString(obj) || (jqLite && obj instanceof jqLite)) return true
李晓兵's avatar
李晓兵 committed
1125 1126 1127

      // Support: iOS 8.2 (not reproducible in simulator)
      // "length" in obj used to prevent JIT error (gh-11508)
JingChao's avatar
JingChao committed
1128
      var length = 'length' in Object(obj) && obj.length
李晓兵's avatar
李晓兵 committed
1129 1130 1131 1132

      // NodeList objects (with `item` method) and
      // other objects with suitable length characteristics are array-like
      return vum.isNumber(length) &&
JingChao's avatar
JingChao committed
1133
        (length >= 0 && ((length - 1) in obj || obj instanceof Array) || typeof obj.item === 'function')
李晓兵's avatar
李晓兵 committed
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
    }

    /**
     * @ngdoc function
     * @name angular.forEach
     * @module ng
     * @kind function
     *
     * @description
     * Invokes the `iterator` function once for each item in `obj` collection, which can be either an
     * object or an array. The `iterator` function is invoked with `iterator(value, key, obj)`, where `value`
     * is the value of an object property or an array element, `key` is the object property key or
     * array element index and obj is the `obj` itself. Specifying a `context` for the function is optional.
     *
     * It is worth noting that `.forEach` does not iterate over inherited properties because it filters
     * using the `hasOwnProperty` method.
     *
     * Unlike ES262's
     * [Array.prototype.forEach](http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.18),
     * providing 'undefined' or 'null' values for `obj` will not throw a TypeError, but rather just
     * return the value provided.
     *
     ```js
     var values = {name: 'misko', gender: 'male'};
     var log = [];
     angular.forEach(values, function(value, key) {
       this.push(key + ': ' + value);
     }, log);
     expect(log).toEqual(['name: misko', 'gender: male']);
     ```
     *
     * @param {Object|Array} obj Object to iterate over.
     * @param {Function} iterator Iterator function.
     * @param {Object=} context Object to become context (`this`) for the iterator function.
     * @returns {Object|Array} Reference to `obj`.
     */

    vum.forEach = function (obj, iterator, context) {
JingChao's avatar
JingChao committed
1172
      var key, length
李晓兵's avatar
李晓兵 committed
1173 1174 1175 1176 1177 1178
      if (obj) {
        if (vum.isFunction(obj)) {
          for (key in obj) {
            // Need to check if hasOwnProperty exists,
            // as on IE8 the result of querySelectorAll is an object without a hasOwnProperty function
            if (key != 'prototype' && key != 'length' && key != 'name' && (!obj.hasOwnProperty || obj.hasOwnProperty(key))) {
JingChao's avatar
JingChao committed
1179
              iterator.call(context, obj[key], key, obj)
李晓兵's avatar
李晓兵 committed
1180 1181 1182
            }
          }
        } else if (isArray(obj) || isArrayLike(obj)) {
JingChao's avatar
JingChao committed
1183
          var isPrimitive = typeof obj !== 'object'
李晓兵's avatar
李晓兵 committed
1184 1185
          for (key = 0, length = obj.length; key < length; key++) {
            if (isPrimitive || key in obj) {
JingChao's avatar
JingChao committed
1186
              iterator.call(context, obj[key], key, obj)
李晓兵's avatar
李晓兵 committed
1187 1188 1189
            }
          }
        } else if (obj.forEach && obj.forEach !== forEach) {
JingChao's avatar
JingChao committed
1190
          obj.forEach(iterator, context, obj)
李晓兵's avatar
李晓兵 committed
1191 1192 1193
        } else if (vum.isBlankObject(obj)) {
          // createMap() fast path --- Safe to avoid hasOwnProperty check because prototype chain is empty
          for (key in obj) {
JingChao's avatar
JingChao committed
1194
            iterator.call(context, obj[key], key, obj)
李晓兵's avatar
李晓兵 committed
1195 1196 1197 1198 1199
          }
        } else if (typeof obj.hasOwnProperty === 'function') {
          // Slow path for objects inheriting Object.prototype, hasOwnProperty check needed
          for (key in obj) {
            if (obj.hasOwnProperty(key)) {
JingChao's avatar
JingChao committed
1200
              iterator.call(context, obj[key], key, obj)
李晓兵's avatar
李晓兵 committed
1201 1202 1203 1204 1205 1206
            }
          }
        } else {
          // Slow path for objects which do not have a method `hasOwnProperty`
          for (key in obj) {
            if (hasOwnProperty.call(obj, key)) {
JingChao's avatar
JingChao committed
1207
              iterator.call(context, obj[key], key, obj)
李晓兵's avatar
李晓兵 committed
1208 1209 1210 1211
            }
          }
        }
      }
JingChao's avatar
JingChao committed
1212
      return obj
李晓兵's avatar
李晓兵 committed
1213 1214 1215
    }

    vum.forEachSorted = function (obj, iterator, context) {
JingChao's avatar
JingChao committed
1216
      var keys = Object.keys(obj).sort()
李晓兵's avatar
李晓兵 committed
1217
      for (var i = 0; i < keys.length; i++) {
JingChao's avatar
JingChao committed
1218
        iterator.call(context, obj[keys[i]], keys[i])
李晓兵's avatar
李晓兵 committed
1219
      }
JingChao's avatar
JingChao committed
1220
      return keys
李晓兵's avatar
李晓兵 committed
1221 1222
    }
  })(window.vum)
JingChao's avatar
JingChao committed
1223
})()