Skip to content

Instantly share code, notes, and snippets.

@pke
pke / tracking.coffee
Created May 23, 2013 12:03
sample usage of google.analytics framework for Windows Modern UI apps
ApplicationView = Windows.UI.ViewManagement.ApplicationView
ApplicationViewState = Windows.UI.ViewManagement.ApplicationViewState
google.analytics.configure("UA-YOURID", someUniqueDeviceToken, "Your APP NAME", "YOUR APP VERSION")
###
EVENT LISTENERS FOR ANALYTICS REPORTING
###
#---------------------
@pke
pke / requireAsync.js
Created April 10, 2014 23:33
Async version of "require" for requireJS in WinJS Apps
window.requireAsync = function(deps) {
if (arguments.length === 0) {
throw new TypeError("requireAsync: No dependencies given");
}
if (typeof deps === "string" && arguments.length === 1) {
deps = deps.split(",");
} else if (!Array.isArray(deps)) {
deps = Array.prototype.slice.call(arguments, 0);
}
return new WinJS.Promise(function(c, e, p) {
@pke
pke / XmlLoader.coffee
Created June 4, 2014 14:42
WinRT XmlLoader
StorageFile = Windows.Storage.StorageFile
XmlDocument = Windows.Data.Xml.Dom.XmlDocument
define [], ->
exports =
loadFromFileAsync: (file) ->
return WinJS.Promise.wrapError(new TypeError("file isn't a Windows.Storage.StorageFile")) unless file instanceof Windows.Storage.StorageFile
settings = new Windows.Data.Xml.Dom.XmlLoadSettings()
settings.elementContentWhiteSpace = false
XmlDocument.loadFromFileAsync(file, settings)
@pke
pke / gist:25060515f2514395a07c
Created June 13, 2014 15:27
One time listener pattern (easy in coffescript)
document.addEventListener "keydown", listener = (event) ->
document.removeEventListener("keydown", listener)
console.log(event)
@pke
pke / buildVersion.js
Created June 18, 2014 22:20
MSBuild targets for git and other build versioning infomations
define(function() {
return {
gitCommitHash: "${gitCommitHash}",
gitBranchDirty: ${gitPendingChanges},
gitBranch: "${gitBranch}",
buildEnv: "${buildEnv}",
buildTime: "${buildTime}",
buildConfiguration: "${buildConfiguration}",
buildPlatform: "${buildPlatform}"
};
savesPending: 0
saveAsync: () ->
#assert(!@finished, "Inspection already finished!")
logger.debug("Save requested (_cachedFile=#{@_cachedFile})")
if @saving
@savesPending += 1
logger.debug("saveAsync: already saving, #{@savesPending} pending now")
return @saving
@saving = getCachedFile(@serialNumber)
.then (file) =>
@pke
pke / gist:36e63e557b0f7864fbfe
Created March 10, 2015 12:23
WinJS Load CSS from app folder and watch its changes
"ms-appdata:///local/conf/dailyPage.css".toStorageFileAsync()
.then (cssFile) =>
style = document.createElement("style")
style.type = "text/css"
readFileAsync = ->
Windows.Storage.FileIO.readTextAsync(cssFile)
.then (cssText) ->
style.sheet.cssText = cssText
readFileAsync()
.then ->
@pke
pke / pubnubproxy
Created April 1, 2015 19:04
Serving Trello notifications to pubnub
var http = require('http');
var pubnub = require("pubnub")({
ssl : true, // <- enable TLS Tunneling over TCP
publish_key: 'pub-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
subscribe_key: 'sub-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
});
var port = process.env.port || 1337;
http.createServer(function (req, res) {
if (req.method === "POST") {
var jsonString = '';
@pke
pke / iso8601todate
Last active August 29, 2015 14:23
Convert ISO8601 Dates to Excel Date (Variant)
Public Function iso8601todate(iso As String)
Dim result As Date: result = DateValue(Mid(iso, 1, 10)) + TimeValue(Mid(iso, 12, 8))
Dim signPos As Integer: signPos = InStr(iso, "Z")
If signPos = 0 Then signPos = InStr(iso, "+")
If signPos = 0 Then signPos = InStr(InStr(iso, "T"), iso, "-")
If signPos <> 0 Then
Dim zone As String: zone = Mid(iso, signPos)
Dim sign As String: sign = Left(zone, 1)
If sign <> "" And sign <> "Z" Then
@pke
pke / writeTextAsync.coffee
Last active September 2, 2015 23:24
For reasons beyond my comprehension Windows.Storage.PathIO.writeTextAsync can only write to existing files, not create new ones on the fly.
writeTextAsync = (path, text) ->
# First try if an existing file can be written
Windows.Storage.PathIO.writeTextAsync(path, text)
.then null, (error) ->
[_, folder, subFolders, fileName] = /^ms-appdata:\/\/\/(temp|local|roaming)\/(.+?)\/([\w]+\.?\w+)$/.exec(path)
folder = "temporary" if folder is "temp"
createSubFolders = subFolders.split("/").reduce((promise, folderName) ->
promise = promise.then (folder) ->
folder.createFolderAsync(folderName, Windows.Storage.CreationCollisionOption.openIfExists)