Skip to content
This repository has been archived by the owner on May 21, 2021. It is now read-only.

Commit

Permalink
feat(project): add first version of directive, services and utils for…
Browse files Browse the repository at this point in the history
… AngularJS and Flex
  • Loading branch information
Philipp Burgmer committed Jul 1, 2014
1 parent f546e48 commit 3831699
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/flash/w11k/flash/AngularJSAdapter.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package w11k.flash {
import flash.external.ExternalInterface;

import mx.core.FlexGlobals;

public class AngularJSAdapter {

private static var instance :AngularJSAdapter = new AngularJSAdapter();

public static function getInstance() :AngularJSAdapter {
return instance;
}

private var flashId :String;

public function AngularJSAdapter() {
if (instance) {
throw new Error("Singleton, use getInstance");
}

if (ExternalInterface.available) {
const application :* = FlexGlobals.topLevelApplication;
const parameters :* = application.parameters;

flashId = parameters.w11kFlashId;
}
else {
throw new Error('ExternalInterface has to be availabe to be able to use this adapter');
}

instance = this;
}

public function fireFlashReady() :void {
ExternalInterface.call("w11kFlashIsReady", flashId);
}

public function call(expression :String, locals :Object = null) :* {
return ExternalInterface.call("w11kFlashCall", flashId, expression, locals);
}
}
}
8 changes: 8 additions & 0 deletions src/js/w11k-flash.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.w11k-flash-container {
position: relative;
overflow: hidden;
}

.w11k-flash-container object {
visibility: inherit !important;
}
168 changes: 168 additions & 0 deletions src/js/w11k-flash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
'use strict';

angular.module('w11k.flash', []);

// register swfobject as service to be able to mock it
angular.module('w11k.flash').factory('swfobject', function ($window) {
return $window.swfobject;
});

angular.module('w11k.flash').factory('w11kFlashRegistry', function () {
var flashIdPrefix = 'w11k-flash-id-';
var flashIdCounter = 0;
var flashMap = {};

return {
getFlashId: function () {
return flashIdPrefix + flashIdCounter++;
},
registerFlash: function (flashId, flashObject) {
flashMap[flashId] = flashObject;
},
unregisterFlash: function (flashId) {
delete flashMap[flashId];
},
getFlash: function (flashId) {
return flashMap[flashId];
}
};
});

angular.module('w11k.flash').run(function ($window, w11kFlashRegistry) {
if (angular.isFunction($window.w11kFlashIsReady) === false) {
$window.w11kFlashIsReady = function (flashId) {
var flash = w11kFlashRegistry.getFlash(flashId);
if (angular.isDefined(flash)) {
flash.deferred.resolve(flash.object);
}
else {
throw new Error('unknown flashId');
}
};
}
});

angular.module('w11k.flash').run(function ($window, w11kFlashRegistry) {
if (angular.isFunction($window.w11kFlashCall) === false) {
$window.w11kFlashCall = function (flashId, expression, locals) {
var flash = w11kFlashRegistry.getFlash(flashId);
if (angular.isDefined(flash)) {
var scope = flash.element.scope();

// we have to evaluate the expression outside of the apply function,
// otherwise we are unable to return the result to flash
var result = scope.$eval(expression, locals);
scope.$apply(function () { });

return result;
}
else {
throw new Error('unknown flashId');
}
};
}
});

// extract config from directive and define overridable defaults
angular.module('w11k.flash').constant('w11kFlashConfig', {
templateUrl: 'w11k-flash.tpl.html',
swfObject: {
minFlashVersion: '10.2.0',
width: 800,
height: 600,
flashvars: {
},
params: {
quality: 'high',
bgcolor: '#ffffff',
allowfullscreen: 'false',
allowScriptAccess: 'always',
wmode: 'opaque'
},
attributes: {
align: 'middle'
}
}
});

angular.module('w11k.flash').directive('w11kFlash', function (swfobject, $window, $q, w11kFlashConfig, $timeout, w11kFlashRegistry) {
return {
restrict: 'EA',
templateUrl: w11kFlashConfig.templateUrl,
link: function (scope, element, attrs) {

var flashContainer = angular.element(element[0].querySelector('.w11k-flash-container'));
var flashElement = angular.element(element[0].querySelector('.w11k-flash-element'));

flashElement.remove();
var included = false;

scope.$watch(attrs.w11kFlashVisible, function (visible) {
if (visible && included === false) {
includeFlash();
included = true;
}

if (visible) {
element.css('visibility', 'visible');
element.css('height', 'auto');
element.css('width', 'auto');
}
else {
element.css('visibility', 'hidden');
element.css('height', '0');
element.css('width', '0');
}

});

var includeFlash = function () {

var customConfig = scope.$eval(attrs.w11kFlash);

var flashId = w11kFlashRegistry.getFlashId();

var config = angular.extend({ flashvars: {}}, w11kFlashConfig.swfObject, customConfig);
config.flashvars.w11kFlashId = flashId;

flashContainer.append(flashElement);
flashElement.attr('id', flashId);

if (swfobject.hasFlashPlayerVersion(config.minFlashVersion)) {

flashElement.css('min-height', config.height);
flashElement.css('min-width', config.width);

var callback = function (event) {
var deferred = $q.defer();

w11kFlashRegistry.registerFlash(flashId, {
deferred: deferred,
object: event.ref,
element: element
});

scope.$on('$destroy', function () {
w11kFlashRegistry.unregisterFlash(flashId);
});

if (angular.isFunction(config.callback)) {
config.callback(deferred.promise);
}
};

swfobject.embedSWF(config.swfUrl,
flashId,
'' + config.width,
'' + config.height,
config.minFlashVersion,
false,
config.flashvars,
config.params,
config.attributes,
callback);
}
};
}
};
});
5 changes: 5 additions & 0 deletions src/js/w11k-flash.tpl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="w11k-flash-container">
<div class="w11k-flash-element">
<p>Adobe Flash Player required.</p>
</div>
</div>

0 comments on commit 3831699

Please sign in to comment.