controls-handlers.js
6 months ago
editor-handler.js
6 months ago
live-editor.js
6 months ago
pa-editor-behavior.js
6 months ago
pa-editor-behavior.min.js
6 months ago
pa-maps-finder.js
6 months ago
premium-cross-cp.js
6 months ago
xdlocalstorage.js
6 months ago
xdlocalstorage.js
182 lines
| 1 | /** |
| 2 | * Created by dagan on 07/04/2014. |
| 3 | */ |
| 4 | 'use strict'; |
| 5 | /* global console, XsUtils */ |
| 6 | window.XsUtils = window.XsUtils || (function () { |
| 7 | |
| 8 | function extend(object, defaultObject) { |
| 9 | var result = defaultObject || {}; |
| 10 | var key; |
| 11 | for (key in object) { |
| 12 | if (object.hasOwnProperty(key)) { |
| 13 | result[key] = object[key]; |
| 14 | } |
| 15 | } |
| 16 | return result; |
| 17 | } |
| 18 | |
| 19 | //public interface |
| 20 | return { |
| 21 | extend: extend |
| 22 | }; |
| 23 | })(); |
| 24 | |
| 25 | window.xsLocalStorage = window.xsLocalStorage || (function () { |
| 26 | var MESSAGE_NAMESPACE = "cross-domain-pa-cp-message", |
| 27 | options = { |
| 28 | iframeId: "cross-domain-iframe", |
| 29 | iframeUrl: undefined, |
| 30 | initCallback: function () { } |
| 31 | }; |
| 32 | var requestId = -1; |
| 33 | var iframe; |
| 34 | var requests = {}; |
| 35 | var wasInit = false; |
| 36 | var iframeReady = true; |
| 37 | |
| 38 | function applyCallback(data) { |
| 39 | if (requests[data.id]) { |
| 40 | requests[data.id](data); |
| 41 | delete requests[data.id]; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | function receiveMessage(event) { |
| 46 | var data; |
| 47 | try { |
| 48 | data = JSON.parse(event.data); |
| 49 | } catch (err) { |
| 50 | //not our message, can ignore |
| 51 | } |
| 52 | if (data && data.namespace === MESSAGE_NAMESPACE) { |
| 53 | if (data.id === 'iframe-ready') { |
| 54 | iframeReady = true; |
| 55 | options.initCallback(); |
| 56 | } else { |
| 57 | applyCallback(data); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | function buildMessage(action, key, value, callback) { |
| 63 | requestId++; |
| 64 | requests[requestId] = callback; |
| 65 | var data = { |
| 66 | namespace: MESSAGE_NAMESPACE, |
| 67 | id: requestId, |
| 68 | action: action, |
| 69 | key: key, |
| 70 | value: value |
| 71 | }; |
| 72 | iframe.contentWindow.postMessage(JSON.stringify(data), '*'); |
| 73 | } |
| 74 | |
| 75 | function init(customOptions) { |
| 76 | options = XsUtils.extend(customOptions, options); |
| 77 | var temp = document.createElement('div'); |
| 78 | |
| 79 | if (window.addEventListener) { |
| 80 | window.addEventListener('message', receiveMessage, false); |
| 81 | } else { |
| 82 | window.attachEvent('onmessage', receiveMessage); |
| 83 | } |
| 84 | |
| 85 | temp.innerHTML = '<iframe id="' + options.iframeId + '" src=' + options.iframeUrl + ' style="display: none;"></iframe>'; |
| 86 | document.body.appendChild(temp); |
| 87 | iframe = document.getElementById(options.iframeId); |
| 88 | } |
| 89 | |
| 90 | function isApiReady() { |
| 91 | if (!wasInit) { |
| 92 | return false; |
| 93 | } |
| 94 | if (!iframeReady) { |
| 95 | return false; |
| 96 | } |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | function isDomReady() { |
| 101 | return (document.readyState === 'complete'); |
| 102 | } |
| 103 | |
| 104 | return { |
| 105 | //callback is optional for cases you use the api before window load. |
| 106 | init: function (customOptions) { |
| 107 | if (!customOptions.iframeUrl) { |
| 108 | throw 'Please specify the iframe URL'; |
| 109 | } |
| 110 | if (wasInit) { |
| 111 | return; |
| 112 | } |
| 113 | wasInit = true; |
| 114 | if (isDomReady()) { |
| 115 | init(customOptions); |
| 116 | } else { |
| 117 | if (document.addEventListener) { |
| 118 | // All browsers expect IE < 9 |
| 119 | document.addEventListener('readystatechange', function () { |
| 120 | if (isDomReady()) { |
| 121 | init(customOptions); |
| 122 | } |
| 123 | }); |
| 124 | } else { |
| 125 | // IE < 9 |
| 126 | document.attachEvent('readystatechange', function () { |
| 127 | if (isDomReady()) { |
| 128 | init(customOptions); |
| 129 | } |
| 130 | }); |
| 131 | } |
| 132 | } |
| 133 | }, |
| 134 | setItem: function (key, value, callback) { |
| 135 | if (!isApiReady()) { |
| 136 | return; |
| 137 | } |
| 138 | buildMessage('set', key, value, callback); |
| 139 | }, |
| 140 | |
| 141 | getItem: function (key, callback) { |
| 142 | if (!isApiReady()) { |
| 143 | return; |
| 144 | } |
| 145 | buildMessage('get', key, null, callback); |
| 146 | }, |
| 147 | removeItem: function (key, callback) { |
| 148 | if (!isApiReady()) { |
| 149 | return; |
| 150 | } |
| 151 | buildMessage('remove', key, null, callback); |
| 152 | }, |
| 153 | key: function (index, callback) { |
| 154 | if (!isApiReady()) { |
| 155 | return; |
| 156 | } |
| 157 | buildMessage('key', index, null, callback); |
| 158 | }, |
| 159 | getSize: function (callback) { |
| 160 | if (!isApiReady()) { |
| 161 | return; |
| 162 | } |
| 163 | buildMessage('size', null, null, callback); |
| 164 | }, |
| 165 | getLength: function (callback) { |
| 166 | if (!isApiReady()) { |
| 167 | return; |
| 168 | } |
| 169 | buildMessage('length', null, null, callback); |
| 170 | }, |
| 171 | clear: function (callback) { |
| 172 | if (!isApiReady()) { |
| 173 | return; |
| 174 | } |
| 175 | buildMessage('clear', null, null, callback); |
| 176 | }, |
| 177 | wasInit: function () { |
| 178 | return wasInit; |
| 179 | } |
| 180 | }; |
| 181 | })(); |
| 182 |